xpcom/ds/nsCRT.h

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

mercurial