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/. */
11 * Much as I hate to do it, we were using string compares wrong.
12 * Often, programmers call functions like strcmp(s1,s2), and pass
13 * one or more null strings. Rather than blow up on these, I've
14 * added quick checks to ensure that cases like this don't cause
17 * In general, if you pass a null into any of these string compare
18 * routines, we simply return 0.
25 //----------------------------------------------------------------------
28 ////////////////////////////////////////////////////////////////////////////////
29 // My lovely strtok routine
31 #define IS_DELIM(m, c) ((m)[(c) >> 3] & (1 << ((c) & 7)))
32 #define SET_DELIM(m, c) ((m)[(c) >> 3] |= (1 << ((c) & 7)))
33 #define DELIM_TABLE_SIZE 32
36 nsCRT::strtok(char* aString
, const char* aDelims
, char** aNewStr
)
39 "Unlike regular strtok, the first argument cannot be null.");
41 char delimTable
[DELIM_TABLE_SIZE
];
46 for (i
= 0; i
< DELIM_TABLE_SIZE
; ++i
) {
50 for (i
= 0; aDelims
[i
]; i
++) {
51 SET_DELIM(delimTable
, static_cast<uint8_t>(aDelims
[i
]));
53 NS_ASSERTION(aDelims
[i
] == '\0', "too many delimiters");
56 while (*str
&& IS_DELIM(delimTable
, static_cast<uint8_t>(*str
))) {
61 // fix up the end of the token
63 if (IS_DELIM(delimTable
, static_cast<uint8_t>(*str
))) {
71 return str
== result
? nullptr : result
;
74 ////////////////////////////////////////////////////////////////////////////////
77 * Compare unichar string ptrs, stopping at the 1st null
78 * NOTE: If both are null, we return 0.
79 * NOTE: We terminate the search upon encountering a nullptr
81 * @update gess 11/10/99
82 * @param s1 and s2 both point to unichar strings
83 * @return 0 if they match, -1 if s1<s2; 1 if s1>s2
86 nsCRT::strcmp(const char16_t
* aStr1
, const char16_t
* aStr2
)
90 char16_t c1
= *aStr1
++;
91 char16_t c2
= *aStr2
++;
98 if (c1
== 0 || c2
== 0) {
103 if (aStr1
) { // aStr2 must have been null
106 if (aStr2
) { // aStr1 must have been null
114 * Compare unichar string ptrs, stopping at the 1st null or nth char.
115 * NOTE: If either is null, we return 0.
116 * NOTE: We DO NOT terminate the search upon encountering nullptr's before N
118 * @update gess 11/10/99
119 * @param s1 and s2 both point to unichar strings
120 * @return 0 if they match, -1 if s1<s2; 1 if s1>s2
123 nsCRT::strncmp(const char16_t
* aStr1
, const char16_t
* aStr2
, uint32_t aNum
)
125 if (aStr1
&& aStr2
) {
128 char16_t c1
= *aStr1
++;
129 char16_t c2
= *aStr2
++;
136 } while (--aNum
!= 0);
143 nsCRT::memmem(const char* aHaystack
, uint32_t aHaystackLen
,
144 const char* aNeedle
, uint32_t aNeedleLen
)
147 if (!(aHaystack
&& aNeedle
&& aHaystackLen
&& aNeedleLen
&&
148 aNeedleLen
<= aHaystackLen
)) {
153 return (const char*)::memmem(aHaystack
, aHaystackLen
, aNeedle
, aNeedleLen
);
155 // No memmem means we need to roll our own. This isn't really optimized
156 // for performance ... if that becomes an issue we can take some inspiration
157 // from the js string compare code in jsstr.cpp
158 for (uint32_t i
= 0; i
< aHaystackLen
- aNeedleLen
; i
++) {
159 if (!memcmp(aHaystack
+ i
, aNeedle
, aNeedleLen
)) {
160 return aHaystack
+ i
;
167 // This should use NSPR but NSPR isn't exporting its PR_strtoll function
170 nsCRT::atoll(const char* aStr
)
178 while (*aStr
&& *aStr
>= '0' && *aStr
<= '9') {