blob: 95e36eea1fc02f24f7e412c43ba9f4db5852037e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_STRINGUTILS_H_
12#define RTC_BASE_STRINGUTILS_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <ctype.h>
15#include <stdarg.h>
16#include <stdio.h>
17#include <string.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020019#if defined(WEBRTC_WIN)
20#include <malloc.h>
21#include <wchar.h>
22#define alloca _alloca
Henrik Kjellanderc0362762017-06-29 08:03:04 +020023#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020025#if defined(WEBRTC_POSIX)
26#ifdef BSD
27#include <stdlib.h>
28#else // BSD
29#include <alloca.h>
30#endif // !BSD
31#endif // WEBRTC_POSIX
32
33#include <string>
34
35///////////////////////////////////////////////////////////////////////////////
36// Generic string/memory utilities
37///////////////////////////////////////////////////////////////////////////////
38
39#define STACK_ARRAY(TYPE, LEN) static_cast<TYPE*>(::alloca((LEN)*sizeof(TYPE)))
40
41namespace rtc {
42
43// Complement to memset. Verifies memory consists of count bytes of value c.
44bool memory_check(const void* memory, int c, size_t count);
45
46// Determines whether the simple wildcard pattern matches target.
47// Alpha characters in pattern match case-insensitively.
48// Asterisks in pattern match 0 or more characters.
49// Ex: string_match("www.TEST.GOOGLE.COM", "www.*.com") -> true
50bool string_match(const char* target, const char* pattern);
51
52} // namespace rtc
53
54///////////////////////////////////////////////////////////////////////////////
55// Rename a bunch of common string functions so they are consistent across
56// platforms and between char and wchar_t variants.
57// Here is the full list of functions that are unified:
58// strlen, strcmp, stricmp, strncmp, strnicmp
59// strchr, vsnprintf, strtoul, tolowercase
60// tolowercase is like tolower, but not compatible with end-of-file value
61//
62// It's not clear if we will ever use wchar_t strings on unix. In theory,
63// all strings should be Utf8 all the time, except when interfacing with Win32
64// APIs that require Utf16.
65///////////////////////////////////////////////////////////////////////////////
66
67inline char tolowercase(char c) {
68 return static_cast<char>(tolower(c));
69}
70
71#if defined(WEBRTC_WIN)
72
73inline size_t strlen(const wchar_t* s) {
74 return wcslen(s);
75}
76inline int strcmp(const wchar_t* s1, const wchar_t* s2) {
77 return wcscmp(s1, s2);
78}
79inline int stricmp(const wchar_t* s1, const wchar_t* s2) {
80 return _wcsicmp(s1, s2);
81}
82inline int strncmp(const wchar_t* s1, const wchar_t* s2, size_t n) {
83 return wcsncmp(s1, s2, n);
84}
85inline int strnicmp(const wchar_t* s1, const wchar_t* s2, size_t n) {
86 return _wcsnicmp(s1, s2, n);
87}
88inline const wchar_t* strchr(const wchar_t* s, wchar_t c) {
89 return wcschr(s, c);
90}
91inline const wchar_t* strstr(const wchar_t* haystack, const wchar_t* needle) {
92 return wcsstr(haystack, needle);
93}
94#ifndef vsnprintf
95inline int vsnprintf(wchar_t* buf, size_t n, const wchar_t* fmt, va_list args) {
96 return _vsnwprintf(buf, n, fmt, args);
97}
98#endif // !vsnprintf
99inline unsigned long strtoul(const wchar_t* snum, wchar_t** end, int base) {
100 return wcstoul(snum, end, base);
101}
102inline wchar_t tolowercase(wchar_t c) {
103 return static_cast<wchar_t>(towlower(c));
104}
105
Henrik Kjellanderc0362762017-06-29 08:03:04 +0200106#endif // WEBRTC_WIN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200107
108#if defined(WEBRTC_POSIX)
109
110inline int _stricmp(const char* s1, const char* s2) {
111 return strcasecmp(s1, s2);
112}
113inline int _strnicmp(const char* s1, const char* s2, size_t n) {
114 return strncasecmp(s1, s2, n);
115}
116
117#endif // WEBRTC_POSIX
118
119///////////////////////////////////////////////////////////////////////////////
120// Traits simplifies porting string functions to be CTYPE-agnostic
121///////////////////////////////////////////////////////////////////////////////
122
123namespace rtc {
124
125const size_t SIZE_UNKNOWN = static_cast<size_t>(-1);
126
127template<class CTYPE>
128struct Traits {
129 // STL string type
130 //typedef XXX string;
131 // Null-terminated string
132 //inline static const CTYPE* empty_str();
133};
134
135///////////////////////////////////////////////////////////////////////////////
136// String utilities which work with char or wchar_t
137///////////////////////////////////////////////////////////////////////////////
138
139template <class CTYPE>
140inline const CTYPE* nonnull(const CTYPE* str, const CTYPE* def_str = nullptr) {
141 return str ? str : (def_str ? def_str : Traits<CTYPE>::empty_str());
142}
143
144template<class CTYPE>
145const CTYPE* strchr(const CTYPE* str, const CTYPE* chs) {
146 for (size_t i=0; str[i]; ++i) {
147 for (size_t j=0; chs[j]; ++j) {
148 if (str[i] == chs[j]) {
149 return str + i;
150 }
151 }
152 }
153 return 0;
154}
155
156template<class CTYPE>
157const CTYPE* strchrn(const CTYPE* str, size_t slen, CTYPE ch) {
158 for (size_t i=0; i<slen && str[i]; ++i) {
159 if (str[i] == ch) {
160 return str + i;
161 }
162 }
163 return 0;
164}
165
166template<class CTYPE>
167size_t strlenn(const CTYPE* buffer, size_t buflen) {
168 size_t bufpos = 0;
169 while (buffer[bufpos] && (bufpos < buflen)) {
170 ++bufpos;
171 }
172 return bufpos;
173}
174
175// Safe versions of strncpy, strncat, snprintf and vsnprintf that always
176// null-terminate.
177
178template<class CTYPE>
179size_t strcpyn(CTYPE* buffer, size_t buflen,
180 const CTYPE* source, size_t srclen = SIZE_UNKNOWN) {
181 if (buflen <= 0)
182 return 0;
183
184 if (srclen == SIZE_UNKNOWN) {
185 srclen = strlenn(source, buflen - 1);
186 } else if (srclen >= buflen) {
187 srclen = buflen - 1;
188 }
189 memcpy(buffer, source, srclen * sizeof(CTYPE));
190 buffer[srclen] = 0;
191 return srclen;
192}
193
194template<class CTYPE>
195size_t strcatn(CTYPE* buffer, size_t buflen,
196 const CTYPE* source, size_t srclen = SIZE_UNKNOWN) {
197 if (buflen <= 0)
198 return 0;
199
200 size_t bufpos = strlenn(buffer, buflen - 1);
201 return bufpos + strcpyn(buffer + bufpos, buflen - bufpos, source, srclen);
202}
203
204// Some compilers (clang specifically) require vsprintfn be defined before
205// sprintfn.
206template<class CTYPE>
207size_t vsprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format,
208 va_list args) {
209 int len = vsnprintf(buffer, buflen, format, args);
210 if ((len < 0) || (static_cast<size_t>(len) >= buflen)) {
211 len = static_cast<int>(buflen - 1);
212 buffer[len] = 0;
213 }
214 return len;
215}
216
217template<class CTYPE>
218size_t sprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format, ...);
219template<class CTYPE>
220size_t sprintfn(CTYPE* buffer, size_t buflen, const CTYPE* format, ...) {
221 va_list args;
222 va_start(args, format);
223 size_t len = vsprintfn(buffer, buflen, format, args);
224 va_end(args);
225 return len;
226}
227
228///////////////////////////////////////////////////////////////////////////////
229// Allow safe comparing and copying ascii (not UTF-8) with both wide and
230// non-wide character strings.
231///////////////////////////////////////////////////////////////////////////////
232
233inline int asccmp(const char* s1, const char* s2) {
234 return strcmp(s1, s2);
235}
236inline int ascicmp(const char* s1, const char* s2) {
237 return _stricmp(s1, s2);
238}
239inline int ascncmp(const char* s1, const char* s2, size_t n) {
240 return strncmp(s1, s2, n);
241}
242inline int ascnicmp(const char* s1, const char* s2, size_t n) {
243 return _strnicmp(s1, s2, n);
244}
245inline size_t asccpyn(char* buffer, size_t buflen,
246 const char* source, size_t srclen = SIZE_UNKNOWN) {
247 return strcpyn(buffer, buflen, source, srclen);
248}
249
250#if defined(WEBRTC_WIN)
251
252typedef wchar_t(*CharacterTransformation)(wchar_t);
253inline wchar_t identity(wchar_t c) { return c; }
254int ascii_string_compare(const wchar_t* s1, const char* s2, size_t n,
255 CharacterTransformation transformation);
256
257inline int asccmp(const wchar_t* s1, const char* s2) {
258 return ascii_string_compare(s1, s2, static_cast<size_t>(-1), identity);
259}
260inline int ascicmp(const wchar_t* s1, const char* s2) {
261 return ascii_string_compare(s1, s2, static_cast<size_t>(-1), tolowercase);
262}
263inline int ascncmp(const wchar_t* s1, const char* s2, size_t n) {
264 return ascii_string_compare(s1, s2, n, identity);
265}
266inline int ascnicmp(const wchar_t* s1, const char* s2, size_t n) {
267 return ascii_string_compare(s1, s2, n, tolowercase);
268}
269size_t asccpyn(wchar_t* buffer, size_t buflen,
270 const char* source, size_t srclen = SIZE_UNKNOWN);
271
Henrik Kjellanderc0362762017-06-29 08:03:04 +0200272#endif // WEBRTC_WIN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200273
274///////////////////////////////////////////////////////////////////////////////
275// Traits<char> specializations
276///////////////////////////////////////////////////////////////////////////////
277
278template<>
279struct Traits<char> {
280 typedef std::string string;
281 inline static const char* empty_str() { return ""; }
282};
283
284///////////////////////////////////////////////////////////////////////////////
285// Traits<wchar_t> specializations (Windows only, currently)
286///////////////////////////////////////////////////////////////////////////////
287
288#if defined(WEBRTC_WIN)
289
290template<>
291struct Traits<wchar_t> {
292 typedef std::wstring string;
293 inline static const wchar_t* empty_str() { return L""; }
294};
295
Henrik Kjellanderc0362762017-06-29 08:03:04 +0200296#endif // WEBRTC_WIN
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200297
298// Replaces all occurrences of "search" with "replace".
299void replace_substrs(const char *search,
300 size_t search_len,
301 const char *replace,
302 size_t replace_len,
303 std::string *s);
304
305// True iff s1 starts with s2.
306bool starts_with(const char *s1, const char *s2);
307
308// True iff s1 ends with s2.
309bool ends_with(const char *s1, const char *s2);
310
311// Remove leading and trailing whitespaces.
312std::string string_trim(const std::string& s);
313
314} // namespace rtc
315
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200316#endif // RTC_BASE_STRINGUTILS_H_