nsCRTGlue.cpp 8.7 KB

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