Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

301 lines
7.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. /*
  9. *
  10. * Copyright (c) 1998-9
  11. * Dr John Maddock
  12. *
  13. * Permission to use, copy, modify, distribute and sell this software
  14. * and its documentation for any purpose is hereby granted without fee,
  15. * provided that the above copyright notice appear in all copies and
  16. * that both that copyright notice and this permission notice appear
  17. * in supporting documentation. Dr John Maddock makes no representations
  18. * about the suitability of this software for any purpose.
  19. * It is provided "as is" without express or implied warranty.
  20. *
  21. */
  22. /*
  23. * FILE re_str.h
  24. * VERSION 2.12
  25. * This is an internal header file, do not include directly.
  26. * String support and helper functions, for regular
  27. * expression library.
  28. */
  29. #ifndef RE_STR_H
  30. #define RE_STR_H
  31. #ifndef JM_CFG_H
  32. #include <jm/jm_cfg.h>
  33. #endif
  34. #include <string.h>
  35. JM_NAMESPACE(__JM)
  36. //
  37. // start by defining some template function aliases for C API functions:
  38. //
  39. template <class charT>
  40. size_t RE_CALL re_strlen(const charT *s)
  41. {
  42. size_t len = 0;
  43. while(*s)
  44. {
  45. ++s;
  46. ++len;
  47. }
  48. return len;
  49. }
  50. template <class charT>
  51. int RE_CALL re_strcmp(const charT *s1, const charT *s2)
  52. {
  53. while(*s1 && *s2)
  54. {
  55. if(*s1 != *s2)
  56. return *s1 - *s2;
  57. ++s1;
  58. ++s2;
  59. }
  60. return *s1 - *s2;
  61. }
  62. template <class charT>
  63. charT* RE_CALL re_strcpy(charT *s1, const charT *s2)
  64. {
  65. charT* base = s1;
  66. while(*s2)
  67. {
  68. *s1 = *s2;
  69. ++s1;
  70. ++s2;
  71. }
  72. *s1 = *s2;
  73. return base;
  74. }
  75. template <class charT>
  76. unsigned int RE_CALL re_strwiden(charT *s1, unsigned int len, const char *s2)
  77. {
  78. unsigned int result = 1 + re_strlen(s2);
  79. if(result > len)
  80. return result;
  81. while(*s2)
  82. {
  83. *s1 = (unsigned char)*s2;
  84. ++s2;
  85. ++s1;
  86. }
  87. *s1 = (unsigned char)*s2;
  88. return result;
  89. }
  90. template <class charT>
  91. unsigned int RE_CALL re_strnarrow(char *s1, unsigned int len, const charT *s2)
  92. {
  93. unsigned int result = 1 + re_strlen(s2);
  94. if(result > len)
  95. return result;
  96. while(*s2)
  97. {
  98. *s1 = (char)(unsigned char)*s2;
  99. ++s2;
  100. ++s1;
  101. }
  102. *s1 = (char)(unsigned char)*s2;
  103. return result;
  104. }
  105. inline size_t RE_CALL re_strlen(const char *s)
  106. {
  107. return strlen(s);
  108. }
  109. inline int RE_CALL re_strcmp(const char *s1, const char *s2)
  110. {
  111. return strcmp(s1, s2);
  112. }
  113. inline char* RE_CALL re_strcpy(char *s1, const char *s2)
  114. {
  115. return strcpy(s1, s2);
  116. }
  117. #ifndef JM_NO_WCSTRING
  118. inline size_t RE_CALL re_strlen(const wchar_t *s)
  119. {
  120. return wcslen(s);
  121. }
  122. inline int RE_CALL re_strcmp(const wchar_t *s1, const wchar_t *s2)
  123. {
  124. return wcscmp(s1, s2);
  125. }
  126. inline wchar_t* RE_CALL re_strcpy(wchar_t *s1, const wchar_t *s2)
  127. {
  128. return wcscpy(s1, s2);
  129. }
  130. #endif
  131. #if !defined(JM_NO_WCSTRING) || defined(JM_PLATFORM_W32)
  132. JM_IX_DECL unsigned int RE_CALL _re_strnarrow(char *s1, unsigned int len, const wchar_t *s2);
  133. JM_IX_DECL unsigned int RE_CALL _re_strwiden(wchar_t *s1, unsigned int len, const char *s2);
  134. inline unsigned int RE_CALL re_strnarrow(char *s1, unsigned int len, const wchar_t *s2)
  135. {
  136. return _re_strnarrow(s1, len, s2);
  137. }
  138. inline unsigned int RE_CALL re_strwiden(wchar_t *s1, unsigned int len, const char *s2)
  139. {
  140. return _re_strwiden(s1, len, s2);
  141. }
  142. #endif
  143. template <class charT>
  144. charT* RE_CALL re_strdup(const charT* p)
  145. {
  146. charT* buf = new charT[re_strlen(p) + 1];
  147. re_strcpy(buf, p);
  148. return buf;
  149. }
  150. template <class charT>
  151. charT* RE_CALL re_strdup(const charT* p1, const charT* p2)
  152. {
  153. unsigned int len = p2 - p1 + 1;
  154. charT* buf = new charT[len];
  155. memcpy(buf, p1, (len - 1) * sizeof(charT));
  156. *(buf + len - 1) = 0;
  157. return buf;
  158. }
  159. template <class charT>
  160. inline void RE_CALL re_strfree(charT* p)
  161. {
  162. delete[] p;
  163. }
  164. template <class charT>
  165. class re_str
  166. {
  167. charT* buf;
  168. public:
  169. re_str()
  170. {
  171. charT c = 0;
  172. buf = re_strdup(&c);
  173. }
  174. ~re_str();
  175. re_str(const re_str& other);
  176. re_str(const charT* p1);
  177. re_str(const charT* p1, const charT* p2);
  178. re_str(charT c);
  179. re_str& RE_CALL operator=(const re_str& other)
  180. {
  181. re_strfree(buf);
  182. buf = re_strdup(other.buf);
  183. return *this;
  184. }
  185. re_str& RE_CALL operator=(const charT* p)
  186. {
  187. re_strfree(buf);
  188. buf = re_strdup(p);
  189. return *this;
  190. }
  191. re_str& RE_CALL operator=(charT c)
  192. {
  193. re_strfree(buf);
  194. buf = re_strdup(&c, &c+1);
  195. return *this;
  196. }
  197. const charT* RE_CALL c_str()const { return buf; }
  198. RE_CALL operator const charT*()const { return buf; }
  199. unsigned int RE_CALL size()const { return re_strlen(buf); }
  200. charT& RE_CALL operator[](unsigned int i) { return buf[i]; }
  201. charT RE_CALL operator[](unsigned int i)const { return buf[i]; }
  202. bool RE_CALL operator==(const re_str& other)const { return re_strcmp(buf, other.buf) == 0; }
  203. bool RE_CALL operator==(const charT* p)const { return re_strcmp(buf, p) == 0; }
  204. bool RE_CALL operator==(const charT c)const
  205. {
  206. if((*buf) && (*buf == c) && (*(buf+1) == 0))
  207. return true;
  208. return false;
  209. }
  210. bool RE_CALL operator!=(const re_str& other)const { return re_strcmp(buf, other.buf) != 0; }
  211. bool RE_CALL operator!=(const charT* p)const { return re_strcmp(buf, p) != 0; }
  212. bool RE_CALL operator!=(const charT c)const { return !(*this == c); }
  213. bool RE_CALL operator<(const re_str& other)const { return re_strcmp(buf, other.buf) < 0; }
  214. bool RE_CALL operator<=(const re_str& other)const { return re_strcmp(buf, other.buf) <= 0; }
  215. bool RE_CALL operator>(const re_str& other)const { return re_strcmp(buf, other.buf) > 0; }
  216. bool RE_CALL operator>=(const re_str& other)const { return re_strcmp(buf, other.buf) >= 0; }
  217. bool RE_CALL operator<(const charT* p)const { return re_strcmp(buf, p) < 0; }
  218. bool RE_CALL operator<=(const charT* p)const { return re_strcmp(buf, p) <= 0; }
  219. bool RE_CALL operator>(const charT* p)const { return re_strcmp(buf, p) > 0; }
  220. bool RE_CALL operator>=(const charT* p)const { return re_strcmp(buf, p) >= 0; }
  221. };
  222. template <class charT>
  223. CONSTRUCTOR_INLINE re_str<charT>::~re_str() { re_strfree(buf); }
  224. template <class charT>
  225. CONSTRUCTOR_INLINE re_str<charT>::re_str(const re_str<charT>& other) { buf = re_strdup(other.buf); }
  226. template <class charT>
  227. CONSTRUCTOR_INLINE re_str<charT>::re_str(const charT* p1) { buf = re_strdup(p1); }
  228. template <class charT>
  229. CONSTRUCTOR_INLINE re_str<charT>::re_str(const charT* p1, const charT* p2) { buf = re_strdup(p1, p2); }
  230. template <class charT>
  231. CONSTRUCTOR_INLINE re_str<charT>::re_str(charT c) { buf = re_strdup(&c, &c+1); }
  232. #ifndef JM_NO_WCSTRING
  233. JM_IX_DECL void RE_CALL re_transform(re_str<wchar_t>& out, const re_str<wchar_t>& in);
  234. #endif
  235. JM_IX_DECL void RE_CALL re_transform(re_str<char>& out, const re_str<char>& in);
  236. template <class charT>
  237. void RE_CALL re_trunc_primary(re_str<charT>& s)
  238. {
  239. for(unsigned int i = 0; i < s.size(); ++i)
  240. {
  241. if(s[i] <= 1)
  242. {
  243. s[i] = 0;
  244. break;
  245. }
  246. }
  247. }
  248. #ifdef RE_LOCALE_C
  249. #define TRANSFORM_ERROR (size_t)-1
  250. #else
  251. #define TRANSFORM_ERROR 0
  252. #endif
  253. JM_END_NAMESPACE
  254. #endif