2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MODULE NOTES:
|
|
|
|
* @update gess7/30/98
|
|
|
|
*
|
|
|
|
* Much as I hate to do it, we were using string compares wrong.
|
|
|
|
* Often, programmers call functions like strcmp(s1,s2), and pass
|
|
|
|
* one or more null strings. Rather than blow up on these, I've
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsCRT.h"
|
2013-09-19 12:31:21 -07:00
|
|
|
#include "nsDebug.h"
|
2007-03-22 10:30:00 -07: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
|
|
|
|
|
|
|
|
char* nsCRT::strtok(char* string, const char* delims, char* *newStr)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(string, "Unlike regular strtok, the first argument cannot be null.");
|
|
|
|
|
|
|
|
char delimTable[DELIM_TABLE_SIZE];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
char* result;
|
|
|
|
char* str = string;
|
|
|
|
|
|
|
|
for (i = 0; i < DELIM_TABLE_SIZE; i++)
|
|
|
|
delimTable[i] = '\0';
|
|
|
|
|
|
|
|
for (i = 0; delims[i]; i++) {
|
2012-08-22 08:56:38 -07:00
|
|
|
SET_DELIM(delimTable, static_cast<uint8_t>(delims[i]));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
NS_ASSERTION(delims[i] == '\0', "too many delimiters");
|
|
|
|
|
|
|
|
// skip to beginning
|
2012-08-22 08:56:38 -07:00
|
|
|
while (*str && IS_DELIM(delimTable, static_cast<uint8_t>(*str))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
str++;
|
|
|
|
}
|
|
|
|
result = str;
|
|
|
|
|
|
|
|
// fix up the end of the token
|
|
|
|
while (*str) {
|
2012-08-22 08:56:38 -07:00
|
|
|
if (IS_DELIM(delimTable, static_cast<uint8_t>(*str))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*str++ = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
*newStr = str;
|
|
|
|
|
2013-10-10 13:41:00 -07:00
|
|
|
return str == result ? nullptr : result;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-01-03 15:36:16 -08:00
|
|
|
/**
|
|
|
|
* Compare unichar string ptrs, stopping at the 1st null
|
|
|
|
* NOTE: If both are null, we return 0.
|
2013-10-10 13:41:00 -07:00
|
|
|
* NOTE: We terminate the search upon encountering a nullptr
|
2013-01-03 15:36:16 -08: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
|
|
|
|
*/
|
|
|
|
int32_t nsCRT::strcmp(const PRUnichar* s1, const PRUnichar* s2) {
|
|
|
|
if(s1 && s2) {
|
|
|
|
for (;;) {
|
|
|
|
PRUnichar c1 = *s1++;
|
|
|
|
PRUnichar c2 = *s2++;
|
|
|
|
if (c1 != c2) {
|
|
|
|
if (c1 < c2) return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ((0==c1) || (0==c2)) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (s1) // s2 must have been null
|
|
|
|
return -1;
|
|
|
|
if (s2) // s1 must have been null
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Compare unichar string ptrs, stopping at the 1st null or nth char.
|
|
|
|
* NOTE: If either is null, we return 0.
|
2013-10-10 13:41:00 -07:00
|
|
|
* NOTE: We DO NOT terminate the search upon encountering nullptr's before N
|
2007-03-22 10:30:00 -07: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
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nsCRT::strncmp(const PRUnichar* s1, const PRUnichar* s2, uint32_t n) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if(s1 && s2) {
|
|
|
|
if(n != 0) {
|
|
|
|
do {
|
|
|
|
PRUnichar c1 = *s1++;
|
|
|
|
PRUnichar c2 = *s2++;
|
|
|
|
if (c1 != c2) {
|
|
|
|
if (c1 < c2) return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} while (--n != 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const char* nsCRT::memmem(const char* haystack, uint32_t haystackLen,
|
|
|
|
const char* needle, uint32_t needleLen)
|
2012-04-24 17:51:53 -07:00
|
|
|
{
|
|
|
|
// Sanity checking
|
|
|
|
if (!(haystack && needle && haystackLen && needleLen &&
|
|
|
|
needleLen <= haystackLen))
|
2013-10-10 13:41:00 -07:00
|
|
|
return nullptr;
|
2012-04-24 17:51:53 -07:00
|
|
|
|
|
|
|
#ifdef HAVE_MEMMEM
|
|
|
|
return (const char*)::memmem(haystack, haystackLen, needle, needleLen);
|
|
|
|
#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
|
2013-01-10 00:39:40 -08:00
|
|
|
for (uint32_t i = 0; i < haystackLen - needleLen; i++) {
|
2012-04-24 17:51:53 -07:00
|
|
|
if (!memcmp(haystack + i, needle, needleLen))
|
|
|
|
return haystack + i;
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-10 13:41:00 -07:00
|
|
|
return nullptr;
|
2012-04-24 17:51:53 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// This should use NSPR but NSPR isn't exporting its PR_strtoll function
|
|
|
|
// Until then...
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t nsCRT::atoll(const char *str)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!str)
|
2012-10-25 16:25:59 -07:00
|
|
|
return 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-25 16:25:59 -07:00
|
|
|
int64_t ll = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
while (*str && *str >= '0' && *str <= '9') {
|
2012-10-04 22:05:28 -07:00
|
|
|
ll *= 10;
|
2012-10-25 16:25:57 -07:00
|
|
|
ll += *str - '0';
|
2007-03-22 10:30:00 -07:00
|
|
|
str++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ll;
|
|
|
|
}
|
|
|
|
|