2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1998-04-13 20:24:54 +00:00
|
|
|
#ifndef nsCRT_h___
|
|
|
|
#define nsCRT_h___
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2003-04-01 19:38:12 +00:00
|
|
|
#include <ctype.h>
|
1998-04-13 20:24:54 +00:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "nscore.h"
|
2006-05-03 16:11:14 +00:00
|
|
|
#include "nsCRTGlue.h"
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2005-06-23 23:42:02 +00:00
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
|
|
|
# define NS_LINEBREAK "\015\012"
|
|
|
|
# define NS_LINEBREAK_LEN 2
|
1999-06-22 18:50:12 +00:00
|
|
|
#else
|
Bug 627277 - Remove (broken) BeOS support. r=biesi,dwitte,gavin,joe,jorendorff,josh,khuey,mfinkle,neil,Pike,roc,shaver,smontagu,taras
2011-02-19 19:10:24 +00:00
|
|
|
# ifdef XP_UNIX
|
2005-06-23 23:42:02 +00:00
|
|
|
# define NS_LINEBREAK "\012"
|
|
|
|
# define NS_LINEBREAK_LEN 1
|
|
|
|
# endif /* XP_UNIX */
|
|
|
|
#endif /* XP_WIN || XP_OS2 */
|
1999-06-22 18:50:12 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
extern const char16_t kIsoLatin1ToUCS2[256];
|
1999-03-22 09:54:46 +00:00
|
|
|
|
1999-02-26 19:38:53 +00:00
|
|
|
// This macro can be used in a class declaration for classes that want
|
|
|
|
// to ensure that their instance memory is zeroed.
|
2002-10-08 06:12:00 +00:00
|
|
|
#define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW \
|
2002-07-02 20:25:30 +00:00
|
|
|
void* operator new(size_t sz) CPP_THROW_NEW { \
|
2002-10-08 06:12:00 +00:00
|
|
|
void* rv = ::operator new(sz); \
|
|
|
|
if (rv) { \
|
|
|
|
memset(rv, 0, sz); \
|
|
|
|
} \
|
|
|
|
return rv; \
|
|
|
|
} \
|
|
|
|
void operator delete(void* ptr) { \
|
|
|
|
::operator delete(ptr); \
|
1999-02-26 19:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This macro works with the next macro to declare a non-inlined
|
|
|
|
// version of the above.
|
2002-10-08 06:12:00 +00:00
|
|
|
#define NS_DECL_ZEROING_OPERATOR_NEW \
|
|
|
|
void* operator new(size_t sz) CPP_THROW_NEW; \
|
1999-02-26 19:38:53 +00:00
|
|
|
void operator delete(void* ptr);
|
|
|
|
|
2002-10-08 06:12:00 +00:00
|
|
|
#define NS_IMPL_ZEROING_OPERATOR_NEW(_class) \
|
2002-07-02 20:25:30 +00:00
|
|
|
void* _class::operator new(size_t sz) CPP_THROW_NEW { \
|
2002-10-08 06:12:00 +00:00
|
|
|
void* rv = ::operator new(sz); \
|
|
|
|
if (rv) { \
|
|
|
|
memset(rv, 0, sz); \
|
|
|
|
} \
|
|
|
|
return rv; \
|
|
|
|
} \
|
|
|
|
void _class::operator delete(void* ptr) { \
|
|
|
|
::operator delete(ptr); \
|
1999-02-26 19:38:53 +00:00
|
|
|
}
|
1999-02-09 04:43:58 +00:00
|
|
|
|
1999-08-23 08:27:42 +00:00
|
|
|
// Freeing helper
|
|
|
|
#define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
/// This is a wrapper class around all the C runtime functions.
|
|
|
|
|
2011-08-18 13:46:39 +00:00
|
|
|
class nsCRT {
|
1998-04-13 20:24:54 +00:00
|
|
|
public:
|
2001-04-18 05:59:43 +00:00
|
|
|
enum {
|
|
|
|
LF='\n' /* Line Feed */,
|
|
|
|
VTAB='\v' /* Vertical Tab */,
|
|
|
|
CR='\r' /* Carriage Return */
|
|
|
|
};
|
1999-02-09 04:19:58 +00:00
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
/// Compare s1 and s2.
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t strcmp(const char* s1, const char* s2) {
|
|
|
|
return int32_t(PL_strcmp(s1, s2));
|
1999-02-06 03:56:17 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t strncmp(const char* s1, const char* s2,
|
|
|
|
uint32_t aMaxLen) {
|
|
|
|
return int32_t(PL_strncmp(s1, s2, aMaxLen));
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Case-insensitive string comparison.
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t strcasecmp(const char* s1, const char* s2) {
|
|
|
|
return int32_t(PL_strcasecmp(s1, s2));
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
1998-04-30 16:40:38 +00:00
|
|
|
/// Case-insensitive string comparison with length
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t strncasecmp(const char* s1, const char* s2, uint32_t aMaxLen) {
|
|
|
|
int32_t result=int32_t(PL_strncasecmp(s1, s2, aMaxLen));
|
2002-10-08 06:12:00 +00:00
|
|
|
//Egads. PL_strncasecmp is returning *very* negative numbers.
|
|
|
|
//Some folks expect -1,0,1, so let's temper its enthusiasm.
|
|
|
|
if (result<0)
|
1999-12-02 10:14:38 +00:00
|
|
|
result=-1;
|
|
|
|
return result;
|
1998-04-30 16:40:38 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t strncmp(const char* s1, const char* s2, int32_t aMaxLen) {
|
1999-08-24 08:45:17 +00:00
|
|
|
// inline the first test (assumes strings are not null):
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
|
1999-08-24 08:45:17 +00:00
|
|
|
if (diff != 0) return diff;
|
2012-08-22 15:56:38 +00:00
|
|
|
return int32_t(PL_strncmp(s1,s2,unsigned(aMaxLen)));
|
1999-02-10 08:22:47 +00:00
|
|
|
}
|
|
|
|
|
1999-02-06 03:56:17 +00:00
|
|
|
/**
|
1999-05-04 20:28:24 +00:00
|
|
|
|
1999-02-06 03:56:17 +00:00
|
|
|
How to use this fancy (thread-safe) version of strtok:
|
|
|
|
|
2002-10-08 06:12:00 +00:00
|
|
|
void main(void) {
|
|
|
|
printf("%s\n\nTokens:\n", string);
|
1999-02-06 03:56:17 +00:00
|
|
|
// Establish string and get the first token:
|
|
|
|
char* newStr;
|
2002-10-08 06:12:00 +00:00
|
|
|
token = nsCRT::strtok(string, seps, &newStr);
|
2013-10-10 20:41:00 +00:00
|
|
|
while (token != nullptr) {
|
1999-02-06 03:56:17 +00:00
|
|
|
// While there are tokens in "string"
|
2002-10-08 06:12:00 +00:00
|
|
|
printf(" %s\n", token);
|
1999-02-06 03:56:17 +00:00
|
|
|
// Get next token:
|
2002-10-08 06:12:00 +00:00
|
|
|
token = nsCRT::strtok(newStr, seps, &newStr);
|
1999-02-06 03:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
1999-05-04 20:28:24 +00:00
|
|
|
* WARNING - STRTOK WHACKS str THE FIRST TIME IT IS CALLED *
|
|
|
|
* MAKE A COPY OF str IF YOU NEED TO USE IT AFTER strtok() *
|
1999-02-06 03:56:17 +00:00
|
|
|
*/
|
|
|
|
static char* strtok(char* str, const char* delims, char* *newStr);
|
|
|
|
|
2013-01-03 23:36:16 +00:00
|
|
|
/// Like strcmp except for ucs2 strings
|
2014-01-04 15:02:17 +00:00
|
|
|
static int32_t strcmp(const char16_t* s1, const char16_t* s2);
|
1998-04-13 20:24:54 +00:00
|
|
|
/// Like strcmp except for ucs2 strings
|
2014-01-04 15:02:17 +00:00
|
|
|
static int32_t strncmp(const char16_t* s1, const char16_t* s2,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aMaxLen);
|
1998-04-13 20:24:54 +00:00
|
|
|
|
2012-04-25 00:51:53 +00:00
|
|
|
// The GNU libc has memmem, which is strstr except for binary data
|
|
|
|
// This is our own implementation that uses memmem on platforms
|
|
|
|
// where it's available.
|
2012-08-22 15:56:38 +00:00
|
|
|
static const char* memmem(const char* haystack, uint32_t haystackLen,
|
|
|
|
const char* needle, uint32_t needleLen);
|
2012-04-25 00:51:53 +00:00
|
|
|
|
2001-10-18 04:23:25 +00:00
|
|
|
// String to longlong
|
2012-08-22 15:56:38 +00:00
|
|
|
static int64_t atoll(const char *str);
|
1999-03-02 18:24:42 +00:00
|
|
|
|
2006-05-03 16:11:14 +00:00
|
|
|
static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
|
|
|
|
static char ToLower(char aChar) { return NS_ToLower(aChar); }
|
2000-08-23 17:27:06 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool IsUpper(char aChar) { return NS_IsUpper(aChar); }
|
|
|
|
static bool IsLower(char aChar) { return NS_IsLower(aChar); }
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
static bool IsAscii(char16_t aChar) { return NS_IsAscii(aChar); }
|
|
|
|
static bool IsAscii(const char16_t* aString) { return NS_IsAscii(aString); }
|
|
|
|
static bool IsAsciiAlpha(char16_t aChar) { return NS_IsAsciiAlpha(aChar); }
|
|
|
|
static bool IsAsciiDigit(char16_t aChar) { return NS_IsAsciiDigit(aChar); }
|
|
|
|
static bool IsAsciiSpace(char16_t aChar) { return NS_IsAsciiWhitespace(aChar); }
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool IsAscii(const char* aString) { return NS_IsAscii(aString); }
|
2012-08-22 15:56:38 +00:00
|
|
|
static bool IsAscii(const char* aString, uint32_t aLength) { return NS_IsAscii(aString, aLength); }
|
1998-04-13 20:24:54 +00:00
|
|
|
};
|
|
|
|
|
2008-04-02 16:05:55 +00:00
|
|
|
|
2012-08-09 07:09:40 +00:00
|
|
|
inline bool
|
2014-01-04 15:02:17 +00:00
|
|
|
NS_IS_SPACE(char16_t c)
|
2012-08-09 07:09:40 +00:00
|
|
|
{
|
|
|
|
return ((int(c) & 0x7f) == int(c)) && isspace(int(c));
|
|
|
|
}
|
2003-04-01 19:38:12 +00:00
|
|
|
|
|
|
|
#define NS_IS_CNTRL(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : iscntrl(i))
|
|
|
|
#define NS_IS_DIGIT(i) ((((unsigned int) (i)) > 0x7f) ? (int) 0 : isdigit(i))
|
2003-04-02 08:31:39 +00:00
|
|
|
#if defined(XP_WIN) || defined(XP_OS2)
|
2003-04-01 19:38:12 +00:00
|
|
|
#define NS_IS_ALPHA(VAL) (isascii((int)(VAL)) && isalpha((int)(VAL)))
|
|
|
|
#else
|
|
|
|
#define NS_IS_ALPHA(VAL) ((((unsigned int) (VAL)) > 0x7f) ? (int) 0 : isalpha((int)(VAL)))
|
|
|
|
#endif
|
|
|
|
|
2002-02-12 03:57:39 +00:00
|
|
|
|
2001-04-18 05:59:43 +00:00
|
|
|
#endif /* nsCRT_h___ */
|