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