no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / xpcom / ds / nsCRT.h
blob14b46c505949899e8a6e9395701c8a70687a6763
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 #ifndef nsCRT_h___
7 #define nsCRT_h___
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include "plstr.h"
12 #include "nscore.h"
13 #include "nsCRTGlue.h"
15 #if defined(XP_WIN)
16 # define NS_LINEBREAK "\015\012"
17 # define NS_ULINEBREAK u"\015\012"
18 # define NS_LINEBREAK_LEN 2
19 #else
20 # ifdef XP_UNIX
21 # define NS_LINEBREAK "\012"
22 # define NS_ULINEBREAK u"\012"
23 # define NS_LINEBREAK_LEN 1
24 # endif /* XP_UNIX */
25 #endif /* XP_WIN */
27 /// This is a wrapper class around all the C runtime functions.
29 class nsCRT {
30 public:
31 enum {
32 LF = '\n' /* Line Feed */,
33 VTAB = '\v' /* Vertical Tab */,
34 CR = '\r' /* Carriage Return */
37 /// String comparison.
38 static int32_t strcmp(const char* aStr1, const char* aStr2) {
39 return int32_t(PL_strcmp(aStr1, aStr2));
42 /// Case-insensitive string comparison.
43 static int32_t strcasecmp(const char* aStr1, const char* aStr2) {
44 /* Some functions like `PL_strcasecmp` are reimplementations
45 * of the their native POSIX counterparts, which breaks libFuzzer.
46 * For this purpose, we use the natives instead when fuzzing.
48 #if defined(LIBFUZZER) && defined(LINUX)
49 return int32_t(::strcasecmp(aStr1, aStr2));
50 #else
51 return int32_t(PL_strcasecmp(aStr1, aStr2));
52 #endif
55 /// Case-insensitive string comparison with length
56 static int32_t strncasecmp(const char* aStr1, const char* aStr2,
57 uint32_t aMaxLen) {
58 #if defined(LIBFUZZER) && defined(LINUX)
59 int32_t result = int32_t(::strncasecmp(aStr1, aStr2, aMaxLen));
60 #else
61 int32_t result = int32_t(PL_strncasecmp(aStr1, aStr2, aMaxLen));
62 #endif
63 // Egads. PL_strncasecmp is returning *very* negative numbers.
64 // Some folks expect -1,0,1, so let's temper its enthusiasm.
65 if (result < 0) {
66 result = -1;
68 return result;
71 /// Case-insensitive substring search.
72 static char* strcasestr(const char* aStr1, const char* aStr2) {
73 #if defined(LIBFUZZER) && defined(LINUX)
74 return const_cast<char*>(::strcasestr(aStr1, aStr2));
75 #else
76 return PL_strcasestr(aStr1, aStr2);
77 #endif
80 /**
82 How to use this fancy (thread-safe) version of strtok:
84 void main(void) {
85 printf("%s\n\nTokens:\n", string);
86 // Establish string and get the first token:
87 char* newStr;
88 token = nsCRT::strtok(string, seps, &newStr);
89 while (token != nullptr) {
90 // While there are tokens in "string"
91 printf(" %s\n", token);
92 // Get next token:
93 token = nsCRT::strtok(newStr, seps, &newStr);
96 * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
97 * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
99 static char* strtok(char* aStr, const char* aDelims, char** aNewStr);
101 /// Like strcmp except for ucs2 strings
102 static int32_t strcmp(const char16_t* aStr1, const char16_t* aStr2);
104 // String to longlong
105 static int64_t atoll(const char* aStr);
107 static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
108 static char ToLower(char aChar) { return NS_ToLower(aChar); }
110 static bool IsAsciiSpace(char16_t aChar) {
111 return NS_IsAsciiWhitespace(aChar);
115 inline bool NS_IS_SPACE(char16_t aChar) {
116 return ((int(aChar) & 0x7f) == int(aChar)) && isspace(int(aChar));
119 #endif /* nsCRT_h___ */