michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsCRTGlue.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsDebug.h" michael@0: #include "prtime.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef XP_WIN michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef ANDROID michael@0: #include michael@0: #endif michael@0: michael@0: const char* michael@0: NS_strspnp(const char *delims, const char *str) michael@0: { michael@0: const char *d; michael@0: do { michael@0: for (d = delims; *d != '\0'; ++d) { michael@0: if (*str == *d) { michael@0: ++str; michael@0: break; michael@0: } michael@0: } michael@0: } while (*d); michael@0: michael@0: return str; michael@0: } michael@0: michael@0: char* michael@0: NS_strtok(const char *delims, char **str) michael@0: { michael@0: if (!*str) michael@0: return nullptr; michael@0: michael@0: char *ret = (char*) NS_strspnp(delims, *str); michael@0: michael@0: if (!*ret) { michael@0: *str = ret; michael@0: return nullptr; michael@0: } michael@0: michael@0: char *i = ret; michael@0: do { michael@0: for (const char *d = delims; *d != '\0'; ++d) { michael@0: if (*i == *d) { michael@0: *i = '\0'; michael@0: *str = ++i; michael@0: return ret; michael@0: } michael@0: } michael@0: ++i; michael@0: } while (*i); michael@0: michael@0: *str = nullptr; michael@0: return ret; michael@0: } michael@0: michael@0: uint32_t michael@0: NS_strlen(const char16_t *aString) michael@0: { michael@0: const char16_t *end; michael@0: michael@0: for (end = aString; *end; ++end) { michael@0: // empty loop michael@0: } michael@0: michael@0: return end - aString; michael@0: } michael@0: michael@0: int michael@0: NS_strcmp(const char16_t *a, const char16_t *b) michael@0: { michael@0: while (*b) { michael@0: int r = *a - *b; michael@0: if (r) michael@0: return r; michael@0: michael@0: ++a; michael@0: ++b; michael@0: } michael@0: michael@0: return *a != '\0'; michael@0: } michael@0: michael@0: char16_t* michael@0: NS_strdup(const char16_t *aString) michael@0: { michael@0: uint32_t len = NS_strlen(aString); michael@0: return NS_strndup(aString, len); michael@0: } michael@0: michael@0: char16_t* michael@0: NS_strndup(const char16_t *aString, uint32_t aLen) michael@0: { michael@0: char16_t *newBuf = (char16_t*) NS_Alloc((aLen + 1) * sizeof(char16_t)); michael@0: if (newBuf) { michael@0: memcpy(newBuf, aString, aLen * sizeof(char16_t)); michael@0: newBuf[aLen] = '\0'; michael@0: } michael@0: return newBuf; michael@0: } michael@0: michael@0: char* michael@0: NS_strdup(const char *aString) michael@0: { michael@0: uint32_t len = strlen(aString); michael@0: char *str = (char*) NS_Alloc(len + 1); michael@0: if (str) { michael@0: memcpy(str, aString, len); michael@0: str[len] = '\0'; michael@0: } michael@0: return str; michael@0: } michael@0: michael@0: // This table maps uppercase characters to lower case characters; michael@0: // characters that are neither upper nor lower case are unaffected. michael@0: const unsigned char nsLowerUpperUtils::kUpper2Lower[256] = { michael@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, michael@0: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, michael@0: 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, michael@0: 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, michael@0: 64, michael@0: michael@0: // upper band mapped to lower [A-Z] => [a-z] michael@0: 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, michael@0: 112,113,114,115,116,117,118,119,120,121,122, michael@0: michael@0: 91, 92, 93, 94, 95, michael@0: 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111, michael@0: 112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127, michael@0: 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, michael@0: 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, michael@0: 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, michael@0: 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, michael@0: 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, michael@0: 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, michael@0: 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, michael@0: 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 michael@0: }; michael@0: michael@0: const unsigned char nsLowerUpperUtils::kLower2Upper[256] = { michael@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, michael@0: 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, michael@0: 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, michael@0: 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, michael@0: 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, michael@0: 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, michael@0: 96, michael@0: michael@0: // lower band mapped to upper [a-z] => [A-Z] michael@0: 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, michael@0: 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, michael@0: michael@0: 123,124,125,126,127, michael@0: 128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, michael@0: 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, michael@0: 160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175, michael@0: 176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, michael@0: 192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207, michael@0: 208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223, michael@0: 224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, michael@0: 240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255 michael@0: }; michael@0: michael@0: bool NS_IsUpper(char aChar) michael@0: { michael@0: return aChar != (char)nsLowerUpperUtils::kUpper2Lower[(unsigned char)aChar]; michael@0: } michael@0: michael@0: bool NS_IsLower(char aChar) michael@0: { michael@0: return aChar != (char)nsLowerUpperUtils::kLower2Upper[(unsigned char)aChar]; michael@0: } michael@0: michael@0: bool NS_IsAscii(char16_t aChar) michael@0: { michael@0: return (0x0080 > aChar); michael@0: } michael@0: michael@0: bool NS_IsAscii(const char16_t *aString) michael@0: { michael@0: while(*aString) { michael@0: if( 0x0080 <= *aString) michael@0: return false; michael@0: aString++; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool NS_IsAscii(const char *aString) michael@0: { michael@0: while(*aString) { michael@0: if( 0x80 & *aString) michael@0: return false; michael@0: aString++; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool NS_IsAscii(const char* aString, uint32_t aLength) michael@0: { michael@0: const char* end = aString + aLength; michael@0: while (aString < end) { michael@0: if (0x80 & *aString) michael@0: return false; michael@0: ++aString; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool NS_IsAsciiAlpha(char16_t aChar) michael@0: { michael@0: return ((aChar >= 'A') && (aChar <= 'Z')) || michael@0: ((aChar >= 'a') && (aChar <= 'z')); michael@0: } michael@0: michael@0: bool NS_IsAsciiWhitespace(char16_t aChar) michael@0: { michael@0: return aChar == ' ' || michael@0: aChar == '\r' || michael@0: aChar == '\n' || michael@0: aChar == '\t'; michael@0: } michael@0: michael@0: bool NS_IsAsciiDigit(char16_t aChar) michael@0: { michael@0: return aChar >= '0' && aChar <= '9'; michael@0: } michael@0: michael@0: michael@0: #ifndef XPCOM_GLUE_AVOID_NSPR michael@0: #define TABLE_SIZE 36 michael@0: static const char table[] = { michael@0: 'a','b','c','d','e','f','g','h','i','j', michael@0: 'k','l','m','n','o','p','q','r','s','t', michael@0: 'u','v','w','x','y','z','0','1','2','3', michael@0: '4','5','6','7','8','9' michael@0: }; michael@0: michael@0: void NS_MakeRandomString(char *aBuf, int32_t aBufLen) michael@0: { michael@0: // turn PR_Now() into milliseconds since epoch michael@0: // and salt rand with that. michael@0: static unsigned int seed = 0; michael@0: if (seed == 0) { michael@0: double fpTime = double(PR_Now()); michael@0: seed = (unsigned int)(fpTime * 1e-6 + 0.5); // use 1e-6, granularity of PR_Now() on the mac is seconds michael@0: srand(seed); michael@0: } michael@0: michael@0: int32_t i; michael@0: for (i=0;i