Bug 1866894 - Update failing subtest for content-visibility-auto-resize.html. r=fredw
[gecko.git] / xpcom / ds / nsCRT.cpp
blob9d2fcb712046718377e203d1e8f61f4a589943a4
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/. */
7 /**
8 * MODULE NOTES:
9 * @update gess7/30/98
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
15 * us to fail.
17 * In general, if you pass a null into any of these string compare
18 * routines, we simply return 0.
21 #include "nsCRT.h"
22 #include "nsDebug.h"
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) {
34 NS_ASSERTION(aString,
35 "Unlike regular strtok, the first argument cannot be null.");
37 char delimTable[DELIM_TABLE_SIZE];
38 uint32_t i;
39 char* result;
40 char* str = aString;
42 for (i = 0; i < DELIM_TABLE_SIZE; ++i) {
43 delimTable[i] = '\0';
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");
51 // skip to beginning
52 while (*str && IS_DELIM(delimTable, static_cast<uint8_t>(*str))) {
53 str++;
55 result = str;
57 // fix up the end of the token
58 while (*str) {
59 if (IS_DELIM(delimTable, static_cast<uint8_t>(*str))) {
60 *str++ = '\0';
61 break;
63 str++;
65 *aNewStr = str;
67 return str == result ? nullptr : result;
70 ////////////////////////////////////////////////////////////////////////////////
72 /**
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) {
82 if (aStr1 && aStr2) {
83 for (;;) {
84 char16_t c1 = *aStr1++;
85 char16_t c2 = *aStr2++;
86 if (c1 != c2) {
87 if (c1 < c2) {
88 return -1;
90 return 1;
92 if (c1 == 0 || c2 == 0) {
93 break;
96 } else {
97 if (aStr1) { // aStr2 must have been null
98 return -1;
100 if (aStr2) { // aStr1 must have been null
101 return 1;
104 return 0;
107 // This should use NSPR but NSPR isn't exporting its PR_strtoll function
108 // Until then...
109 int64_t nsCRT::atoll(const char* aStr) {
110 if (!aStr) {
111 return 0;
114 int64_t ll = 0;
116 while (*aStr && *aStr >= '0' && *aStr <= '9') {
117 ll *= 10;
118 ll += *aStr - '0';
119 aStr++;
122 return ll;