xpcom/ds/nsCRT.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/ds/nsCRT.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +#ifndef nsCRT_h___
     1.9 +#define nsCRT_h___
    1.10 +
    1.11 +#include <stdlib.h>
    1.12 +#include <ctype.h>
    1.13 +#include "plstr.h"
    1.14 +#include "nscore.h"
    1.15 +#include "nsCRTGlue.h"
    1.16 +
    1.17 +#if defined(XP_WIN)
    1.18 +#  define NS_LINEBREAK           "\015\012"
    1.19 +#  define NS_LINEBREAK_LEN       2
    1.20 +#else
    1.21 +#  ifdef XP_UNIX
    1.22 +#    define NS_LINEBREAK         "\012"
    1.23 +#    define NS_LINEBREAK_LEN     1
    1.24 +#  endif /* XP_UNIX */
    1.25 +#endif /* XP_WIN */
    1.26 +
    1.27 +extern const char16_t kIsoLatin1ToUCS2[256];
    1.28 +
    1.29 +// This macro can be used in a class declaration for classes that want
    1.30 +// to ensure that their instance memory is zeroed.
    1.31 +#define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW   \
    1.32 +  void* operator new(size_t sz) CPP_THROW_NEW { \
    1.33 +    void* rv = ::operator new(sz);              \
    1.34 +    if (rv) {                                   \
    1.35 +      memset(rv, 0, sz);                        \
    1.36 +    }                                           \
    1.37 +    return rv;                                  \
    1.38 +  }                                             \
    1.39 +  void operator delete(void* ptr) {             \
    1.40 +    ::operator delete(ptr);                     \
    1.41 +  }
    1.42 +
    1.43 +// This macro works with the next macro to declare a non-inlined
    1.44 +// version of the above.
    1.45 +#define NS_DECL_ZEROING_OPERATOR_NEW           \
    1.46 +  void* operator new(size_t sz) CPP_THROW_NEW; \
    1.47 +  void operator delete(void* ptr);
    1.48 +
    1.49 +#define NS_IMPL_ZEROING_OPERATOR_NEW(_class)            \
    1.50 +  void* _class::operator new(size_t sz) CPP_THROW_NEW { \
    1.51 +    void* rv = ::operator new(sz);                      \
    1.52 +    if (rv) {                                           \
    1.53 +      memset(rv, 0, sz);                                \
    1.54 +    }                                                   \
    1.55 +    return rv;                                          \
    1.56 +  }                                                     \
    1.57 +  void _class::operator delete(void* ptr) {             \
    1.58 +    ::operator delete(ptr);                             \
    1.59 +  }
    1.60 +
    1.61 +// Freeing helper
    1.62 +#define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
    1.63 +
    1.64 +/// This is a wrapper class around all the C runtime functions. 
    1.65 +
    1.66 +class nsCRT {
    1.67 +public:
    1.68 +  enum {
    1.69 +    LF='\n'   /* Line Feed */,
    1.70 +    VTAB='\v' /* Vertical Tab */,
    1.71 +    CR='\r'   /* Carriage Return */
    1.72 +  };
    1.73 +
    1.74 +  /// Compare s1 and s2.
    1.75 +  static int32_t strcmp(const char* s1, const char* s2) {
    1.76 +    return int32_t(PL_strcmp(s1, s2));
    1.77 +  }
    1.78 +
    1.79 +  static int32_t strncmp(const char* s1, const char* s2,
    1.80 +                         uint32_t aMaxLen) {
    1.81 +    return int32_t(PL_strncmp(s1, s2, aMaxLen));
    1.82 +  }
    1.83 +
    1.84 +  /// Case-insensitive string comparison.
    1.85 +  static int32_t strcasecmp(const char* s1, const char* s2) {
    1.86 +    return int32_t(PL_strcasecmp(s1, s2));
    1.87 +  }
    1.88 +
    1.89 +  /// Case-insensitive string comparison with length
    1.90 +  static int32_t strncasecmp(const char* s1, const char* s2, uint32_t aMaxLen) {
    1.91 +    int32_t result=int32_t(PL_strncasecmp(s1, s2, aMaxLen));
    1.92 +    //Egads. PL_strncasecmp is returning *very* negative numbers.
    1.93 +    //Some folks expect -1,0,1, so let's temper its enthusiasm.
    1.94 +    if (result<0) 
    1.95 +      result=-1;
    1.96 +    return result;
    1.97 +  }
    1.98 +
    1.99 +  static int32_t strncmp(const char* s1, const char* s2, int32_t aMaxLen) {
   1.100 +    // inline the first test (assumes strings are not null):
   1.101 +    int32_t diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
   1.102 +    if (diff != 0) return diff;
   1.103 +    return int32_t(PL_strncmp(s1,s2,unsigned(aMaxLen)));
   1.104 +  }
   1.105 +  
   1.106 +  /**
   1.107 +
   1.108 +    How to use this fancy (thread-safe) version of strtok: 
   1.109 +
   1.110 +    void main(void) {
   1.111 +      printf("%s\n\nTokens:\n", string);
   1.112 +      // Establish string and get the first token:
   1.113 +      char* newStr;
   1.114 +      token = nsCRT::strtok(string, seps, &newStr);   
   1.115 +      while (token != nullptr) {
   1.116 +        // While there are tokens in "string"
   1.117 +        printf(" %s\n", token);
   1.118 +        // Get next token:
   1.119 +        token = nsCRT::strtok(newStr, seps, &newStr);
   1.120 +      }
   1.121 +    }
   1.122 +    * WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
   1.123 +    * MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
   1.124 +  */
   1.125 +  static char* strtok(char* str, const char* delims, char* *newStr); 
   1.126 +
   1.127 +  /// Like strcmp except for ucs2 strings
   1.128 +  static int32_t strcmp(const char16_t* s1, const char16_t* s2);
   1.129 +  /// Like strcmp except for ucs2 strings
   1.130 +  static int32_t strncmp(const char16_t* s1, const char16_t* s2,
   1.131 +                         uint32_t aMaxLen);
   1.132 +
   1.133 +  // The GNU libc has memmem, which is strstr except for binary data
   1.134 +  // This is our own implementation that uses memmem on platforms
   1.135 +  // where it's available.
   1.136 +  static const char* memmem(const char* haystack, uint32_t haystackLen,
   1.137 +                            const char* needle, uint32_t needleLen);
   1.138 +
   1.139 +  // String to longlong
   1.140 +  static int64_t atoll(const char *str);
   1.141 +  
   1.142 +  static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
   1.143 +  static char ToLower(char aChar) { return NS_ToLower(aChar); }
   1.144 +  
   1.145 +  static bool IsUpper(char aChar) { return NS_IsUpper(aChar); }
   1.146 +  static bool IsLower(char aChar) { return NS_IsLower(aChar); }
   1.147 +
   1.148 +  static bool IsAscii(char16_t aChar) { return NS_IsAscii(aChar); }
   1.149 +  static bool IsAscii(const char16_t* aString) { return NS_IsAscii(aString); }
   1.150 +  static bool IsAsciiAlpha(char16_t aChar) { return NS_IsAsciiAlpha(aChar); }
   1.151 +  static bool IsAsciiDigit(char16_t aChar) { return NS_IsAsciiDigit(aChar); }
   1.152 +  static bool IsAsciiSpace(char16_t aChar) { return NS_IsAsciiWhitespace(aChar); }
   1.153 +  static bool IsAscii(const char* aString) { return NS_IsAscii(aString); }
   1.154 +  static bool IsAscii(const char* aString, uint32_t aLength) { return NS_IsAscii(aString, aLength); }
   1.155 +};
   1.156 +
   1.157 +
   1.158 +inline bool
   1.159 +NS_IS_SPACE(char16_t c)
   1.160 +{
   1.161 +  return ((int(c) & 0x7f) == int(c)) && isspace(int(c));
   1.162 +}
   1.163 +
   1.164 +#define NS_IS_CNTRL(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
   1.165 +#define NS_IS_DIGIT(i)   ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
   1.166 +#if defined(XP_WIN)
   1.167 +#define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
   1.168 +#else
   1.169 +#define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
   1.170 +#endif
   1.171 +
   1.172 +
   1.173 +#endif /* nsCRT_h___ */

mercurial