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.

338 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_STRINGUTILS_H__
  28. #define TALK_BASE_STRINGUTILS_H__
  29. #include <ctype.h>
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #ifdef WIN32
  33. #include <malloc.h>
  34. #include <wchar.h>
  35. #define alloca _alloca
  36. #endif // WIN32
  37. #ifdef POSIX
  38. #ifdef BSD
  39. #include <stdlib.h>
  40. #else // BSD
  41. #include <alloca.h>
  42. #endif // !BSD
  43. #endif // POSIX
  44. #include <cstring>
  45. #include <string>
  46. #include "talk/base/basictypes.h"
  47. ///////////////////////////////////////////////////////////////////////////////
  48. // Generic string/memory utilities
  49. ///////////////////////////////////////////////////////////////////////////////
  50. #define STACK_ARRAY(TYPE, LEN) static_cast<TYPE*>(::alloca((LEN)*sizeof(TYPE)))
  51. namespace talk_base {
  52. // Complement to memset. Verifies memory consists of count bytes of value c.
  53. bool memory_check(const void* memory, int c, size_t count);
  54. // Determines whether the simple wildcard pattern matches target.
  55. // Alpha characters in pattern match case-insensitively.
  56. // Asterisks in pattern match 0 or more characters.
  57. // Ex: string_match("www.TEST.GOOGLE.COM", "www.*.com") -> true
  58. bool string_match(const char* target, const char* pattern);
  59. } // namespace talk_base
  60. ///////////////////////////////////////////////////////////////////////////////
  61. // Rename a bunch of common string functions so they are consistent across
  62. // platforms and between char and wchar_t variants.
  63. // Here is the full list of functions that are unified:
  64. // strlen, strcmp, stricmp, strncmp, strnicmp
  65. // strchr, vsnprintf, strtoul, tolowercase
  66. // tolowercase is like tolower, but not compatible with end-of-file value
  67. //
  68. // It's not clear if we will ever use wchar_t strings on unix. In theory,
  69. // all strings should be Utf8 all the time, except when interfacing with Win32
  70. // APIs that require Utf16.
  71. ///////////////////////////////////////////////////////////////////////////////
  72. inline char tolowercase(char c) {
  73. return static_cast<char>(tolower(c));
  74. }
  75. #ifdef WIN32
  76. inline size_t strlen(const wchar_t* s) {
  77. return wcslen(s);
  78. }
  79. inline int strcmp(const wchar_t* s1, const wchar_t* s2) {
  80. return wcscmp(s1, s2);
  81. }
  82. inline int stricmp(const wchar_t* s1, const wchar_t* s2) {
  83. return _wcsicmp(s1, s2);
  84. }
  85. inline int strncmp(const wchar_t* s1, const wchar_t* s2, size_t n) {
  86. return wcsncmp(s1, s2, n);
  87. }
  88. inline int strnicmp(const wchar_t* s1, const wchar_t* s2, size_t n) {
  89. return _wcsnicmp(s1, s2, n);
  90. }
  91. inline const wchar_t* strchr(const wchar_t* s, wchar_t c) {
  92. return wcschr(s, c);
  93. }
  94. inline const wchar_t* strstr(const wchar_t* haystack, const wchar_t* needle) {
  95. return wcsstr(haystack, needle);
  96. }
  97. #ifndef vsnprintf
  98. inline int vsnprintf(char* buf, size_t n, const char* fmt, va_list args) {
  99. return _vsnprintf(buf, n, fmt, args);
  100. }
  101. inline int vsnprintf(wchar_t* buf, size_t n, const wchar_t* fmt, va_list args) {
  102. return _vsnwprintf(buf, n, fmt, args);
  103. }
  104. #endif // !vsnprintf
  105. inline unsigned long strtoul(const wchar_t* snum, wchar_t** end, int base) {
  106. return wcstoul(snum, end, base);
  107. }
  108. inline wchar_t tolowercase(wchar_t c) {
  109. return static_cast<wchar_t>(towlower(c));
  110. }
  111. #endif // WIN32
  112. #ifdef POSIX
  113. inline int _stricmp(const char* s1, const char* s2) {
  114. return strcasecmp(s1, s2);
  115. }
  116. inline int _strnicmp(const char* s1, const char* s2, size_t n) {
  117. return strncasecmp(s1, s2, n);
  118. }
  119. #endif // POSIX
  120. ///////////////////////////////////////////////////////////////////////////////
  121. // Traits simplifies porting string functions to be CTYPE-agnostic
  122. ///////////////////////////////////////////////////////////////////////////////
  123. namespace talk_base {
  124. const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
  125. template<class CTYPE>
  126. struct Traits {
  127. // STL string type
  128. //typedef XXX string;
  129. // Null-terminated string
  130. //inline static const CTYPE* empty_str();
  131. };
  132. ///////////////////////////////////////////////////////////////////////////////
  133. // String utilities which work with char or wchar_t
  134. ///////////////////////////////////////////////////////////////////////////////
  135. template<class CTYPE>
  136. inline const CTYPE* nonnull(const CTYPE* str, const CTYPE* def_str = NULL) {
  137. return str ? str : (def_str ? def_str : Traits<CTYPE>::empty_str());
  138. }
  139. template<class CTYPE>
  140. const CTYPE* strchr(const CTYPE* str, const CTYPE* chs) {
  141. for (size_t i=0; str[i]; ++i) {
  142. for (size_t j=0; chs[j]; ++j) {
  143. if (str[i] == chs[j]) {
  144. return str + i;
  145. }
  146. }
  147. }
  148. return 0;
  149. }
  150. template<class CTYPE>
  151. const CTYPE* strchrn(const CTYPE* str, size_t slen, CTYPE ch) {
  152. for (size_t i=0; i<slen && str[i]; ++i) {
  153. if (str[i] == ch) {
  154. return str + i;
  155. }
  156. }
  157. return 0;
  158. }
  159. template<class CTYPE>
  160. size_t strlenn(const CTYPE* buffer, size_t buflen) {
  161. size_t bufpos = 0;
  162. while (buffer[bufpos] && (bufpos < buflen)) {
  163. ++bufpos;
  164. }
  165. return bufpos;
  166. }
  167. // Safe versions of strncpy, strncat, snprintf and vsnprintf that always
  168. // null-terminate.
  169. template<class CTYPE>
  170. size_t strcpyn(CTYPE* buffer, size_t buflen,
  171. const CTYPE* source, size_t srclen = SIZE_UNKNOWN) {
  172. if (buflen <= 0)
  173. return 0;
  174. if (srclen == SIZE_UNKNOWN) {
  175. srclen = strlenn(source, buflen - 1);
  176. } else if (srclen >= buflen) {
  177. srclen = buflen - 1;
  178. }
  179. memcpy(buffer, source, srclen * sizeof(CTYPE));
  180. buffer[srclen] = 0;
  181. return srclen;
  182. }
  183. template<class CTYPE>
  184. size_t strcatn(CTYPE* buffer, size_t buflen,
  185. const CTYPE* source, size_t srclen = SIZE_UNKNOWN) {
  186. if (buflen <= 0)
  187. return 0;
  188. size_t bufpos = strlenn(buffer, buflen - 1);
  189. return bufpos + strcpyn(buffer + bufpos, buflen - bufpos, source, srclen);
  190. }
  191. // Some compilers (clang specifically) require vsprintfn be defined before
  192. // sprintfn.
  193. template<class CTYPE>
  194. size_t vsprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format,
  195. va_list args) {
  196. int len = vsnprintf(buffer, buflen, format, args);
  197. if ((len < 0) || (static_cast<size_t>(len) >= buflen)) {
  198. len = static_cast<int>(buflen - 1);
  199. buffer[len] = 0;
  200. }
  201. return len;
  202. }
  203. template<class CTYPE>
  204. size_t sprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format, ...);
  205. template<class CTYPE>
  206. size_t sprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format, ...) {
  207. va_list args;
  208. va_start(args, format);
  209. size_t len = vsprintfn(buffer, buflen, format, args);
  210. va_end(args);
  211. return len;
  212. }
  213. ///////////////////////////////////////////////////////////////////////////////
  214. // Allow safe comparing and copying ascii (not UTF-8) with both wide and
  215. // non-wide character strings.
  216. ///////////////////////////////////////////////////////////////////////////////
  217. inline int asccmp(const char* s1, const char* s2) {
  218. return strcmp(s1, s2);
  219. }
  220. inline int ascicmp(const char* s1, const char* s2) {
  221. return _stricmp(s1, s2);
  222. }
  223. inline int ascncmp(const char* s1, const char* s2, size_t n) {
  224. return strncmp(s1, s2, n);
  225. }
  226. inline int ascnicmp(const char* s1, const char* s2, size_t n) {
  227. return _strnicmp(s1, s2, n);
  228. }
  229. inline size_t asccpyn(char* buffer, size_t buflen,
  230. const char* source, size_t srclen = SIZE_UNKNOWN) {
  231. return strcpyn(buffer, buflen, source, srclen);
  232. }
  233. #ifdef WIN32
  234. typedef wchar_t(*CharacterTransformation)(wchar_t);
  235. inline wchar_t identity(wchar_t c) { return c; }
  236. int ascii_string_compare(const wchar_t* s1, const char* s2, size_t n,
  237. CharacterTransformation transformation);
  238. inline int asccmp(const wchar_t* s1, const char* s2) {
  239. return ascii_string_compare(s1, s2, static_cast<size_t>(-1), identity);
  240. }
  241. inline int ascicmp(const wchar_t* s1, const char* s2) {
  242. return ascii_string_compare(s1, s2, static_cast<size_t>(-1), tolowercase);
  243. }
  244. inline int ascncmp(const wchar_t* s1, const char* s2, size_t n) {
  245. return ascii_string_compare(s1, s2, n, identity);
  246. }
  247. inline int ascnicmp(const wchar_t* s1, const char* s2, size_t n) {
  248. return ascii_string_compare(s1, s2, n, tolowercase);
  249. }
  250. size_t asccpyn(wchar_t* buffer, size_t buflen,
  251. const char* source, size_t srclen = SIZE_UNKNOWN);
  252. #endif // WIN32
  253. ///////////////////////////////////////////////////////////////////////////////
  254. // Traits<char> specializations
  255. ///////////////////////////////////////////////////////////////////////////////
  256. template<>
  257. struct Traits<char> {
  258. typedef std::string string;
  259. inline static const char* empty_str() { return ""; }
  260. };
  261. ///////////////////////////////////////////////////////////////////////////////
  262. // Traits<wchar_t> specializations (Windows only, currently)
  263. ///////////////////////////////////////////////////////////////////////////////
  264. #ifdef WIN32
  265. template<>
  266. struct Traits<wchar_t> {
  267. typedef std::wstring string;
  268. inline static const wchar_t* Traits<wchar_t>::empty_str() { return L""; }
  269. };
  270. #endif // WIN32
  271. // Replaces all occurrences of "search" with "replace".
  272. void replace_substrs(const char *search,
  273. size_t search_len,
  274. const char *replace,
  275. size_t replace_len,
  276. std::string *s);
  277. // True iff s1 starts with s2.
  278. bool starts_with(const char *s1, const char *s2);
  279. // True iff s1 ends with s2.
  280. bool ends_with(const char *s1, const char *s2);
  281. // Remove leading and trailing whitespaces.
  282. std::string string_trim(const std::string& s);
  283. } // namespace talk_base
  284. #endif // TALK_BASE_STRINGUTILS_H__