2014-05-05 21:30:46 +04: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/. */
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString obsolete API support
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
#if MOZ_STRING_WITH_OBSOLETE_API
|
|
|
|
|
|
|
|
#include "nsDependentString.h"
|
|
|
|
#include "nsDependentSubstring.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsUTF8Utils.h"
|
|
|
|
#include "prdtoa.h"
|
|
|
|
|
|
|
|
/* ***** BEGIN RICKG BLOCK *****
|
|
|
|
*
|
|
|
|
* NOTE: This section of code was extracted from rickg's bufferRoutines.h file.
|
|
|
|
* For the most part it remains unmodified. We want to eliminate (or at
|
|
|
|
* least clean up) this code at some point. If you find the formatting
|
|
|
|
* in this section somewhat inconsistent, don't blame me! ;-)
|
|
|
|
*/
|
|
|
|
|
2005-02-16 10:20:20 +03:00
|
|
|
// avoid STDC's tolower since it may do weird things with non-ASCII bytes
|
2004-02-20 04:53:23 +03:00
|
|
|
inline char
|
|
|
|
ascii_tolower(char aChar)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
|
|
|
if (aChar >= 'A' && aChar <= 'Z')
|
|
|
|
return aChar + ('a' - 'A');
|
|
|
|
return aChar;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// This set of methods is used to search a buffer looking for a char.
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methods cans the given buffer for the given char
|
2014-05-05 21:30:46 +04:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* @update gess 02/17/00
|
|
|
|
* @param aDest is the buffer to be searched
|
|
|
|
* @param aDestLength is the size (in char-units, not bytes) of the buffer
|
|
|
|
* @param anOffset is the start pos to begin searching
|
|
|
|
* @param aChar is the target character we're looking for
|
|
|
|
* @param aCount tells us how many characters to iterate through (which may be different than aLength); -1 means use full length.
|
|
|
|
* @return index of pos if found, else -1 (kNotFound)
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
FindChar1(const char* aDest,uint32_t aDestLength,int32_t anOffset,const char16_t aChar,int32_t aCount) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if(anOffset < 0)
|
|
|
|
anOffset=0;
|
|
|
|
|
|
|
|
if(aCount < 0)
|
2012-08-22 19:56:38 +04:00
|
|
|
aCount = (int32_t)aDestLength;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
if((aChar < 256) && (0 < aDestLength) && ((uint32_t)anOffset < aDestLength)) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
//We'll only search if the given aChar is within the normal ascii a range,
|
|
|
|
//(Since this string is definitely within the ascii range).
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if(0<aCount) {
|
|
|
|
|
|
|
|
const char* left= aDest+anOffset;
|
|
|
|
const char* last= left+aCount;
|
|
|
|
const char* max = aDest+aDestLength;
|
|
|
|
const char* end = (last<max) ? last : max;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t theMax = end-left;
|
2004-02-19 05:44:03 +03:00
|
|
|
if(0<theMax) {
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
unsigned char theChar = (unsigned char) aChar;
|
|
|
|
const char* result=(const char*)memchr(left, (int)theChar, theMax);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if(result)
|
|
|
|
return result-aDest;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methods cans the given buffer for the given char
|
2014-05-05 21:30:46 +04:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* @update gess 3/25/98
|
|
|
|
* @param aDest is the buffer to be searched
|
|
|
|
* @param aDestLength is the size (in char-units, not bytes) of the buffer
|
|
|
|
* @param anOffset is the start pos to begin searching
|
|
|
|
* @param aChar is the target character we're looking for
|
|
|
|
* @param aCount tells us how many characters to iterate through (which may be different than aLength); -1 means use full length.
|
|
|
|
* @return index of pos if found, else -1 (kNotFound)
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
FindChar2(const char16_t* aDest,uint32_t aDestLength,int32_t anOffset,const char16_t aChar,int32_t aCount) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if(anOffset < 0)
|
|
|
|
anOffset=0;
|
|
|
|
|
|
|
|
if(aCount < 0)
|
2012-08-22 19:56:38 +04:00
|
|
|
aCount = (int32_t)aDestLength;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
if((0<aDestLength) && ((uint32_t)anOffset < aDestLength)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if(0<aCount) {
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* root = aDest;
|
|
|
|
const char16_t* left = root+anOffset;
|
|
|
|
const char16_t* last = left+aCount;
|
|
|
|
const char16_t* max = root+aDestLength;
|
|
|
|
const char16_t* end = (last<max) ? last : max;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
while(left<end){
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if(*left==aChar)
|
|
|
|
return (left-root);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
++left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methods cans the given buffer (in reverse) for the given char
|
2014-05-05 21:30:46 +04:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* @update gess 02/17/00
|
|
|
|
* @param aDest is the buffer to be searched
|
|
|
|
* @param aDestLength is the size (in char-units, not bytes) of the buffer
|
|
|
|
* @param anOffset is the start pos to begin searching
|
|
|
|
* @param aChar is the target character we're looking for
|
|
|
|
* @param aCount tells us how many characters to iterate through (which may be different than aLength); -1 means use full length.
|
|
|
|
* @return index of pos if found, else -1 (kNotFound)
|
|
|
|
*/
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
RFindChar1(const char* aDest,uint32_t aDestLength,int32_t anOffset,const char16_t aChar,int32_t aCount) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if(anOffset < 0)
|
2012-08-22 19:56:38 +04:00
|
|
|
anOffset=(int32_t)aDestLength-1;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if(aCount < 0)
|
2012-08-22 19:56:38 +04:00
|
|
|
aCount = int32_t(aDestLength);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
if((aChar<256) && (0 < aDestLength) && ((uint32_t)anOffset < aDestLength)) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
//We'll only search if the given aChar is within the normal ascii a range,
|
|
|
|
//(Since this string is definitely within the ascii range).
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if(0 < aCount) {
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
const char* rightmost = aDest + anOffset;
|
2004-02-19 05:44:03 +03:00
|
|
|
const char* min = rightmost - aCount + 1;
|
|
|
|
const char* leftmost = (min<aDest) ? aDest: min;
|
|
|
|
|
|
|
|
char theChar=(char)aChar;
|
|
|
|
while(leftmost <= rightmost){
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if((*rightmost) == theChar)
|
|
|
|
return rightmost - aDest;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
--rightmost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This methods cans the given buffer for the given char
|
2014-05-05 21:30:46 +04:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* @update gess 3/25/98
|
|
|
|
* @param aDest is the buffer to be searched
|
|
|
|
* @param aDestLength is the size (in char-units, not bytes) of the buffer
|
|
|
|
* @param anOffset is the start pos to begin searching
|
|
|
|
* @param aChar is the target character we're looking for
|
|
|
|
* @param aCount tells us how many characters to iterate through (which may be different than aLength); -1 means use full length.
|
|
|
|
* @return index of pos if found, else -1 (kNotFound)
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
RFindChar2(const char16_t* aDest,uint32_t aDestLength,int32_t anOffset,const char16_t aChar,int32_t aCount) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if(anOffset < 0)
|
2012-08-22 19:56:38 +04:00
|
|
|
anOffset=(int32_t)aDestLength-1;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if(aCount < 0)
|
2012-08-22 19:56:38 +04:00
|
|
|
aCount = int32_t(aDestLength);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
if((0 < aDestLength) && ((uint32_t)anOffset < aDestLength)) {
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if(0 < aCount) {
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* root = aDest;
|
2014-05-05 21:30:46 +04:00
|
|
|
const char16_t* rightmost = root + anOffset;
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* min = rightmost - aCount + 1;
|
|
|
|
const char16_t* leftmost = (min<root) ? root: min;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
while(leftmost <= rightmost){
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if((*rightmost) == aChar)
|
|
|
|
return rightmost - root;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
--rightmost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// This set of methods is used to compare one buffer onto another. The
|
|
|
|
// functions are differentiated by the size of source and dest character
|
|
|
|
// sizes. WARNING: Your destination buffer MUST be big enough to hold all the
|
|
|
|
// source bytes. We don't validate these ranges here (this should be done in
|
|
|
|
// higher level routines).
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method compares the data in one buffer with another
|
|
|
|
* @update gess 01/04/99
|
|
|
|
* @param aStr1 is the first buffer to be compared
|
|
|
|
* @param aStr2 is the 2nd buffer to be compared
|
|
|
|
* @param aCount is the number of chars to compare
|
2004-09-05 17:25:51 +04:00
|
|
|
* @param aIgnoreCase tells us whether to use a case-sensitive comparison
|
2004-02-19 05:44:03 +03:00
|
|
|
* @return -1,0,1 depending on <,==,>
|
|
|
|
*/
|
2004-02-23 04:43:44 +03:00
|
|
|
static
|
|
|
|
#ifdef __SUNPRO_CC
|
|
|
|
inline
|
|
|
|
#endif /* __SUNPRO_CC */
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2014-05-05 21:30:46 +04:00
|
|
|
Compare1To1(const char* aStr1,const char* aStr2,uint32_t aCount,bool aIgnoreCase) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result=0;
|
2004-02-19 05:44:03 +03:00
|
|
|
if(aIgnoreCase)
|
2012-08-22 19:56:38 +04:00
|
|
|
result=int32_t(PL_strncasecmp(aStr1, aStr2, aCount));
|
2014-05-05 21:30:46 +04:00
|
|
|
else
|
2004-02-19 05:44:03 +03:00
|
|
|
result=nsCharTraits<char>::compare(aStr1,aStr2,aCount);
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// alien comparisons may return out-of-bound answers
|
|
|
|
// instead of the -1, 0, 1 expected by most clients
|
2004-02-19 05:44:03 +03:00
|
|
|
if ( result < -1 )
|
|
|
|
result = -1;
|
|
|
|
else if ( result > 1 )
|
|
|
|
result = 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method compares the data in one buffer with another
|
|
|
|
* @update gess 01/04/99
|
|
|
|
* @param aStr1 is the first buffer to be compared
|
|
|
|
* @param aStr2 is the 2nd buffer to be compared
|
|
|
|
* @param aCount is the number of chars to compare
|
2004-09-05 17:25:51 +04:00
|
|
|
* @param aIgnoreCase tells us whether to use a case-sensitive comparison
|
2004-02-19 05:44:03 +03:00
|
|
|
* @return -1,0,1 depending on <,==,>
|
|
|
|
*/
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
2004-02-23 04:43:44 +03:00
|
|
|
#ifdef __SUNPRO_CC
|
|
|
|
inline
|
|
|
|
#endif /* __SUNPRO_CC */
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
Compare2To2(const char16_t* aStr1,const char16_t* aStr2,uint32_t aCount){
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if ( aStr1 && aStr2 )
|
2014-01-04 19:02:17 +04:00
|
|
|
result = nsCharTraits<char16_t>::compare(aStr1, aStr2, aCount);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// The following cases are rare and survivable caller errors.
|
|
|
|
// Two null pointers are equal, but any string, even 0 length
|
|
|
|
// is greater than a null pointer. It might not really matter,
|
|
|
|
// but we pick something reasonable anyway.
|
2004-02-19 05:44:03 +03:00
|
|
|
else if ( !aStr1 && !aStr2 )
|
|
|
|
result = 0;
|
|
|
|
else if ( aStr1 )
|
|
|
|
result = 1;
|
|
|
|
else
|
|
|
|
result = -1;
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// alien comparisons may give answers outside the -1, 0, 1 expected by callers
|
2004-02-19 05:44:03 +03:00
|
|
|
if ( result < -1 )
|
|
|
|
result = -1;
|
|
|
|
else if ( result > 1 )
|
|
|
|
result = 1;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method compares the data in one buffer with another
|
|
|
|
* @update gess 01/04/99
|
|
|
|
* @param aStr1 is the first buffer to be compared
|
|
|
|
* @param aStr2 is the 2nd buffer to be compared
|
|
|
|
* @param aCount is the number of chars to compare
|
2004-09-05 17:25:51 +04:00
|
|
|
* @param aIgnoreCase tells us whether to use a case-sensitive comparison
|
2004-02-19 05:44:03 +03:00
|
|
|
* @return -1,0,1 depending on <,==,>
|
|
|
|
*/
|
2004-02-23 04:43:44 +03:00
|
|
|
static
|
|
|
|
#ifdef __SUNPRO_CC
|
|
|
|
inline
|
|
|
|
#endif /* __SUNPRO_CC */
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
Compare2To1(const char16_t* aStr1,const char* aStr2,uint32_t aCount,bool aIgnoreCase){
|
|
|
|
const char16_t* s1 = aStr1;
|
2004-02-19 05:44:03 +03:00
|
|
|
const char *s2 = aStr2;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if (aStr1 && aStr2) {
|
|
|
|
if (aCount != 0) {
|
|
|
|
do {
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t c1 = *s1++;
|
|
|
|
char16_t c2 = char16_t((unsigned char)*s2++);
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if (c1 != c2) {
|
2012-06-25 23:59:42 +04:00
|
|
|
#ifdef DEBUG
|
2004-02-19 05:44:03 +03:00
|
|
|
// we won't warn on c1>=128 (the 2-byte value) because often
|
|
|
|
// it is just fine to compare an constant, ascii value (i.e. "body")
|
|
|
|
// against some non-ascii value (i.e. a unicode string that
|
|
|
|
// was downloaded from a web page)
|
|
|
|
if (aIgnoreCase && c2>=128)
|
|
|
|
NS_WARNING("got a non-ASCII string, but we can't do an accurate case conversion!");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// can't do case conversion on characters out of our range
|
|
|
|
if (aIgnoreCase && c1<128 && c2<128) {
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
c1 = ascii_tolower(char(c1));
|
|
|
|
c2 = ascii_tolower(char(c2));
|
|
|
|
|
|
|
|
if (c1 == c2) continue;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (c1 < c2) return -1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} while (--aCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method compares the data in one buffer with another
|
|
|
|
* @update gess 01/04/99
|
|
|
|
* @param aStr1 is the first buffer to be compared
|
|
|
|
* @param aStr2 is the 2nd buffer to be compared
|
|
|
|
* @param aCount is the number of chars to compare
|
2004-09-05 17:25:51 +04:00
|
|
|
* @param aIgnoreCase tells us whether to use a case-sensitive comparison
|
2004-02-19 05:44:03 +03:00
|
|
|
* @return -1,0,1 depending on <,==,>
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
inline int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
Compare1To2(const char* aStr1,const char16_t* aStr2,uint32_t aCount,bool aIgnoreCase){
|
2004-02-19 05:44:03 +03:00
|
|
|
return Compare2To1(aStr2, aStr1, aCount, aIgnoreCase) * -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// This set of methods is used compress char sequences in a buffer...
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-05 21:30:46 +04:00
|
|
|
* This method compresses duplicate runs of a given char from the given buffer
|
2004-02-19 05:44:03 +03:00
|
|
|
*
|
|
|
|
* @update rickg 03.23.2000
|
|
|
|
* @param aString is the buffer to be manipulated
|
|
|
|
* @param aLength is the length of the buffer
|
|
|
|
* @param aSet tells us which chars to compress from given buffer
|
|
|
|
* @param aEliminateLeading tells us whether to strip chars from the start of the buffer
|
|
|
|
* @param aEliminateTrailing tells us whether to strip chars from the start of the buffer
|
|
|
|
* @return the new length of the given buffer
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
|
|
|
CompressChars1(char* aString,uint32_t aLength,const char* aSet){
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
char* from = aString;
|
|
|
|
char* end = aString + aLength;
|
|
|
|
char* to = from;
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//this code converts /n, /t, /r into normal space ' ';
|
|
|
|
//it also compresses runs of whitespace down to a single char...
|
2004-02-19 05:44:03 +03:00
|
|
|
if(aSet && aString && (0 < aLength)){
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aSetLen=strlen(aSet);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
while (from < end) {
|
|
|
|
char theChar = *from++;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
*to++=theChar; //always copy this char...
|
|
|
|
|
|
|
|
if((kNotFound!=FindChar1(aSet,aSetLen,0,theChar,aSetLen))){
|
|
|
|
while (from < end) {
|
|
|
|
theChar = *from++;
|
|
|
|
if(kNotFound==FindChar1(aSet,aSetLen,0,theChar,aSetLen)){
|
|
|
|
*to++ = theChar;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} //while
|
|
|
|
} //if
|
|
|
|
} //if
|
|
|
|
*to = 0;
|
|
|
|
}
|
|
|
|
return to - aString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-05 21:30:46 +04:00
|
|
|
* This method compresses duplicate runs of a given char from the given buffer
|
2004-02-19 05:44:03 +03:00
|
|
|
*
|
|
|
|
* @update rickg 03.23.2000
|
|
|
|
* @param aString is the buffer to be manipulated
|
|
|
|
* @param aLength is the length of the buffer
|
|
|
|
* @param aSet tells us which chars to compress from given buffer
|
|
|
|
* @param aEliminateLeading tells us whether to strip chars from the start of the buffer
|
|
|
|
* @param aEliminateTrailing tells us whether to strip chars from the start of the buffer
|
|
|
|
* @return the new length of the given buffer
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
2014-05-05 21:30:46 +04:00
|
|
|
CompressChars2(char16_t* aString,uint32_t aLength,const char* aSet) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* from = aString;
|
|
|
|
char16_t* end = from + aLength;
|
|
|
|
char16_t* to = from;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//this code converts /n, /t, /r into normal space ' ';
|
|
|
|
//it also compresses runs of whitespace down to a single char...
|
2004-02-19 05:44:03 +03:00
|
|
|
if(aSet && aString && (0 < aLength)){
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aSetLen=strlen(aSet);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
while (from < end) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t theChar = *from++;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
*to++=theChar; //always copy this char...
|
|
|
|
|
|
|
|
if((theChar<256) && (kNotFound!=FindChar1(aSet,aSetLen,0,theChar,aSetLen))){
|
|
|
|
while (from < end) {
|
|
|
|
theChar = *from++;
|
|
|
|
if(kNotFound==FindChar1(aSet,aSetLen,0,theChar,aSetLen)){
|
|
|
|
*to++ = theChar;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} //while
|
|
|
|
} //if
|
|
|
|
} //if
|
|
|
|
*to = 0;
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
return to - (char16_t*)aString;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-05 21:30:46 +04:00
|
|
|
* This method strips chars in a given set from the given buffer
|
2004-02-19 05:44:03 +03:00
|
|
|
*
|
|
|
|
* @update gess 01/04/99
|
|
|
|
* @param aString is the buffer to be manipulated
|
|
|
|
* @param aLength is the length of the buffer
|
|
|
|
* @param aSet tells us which chars to compress from given buffer
|
|
|
|
* @param aEliminateLeading tells us whether to strip chars from the start of the buffer
|
|
|
|
* @param aEliminateTrailing tells us whether to strip chars from the start of the buffer
|
|
|
|
* @return the new length of the given buffer
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
2014-05-05 21:30:46 +04:00
|
|
|
StripChars1(char* aString,uint32_t aLength,const char* aSet) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2005-02-16 10:20:20 +03:00
|
|
|
// XXX(darin): this code should defer writing until necessary.
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
char* to = aString;
|
|
|
|
char* from = aString-1;
|
|
|
|
char* end = aString + aLength;
|
|
|
|
|
|
|
|
if(aSet && aString && (0 < aLength)){
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aSetLen=strlen(aSet);
|
2004-02-19 05:44:03 +03:00
|
|
|
while (++from < end) {
|
|
|
|
char theChar = *from;
|
|
|
|
if(kNotFound==FindChar1(aSet,aSetLen,0,theChar,aSetLen)){
|
|
|
|
*to++ = theChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to = 0;
|
|
|
|
}
|
|
|
|
return to - (char*)aString;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-05 21:30:46 +04:00
|
|
|
* This method strips chars in a given set from the given buffer
|
2004-02-19 05:44:03 +03:00
|
|
|
*
|
|
|
|
* @update gess 01/04/99
|
|
|
|
* @param aString is the buffer to be manipulated
|
|
|
|
* @param aLength is the length of the buffer
|
|
|
|
* @param aSet tells us which chars to compress from given buffer
|
|
|
|
* @param aEliminateLeading tells us whether to strip chars from the start of the buffer
|
|
|
|
* @param aEliminateTrailing tells us whether to strip chars from the start of the buffer
|
|
|
|
* @return the new length of the given buffer
|
|
|
|
*/
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t
|
2014-05-05 21:30:46 +04:00
|
|
|
StripChars2(char16_t* aString,uint32_t aLength,const char* aSet) {
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2005-02-16 10:20:20 +03:00
|
|
|
// XXX(darin): this code should defer writing until necessary.
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* to = aString;
|
|
|
|
char16_t* from = aString-1;
|
|
|
|
char16_t* end = to + aLength;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if(aSet && aString && (0 < aLength)){
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aSetLen=strlen(aSet);
|
2004-02-19 05:44:03 +03:00
|
|
|
while (++from < end) {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t theChar = *from;
|
2014-05-05 21:30:46 +04:00
|
|
|
//Note the test for ascii range below. If you have a real unicode char,
|
2004-02-19 05:44:03 +03:00
|
|
|
//and you're searching for chars in the (given) ascii string, there's no
|
|
|
|
//point in doing the real search since it's out of the ascii range.
|
|
|
|
if((255<theChar) || (kNotFound==FindChar1(aSet,aSetLen,0,theChar,aSetLen))){
|
|
|
|
*to++ = theChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to = 0;
|
|
|
|
}
|
2014-01-04 19:02:17 +04:00
|
|
|
return to - (char16_t*)aString;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ***** END RICKG BLOCK ***** */
|
|
|
|
|
2015-10-12 20:47:57 +03:00
|
|
|
static const char* kWhitespace="\f\t\r\n ";
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
// This function is used to implement FindCharInSet and friends
|
|
|
|
template <class CharT>
|
2004-02-23 04:43:44 +03:00
|
|
|
#ifndef __SUNPRO_CC
|
|
|
|
static
|
|
|
|
#endif /* !__SUNPRO_CC */
|
|
|
|
CharT
|
2004-02-19 05:44:03 +03:00
|
|
|
GetFindInSetFilter( const CharT* set)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
CharT filter = ~CharT(0); // All bits set
|
|
|
|
while (*set) {
|
|
|
|
filter &= ~(*set);
|
|
|
|
++set;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
return filter;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
// This template class is used by our code to access rickg's buffer routines.
|
|
|
|
template <class CharT> struct nsBufferRoutines {};
|
|
|
|
|
2011-12-13 18:17:59 +04:00
|
|
|
template <>
|
2004-02-19 05:44:03 +03:00
|
|
|
struct nsBufferRoutines<char>
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
static
|
|
|
|
int32_t compare( const char* a, const char* b, uint32_t max, bool ic )
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
return Compare1To1(a, b, max, ic);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t compare( const char* a, const char16_t* b, uint32_t max, bool ic )
|
|
|
|
{
|
|
|
|
return Compare1To2(a, b, max, ic);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t find_char( const char* s, uint32_t max, int32_t offset, const char16_t c, int32_t count )
|
|
|
|
{
|
|
|
|
return FindChar1(s, max, offset, c, count);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t rfind_char( const char* s, uint32_t max, int32_t offset, const char16_t c, int32_t count )
|
|
|
|
{
|
|
|
|
return RFindChar1(s, max, offset, c, count);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
char get_find_in_set_filter( const char* set )
|
|
|
|
{
|
|
|
|
return GetFindInSetFilter(set);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t strip_chars( char* s, uint32_t len, const char* set )
|
|
|
|
{
|
|
|
|
return StripChars1(s, len, set);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t compress_chars( char* s, uint32_t len, const char* set )
|
|
|
|
{
|
|
|
|
return CompressChars1(s, len, set);
|
|
|
|
}
|
|
|
|
};
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2011-12-13 18:17:59 +04:00
|
|
|
template <>
|
2014-01-04 19:02:17 +04:00
|
|
|
struct nsBufferRoutines<char16_t>
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
static
|
|
|
|
int32_t compare( const char16_t* a, const char16_t* b, uint32_t max, bool ic )
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
NS_ASSERTION(!ic, "no case-insensitive compare here");
|
|
|
|
return Compare2To2(a, b, max);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t compare( const char16_t* a, const char* b, uint32_t max, bool ic )
|
|
|
|
{
|
|
|
|
return Compare2To1(a, b, max, ic);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t find_char( const char16_t* s, uint32_t max, int32_t offset, const char16_t c, int32_t count )
|
|
|
|
{
|
|
|
|
return FindChar2(s, max, offset, c, count);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t rfind_char( const char16_t* s, uint32_t max, int32_t offset, const char16_t c, int32_t count )
|
|
|
|
{
|
|
|
|
return RFindChar2(s, max, offset, c, count);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
char16_t get_find_in_set_filter( const char16_t* set )
|
|
|
|
{
|
|
|
|
return GetFindInSetFilter(set);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
char16_t get_find_in_set_filter( const char* set )
|
|
|
|
{
|
|
|
|
return (~char16_t(0)^~char(0)) | GetFindInSetFilter(set);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t strip_chars( char16_t* s, uint32_t max, const char* set )
|
|
|
|
{
|
|
|
|
return StripChars2(s, max, set);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
static
|
|
|
|
int32_t compress_chars( char16_t* s, uint32_t len, const char* set )
|
|
|
|
{
|
|
|
|
return CompressChars2(s, len, set);
|
|
|
|
}
|
|
|
|
};
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <class L, class R>
|
2004-02-23 04:43:44 +03:00
|
|
|
#ifndef __SUNPRO_CC
|
|
|
|
static
|
|
|
|
#endif /* !__SUNPRO_CC */
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
FindSubstring( const L* big, uint32_t bigLen,
|
|
|
|
const R* little, uint32_t littleLen,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ignoreCase )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (littleLen > bigLen)
|
2004-02-19 05:44:03 +03:00
|
|
|
return kNotFound;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
int32_t i, max = int32_t(bigLen - littleLen);
|
|
|
|
for (i=0; i<=max; ++i, ++big)
|
|
|
|
{
|
|
|
|
if (nsBufferRoutines<L>::compare(big, little, littleLen, ignoreCase) == 0)
|
|
|
|
return i;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
template <class L, class R>
|
2004-02-23 04:43:44 +03:00
|
|
|
#ifndef __SUNPRO_CC
|
|
|
|
static
|
|
|
|
#endif /* !__SUNPRO_CC */
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
RFindSubstring( const L* big, uint32_t bigLen,
|
|
|
|
const R* little, uint32_t littleLen,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ignoreCase )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (littleLen > bigLen)
|
|
|
|
return kNotFound;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t i, max = int32_t(bigLen - littleLen);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
const L* iter = big + max;
|
|
|
|
for (i=max; iter >= big; --i, --iter)
|
|
|
|
{
|
|
|
|
if (nsBufferRoutines<L>::compare(iter, little, littleLen, ignoreCase) == 0)
|
|
|
|
return i;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
template <class CharT, class SetCharT>
|
2004-02-23 04:43:44 +03:00
|
|
|
#ifndef __SUNPRO_CC
|
|
|
|
static
|
|
|
|
#endif /* !__SUNPRO_CC */
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
FindCharInSet( const CharT* data, uint32_t dataLen, const SetCharT* set )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
CharT filter = nsBufferRoutines<CharT>::get_find_in_set_filter(set);
|
|
|
|
|
|
|
|
const CharT* end = data + dataLen;
|
|
|
|
for (const CharT* iter = data; iter < end; ++iter)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
CharT currentChar = *iter;
|
|
|
|
if (currentChar & filter)
|
|
|
|
continue; // char is not in filter set; go on with next char.
|
|
|
|
|
|
|
|
// test all chars
|
|
|
|
const SetCharT* charInSet = set;
|
|
|
|
CharT setChar = CharT(*charInSet);
|
|
|
|
while (setChar)
|
|
|
|
{
|
|
|
|
if (setChar == currentChar)
|
|
|
|
return iter - data; // found it! return index of the found char.
|
|
|
|
|
|
|
|
setChar = CharT(*(++charInSet));
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
return kNotFound;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
template <class CharT, class SetCharT>
|
2004-02-23 04:43:44 +03:00
|
|
|
#ifndef __SUNPRO_CC
|
|
|
|
static
|
|
|
|
#endif /* !__SUNPRO_CC */
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
RFindCharInSet( const CharT* data, uint32_t dataLen, const SetCharT* set )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
CharT filter = nsBufferRoutines<CharT>::get_find_in_set_filter(set);
|
|
|
|
|
|
|
|
for (const CharT* iter = data + dataLen - 1; iter >= data; --iter)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
CharT currentChar = *iter;
|
|
|
|
if (currentChar & filter)
|
|
|
|
continue; // char is not in filter set; go on with next char.
|
|
|
|
|
|
|
|
// test all chars
|
|
|
|
const CharT* charInSet = set;
|
|
|
|
CharT setChar = *charInSet;
|
|
|
|
while (setChar)
|
|
|
|
{
|
|
|
|
if (setChar == currentChar)
|
|
|
|
return iter - data; // found it! return index of the found char.
|
|
|
|
|
|
|
|
setChar = *(++charInSet);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
return kNotFound;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* this method changes the meaning of |offset| and |count|:
|
|
|
|
*
|
|
|
|
* upon return,
|
|
|
|
* |offset| specifies start of search range
|
|
|
|
* |count| specifies length of search range
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
static void
|
2012-08-22 19:56:38 +04:00
|
|
|
Find_ComputeSearchRange( uint32_t bigLen, uint32_t littleLen, int32_t& offset, int32_t& count )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// |count| specifies how many iterations to make from |offset|
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (offset < 0)
|
|
|
|
{
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
else if (uint32_t(offset) > bigLen)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t maxCount = bigLen - offset;
|
|
|
|
if (count < 0 || count > maxCount)
|
|
|
|
{
|
|
|
|
count = maxCount;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
count += littleLen;
|
|
|
|
if (count > maxCount)
|
|
|
|
count = maxCount;
|
|
|
|
}
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* this method changes the meaning of |offset| and |count|:
|
|
|
|
*
|
|
|
|
* upon entry,
|
|
|
|
* |offset| specifies the end point from which to search backwards
|
|
|
|
* |count| specifies the number of iterations from |offset|
|
|
|
|
*
|
|
|
|
* upon return,
|
|
|
|
* |offset| specifies start of search range
|
|
|
|
* |count| specifies length of search range
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* EXAMPLE
|
|
|
|
*
|
|
|
|
* + -- littleLen=4 -- +
|
|
|
|
* : :
|
|
|
|
* |____|____|____|____|____|____|____|____|____|____|____|____|
|
|
|
|
* : :
|
|
|
|
* offset=5 bigLen=12
|
|
|
|
*
|
|
|
|
* if count = 4, then we expect this function to return offset = 2 and
|
|
|
|
* count = 7.
|
|
|
|
*
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
static void
|
2012-08-22 19:56:38 +04:00
|
|
|
RFind_ComputeSearchRange( uint32_t bigLen, uint32_t littleLen, int32_t& offset, int32_t& count )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (littleLen > bigLen)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
offset = 0;
|
|
|
|
count = 0;
|
|
|
|
return;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (offset < 0)
|
|
|
|
offset = bigLen - littleLen;
|
|
|
|
if (count < 0)
|
|
|
|
count = offset + 1;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t start = offset - count + 1;
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
count = offset + littleLen - start;
|
|
|
|
offset = start;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// define nsString obsolete methods
|
2004-02-19 05:44:03 +03:00
|
|
|
#include "string-template-def-unichar.h"
|
|
|
|
#include "nsTStringObsolete.cpp"
|
|
|
|
#include "string-template-undef.h"
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// define nsCString obsolete methods
|
2004-02-19 05:44:03 +03:00
|
|
|
#include "string-template-def-char.h"
|
|
|
|
#include "nsTStringObsolete.cpp"
|
|
|
|
#include "string-template-undef.h"
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// specialized methods:
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsString::Find( const nsAFlatString& aString, int32_t aOffset, int32_t aCount ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// this method changes the meaning of aOffset and aCount:
|
|
|
|
Find_ComputeSearchRange(mLength, aString.Length(), aOffset, aCount);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t result = FindSubstring(mData + aOffset, aCount, static_cast<const char16_t*>(aString.get()), aString.Length(), false);
|
|
|
|
if (result != kNotFound)
|
|
|
|
result += aOffset;
|
|
|
|
return result;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
nsString::Find( const char16_t* aString, int32_t aOffset, int32_t aCount ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
return Find(nsDependentString(aString), aOffset, aCount);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsString::RFind( const nsAFlatString& aString, int32_t aOffset, int32_t aCount ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// this method changes the meaning of aOffset and aCount:
|
|
|
|
RFind_ComputeSearchRange(mLength, aString.Length(), aOffset, aCount);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t result = RFindSubstring(mData + aOffset, aCount, static_cast<const char16_t*>(aString.get()), aString.Length(), false);
|
|
|
|
if (result != kNotFound)
|
|
|
|
result += aOffset;
|
|
|
|
return result;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
nsString::RFind( const char16_t* aString, int32_t aOffset, int32_t aCount ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
return RFind(nsDependentString(aString), aOffset, aCount);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
nsString::FindCharInSet( const char16_t* aSet, int32_t aOffset ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (aOffset < 0)
|
|
|
|
aOffset = 0;
|
|
|
|
else if (aOffset >= int32_t(mLength))
|
|
|
|
return kNotFound;
|
|
|
|
|
|
|
|
int32_t result = ::FindCharInSet(mData + aOffset, mLength - aOffset, aSet);
|
|
|
|
if (result != kNotFound)
|
|
|
|
result += aOffset;
|
|
|
|
return result;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-03-05 23:04:36 +04:00
|
|
|
void
|
|
|
|
nsString::ReplaceChar( const char16_t* aSet, char16_t aNewChar )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (!EnsureMutable()) // XXX do this lazily?
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(mLength);
|
2014-03-05 23:04:36 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
char16_t* data = mData;
|
|
|
|
uint32_t lenRemaining = mLength;
|
2014-03-05 23:04:36 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
while (lenRemaining)
|
|
|
|
{
|
|
|
|
int32_t i = ::FindCharInSet(data, lenRemaining, aSet);
|
|
|
|
if (i == kNotFound)
|
|
|
|
break;
|
2014-03-05 23:04:36 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
data[i++] = aNewChar;
|
|
|
|
data += i;
|
|
|
|
lenRemaining -= i;
|
2014-03-05 23:04:36 +04:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2014-03-05 23:04:36 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::Compare,CompareWithConversion,etc.
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsCString::Compare( const char* aString, bool aIgnoreCase, int32_t aCount ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
uint32_t strLen = char_traits::length(aString);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t maxCount = int32_t(XPCOM_MIN(mLength, strLen));
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t compareCount;
|
|
|
|
if (aCount < 0 || aCount > maxCount)
|
|
|
|
compareCount = maxCount;
|
|
|
|
else
|
|
|
|
compareCount = aCount;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t result =
|
|
|
|
nsBufferRoutines<char>::compare(mData, aString, compareCount, aIgnoreCase);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (result == 0 &&
|
|
|
|
(aCount < 0 || strLen < uint32_t(aCount) || mLength < uint32_t(aCount)))
|
|
|
|
{
|
|
|
|
// Since the caller didn't give us a length to test, or strings shorter
|
|
|
|
// than aCount, and compareCount characters matched, we have to assume
|
|
|
|
// that the longer string is greater.
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (mLength != strLen)
|
|
|
|
result = (mLength < strLen) ? -1 : 1;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
return result;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsString::EqualsIgnoreCase( const char* aString, int32_t aCount ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
uint32_t strLen = nsCharTraits<char>::length(aString);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t maxCount = int32_t(XPCOM_MIN(mLength, strLen));
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t compareCount;
|
|
|
|
if (aCount < 0 || aCount > maxCount)
|
|
|
|
compareCount = maxCount;
|
|
|
|
else
|
|
|
|
compareCount = aCount;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t result =
|
|
|
|
nsBufferRoutines<char16_t>::compare(mData, aString, compareCount, true);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (result == 0 &&
|
|
|
|
(aCount < 0 || strLen < uint32_t(aCount) || mLength < uint32_t(aCount)))
|
|
|
|
{
|
|
|
|
// Since the caller didn't give us a length to test, or strings shorter
|
|
|
|
// than aCount, and compareCount characters matched, we have to assume
|
|
|
|
// that the longer string is greater.
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (mLength != strLen)
|
|
|
|
result = 1; // Arbitrarily using any number != 0
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
return result == 0;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2011-03-25 14:13:59 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::ToDouble
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2011-03-25 14:13:59 +03:00
|
|
|
double
|
2012-07-27 17:59:29 +04:00
|
|
|
nsCString::ToDouble(nsresult* aErrorCode) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
double res = 0.0;
|
|
|
|
if (mLength > 0)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
char *conv_stopped;
|
|
|
|
const char *str = mData;
|
|
|
|
// Use PR_strtod, not strtod, since we don't want locale involved.
|
|
|
|
res = PR_strtod(str, &conv_stopped);
|
|
|
|
if (conv_stopped == str+mLength)
|
|
|
|
*aErrorCode = NS_OK;
|
|
|
|
else // Not all the string was scanned
|
|
|
|
*aErrorCode = NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The string was too short (0 characters)
|
|
|
|
*aErrorCode = NS_ERROR_ILLEGAL_VALUE;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
return res;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2011-03-25 14:13:59 +03:00
|
|
|
double
|
2012-07-27 17:59:29 +04:00
|
|
|
nsString::ToDouble(nsresult* aErrorCode) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
return NS_LossyConvertUTF16toASCII(*this).ToDouble(aErrorCode);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::AssignWithConversion
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsCString::AssignWithConversion( const nsAString& aData )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
LossyCopyUTF16toASCII(aData, *this);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsString::AssignWithConversion( const nsACString& aData )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
CopyASCIItoUTF16(aData, *this);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
#endif // !MOZ_STRING_WITH_OBSOLETE_API
|