1 /* -*- Mode: C++; tab-width: 2; 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/. */
12 #include "nsCRTGlue.h"
15 # define NS_LINEBREAK "\015\012"
16 # define NS_LINEBREAK_LEN 2
19 # define NS_LINEBREAK "\012"
20 # define NS_LINEBREAK_LEN 1
24 extern const char16_t kIsoLatin1ToUCS2
[256];
26 // This macro can be used in a class declaration for classes that want
27 // to ensure that their instance memory is zeroed.
28 #define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW \
29 void* operator new(size_t sz) CPP_THROW_NEW { \
30 void* rv = ::operator new(sz); \
36 void operator delete(void* ptr) { \
37 ::operator delete(ptr); \
40 // This macro works with the next macro to declare a non-inlined
41 // version of the above.
42 #define NS_DECL_ZEROING_OPERATOR_NEW \
43 void* operator new(size_t sz) CPP_THROW_NEW; \
44 void operator delete(void* ptr);
46 #define NS_IMPL_ZEROING_OPERATOR_NEW(_class) \
47 void* _class::operator new(size_t sz) CPP_THROW_NEW { \
48 void* rv = ::operator new(sz); \
54 void _class::operator delete(void* ptr) { \
55 ::operator delete(ptr); \
59 #define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
61 /// This is a wrapper class around all the C runtime functions.
68 LF
= '\n' /* Line Feed */,
69 VTAB
= '\v' /* Vertical Tab */,
70 CR
= '\r' /* Carriage Return */
73 /// String comparison.
74 static int32_t strcmp(const char* aStr1
, const char* aStr2
)
76 return int32_t(PL_strcmp(aStr1
, aStr2
));
79 static int32_t strncmp(const char* aStr1
, const char* aStr2
,
82 return int32_t(PL_strncmp(aStr1
, aStr2
, aMaxLen
));
85 /// Case-insensitive string comparison.
86 static int32_t strcasecmp(const char* aStr1
, const char* aStr2
)
88 return int32_t(PL_strcasecmp(aStr1
, aStr2
));
91 /// Case-insensitive string comparison with length
92 static int32_t strncasecmp(const char* aStr1
, const char* aStr2
,
95 int32_t result
= int32_t(PL_strncasecmp(aStr1
, aStr2
, aMaxLen
));
96 //Egads. PL_strncasecmp is returning *very* negative numbers.
97 //Some folks expect -1,0,1, so let's temper its enthusiasm.
104 static int32_t strncmp(const char* aStr1
, const char* aStr2
, int32_t aMaxLen
)
106 // inline the first test (assumes strings are not null):
108 ((const unsigned char*)aStr1
)[0] - ((const unsigned char*)aStr2
)[0];
112 return int32_t(PL_strncmp(aStr1
, aStr2
, unsigned(aMaxLen
)));
117 How to use this fancy (thread-safe) version of strtok:
120 printf("%s\n\nTokens:\n", string);
121 // Establish string and get the first token:
123 token = nsCRT::strtok(string, seps, &newStr);
124 while (token != nullptr) {
125 // While there are tokens in "string"
126 printf(" %s\n", token);
128 token = nsCRT::strtok(newStr, seps, &newStr);
131 * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
132 * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
134 static char* strtok(char* aStr
, const char* aDelims
, char** aNewStr
);
136 /// Like strcmp except for ucs2 strings
137 static int32_t strcmp(const char16_t
* aStr1
, const char16_t
* aStr2
);
138 /// Like strcmp except for ucs2 strings
139 static int32_t strncmp(const char16_t
* aStr1
, const char16_t
* aStr2
,
142 // The GNU libc has memmem, which is strstr except for binary data
143 // This is our own implementation that uses memmem on platforms
144 // where it's available.
145 static const char* memmem(const char* aHaystack
, uint32_t aHaystackLen
,
146 const char* aNeedle
, uint32_t aNeedleLen
);
148 // String to longlong
149 static int64_t atoll(const char* aStr
);
151 static char ToUpper(char aChar
) { return NS_ToUpper(aChar
); }
152 static char ToLower(char aChar
) { return NS_ToLower(aChar
); }
154 static bool IsUpper(char aChar
) { return NS_IsUpper(aChar
); }
155 static bool IsLower(char aChar
) { return NS_IsLower(aChar
); }
157 static bool IsAscii(char16_t aChar
) { return NS_IsAscii(aChar
); }
158 static bool IsAscii(const char16_t
* aString
) { return NS_IsAscii(aString
); }
159 static bool IsAsciiAlpha(char16_t aChar
) { return NS_IsAsciiAlpha(aChar
); }
160 static bool IsAsciiDigit(char16_t aChar
) { return NS_IsAsciiDigit(aChar
); }
161 static bool IsAsciiSpace(char16_t aChar
) { return NS_IsAsciiWhitespace(aChar
); }
162 static bool IsAscii(const char* aString
) { return NS_IsAscii(aString
); }
163 static bool IsAscii(const char* aString
, uint32_t aLength
)
165 return NS_IsAscii(aString
, aLength
);
171 NS_IS_SPACE(char16_t aChar
)
173 return ((int(aChar
) & 0x7f) == int(aChar
)) && isspace(int(aChar
));
176 #define NS_IS_CNTRL(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
177 #define NS_IS_DIGIT(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
179 #define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
181 #define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
185 #endif /* nsCRT_h___ */