1998-04-14 00:24:54 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1998-04-14 00:24:54 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1998-04-14 00:24:54 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1998-04-14 00:24:54 +04:00
|
|
|
*/
|
|
|
|
#ifndef nsCRT_h___
|
|
|
|
#define nsCRT_h___
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "nscore.h"
|
1999-03-22 12:54:46 +03:00
|
|
|
#include "prtypes.h"
|
1999-07-22 22:23:02 +04:00
|
|
|
#include "nsCppSharedAllocator.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1999-02-09 07:19:58 +03:00
|
|
|
#define CR '\015'
|
|
|
|
#define LF '\012'
|
|
|
|
#define VTAB '\013'
|
|
|
|
#define FF '\014'
|
|
|
|
#define TAB '\011'
|
1999-12-02 01:26:58 +03:00
|
|
|
|
|
|
|
#define CRSTR "\015"
|
|
|
|
#define LFSTR "\012"
|
1999-02-09 07:19:58 +03:00
|
|
|
#define CRLF "\015\012" /* A CR LF equivalent string */
|
|
|
|
|
1999-06-22 22:50:12 +04:00
|
|
|
#ifdef XP_MAC
|
1999-08-03 16:42:19 +04:00
|
|
|
# define NS_LINEBREAK "\015"
|
1999-06-22 22:50:12 +04:00
|
|
|
# define NS_LINEBREAK_LEN 1
|
|
|
|
#else
|
|
|
|
# ifdef XP_PC
|
|
|
|
# define NS_LINEBREAK "\015\012"
|
|
|
|
# define NS_LINEBREAK_LEN 2
|
|
|
|
# else
|
1999-06-29 14:27:58 +04:00
|
|
|
# if defined(XP_UNIX) || defined(XP_BEOS)
|
1999-06-22 22:50:12 +04:00
|
|
|
# define NS_LINEBREAK "\012"
|
|
|
|
# define NS_LINEBREAK_LEN 1
|
|
|
|
# endif /* XP_UNIX */
|
|
|
|
# endif /* XP_PC */
|
|
|
|
#endif /* XP_MAC */
|
|
|
|
|
1999-02-09 07:43:58 +03:00
|
|
|
|
1999-03-22 12:54:46 +03:00
|
|
|
extern const PRUnichar kIsoLatin1ToUCS2[256];
|
|
|
|
|
|
|
|
|
1999-02-26 22:38:53 +03:00
|
|
|
// This macro can be used in a class declaration for classes that want
|
|
|
|
// to ensure that their instance memory is zeroed.
|
|
|
|
#define NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW \
|
|
|
|
void* operator new(size_t sz) { \
|
|
|
|
void* rv = ::operator new(sz); \
|
|
|
|
if (rv) { \
|
|
|
|
nsCRT::zero(rv, sz); \
|
|
|
|
} \
|
|
|
|
return rv; \
|
|
|
|
} \
|
|
|
|
void operator delete(void* ptr) { \
|
|
|
|
::operator delete(ptr); \
|
|
|
|
}
|
|
|
|
|
|
|
|
// This macro works with the next macro to declare a non-inlined
|
|
|
|
// version of the above.
|
|
|
|
#define NS_DECL_ZEROING_OPERATOR_NEW \
|
|
|
|
void* operator new(size_t sz); \
|
|
|
|
void operator delete(void* ptr);
|
|
|
|
|
|
|
|
#define NS_IMPL_ZEROING_OPERATOR_NEW(_class) \
|
|
|
|
void* _class::operator new(size_t sz) { \
|
|
|
|
void* rv = ::operator new(sz); \
|
|
|
|
if (rv) { \
|
|
|
|
nsCRT::zero(rv, sz); \
|
|
|
|
} \
|
|
|
|
return rv; \
|
|
|
|
} \
|
|
|
|
void _class::operator delete(void* ptr) { \
|
|
|
|
::operator delete(ptr); \
|
|
|
|
}
|
1999-02-09 07:43:58 +03:00
|
|
|
|
1999-08-23 12:27:42 +04:00
|
|
|
// Freeing helper
|
|
|
|
#define CRTFREEIF(x) if (x) { nsCRT::free(x); x = 0; }
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/// This is a wrapper class around all the C runtime functions.
|
|
|
|
|
1999-05-26 05:38:36 +04:00
|
|
|
class NS_COM nsCRT {
|
1998-04-14 00:24:54 +04:00
|
|
|
public:
|
1999-02-09 07:19:58 +03:00
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/** Copy bytes from aSrc to aDest.
|
|
|
|
@param aDest the destination address
|
|
|
|
@param aSrc the source address
|
|
|
|
@param aCount the number of bytes to copy
|
|
|
|
*/
|
1999-02-06 06:56:17 +03:00
|
|
|
static void memcpy(void* aDest, const void* aSrc, PRUint32 aCount) {
|
1998-04-14 00:24:54 +04:00
|
|
|
::memcpy(aDest, aSrc, (size_t)aCount);
|
|
|
|
}
|
|
|
|
|
1999-02-06 06:56:17 +03:00
|
|
|
static void memmove(void* aDest, const void* aSrc, PRUint32 aCount) {
|
1998-04-14 00:24:54 +04:00
|
|
|
::memmove(aDest, aSrc, (size_t)aCount);
|
|
|
|
}
|
|
|
|
|
1999-02-06 06:56:17 +03:00
|
|
|
static void memset(void* aDest, PRUint8 aByte, PRUint32 aCount) {
|
1998-04-14 00:24:54 +04:00
|
|
|
::memset(aDest, aByte, aCount);
|
|
|
|
}
|
|
|
|
|
1999-02-06 06:56:17 +03:00
|
|
|
static void zero(void* aDest, PRUint32 aCount) {
|
1998-04-14 00:24:54 +04:00
|
|
|
::memset(aDest, 0, (size_t)aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Compute the string length of s
|
|
|
|
@param s the string in question
|
|
|
|
@return the length of s
|
|
|
|
*/
|
1999-02-06 06:56:17 +03:00
|
|
|
static PRUint32 strlen(const char* s) {
|
|
|
|
return PRUint32(::strlen(s));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Compare s1 and s2.
|
|
|
|
static PRInt32 strcmp(const char* s1, const char* s2) {
|
2000-03-23 01:55:10 +03:00
|
|
|
return PRInt32(PL_strcmp(s1, s2));
|
1999-02-06 06:56:17 +03:00
|
|
|
}
|
|
|
|
|
1999-06-05 05:15:00 +04:00
|
|
|
static PRInt32 strncmp(const char* s1, const char* s2,
|
1999-02-06 06:56:17 +03:00
|
|
|
PRUint32 aMaxLen) {
|
|
|
|
return PRInt32(PL_strncmp(s1, s2, aMaxLen));
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Case-insensitive string comparison.
|
|
|
|
static PRInt32 strcasecmp(const char* s1, const char* s2) {
|
|
|
|
return PRInt32(PL_strcasecmp(s1, s2));
|
|
|
|
}
|
|
|
|
|
1998-04-30 20:40:38 +04:00
|
|
|
/// Case-insensitive string comparison with length
|
1999-02-06 06:56:17 +03:00
|
|
|
static PRInt32 strncasecmp(const char* s1, const char* s2, PRUint32 aMaxLen) {
|
1999-12-02 13:14:38 +03:00
|
|
|
PRInt32 result=PRInt32(PL_strncasecmp(s1, s2, aMaxLen));
|
|
|
|
//Egags. PL_strncasecmp is returning *very* negative numbers.
|
|
|
|
//Some folks expect -1,0,1, so let's temper it's enthusiasm.
|
|
|
|
if(result<0)
|
|
|
|
result=-1;
|
|
|
|
return result;
|
1998-04-30 20:40:38 +04:00
|
|
|
}
|
|
|
|
|
1999-02-10 11:22:47 +03:00
|
|
|
static PRInt32 strncmp(const char* s1, const char* s2, PRInt32 aMaxLen) {
|
1999-08-24 12:45:17 +04:00
|
|
|
// inline the first test (assumes strings are not null):
|
|
|
|
PRInt32 diff = ((const unsigned char*)s1)[0] - ((const unsigned char*)s2)[0];
|
|
|
|
if (diff != 0) return diff;
|
2000-03-23 01:55:10 +03:00
|
|
|
return PRInt32(PL_strncmp(s1,s2,unsigned(aMaxLen)));
|
1999-02-10 11:22:47 +03:00
|
|
|
}
|
|
|
|
|
1998-12-16 08:40:20 +03:00
|
|
|
static char* strdup(const char* str) {
|
|
|
|
return PL_strdup(str);
|
|
|
|
}
|
|
|
|
|
1999-04-22 08:25:17 +04:00
|
|
|
static void free(char* str) {
|
|
|
|
PL_strfree(str);
|
|
|
|
}
|
|
|
|
|
1999-02-06 06:56:17 +03:00
|
|
|
/**
|
1999-05-05 00:28:24 +04:00
|
|
|
|
1999-02-06 06:56:17 +03:00
|
|
|
How to use this fancy (thread-safe) version of strtok:
|
|
|
|
|
|
|
|
void main( void ) {
|
|
|
|
printf( "%s\n\nTokens:\n", string );
|
|
|
|
// Establish string and get the first token:
|
|
|
|
char* newStr;
|
|
|
|
token = nsCRT::strtok( string, seps, &newStr );
|
|
|
|
while( token != NULL ) {
|
|
|
|
// While there are tokens in "string"
|
|
|
|
printf( " %s\n", token );
|
|
|
|
// Get next token:
|
|
|
|
token = nsCRT::strtok( newStr, seps, &newStr );
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
|
|
|
static char* strtok(char* str, const char* delims, char* *newStr);
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/// Like strlen except for ucs2 strings
|
1999-02-06 06:56:17 +03:00
|
|
|
static PRUint32 strlen(const PRUnichar* s);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
/// Like strcmp except for ucs2 strings
|
|
|
|
static PRInt32 strcmp(const PRUnichar* s1, const PRUnichar* s2);
|
|
|
|
/// Like strcmp except for ucs2 strings
|
|
|
|
static PRInt32 strncmp(const PRUnichar* s1, const PRUnichar* s2,
|
1999-02-06 06:56:17 +03:00
|
|
|
PRUint32 aMaxLen);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
/// Like strcasecmp except for ucs2 strings
|
|
|
|
static PRInt32 strcasecmp(const PRUnichar* s1, const PRUnichar* s2);
|
|
|
|
/// Like strncasecmp except for ucs2 strings
|
|
|
|
static PRInt32 strncasecmp(const PRUnichar* s1, const PRUnichar* s2,
|
1999-02-06 06:56:17 +03:00
|
|
|
PRUint32 aMaxLen);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
/// Like strcmp with a char* and a ucs2 string
|
|
|
|
static PRInt32 strcmp(const PRUnichar* s1, const char* s2);
|
|
|
|
/// Like strncmp with a char* and a ucs2 string
|
|
|
|
static PRInt32 strncmp(const PRUnichar* s1, const char* s2,
|
1999-02-06 06:56:17 +03:00
|
|
|
PRUint32 aMaxLen);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
/// Like strcasecmp with a char* and a ucs2 string
|
|
|
|
static PRInt32 strcasecmp(const PRUnichar* s1, const char* s2);
|
|
|
|
/// Like strncasecmp with a char* and a ucs2 string
|
|
|
|
static PRInt32 strncasecmp(const PRUnichar* s1, const char* s2,
|
1999-02-06 06:56:17 +03:00
|
|
|
PRUint32 aMaxLen);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
1999-07-22 09:34:19 +04:00
|
|
|
// Note: uses new[] to allocate memory, so you must use delete[] to
|
|
|
|
// free the memory
|
1998-12-16 08:40:20 +03:00
|
|
|
static PRUnichar* strdup(const PRUnichar* str);
|
|
|
|
|
1999-04-22 08:25:17 +04:00
|
|
|
static void free(PRUnichar* str) {
|
1999-07-22 22:23:02 +04:00
|
|
|
nsCppSharedAllocator<PRUnichar> shared_allocator;
|
|
|
|
shared_allocator.deallocate(str, 0 /*we never new or kept the size*/);
|
1999-04-22 08:25:17 +04:00
|
|
|
}
|
|
|
|
|
1999-06-26 10:32:35 +04:00
|
|
|
/// Compute a hashcode for a C string
|
|
|
|
static PRUint32 HashValue(const char* s1);
|
|
|
|
|
|
|
|
/// Same as above except that we return the length in s1len
|
|
|
|
static PRUint32 HashValue(const char* s1, PRUint32* s1len);
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
/// Compute a hashcode for a ucs2 string
|
1998-09-02 05:59:30 +04:00
|
|
|
static PRUint32 HashValue(const PRUnichar* s1);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
/// Same as above except that we return the length in s1len
|
1999-02-06 06:56:17 +03:00
|
|
|
static PRUint32 HashValue(const PRUnichar* s1, PRUint32* s1len);
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
/// String to integer.
|
|
|
|
static PRInt32 atoi( const PRUnichar *string );
|
|
|
|
|
|
|
|
static PRUnichar ToUpper(PRUnichar aChar);
|
|
|
|
|
|
|
|
static PRUnichar ToLower(PRUnichar aChar);
|
1999-03-02 21:24:42 +03:00
|
|
|
|
|
|
|
static PRBool IsUpper(PRUnichar aChar);
|
|
|
|
|
|
|
|
static PRBool IsLower(PRUnichar aChar);
|
2000-03-12 10:47:09 +03:00
|
|
|
|
|
|
|
static PRBool IsAsciiAlpha(PRUnichar aChar);
|
|
|
|
static PRBool IsAsciiDigit(PRUnichar aChar);
|
|
|
|
static PRBool IsAsciiSpace(PRUnichar aChar);
|
1998-04-14 00:24:54 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsCRT_h___ */
|
2000-03-12 10:47:09 +03:00
|
|
|
|
|
|
|
|