2015-04-09 20:25:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04: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-14 00:24:54 +04:00
|
|
|
#ifndef nsCRT_h___
|
|
|
|
#define nsCRT_h___
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2003-04-01 23:38:12 +04:00
|
|
|
#include <ctype.h>
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "nscore.h"
|
2006-05-03 20:11:14 +04:00
|
|
|
#include "nsCRTGlue.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_WIN)
|
2005-06-24 03:42:02 +04:00
|
|
|
# define NS_LINEBREAK "\015\012"
|
|
|
|
# define NS_LINEBREAK_LEN 2
|
1999-06-22 22:50:12 +04: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 22:10:24 +03:00
|
|
|
# ifdef XP_UNIX
|
2005-06-24 03:42:02 +04:00
|
|
|
# define NS_LINEBREAK "\012"
|
|
|
|
# define NS_LINEBREAK_LEN 1
|
|
|
|
# endif /* XP_UNIX */
|
2014-02-11 02:57:01 +04:00
|
|
|
#endif /* XP_WIN */
|
1999-06-22 22:50:12 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
extern const char16_t kIsoLatin1ToUCS2[256];
|
1999-03-22 12:54:46 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
/// This is a wrapper class around all the C runtime functions.
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
class nsCRT
|
|
|
|
{
|
1998-04-14 00:24:54 +04:00
|
|
|
public:
|
2014-07-09 19:15:21 +04:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
LF = '\n' /* Line Feed */,
|
|
|
|
VTAB = '\v' /* Vertical Tab */,
|
|
|
|
CR = '\r' /* Carriage Return */
|
2001-04-18 09:59:43 +04:00
|
|
|
};
|
1999-02-09 07:19:58 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
/// String comparison.
|
|
|
|
static int32_t strcmp(const char* aStr1, const char* aStr2)
|
|
|
|
{
|
|
|
|
return int32_t(PL_strcmp(aStr1, aStr2));
|
1999-02-06 06:56:17 +03:00
|
|
|
}
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/// Case-insensitive string comparison.
|
2014-07-09 19:15:21 +04:00
|
|
|
static int32_t strcasecmp(const char* aStr1, const char* aStr2)
|
|
|
|
{
|
|
|
|
return int32_t(PL_strcasecmp(aStr1, aStr2));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
1998-04-30 20:40:38 +04:00
|
|
|
/// Case-insensitive string comparison with length
|
2014-07-09 19:15:21 +04:00
|
|
|
static int32_t strncasecmp(const char* aStr1, const char* aStr2,
|
|
|
|
uint32_t aMaxLen)
|
|
|
|
{
|
|
|
|
int32_t result = int32_t(PL_strncasecmp(aStr1, aStr2, aMaxLen));
|
2002-10-08 10:12:00 +04:00
|
|
|
//Egads. PL_strncasecmp is returning *very* negative numbers.
|
|
|
|
//Some folks expect -1,0,1, so let's temper its enthusiasm.
|
2014-07-09 19:15:21 +04:00
|
|
|
if (result < 0) {
|
|
|
|
result = -1;
|
|
|
|
}
|
1999-12-02 13:14:38 +03:00
|
|
|
return result;
|
1998-04-30 20:40:38 +04:00
|
|
|
}
|
|
|
|
|
1999-02-06 06:56:17 +03:00
|
|
|
/**
|
1999-05-05 00:28:24 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
How to use this fancy (thread-safe) version of strtok:
|
1999-02-06 06:56:17 +03:00
|
|
|
|
2002-10-08 10:12:00 +04:00
|
|
|
void main(void) {
|
|
|
|
printf("%s\n\nTokens:\n", string);
|
1999-02-06 06:56:17 +03:00
|
|
|
// Establish string and get the first token:
|
|
|
|
char* newStr;
|
2014-07-09 19:15:21 +04:00
|
|
|
token = nsCRT::strtok(string, seps, &newStr);
|
2013-10-11 00:41:00 +04:00
|
|
|
while (token != nullptr) {
|
1999-02-06 06:56:17 +03:00
|
|
|
// While there are tokens in "string"
|
2002-10-08 10:12:00 +04:00
|
|
|
printf(" %s\n", token);
|
1999-02-06 06:56:17 +03:00
|
|
|
// Get next token:
|
2002-10-08 10:12:00 +04:00
|
|
|
token = nsCRT::strtok(newStr, seps, &newStr);
|
1999-02-06 06:56:17 +03:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 00:28:24 +04: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 06:56:17 +03:00
|
|
|
*/
|
2014-07-09 19:15:21 +04:00
|
|
|
static char* strtok(char* aStr, const char* aDelims, char** aNewStr);
|
1999-02-06 06:56:17 +03:00
|
|
|
|
2013-01-04 03:36:16 +04:00
|
|
|
/// Like strcmp except for ucs2 strings
|
2014-07-09 19:15:21 +04:00
|
|
|
static int32_t strcmp(const char16_t* aStr1, const char16_t* aStr2);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
2001-10-18 08:23:25 +04:00
|
|
|
// String to longlong
|
2014-07-09 19:15:21 +04:00
|
|
|
static int64_t atoll(const char* aStr);
|
|
|
|
|
2006-05-03 20:11:14 +04:00
|
|
|
static char ToUpper(char aChar) { return NS_ToUpper(aChar); }
|
|
|
|
static char ToLower(char aChar) { return NS_ToLower(aChar); }
|
2014-07-09 19:15:21 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool IsUpper(char aChar) { return NS_IsUpper(aChar); }
|
|
|
|
static bool IsLower(char aChar) { return NS_IsLower(aChar); }
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
static bool IsAscii(char16_t aChar) { return NS_IsAscii(aChar); }
|
|
|
|
static bool IsAscii(const char16_t* aString) { return NS_IsAscii(aString); }
|
|
|
|
static bool IsAsciiSpace(char16_t aChar) { return NS_IsAsciiWhitespace(aChar); }
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool IsAscii(const char* aString) { return NS_IsAscii(aString); }
|
2014-07-09 19:15:21 +04:00
|
|
|
static bool IsAscii(const char* aString, uint32_t aLength)
|
|
|
|
{
|
|
|
|
return NS_IsAscii(aString, aLength);
|
|
|
|
}
|
1998-04-14 00:24:54 +04:00
|
|
|
};
|
|
|
|
|
2008-04-02 20:05:55 +04:00
|
|
|
|
2012-08-09 11:09:40 +04:00
|
|
|
inline bool
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_IS_SPACE(char16_t aChar)
|
2012-08-09 11:09:40 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
return ((int(aChar) & 0x7f) == int(aChar)) && isspace(int(aChar));
|
2012-08-09 11:09:40 +04:00
|
|
|
}
|
2003-04-01 23:38:12 +04: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))
|
2014-02-11 02:57:01 +04:00
|
|
|
#if defined(XP_WIN)
|
2003-04-01 23:38:12 +04: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 06:57:39 +03:00
|
|
|
|
2001-04-18 09:59:43 +04:00
|
|
|
#endif /* nsCRT_h___ */
|