1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
43 * Much as I hate to do it, we were using string compares wrong.
44 * Often, programmers call functions like strcmp(s1,s2), and pass
45 * one or more null strings. Rather than blow up on these, I've
46 * added quick checks to ensure that cases like this don't cause
49 * In general, if you pass a null into any of these string compare
50 * routines, we simply return 0.
55 #include "nsIServiceManager.h"
56 #include "nsCharTraits.h"
59 #define ADD_TO_HASHVAL(hashval, c) \
60 hashval = PR_ROTATE_LEFT32(hashval, 4) ^ (c);
62 //----------------------------------------------------------------------
65 ////////////////////////////////////////////////////////////////////////////////
66 // My lovely strtok routine
68 #define IS_DELIM(m, c) ((m)[(c) >> 3] & (1 << ((c) & 7)))
69 #define SET_DELIM(m, c) ((m)[(c) >> 3] |= (1 << ((c) & 7)))
70 #define DELIM_TABLE_SIZE 32
72 char* nsCRT::strtok(char* string
, const char* delims
, char* *newStr
)
74 NS_ASSERTION(string
, "Unlike regular strtok, the first argument cannot be null.");
76 char delimTable
[DELIM_TABLE_SIZE
];
81 for (i
= 0; i
< DELIM_TABLE_SIZE
; i
++)
84 for (i
= 0; delims
[i
]; i
++) {
85 SET_DELIM(delimTable
, static_cast<PRUint8
>(delims
[i
]));
87 NS_ASSERTION(delims
[i
] == '\0', "too many delimiters");
90 while (*str
&& IS_DELIM(delimTable
, static_cast<PRUint8
>(*str
))) {
95 // fix up the end of the token
97 if (IS_DELIM(delimTable
, static_cast<PRUint8
>(*str
))) {
105 return str
== result
? NULL
: result
;
108 ////////////////////////////////////////////////////////////////////////////////
111 * Compare unichar string ptrs, stopping at the 1st null
112 * NOTE: If both are null, we return 0.
113 * NOTE: We terminate the search upon encountering a NULL
115 * @update gess 11/10/99
116 * @param s1 and s2 both point to unichar strings
117 * @return 0 if they match, -1 if s1<s2; 1 if s1>s2
119 PRInt32
nsCRT::strcmp(const PRUnichar
* s1
, const PRUnichar
* s2
) {
122 PRUnichar c1
= *s1
++;
123 PRUnichar c2
= *s2
++;
125 if (c1
< c2
) return -1;
128 if ((0==c1
) || (0==c2
)) break;
132 if (s1
) // s2 must have been null
134 if (s2
) // s1 must have been null
141 * Compare unichar string ptrs, stopping at the 1st null or nth char.
142 * NOTE: If either is null, we return 0.
143 * NOTE: We DO NOT terminate the search upon encountering NULL's before N
145 * @update gess 11/10/99
146 * @param s1 and s2 both point to unichar strings
147 * @return 0 if they match, -1 if s1<s2; 1 if s1>s2
149 PRInt32
nsCRT::strncmp(const PRUnichar
* s1
, const PRUnichar
* s2
, PRUint32 n
) {
153 PRUnichar c1
= *s1
++;
154 PRUnichar c2
= *s2
++;
156 if (c1
< c2
) return -1;
165 PRUnichar
* nsCRT::strdup(const PRUnichar
* str
)
167 PRUint32 len
= nsCRT::strlen(str
);
168 return strndup(str
, len
);
171 PRUnichar
* nsCRT::strndup(const PRUnichar
* str
, PRUint32 len
)
173 nsCppSharedAllocator
<PRUnichar
> shared_allocator
;
174 PRUnichar
* rslt
= shared_allocator
.allocate(len
+ 1); // add one for the null
175 // PRUnichar* rslt = new PRUnichar[len + 1];
177 if (rslt
== NULL
) return NULL
;
178 memcpy(rslt
, str
, len
* sizeof(PRUnichar
));
184 * |nsCRT::HashCode| is identical to |PL_HashString|, which tests
185 * (http://bugzilla.mozilla.org/showattachment.cgi?attach_id=26596)
186 * show to be the best hash among several other choices.
188 * We re-implement it here rather than calling it for two reasons:
189 * (1) in this interface, we also calculate the length of the
190 * string being hashed; and (2) the narrow and wide and `buffer' versions here
191 * will hash equivalent strings to the same value, e.g., "Hello" and L"Hello".
193 PRUint32
nsCRT::HashCode(const char* str
, PRUint32
* resultingStrLen
)
202 ADD_TO_HASHVAL(h
, c
);
204 if ( resultingStrLen
)
205 *resultingStrLen
= (s
-str
)-1;
209 PRUint32
nsCRT::HashCode(const char* start
, PRUint32 length
)
212 const char* s
= start
;
213 const char* end
= start
+ length
;
218 ADD_TO_HASHVAL(h
, c
);
224 PRUint32
nsCRT::HashCode(const PRUnichar
* str
, PRUint32
* resultingStrLen
)
227 const PRUnichar
* s
= str
;
233 ADD_TO_HASHVAL(h
, c
);
235 if ( resultingStrLen
)
236 *resultingStrLen
= (s
-str
)-1;
240 PRUint32
nsCRT::HashCodeAsUTF8(const PRUnichar
* start
, PRUint32 length
)
243 const PRUnichar
* s
= start
;
244 const PRUnichar
* end
= start
+ length
;
246 PRUint16 W1
= 0; // the first UTF-16 word in a two word tuple
247 PRUint32 U
= 0; // the current char as UCS-4
248 int code_length
= 0; // the number of bytes in the UTF-8 sequence for the current char
255 * On the fly, decoding from UTF-16 (and/or UCS-2) into UTF-8 as per
256 * http://www.ietf.org/rfc/rfc2781.txt
257 * http://www.ietf.org/rfc/rfc3629.txt
262 if ( !IS_SURROGATE(W
) )
267 else if ( W
<= 0x07FF )
272 else if ( NS_IS_HIGH_SURROGATE(W
) && s
< end
)
280 // Treat broken characters as the Unicode replacement
286 NS_WARNING("Got low surrogate but no previous high surrogate");
291 // as required by the standard, this code is careful to
292 // throw out illegal sequences
294 if ( NS_IS_LOW_SURROGATE(W
) )
296 U
= SURROGATE_TO_UCS4(W1
, W
);
297 NS_ASSERTION(IS_VALID_CHAR(U
), "How did this happen?");
302 // Treat broken characters as the Unicode replacement
308 NS_WARNING("High surrogate not followed by low surrogate");
310 // The pointer to the next character points to the second 16-bit
311 // value, not beyond it, as per Unicode 5.0.0 Chapter 3 C10, only
312 // the first code unit of an illegal sequence must be treated as
313 // an illegally terminated code unit sequence (also Chapter 3
314 // D91, "isolated [not paired and ill-formed] UTF-16 code units
315 // in the range D800..DFFF are ill-formed").
323 static const PRUint16 sBytePrefix
[5] = { 0x0000, 0x0000, 0x00C0, 0x00E0, 0x00F0 };
324 static const PRUint16 sShift
[5] = { 0, 0, 6, 12, 18 };
327 * Unlike the algorithm in
328 * http://www.ietf.org/rfc/rfc3629.txt we must calculate the
329 * bytes in left to right order so that our hash result
330 * matches what the narrow version would calculate on an
331 * already UTF-8 string.
334 // hash the first (and often, only, byte)
335 ADD_TO_HASHVAL(h
, (sBytePrefix
[code_length
] | (U
>>sShift
[code_length
])));
337 // an unrolled loop for hashing any remaining bytes in this
339 switch ( code_length
)
340 { // falling through in each case
341 case 4: ADD_TO_HASHVAL(h
, (0x80 | ((U
>>12) & 0x003F)));
342 case 3: ADD_TO_HASHVAL(h
, (0x80 | ((U
>>6 ) & 0x003F)));
343 case 2: ADD_TO_HASHVAL(h
, (0x80 | ( U
& 0x003F)));
344 default: code_length
= 0;
352 PRUint32
nsCRT::BufferHashCode(const PRUnichar
* s
, PRUint32 len
)
355 const PRUnichar
* done
= s
+ len
;
358 h
= PR_ROTATE_LEFT32(h
, 4) ^ PRUint16(*s
++); // cast to unsigned to prevent possible sign extension
362 // This should use NSPR but NSPR isn't exporting its PR_strtoll function
364 PRInt64
nsCRT::atoll(const char *str
)
369 PRInt64 ll
= LL_Zero(), digitll
= LL_Zero();
371 while (*str
&& *str
>= '0' && *str
<= '9') {
373 LL_UI2L(digitll
, (*str
- '0'));
374 LL_ADD(ll
, ll
, digitll
);