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 : #ifndef nsCRT_h___
7 : #define nsCRT_h___
8 :
9 : #include <stdlib.h>
10 : #include <ctype.h>
11 : #include "plstr.h"
12 : #include "nscore.h"
13 : #include "nsCRTGlue.h"
14 :
15 : #if defined(XP_WIN)
16 : # define NS_LINEBREAK "\015\012"
17 : # define NS_LINEBREAK_LEN 2
18 : #else
19 : # ifdef XP_UNIX
20 : # define NS_LINEBREAK "\012"
21 : # define NS_LINEBREAK_LEN 1
22 : # endif /* XP_UNIX */
23 : #endif /* XP_WIN */
24 :
25 : extern const char16_t kIsoLatin1ToUCS2[256];
26 :
27 : /// This is a wrapper class around all the C runtime functions.
28 :
29 : class nsCRT
30 : {
31 : public:
32 : enum
33 : {
34 : LF = '\n' /* Line Feed */,
35 : VTAB = '\v' /* Vertical Tab */,
36 : CR = '\r' /* Carriage Return */
37 : };
38 :
39 : /// String comparison.
40 468 : static int32_t strcmp(const char* aStr1, const char* aStr2)
41 : {
42 468 : return int32_t(PL_strcmp(aStr1, aStr2));
43 : }
44 :
45 0 : static int32_t strncmp(const char* aStr1, const char* aStr2,
46 : uint32_t aMaxLen)
47 : {
48 0 : return int32_t(PL_strncmp(aStr1, aStr2, aMaxLen));
49 : }
50 :
51 : /// Case-insensitive string comparison.
52 30893 : static int32_t strcasecmp(const char* aStr1, const char* aStr2)
53 : {
54 30893 : return int32_t(PL_strcasecmp(aStr1, aStr2));
55 : }
56 :
57 : /// Case-insensitive string comparison with length
58 2 : static int32_t strncasecmp(const char* aStr1, const char* aStr2,
59 : uint32_t aMaxLen)
60 : {
61 2 : int32_t result = int32_t(PL_strncasecmp(aStr1, aStr2, aMaxLen));
62 : //Egads. PL_strncasecmp is returning *very* negative numbers.
63 : //Some folks expect -1,0,1, so let's temper its enthusiasm.
64 2 : if (result < 0) {
65 0 : result = -1;
66 : }
67 2 : return result;
68 : }
69 :
70 107 : static int32_t strncmp(const char* aStr1, const char* aStr2, int32_t aMaxLen)
71 : {
72 : // inline the first test (assumes strings are not null):
73 : int32_t diff =
74 107 : ((const unsigned char*)aStr1)[0] - ((const unsigned char*)aStr2)[0];
75 107 : if (diff != 0) {
76 105 : return diff;
77 : }
78 2 : return int32_t(PL_strncmp(aStr1, aStr2, unsigned(aMaxLen)));
79 : }
80 :
81 : /**
82 :
83 : How to use this fancy (thread-safe) version of strtok:
84 :
85 : void main(void) {
86 : printf("%s\n\nTokens:\n", string);
87 : // Establish string and get the first token:
88 : char* newStr;
89 : token = nsCRT::strtok(string, seps, &newStr);
90 : while (token != nullptr) {
91 : // While there are tokens in "string"
92 : printf(" %s\n", token);
93 : // Get next token:
94 : token = nsCRT::strtok(newStr, seps, &newStr);
95 : }
96 : }
97 : * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
98 : * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
99 : */
100 : static char* strtok(char* aStr, const char* aDelims, char** aNewStr);
101 :
102 : /// Like strcmp except for ucs2 strings
103 : static int32_t strcmp(const char16_t* aStr1, const char16_t* aStr2);
104 : /// Like strcmp except for ucs2 strings
105 : static int32_t strncmp(const char16_t* aStr1, const char16_t* aStr2,
106 : uint32_t aMaxLen);
107 :
108 : // The GNU libc has memmem, which is strstr except for binary data
109 : // This is our own implementation that uses memmem on platforms
110 : // where it's available.
111 : static const char* memmem(const char* aHaystack, uint32_t aHaystackLen,
112 : const char* aNeedle, uint32_t aNeedleLen);
113 :
114 : // String to longlong
115 : static int64_t atoll(const char* aStr);
116 :
117 4 : static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
118 3255 : static char ToLower(char aChar) { return NS_ToLower(aChar); }
119 :
120 : static bool IsUpper(char aChar) { return NS_IsUpper(aChar); }
121 0 : static bool IsLower(char aChar) { return NS_IsLower(aChar); }
122 :
123 0 : static bool IsAscii(char16_t aChar) { return NS_IsAscii(aChar); }
124 8541 : static bool IsAscii(const char16_t* aString) { return NS_IsAscii(aString); }
125 91570 : static bool IsAsciiAlpha(char16_t aChar) { return NS_IsAsciiAlpha(aChar); }
126 7362 : static bool IsAsciiDigit(char16_t aChar) { return NS_IsAsciiDigit(aChar); }
127 2982 : static bool IsAsciiSpace(char16_t aChar) { return NS_IsAsciiWhitespace(aChar); }
128 4425 : static bool IsAscii(const char* aString) { return NS_IsAscii(aString); }
129 0 : static bool IsAscii(const char* aString, uint32_t aLength)
130 : {
131 0 : return NS_IsAscii(aString, aLength);
132 : }
133 : };
134 :
135 :
136 : inline bool
137 0 : NS_IS_SPACE(char16_t aChar)
138 : {
139 0 : return ((int(aChar) & 0x7f) == int(aChar)) && isspace(int(aChar));
140 : }
141 :
142 : #define NS_IS_CNTRL(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
143 : #define NS_IS_DIGIT(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
144 : #if defined(XP_WIN)
145 : #define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
146 : #else
147 : #define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
148 : #endif
149 :
150 :
151 : #endif /* nsCRT_h___ */
|