Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "nsCRTGlue.h"
8 : #include "nsXPCOM.h"
9 : #include "nsDebug.h"
10 : #include "prtime.h"
11 :
12 : #include <stdlib.h>
13 : #include <string.h>
14 : #include <stdio.h>
15 : #include <stdarg.h>
16 :
17 : #include "mozilla/Sprintf.h"
18 :
19 : #ifdef XP_WIN
20 : #include <io.h>
21 : #include <windows.h>
22 : #include "mozilla/UniquePtr.h"
23 : #endif
24 :
25 : #ifdef ANDROID
26 : #include <android/log.h>
27 : #endif
28 :
29 : using namespace mozilla;
30 :
31 : const char*
32 393 : NS_strspnp(const char* aDelims, const char* aStr)
33 : {
34 : const char* d;
35 284 : do {
36 2293 : for (d = aDelims; *d != '\0'; ++d) {
37 2184 : if (*aStr == *d) {
38 284 : ++aStr;
39 284 : break;
40 : }
41 : }
42 393 : } while (*d);
43 :
44 109 : return aStr;
45 : }
46 :
47 : char*
48 40 : NS_strtok(const char* aDelims, char** aStr)
49 : {
50 40 : if (!*aStr) {
51 0 : return nullptr;
52 : }
53 :
54 40 : char* ret = (char*)NS_strspnp(aDelims, *aStr);
55 :
56 40 : if (!*ret) {
57 7 : *aStr = ret;
58 7 : return nullptr;
59 : }
60 :
61 33 : char* i = ret;
62 583 : do {
63 1568 : for (const char* d = aDelims; *d != '\0'; ++d) {
64 984 : if (*i == *d) {
65 32 : *i = '\0';
66 32 : *aStr = ++i;
67 32 : return ret;
68 : }
69 : }
70 584 : ++i;
71 584 : } while (*i);
72 :
73 1 : *aStr = nullptr;
74 1 : return ret;
75 : }
76 :
77 : uint32_t
78 1178 : NS_strlen(const char16_t* aString)
79 : {
80 1178 : MOZ_ASSERT(aString);
81 : const char16_t* end;
82 :
83 1178 : for (end = aString; *end; ++end) {
84 : // empty loop
85 : }
86 :
87 1178 : return end - aString;
88 : }
89 :
90 : int
91 6 : NS_strcmp(const char16_t* aStrA, const char16_t* aStrB)
92 : {
93 6 : while (*aStrB) {
94 0 : int r = *aStrA - *aStrB;
95 0 : if (r) {
96 0 : return r;
97 : }
98 :
99 0 : ++aStrA;
100 0 : ++aStrB;
101 : }
102 :
103 6 : return *aStrA != '\0';
104 : }
105 :
106 : int
107 0 : NS_strncmp(const char16_t* aStrA, const char16_t* aStrB, size_t aLen)
108 : {
109 0 : while (aLen && *aStrB) {
110 0 : int r = *aStrA - *aStrB;
111 0 : if (r) {
112 0 : return r;
113 : }
114 :
115 0 : ++aStrA;
116 0 : ++aStrB;
117 0 : --aLen;
118 : }
119 :
120 0 : return aLen ? *aStrA != '\0' : *aStrA - *aStrB;
121 : }
122 :
123 : char16_t*
124 263 : NS_strdup(const char16_t* aString)
125 : {
126 263 : uint32_t len = NS_strlen(aString);
127 263 : return NS_strndup(aString, len);
128 : }
129 :
130 : template<typename CharT>
131 : CharT*
132 263 : NS_strndup(const CharT* aString, uint32_t aLen)
133 : {
134 263 : auto newBuf = (CharT*)NS_Alloc((aLen + 1) * sizeof(CharT));
135 263 : if (newBuf) {
136 263 : memcpy(newBuf, aString, aLen * sizeof(CharT));
137 263 : newBuf[aLen] = '\0';
138 : }
139 263 : return newBuf;
140 : }
141 :
142 : template char16_t* NS_strndup<char16_t>(const char16_t* aString, uint32_t aLen);
143 : template char* NS_strndup<char>(const char* aString, uint32_t aLen);
144 :
145 : char*
146 1302 : NS_strdup(const char* aString)
147 : {
148 1302 : uint32_t len = strlen(aString);
149 1302 : char* str = (char*)NS_Alloc(len + 1);
150 1302 : if (str) {
151 1302 : memcpy(str, aString, len);
152 1302 : str[len] = '\0';
153 : }
154 1302 : return str;
155 : }
156 :
157 : // This table maps uppercase characters to lower case characters;
158 : // characters that are neither upper nor lower case are unaffected.
159 : const unsigned char nsLowerUpperUtils::kUpper2Lower[256] = {
160 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
161 : 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
162 : 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
163 : 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
164 : 64,
165 :
166 : // upper band mapped to lower [A-Z] => [a-z]
167 : 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
168 : 112,113,114,115,116,117,118,119,120,121,122,
169 :
170 : 91, 92, 93, 94, 95,
171 : 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
172 : 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,
173 : 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
174 : 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
175 : 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
176 : 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
177 : 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
178 : 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
179 : 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
180 : 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
181 : };
182 :
183 : const unsigned char nsLowerUpperUtils::kLower2Upper[256] = {
184 : 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
185 : 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
186 : 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
187 : 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
188 : 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
189 : 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
190 : 96,
191 :
192 : // lower band mapped to upper [a-z] => [A-Z]
193 : 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
194 : 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
195 :
196 : 123,124,125,126,127,
197 : 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
198 : 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,
199 : 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,
200 : 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,
201 : 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,
202 : 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,
203 : 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,
204 : 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255
205 : };
206 :
207 : bool
208 0 : NS_IsUpper(char aChar)
209 : {
210 0 : return aChar != (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar];
211 : }
212 :
213 : bool
214 0 : NS_IsLower(char aChar)
215 : {
216 0 : return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar];
217 : }
218 :
219 : bool
220 0 : NS_IsAscii(char16_t aChar)
221 : {
222 0 : return (0x0080 > aChar);
223 : }
224 :
225 : bool
226 96372 : NS_IsAscii(const char16_t* aString)
227 : {
228 184203 : while (*aString) {
229 87831 : if (0x0080 <= *aString) {
230 0 : return false;
231 : }
232 87831 : aString++;
233 : }
234 8541 : return true;
235 : }
236 :
237 : bool
238 59610 : NS_IsAscii(const char* aString)
239 : {
240 114795 : while (*aString) {
241 55185 : if (0x80 & *aString) {
242 0 : return false;
243 : }
244 55185 : aString++;
245 : }
246 4425 : return true;
247 : }
248 :
249 : bool
250 0 : NS_IsAscii(const char* aString, uint32_t aLength)
251 : {
252 0 : const char* end = aString + aLength;
253 0 : while (aString < end) {
254 0 : if (0x80 & *aString) {
255 0 : return false;
256 : }
257 0 : ++aString;
258 : }
259 0 : return true;
260 : }
261 :
262 : bool
263 91570 : NS_IsAsciiAlpha(char16_t aChar)
264 : {
265 267077 : return (aChar >= 'A' && aChar <= 'Z') ||
266 266821 : (aChar >= 'a' && aChar <= 'z');
267 : }
268 :
269 : bool
270 18353 : NS_IsAsciiWhitespace(char16_t aChar)
271 : {
272 16290 : return aChar == ' ' ||
273 16290 : aChar == '\r' ||
274 34643 : aChar == '\n' ||
275 18353 : aChar == '\t';
276 : }
277 :
278 : bool
279 7362 : NS_IsAsciiDigit(char16_t aChar)
280 : {
281 7362 : return aChar >= '0' && aChar <= '9';
282 : }
283 :
284 : #ifndef XPCOM_GLUE_AVOID_NSPR
285 :
286 : void
287 0 : NS_MakeRandomString(char* aBuf, int32_t aBufLen)
288 : {
289 : #define TABLE_SIZE 36
290 : static const char table[] = {
291 : 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
292 : 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
293 : 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
294 : '4', '5', '6', '7', '8', '9'
295 : };
296 :
297 : // turn PR_Now() into milliseconds since epoch
298 : // and salt rand with that.
299 : static unsigned int seed = 0;
300 0 : if (seed == 0) {
301 0 : double fpTime = double(PR_Now());
302 0 : seed = (unsigned int)(fpTime * 1e-6 + 0.5); // use 1e-6, granularity of PR_Now() on the mac is seconds
303 0 : srand(seed);
304 : }
305 :
306 : int32_t i;
307 0 : for (i = 0; i < aBufLen; ++i) {
308 0 : *aBuf++ = table[rand() % TABLE_SIZE];
309 : }
310 0 : *aBuf = 0;
311 0 : }
312 :
313 : #endif
314 :
315 : #ifdef HAVE_VA_COPY
316 : #define VARARGS_ASSIGN(foo, bar) VA_COPY(foo,bar)
317 : #elif defined(HAVE_VA_LIST_AS_ARRAY)
318 : #define VARARGS_ASSIGN(foo, bar) foo[0] = bar[0]
319 : #else
320 : #define VARARGS_ASSIGN(foo, bar) (foo) = (bar)
321 : #endif
322 :
323 : #if defined(XP_WIN)
324 : void
325 : vprintf_stderr(const char* aFmt, va_list aArgs)
326 : {
327 : if (IsDebuggerPresent()) {
328 : int lengthNeeded = _vscprintf(aFmt, aArgs);
329 : if (lengthNeeded) {
330 : lengthNeeded++;
331 : auto buf = MakeUnique<char[]>(lengthNeeded);
332 : if (buf) {
333 : va_list argsCpy;
334 : VARARGS_ASSIGN(argsCpy, aArgs);
335 : vsnprintf(buf.get(), lengthNeeded, aFmt, argsCpy);
336 : buf[lengthNeeded - 1] = '\0';
337 : va_end(argsCpy);
338 : OutputDebugStringA(buf.get());
339 : }
340 : }
341 : }
342 :
343 : FILE* fp = _fdopen(_dup(2), "a");
344 : if (!fp) {
345 : return;
346 : }
347 :
348 : vfprintf(fp, aFmt, aArgs);
349 :
350 : fclose(fp);
351 : }
352 :
353 : #elif defined(ANDROID)
354 : void
355 : vprintf_stderr(const char* aFmt, va_list aArgs)
356 : {
357 : __android_log_vprint(ANDROID_LOG_INFO, "Gecko", aFmt, aArgs);
358 : }
359 : #else
360 : void
361 23 : vprintf_stderr(const char* aFmt, va_list aArgs)
362 : {
363 23 : vfprintf(stderr, aFmt, aArgs);
364 23 : }
365 : #endif
366 :
367 : void
368 23 : printf_stderr(const char* aFmt, ...)
369 : {
370 : va_list args;
371 23 : va_start(args, aFmt);
372 23 : vprintf_stderr(aFmt, args);
373 23 : va_end(args);
374 23 : }
375 :
376 : void
377 0 : fprintf_stderr(FILE* aFile, const char* aFmt, ...)
378 : {
379 : va_list args;
380 0 : va_start(args, aFmt);
381 0 : if (aFile == stderr) {
382 0 : vprintf_stderr(aFmt, args);
383 : } else {
384 0 : vfprintf(aFile, aFmt, args);
385 : }
386 0 : va_end(args);
387 0 : }
|