3 * Copyright 2004--2005, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #ifndef TALK_BASE_STRINGUTILS_H__
29 #define TALK_BASE_STRINGUTILS_H__
38 #define alloca _alloca
52 #include "talk/base/basictypes.h"
54 ///////////////////////////////////////////////////////////////////////////////
55 // Generic string/memory utilities
56 ///////////////////////////////////////////////////////////////////////////////
58 #define STACK_ARRAY(TYPE, LEN) static_cast<TYPE*>(::alloca((LEN)*sizeof(TYPE)))
62 // Complement to memset. Verifies memory consists of count bytes of value c.
63 bool memory_check(const void* memory, int c, size_t count);
65 // Determines whether the simple wildcard pattern matches target.
66 // Alpha characters in pattern match case-insensitively.
67 // Asterisks in pattern match 0 or more characters.
68 // Ex: string_match("www.TEST.GOOGLE.COM", "www.*.com") -> true
69 bool string_match(const char* target, const char* pattern);
71 } // namespace talk_base
73 ///////////////////////////////////////////////////////////////////////////////
74 // Rename a bunch of common string functions so they are consistent across
75 // platforms and between char and wchar_t variants.
76 // Here is the full list of functions that are unified:
77 // strlen, strcmp, stricmp, strncmp, strnicmp
78 // strchr, vsnprintf, strtoul, tolowercase
79 // tolowercase is like tolower, but not compatible with end-of-file value
81 // It's not clear if we will ever use wchar_t strings on unix. In theory,
82 // all strings should be Utf8 all the time, except when interfacing with Win32
83 // APIs that require Utf16.
84 ///////////////////////////////////////////////////////////////////////////////
86 inline char tolowercase(char c) {
87 return static_cast<char>(tolower(c));
92 inline size_t strlen(const wchar_t* s) {
95 inline int strcmp(const wchar_t* s1, const wchar_t* s2) {
96 return wcscmp(s1, s2);
98 inline int stricmp(const wchar_t* s1, const wchar_t* s2) {
99 return _wcsicmp(s1, s2);
101 inline int strncmp(const wchar_t* s1, const wchar_t* s2, size_t n) {
102 return wcsncmp(s1, s2, n);
104 inline int strnicmp(const wchar_t* s1, const wchar_t* s2, size_t n) {
105 return _wcsnicmp(s1, s2, n);
107 inline const wchar_t* strchr(const wchar_t* s, wchar_t c) {
110 inline const wchar_t* strstr(const wchar_t* haystack, const wchar_t* needle) {
111 return wcsstr(haystack, needle);
114 inline int vsnprintf(wchar_t* buf, size_t n, const wchar_t* fmt, va_list args) {
115 return _vsnwprintf(buf, n, fmt, args);
118 inline unsigned long strtoul(const wchar_t* snum, wchar_t** end, int base) {
119 return wcstoul(snum, end, base);
121 inline wchar_t tolowercase(wchar_t c) {
122 return static_cast<wchar_t>(towlower(c));
129 inline int _stricmp(const char* s1, const char* s2) {
130 return strcasecmp(s1, s2);
132 inline int _strnicmp(const char* s1, const char* s2, size_t n) {
133 return strncasecmp(s1, s2, n);
138 ///////////////////////////////////////////////////////////////////////////////
139 // Traits simplifies porting string functions to be CTYPE-agnostic
140 ///////////////////////////////////////////////////////////////////////////////
142 namespace talk_base {
144 const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
146 template<class CTYPE>
149 //typedef XXX string;
150 // Null-terminated string
151 //inline static const CTYPE* empty_str();
154 ///////////////////////////////////////////////////////////////////////////////
155 // String utilities which work with char or wchar_t
156 ///////////////////////////////////////////////////////////////////////////////
158 template<class CTYPE>
159 inline const CTYPE* nonnull(const CTYPE* str, const CTYPE* def_str = NULL) {
160 return str ? str : (def_str ? def_str : Traits<CTYPE>::empty_str());
163 template<class CTYPE>
164 const CTYPE* strchr(const CTYPE* str, const CTYPE* chs) {
165 for (size_t i=0; str[i]; ++i) {
166 for (size_t j=0; chs[j]; ++j) {
167 if (str[i] == chs[j]) {
175 template<class CTYPE>
176 const CTYPE* strchrn(const CTYPE* str, size_t slen, CTYPE ch) {
177 for (size_t i=0; i<slen && str[i]; ++i) {
185 template<class CTYPE>
186 size_t strlenn(const CTYPE* buffer, size_t buflen) {
188 while (buffer[bufpos] && (bufpos < buflen)) {
194 // Safe versions of strncpy, strncat, snprintf and vsnprintf that always
197 template<class CTYPE>
198 size_t strcpyn(CTYPE* buffer, size_t buflen,
199 const CTYPE* source, size_t srclen = SIZE_UNKNOWN) {
203 if (srclen == SIZE_UNKNOWN) {
204 srclen = strlenn(source, buflen - 1);
205 } else if (srclen >= buflen) {
208 memcpy(buffer, source, srclen * sizeof(CTYPE));
213 template<class CTYPE>
214 size_t strcatn(CTYPE* buffer, size_t buflen,
215 const CTYPE* source, size_t srclen = SIZE_UNKNOWN) {
219 size_t bufpos = strlenn(buffer, buflen - 1);
220 return bufpos + strcpyn(buffer + bufpos, buflen - bufpos, source, srclen);
223 // Some compilers (clang specifically) require vsprintfn be defined before
225 template<class CTYPE>
226 size_t vsprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format,
228 int len = vsnprintf(buffer, buflen, format, args);
229 if ((len < 0) || (static_cast<size_t>(len) >= buflen)) {
230 len = static_cast<int>(buflen - 1);
236 template<class CTYPE>
237 size_t sprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format, ...);
238 template<class CTYPE>
239 size_t sprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format, ...) {
241 va_start(args, format);
242 size_t len = vsprintfn(buffer, buflen, format, args);
247 ///////////////////////////////////////////////////////////////////////////////
248 // Allow safe comparing and copying ascii (not UTF-8) with both wide and
249 // non-wide character strings.
250 ///////////////////////////////////////////////////////////////////////////////
252 inline int asccmp(const char* s1, const char* s2) {
253 return strcmp(s1, s2);
255 inline int ascicmp(const char* s1, const char* s2) {
256 return _stricmp(s1, s2);
258 inline int ascncmp(const char* s1, const char* s2, size_t n) {
259 return strncmp(s1, s2, n);
261 inline int ascnicmp(const char* s1, const char* s2, size_t n) {
262 return _strnicmp(s1, s2, n);
264 inline size_t asccpyn(char* buffer, size_t buflen,
265 const char* source, size_t srclen = SIZE_UNKNOWN) {
266 return strcpyn(buffer, buflen, source, srclen);
271 typedef wchar_t(*CharacterTransformation)(wchar_t);
272 inline wchar_t identity(wchar_t c) { return c; }
273 int ascii_string_compare(const wchar_t* s1, const char* s2, size_t n,
274 CharacterTransformation transformation);
276 inline int asccmp(const wchar_t* s1, const char* s2) {
277 return ascii_string_compare(s1, s2, static_cast<size_t>(-1), identity);
279 inline int ascicmp(const wchar_t* s1, const char* s2) {
280 return ascii_string_compare(s1, s2, static_cast<size_t>(-1), tolowercase);
282 inline int ascncmp(const wchar_t* s1, const char* s2, size_t n) {
283 return ascii_string_compare(s1, s2, n, identity);
285 inline int ascnicmp(const wchar_t* s1, const char* s2, size_t n) {
286 return ascii_string_compare(s1, s2, n, tolowercase);
288 size_t asccpyn(wchar_t* buffer, size_t buflen,
289 const char* source, size_t srclen = SIZE_UNKNOWN);
293 ///////////////////////////////////////////////////////////////////////////////
294 // Traits<char> specializations
295 ///////////////////////////////////////////////////////////////////////////////
298 struct Traits<char> {
299 typedef std::string string;
300 inline static const char* empty_str() { return ""; }
303 ///////////////////////////////////////////////////////////////////////////////
304 // Traits<wchar_t> specializations (Windows only, currently)
305 ///////////////////////////////////////////////////////////////////////////////
310 struct Traits<wchar_t> {
311 typedef std::wstring string;
312 inline static const wchar_t* Traits<wchar_t>::empty_str() { return L""; }
317 // Replaces all occurrences of "search" with "replace".
318 void replace_substrs(const char *search,
324 // True iff s1 starts with s2.
325 bool starts_with(const char *s1, const char *s2);
327 // True iff s1 ends with s2.
328 bool ends_with(const char *s1, const char *s2);
330 // Remove leading and trailing whitespaces.
331 std::string string_trim(const std::string& s);
333 } // namespace talk_base
335 #endif // TALK_BASE_STRINGUTILS_H__