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-08-04 00:46:38 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
|
|
|
* MODULE NOTES:
|
1998-08-04 14:12:45 +04:00
|
|
|
* @update gess7/30/98
|
1998-08-04 00:46:38 +04:00
|
|
|
*
|
|
|
|
* Much as I hate to do it, we were using string compares wrong.
|
|
|
|
* Often, programmers call functions like strcmp(s1,s2), and pass
|
2014-07-09 19:15:21 +04:00
|
|
|
* one or more null strings. Rather than blow up on these, I've
|
1998-08-04 00:46:38 +04:00
|
|
|
* added quick checks to ensure that cases like this don't cause
|
|
|
|
* us to fail.
|
|
|
|
*
|
|
|
|
* In general, if you pass a null into any of these string compare
|
|
|
|
* routines, we simply return 0.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1998-04-14 00:24:54 +04:00
|
|
|
#include "nsCRT.h"
|
2013-09-19 23:31:21 +04:00
|
|
|
#include "nsDebug.h"
|
1998-04-14 00:24:54 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2000-08-23 21:27:06 +04:00
|
|
|
|
1999-02-06 07:18:15 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// My lovely strtok routine
|
|
|
|
|
|
|
|
#define IS_DELIM(m, c) ((m)[(c) >> 3] & (1 << ((c) & 7)))
|
|
|
|
#define SET_DELIM(m, c) ((m)[(c) >> 3] |= (1 << ((c) & 7)))
|
|
|
|
#define DELIM_TABLE_SIZE 32
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
char*
|
|
|
|
nsCRT::strtok(char* aString, const char* aDelims, char** aNewStr)
|
1998-04-14 00:24:54 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_ASSERTION(aString,
|
|
|
|
"Unlike regular strtok, the first argument cannot be null.");
|
1999-02-06 07:18:15 +03:00
|
|
|
|
|
|
|
char delimTable[DELIM_TABLE_SIZE];
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
1999-02-06 07:18:15 +03:00
|
|
|
char* result;
|
2014-07-09 19:15:21 +04:00
|
|
|
char* str = aString;
|
1999-02-06 07:18:15 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
for (i = 0; i < DELIM_TABLE_SIZE; ++i) {
|
1999-02-06 07:18:15 +03:00
|
|
|
delimTable[i] = '\0';
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
1999-02-06 07:18:15 +03:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
for (i = 0; aDelims[i]; i++) {
|
|
|
|
SET_DELIM(delimTable, static_cast<uint8_t>(aDelims[i]));
|
1999-02-06 07:22:43 +03:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
NS_ASSERTION(aDelims[i] == '\0', "too many delimiters");
|
1999-02-06 07:18:15 +03:00
|
|
|
|
|
|
|
// skip to beginning
|
2012-08-22 19:56:38 +04:00
|
|
|
while (*str && IS_DELIM(delimTable, static_cast<uint8_t>(*str))) {
|
1999-02-06 07:18:15 +03:00
|
|
|
str++;
|
|
|
|
}
|
|
|
|
result = str;
|
|
|
|
|
|
|
|
// fix up the end of the token
|
|
|
|
while (*str) {
|
2012-08-22 19:56:38 +04:00
|
|
|
if (IS_DELIM(delimTable, static_cast<uint8_t>(*str))) {
|
1999-02-06 07:18:15 +03:00
|
|
|
*str++ = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
*aNewStr = str;
|
1999-02-06 07:18:15 +03:00
|
|
|
|
2013-10-11 00:41:00 +04:00
|
|
|
return str == result ? nullptr : result;
|
1999-02-06 06:56:17 +03:00
|
|
|
}
|
|
|
|
|
1999-02-06 07:18:15 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-01-04 03:36:16 +04:00
|
|
|
/**
|
2014-07-09 19:15:21 +04:00
|
|
|
* Compare unichar string ptrs, stopping at the 1st null
|
2013-01-04 03:36:16 +04:00
|
|
|
* NOTE: If both are null, we return 0.
|
2013-10-11 00:41:00 +04:00
|
|
|
* NOTE: We terminate the search upon encountering a nullptr
|
2013-01-04 03:36:16 +04:00
|
|
|
*
|
|
|
|
* @update gess 11/10/99
|
|
|
|
* @param s1 and s2 both point to unichar strings
|
|
|
|
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
|
|
|
|
*/
|
2014-07-09 19:15:21 +04:00
|
|
|
int32_t
|
|
|
|
nsCRT::strcmp(const char16_t* aStr1, const char16_t* aStr2)
|
|
|
|
{
|
|
|
|
if (aStr1 && aStr2) {
|
2013-01-04 03:36:16 +04:00
|
|
|
for (;;) {
|
2014-07-09 19:15:21 +04:00
|
|
|
char16_t c1 = *aStr1++;
|
|
|
|
char16_t c2 = *aStr2++;
|
2013-01-04 03:36:16 +04:00
|
|
|
if (c1 != c2) {
|
2014-07-09 19:15:21 +04:00
|
|
|
if (c1 < c2) {
|
|
|
|
return -1;
|
|
|
|
}
|
2013-01-04 03:36:16 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
if (c1 == 0 || c2 == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2013-01-04 03:36:16 +04:00
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
} else {
|
|
|
|
if (aStr1) { // aStr2 must have been null
|
2013-01-04 03:36:16 +04:00
|
|
|
return -1;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
|
|
|
if (aStr2) { // aStr1 must have been null
|
2013-01-04 03:36:16 +04:00
|
|
|
return 1;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2013-01-04 03:36:16 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-08-04 00:46:38 +04:00
|
|
|
/**
|
|
|
|
* Compare unichar string ptrs, stopping at the 1st null or nth char.
|
|
|
|
* NOTE: If either is null, we return 0.
|
2013-10-11 00:41:00 +04:00
|
|
|
* NOTE: We DO NOT terminate the search upon encountering nullptr's before N
|
1999-11-14 09:22:52 +03:00
|
|
|
*
|
|
|
|
* @update gess 11/10/99
|
1998-08-04 00:46:38 +04:00
|
|
|
* @param s1 and s2 both point to unichar strings
|
1998-08-04 01:03:49 +04:00
|
|
|
* @return 0 if they match, -1 if s1<s2; 1 if s1>s2
|
1998-08-04 00:46:38 +04:00
|
|
|
*/
|
2014-07-09 19:15:21 +04:00
|
|
|
int32_t
|
|
|
|
nsCRT::strncmp(const char16_t* aStr1, const char16_t* aStr2, uint32_t aNum)
|
|
|
|
{
|
|
|
|
if (aStr1 && aStr2) {
|
|
|
|
if (aNum != 0) {
|
1999-02-09 01:01:08 +03:00
|
|
|
do {
|
2014-07-09 19:15:21 +04:00
|
|
|
char16_t c1 = *aStr1++;
|
|
|
|
char16_t c2 = *aStr2++;
|
1998-08-04 00:46:38 +04:00
|
|
|
if (c1 != c2) {
|
2014-07-09 19:15:21 +04:00
|
|
|
if (c1 < c2) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-04 00:46:38 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2014-07-09 19:15:21 +04:00
|
|
|
} while (--aNum != 0);
|
1998-04-14 00:24:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
const char*
|
|
|
|
nsCRT::memmem(const char* aHaystack, uint32_t aHaystackLen,
|
|
|
|
const char* aNeedle, uint32_t aNeedleLen)
|
2012-04-25 04:51:53 +04:00
|
|
|
{
|
|
|
|
// Sanity checking
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!(aHaystack && aNeedle && aHaystackLen && aNeedleLen &&
|
|
|
|
aNeedleLen <= aHaystackLen)) {
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
2014-07-09 19:15:21 +04:00
|
|
|
}
|
2012-04-25 04:51:53 +04:00
|
|
|
|
|
|
|
#ifdef HAVE_MEMMEM
|
2014-07-09 19:15:21 +04:00
|
|
|
return (const char*)::memmem(aHaystack, aHaystackLen, aNeedle, aNeedleLen);
|
2012-04-25 04:51:53 +04:00
|
|
|
#else
|
|
|
|
// No memmem means we need to roll our own. This isn't really optimized
|
|
|
|
// for performance ... if that becomes an issue we can take some inspiration
|
|
|
|
// from the js string compare code in jsstr.cpp
|
2014-07-09 19:15:21 +04:00
|
|
|
for (uint32_t i = 0; i < aHaystackLen - aNeedleLen; i++) {
|
|
|
|
if (!memcmp(aHaystack + i, aNeedle, aNeedleLen)) {
|
|
|
|
return aHaystack + i;
|
|
|
|
}
|
2012-04-25 04:51:53 +04:00
|
|
|
}
|
|
|
|
#endif
|
2013-10-11 00:41:00 +04:00
|
|
|
return nullptr;
|
2012-04-25 04:51:53 +04:00
|
|
|
}
|
|
|
|
|
2001-10-18 08:23:25 +04:00
|
|
|
// This should use NSPR but NSPR isn't exporting its PR_strtoll function
|
|
|
|
// Until then...
|
2014-07-09 19:15:21 +04:00
|
|
|
int64_t
|
|
|
|
nsCRT::atoll(const char* aStr)
|
2001-10-18 08:23:25 +04:00
|
|
|
{
|
2014-07-09 19:15:21 +04:00
|
|
|
if (!aStr) {
|
|
|
|
return 0;
|
|
|
|
}
|
2001-10-18 08:23:25 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
int64_t ll = 0;
|
2001-10-18 08:23:25 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
while (*aStr && *aStr >= '0' && *aStr <= '9') {
|
|
|
|
ll *= 10;
|
|
|
|
ll += *aStr - '0';
|
|
|
|
aStr++;
|
|
|
|
}
|
2001-10-18 08:23:25 +04:00
|
|
|
|
2014-07-09 19:15:21 +04:00
|
|
|
return ll;
|
2001-10-18 08:23:25 +04:00
|
|
|
}
|
|
|
|
|