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/. */
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.
24 //----------------------------------------------------------------------
26 ////////////////////////////////////////////////////////////////////////////////
27 // My lovely strtok routine
29 #define IS_DELIM(m, c) ((m)[(c) >> 3] & (1 << ((c)&7)))
30 #define SET_DELIM(m, c) ((m)[(c) >> 3] |= (1 << ((c)&7)))
31 #define DELIM_TABLE_SIZE 32
33 char* nsCRT::strtok(char* aString
, const char* aDelims
, char** aNewStr
) {
35 "Unlike regular strtok, the first argument cannot be null.");
37 char delimTable
[DELIM_TABLE_SIZE
];
42 for (i
= 0; i
< DELIM_TABLE_SIZE
; ++i
) {
46 for (i
= 0; aDelims
[i
]; i
++) {
47 SET_DELIM(delimTable
, static_cast<uint8_t>(aDelims
[i
]));
49 NS_ASSERTION(aDelims
[i
] == '\0', "too many delimiters");
52 while (*str
&& IS_DELIM(delimTable
, static_cast<uint8_t>(*str
))) {
57 // fix up the end of the token
59 if (IS_DELIM(delimTable
, static_cast<uint8_t>(*str
))) {
67 return str
== result
? nullptr : result
;
70 ////////////////////////////////////////////////////////////////////////////////
73 * Compare unichar string ptrs, stopping at the 1st null
74 * NOTE: If both are null, we return 0.
75 * NOTE: We terminate the search upon encountering a nullptr
77 * @update gess 11/10/99
78 * @param s1 and s2 both point to unichar strings
79 * @return 0 if they match, -1 if s1<s2; 1 if s1>s2
81 int32_t nsCRT::strcmp(const char16_t
* aStr1
, const char16_t
* aStr2
) {
84 char16_t c1
= *aStr1
++;
85 char16_t c2
= *aStr2
++;
92 if (c1
== 0 || c2
== 0) {
97 if (aStr1
) { // aStr2 must have been null
100 if (aStr2
) { // aStr1 must have been null
107 // This should use NSPR but NSPR isn't exporting its PR_strtoll function
109 int64_t nsCRT::atoll(const char* aStr
) {
116 while (*aStr
&& *aStr
>= '0' && *aStr
<= '9') {