/src/mozilla-central/xpcom/ds/nsCRT.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | |
8 | | /** |
9 | | * MODULE NOTES: |
10 | | * @update gess7/30/98 |
11 | | * |
12 | | * Much as I hate to do it, we were using string compares wrong. |
13 | | * Often, programmers call functions like strcmp(s1,s2), and pass |
14 | | * one or more null strings. Rather than blow up on these, I've |
15 | | * added quick checks to ensure that cases like this don't cause |
16 | | * us to fail. |
17 | | * |
18 | | * In general, if you pass a null into any of these string compare |
19 | | * routines, we simply return 0. |
20 | | */ |
21 | | |
22 | | |
23 | | #include "nsCRT.h" |
24 | | #include "nsDebug.h" |
25 | | |
26 | | //---------------------------------------------------------------------- |
27 | | |
28 | | |
29 | | //////////////////////////////////////////////////////////////////////////////// |
30 | | // My lovely strtok routine |
31 | | |
32 | 75.1k | #define IS_DELIM(m, c) ((m)[(c) >> 3] & (1 << ((c) & 7))) |
33 | 7.82k | #define SET_DELIM(m, c) ((m)[(c) >> 3] |= (1 << ((c) & 7))) |
34 | 129k | #define DELIM_TABLE_SIZE 32 |
35 | | |
36 | | char* |
37 | | nsCRT::strtok(char* aString, const char* aDelims, char** aNewStr) |
38 | 3.91k | { |
39 | 3.91k | NS_ASSERTION(aString, |
40 | 3.91k | "Unlike regular strtok, the first argument cannot be null."); |
41 | 3.91k | |
42 | 3.91k | char delimTable[DELIM_TABLE_SIZE]; |
43 | 3.91k | uint32_t i; |
44 | 3.91k | char* result; |
45 | 3.91k | char* str = aString; |
46 | 3.91k | |
47 | 129k | for (i = 0; i < DELIM_TABLE_SIZE; ++i) { |
48 | 125k | delimTable[i] = '\0'; |
49 | 125k | } |
50 | 3.91k | |
51 | 11.7k | for (i = 0; aDelims[i]; i++) { |
52 | 7.82k | SET_DELIM(delimTable, static_cast<uint8_t>(aDelims[i])); |
53 | 7.82k | } |
54 | 3.91k | NS_ASSERTION(aDelims[i] == '\0', "too many delimiters"); |
55 | 3.91k | |
56 | 3.91k | // skip to beginning |
57 | 3.91k | while (*str && IS_DELIM(delimTable, static_cast<uint8_t>(*str))) { |
58 | 0 | str++; |
59 | 0 | } |
60 | 3.91k | result = str; |
61 | 3.91k | |
62 | 3.91k | // fix up the end of the token |
63 | 73.9k | while (*str) { |
64 | 72.1k | if (IS_DELIM(delimTable, static_cast<uint8_t>(*str))) { |
65 | 2.08k | *str++ = '\0'; |
66 | 2.08k | break; |
67 | 2.08k | } |
68 | 70.0k | str++; |
69 | 70.0k | } |
70 | 3.91k | *aNewStr = str; |
71 | 3.91k | |
72 | 3.91k | return str == result ? nullptr : result; |
73 | 3.91k | } |
74 | | |
75 | | //////////////////////////////////////////////////////////////////////////////// |
76 | | |
77 | | /** |
78 | | * Compare unichar string ptrs, stopping at the 1st null |
79 | | * NOTE: If both are null, we return 0. |
80 | | * NOTE: We terminate the search upon encountering a nullptr |
81 | | * |
82 | | * @update gess 11/10/99 |
83 | | * @param s1 and s2 both point to unichar strings |
84 | | * @return 0 if they match, -1 if s1<s2; 1 if s1>s2 |
85 | | */ |
86 | | int32_t |
87 | | nsCRT::strcmp(const char16_t* aStr1, const char16_t* aStr2) |
88 | 0 | { |
89 | 0 | if (aStr1 && aStr2) { |
90 | 0 | for (;;) { |
91 | 0 | char16_t c1 = *aStr1++; |
92 | 0 | char16_t c2 = *aStr2++; |
93 | 0 | if (c1 != c2) { |
94 | 0 | if (c1 < c2) { |
95 | 0 | return -1; |
96 | 0 | } |
97 | 0 | return 1; |
98 | 0 | } |
99 | 0 | if (c1 == 0 || c2 == 0) { |
100 | 0 | break; |
101 | 0 | } |
102 | 0 | } |
103 | 0 | } else { |
104 | 0 | if (aStr1) { // aStr2 must have been null |
105 | 0 | return -1; |
106 | 0 | } |
107 | 0 | if (aStr2) { // aStr1 must have been null |
108 | 0 | return 1; |
109 | 0 | } |
110 | 0 | } |
111 | 0 | return 0; |
112 | 0 | } |
113 | | |
114 | | // This should use NSPR but NSPR isn't exporting its PR_strtoll function |
115 | | // Until then... |
116 | | int64_t |
117 | | nsCRT::atoll(const char* aStr) |
118 | 0 | { |
119 | 0 | if (!aStr) { |
120 | 0 | return 0; |
121 | 0 | } |
122 | 0 | |
123 | 0 | int64_t ll = 0; |
124 | 0 |
|
125 | 0 | while (*aStr && *aStr >= '0' && *aStr <= '9') { |
126 | 0 | ll *= 10; |
127 | 0 | ll += *aStr - '0'; |
128 | 0 | aStr++; |
129 | 0 | } |
130 | 0 |
|
131 | 0 | return ll; |
132 | 0 | } |
133 | | |