xpcom/ds/nsCRT.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 #ifndef nsCRT_h___
michael@0 6 #define nsCRT_h___
michael@0 7
michael@0 8 #include <stdlib.h>
michael@0 9 #include <ctype.h>
michael@0 10 #include "plstr.h"
michael@0 11 #include "nscore.h"
michael@0 12 #include "nsCRTGlue.h"
michael@0 13
michael@0 14 #if defined(XP_WIN)
michael@0 15 # define NS_LINEBREAK "\015\012"
michael@0 16 # define NS_LINEBREAK_LEN 2
michael@0 17 #else
michael@0 18 # ifdef XP_UNIX
michael@0 19 # define NS_LINEBREAK "\012"
michael@0 20 # define NS_LINEBREAK_LEN 1
michael@0 21 # endif /* XP_UNIX */
michael@0 22 #endif /* XP_WIN */
michael@0 23
michael@0 24 extern const char16_t kIsoLatin1ToUCS2[256];
michael@0 25
michael@0 26 // This macro can be used in a class declaration for classes that want
michael@0 27 // to ensure that their instance memory is zeroed.
michael@0 28 #define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW \
michael@0 29 void* operator new(size_t sz) CPP_THROW_NEW { \
michael@0 30 void* rv = ::operator new(sz); \
michael@0 31 if (rv) { \
michael@0 32 memset(rv, 0, sz); \
michael@0 33 } \
michael@0 34 return rv; \
michael@0 35 } \
michael@0 36 void operator delete(void* ptr) { \
michael@0 37 ::operator delete(ptr); \
michael@0 38 }
michael@0 39
michael@0 40 // This macro works with the next macro to declare a non-inlined
michael@0 41 // version of the above.
michael@0 42 #define NS_DECL_ZEROING_OPERATOR_NEW \
michael@0 43 void* operator new(size_t sz) CPP_THROW_NEW; \
michael@0 44 void operator delete(void* ptr);
michael@0 45
michael@0 46 #define NS_IMPL_ZEROING_OPERATOR_NEW(_class) \
michael@0 47 void* _class::operator new(size_t sz) CPP_THROW_NEW { \
michael@0 48 void* rv = ::operator new(sz); \
michael@0 49 if (rv) { \
michael@0 50 memset(rv, 0, sz); \
michael@0 51 } \
michael@0 52 return rv; \
michael@0 53 } \
michael@0 54 void _class::operator delete(void* ptr) { \
michael@0 55 ::operator delete(ptr); \
michael@0 56 }
michael@0 57
michael@0 58 // Freeing helper
michael@0 59 #define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
michael@0 60
michael@0 61 /// This is a wrapper class around all the C runtime functions.
michael@0 62
michael@0 63 class nsCRT {
michael@0 64 public:
michael@0 65 enum {
michael@0 66 LF='\n' /* Line Feed */,
michael@0 67 VTAB='\v' /* Vertical Tab */,
michael@0 68 CR='\r' /* Carriage Return */
michael@0 69 };
michael@0 70
michael@0 71 /// Compare s1 and s2.
michael@0 72 static int32_t strcmp(const char* s1, const char* s2) {
michael@0 73 return int32_t(PL_strcmp(s1, s2));
michael@0 74 }
michael@0 75
michael@0 76 static int32_t strncmp(const char* s1, const char* s2,
michael@0 77 uint32_t aMaxLen) {
michael@0 78 return int32_t(PL_strncmp(s1, s2, aMaxLen));
michael@0 79 }
michael@0 80
michael@0 81 /// Case-insensitive string comparison.
michael@0 82 static int32_t strcasecmp(const char* s1, const char* s2) {
michael@0 83 return int32_t(PL_strcasecmp(s1, s2));
michael@0 84 }
michael@0 85
michael@0 86 /// Case-insensitive string comparison with length
michael@0 87 static int32_t strncasecmp(const char* s1, const char* s2, uint32_t aMaxLen) {
michael@0 88 int32_t result=int32_t(PL_strncasecmp(s1, s2, aMaxLen));
michael@0 89 //Egads. PL_strncasecmp is returning *very* negative numbers.
michael@0 90 //Some folks expect -1,0,1, so let's temper its enthusiasm.
michael@0 91 if (result<0)
michael@0 92 result=-1;
michael@0 93 return result;
michael@0 94 }
michael@0 95
michael@0 96 static int32_t strncmp(const char* s1, const char* s2, int32_t aMaxLen) {
michael@0 97 // inline the first test (assumes strings are not null):
michael@0 98 int32_t diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
michael@0 99 if (diff != 0) return diff;
michael@0 100 return int32_t(PL_strncmp(s1,s2,unsigned(aMaxLen)));
michael@0 101 }
michael@0 102
michael@0 103 /**
michael@0 104
michael@0 105 How to use this fancy (thread-safe) version of strtok:
michael@0 106
michael@0 107 void main(void) {
michael@0 108 printf("%s\n\nTokens:\n", string);
michael@0 109 // Establish string and get the first token:
michael@0 110 char* newStr;
michael@0 111 token = nsCRT::strtok(string, seps, &newStr);
michael@0 112 while (token != nullptr) {
michael@0 113 // While there are tokens in "string"
michael@0 114 printf(" %s\n", token);
michael@0 115 // Get next token:
michael@0 116 token = nsCRT::strtok(newStr, seps, &newStr);
michael@0 117 }
michael@0 118 }
michael@0 119 * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
michael@0 120 * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
michael@0 121 */
michael@0 122 static char* strtok(char* str, const char* delims, char* *newStr);
michael@0 123
michael@0 124 /// Like strcmp except for ucs2 strings
michael@0 125 static int32_t strcmp(const char16_t* s1, const char16_t* s2);
michael@0 126 /// Like strcmp except for ucs2 strings
michael@0 127 static int32_t strncmp(const char16_t* s1, const char16_t* s2,
michael@0 128 uint32_t aMaxLen);
michael@0 129
michael@0 130 // The GNU libc has memmem, which is strstr except for binary data
michael@0 131 // This is our own implementation that uses memmem on platforms
michael@0 132 // where it's available.
michael@0 133 static const char* memmem(const char* haystack, uint32_t haystackLen,
michael@0 134 const char* needle, uint32_t needleLen);
michael@0 135
michael@0 136 // String to longlong
michael@0 137 static int64_t atoll(const char *str);
michael@0 138
michael@0 139 static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
michael@0 140 static char ToLower(char aChar) { return NS_ToLower(aChar); }
michael@0 141
michael@0 142 static bool IsUpper(char aChar) { return NS_IsUpper(aChar); }
michael@0 143 static bool IsLower(char aChar) { return NS_IsLower(aChar); }
michael@0 144
michael@0 145 static bool IsAscii(char16_t aChar) { return NS_IsAscii(aChar); }
michael@0 146 static bool IsAscii(const char16_t* aString) { return NS_IsAscii(aString); }
michael@0 147 static bool IsAsciiAlpha(char16_t aChar) { return NS_IsAsciiAlpha(aChar); }
michael@0 148 static bool IsAsciiDigit(char16_t aChar) { return NS_IsAsciiDigit(aChar); }
michael@0 149 static bool IsAsciiSpace(char16_t aChar) { return NS_IsAsciiWhitespace(aChar); }
michael@0 150 static bool IsAscii(const char* aString) { return NS_IsAscii(aString); }
michael@0 151 static bool IsAscii(const char* aString, uint32_t aLength) { return NS_IsAscii(aString, aLength); }
michael@0 152 };
michael@0 153
michael@0 154
michael@0 155 inline bool
michael@0 156 NS_IS_SPACE(char16_t c)
michael@0 157 {
michael@0 158 return ((int(c) & 0x7f) == int(c)) && isspace(int(c));
michael@0 159 }
michael@0 160
michael@0 161 #define NS_IS_CNTRL(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
michael@0 162 #define NS_IS_DIGIT(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
michael@0 163 #if defined(XP_WIN)
michael@0 164 #define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
michael@0 165 #else
michael@0 166 #define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
michael@0 167 #endif
michael@0 168
michael@0 169
michael@0 170 #endif /* nsCRT_h___ */

mercurial