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
|
|
|
|
2014-12-13 02:57:09 +03:00
|
|
|
#include "nsTArray.h"
|
2016-11-29 00:29:04 +03:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::Find
|
|
|
|
*
|
|
|
|
* aOffset specifies starting index
|
|
|
|
* aCount specifies number of string compares (iterations)
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsTString_CharT::Find( const nsCString& aString, bool aIgnoreCase, 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, aString.get(), aString.Length(), aIgnoreCase);
|
|
|
|
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
|
|
|
|
nsTString_CharT::Find( const char* aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
return Find(nsDependentCString(aString), aIgnoreCase, aOffset, aCount);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::RFind
|
|
|
|
*
|
|
|
|
* aOffset specifies starting index
|
|
|
|
* aCount specifies number of string compares (iterations)
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsTString_CharT::RFind( const nsCString& aString, bool aIgnoreCase, 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, aString.get(), aString.Length(), aIgnoreCase);
|
|
|
|
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
|
|
|
|
nsTString_CharT::RFind( const char* aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
return RFind(nsDependentCString(aString), aIgnoreCase, aOffset, aCount);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::RFindChar
|
|
|
|
*/
|
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
|
|
|
nsTString_CharT::RFindChar( char16_t aChar, int32_t aOffset, int32_t aCount) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
return nsBufferRoutines<CharT>::rfind_char(mData, mLength, aOffset, aChar, aCount);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::FindCharInSet
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsTString_CharT::FindCharInSet( const char* 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-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::RFindCharInSet
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsTString_CharT::RFindCharInSet( const CharT* aSet, int32_t aOffset ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// We want to pass a "data length" to ::RFindCharInSet
|
|
|
|
if (aOffset < 0 || aOffset > int32_t(mLength))
|
|
|
|
aOffset = mLength;
|
|
|
|
else
|
|
|
|
++aOffset;
|
2004-04-20 00:18:05 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
return ::RFindCharInSet(mData, aOffset, aSet);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// it's a shame to replicate this code. it was done this way in the past
|
|
|
|
// to help performance. this function also gets to keep the rickg style
|
|
|
|
// indentation :-/
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsTString_CharT::ToInteger( nsresult* aErrorCode, uint32_t aRadix ) const
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
|
|
|
CharT* cp=mData;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect)
|
|
|
|
int32_t result=0;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool negate=false;
|
2004-02-19 05:44:03 +03:00
|
|
|
CharT theChar=0;
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//initial value, override if we find an integer
|
2004-02-19 05:44:03 +03:00
|
|
|
*aErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
if(cp) {
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
//begin by skipping over leading chars that shouldn't be part of the number...
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
CharT* endcp=cp+mLength;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool done=false;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
while((cp<endcp) && (!done)){
|
|
|
|
switch(*cp++) {
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
|
|
theRadix=16;
|
2011-10-17 18:59:28 +04:00
|
|
|
done=true;
|
2004-02-19 05:44:03 +03:00
|
|
|
break;
|
2014-05-05 21:30:46 +04:00
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
2004-02-19 05:44:03 +03:00
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
2011-10-17 18:59:28 +04:00
|
|
|
done=true;
|
2004-02-19 05:44:03 +03:00
|
|
|
break;
|
2014-05-05 21:30:46 +04:00
|
|
|
case '-':
|
2011-10-17 18:59:28 +04:00
|
|
|
negate=true; //fall through...
|
2004-02-19 05:44:03 +03:00
|
|
|
break;
|
2014-05-05 21:30:46 +04:00
|
|
|
case 'X': case 'x':
|
2004-02-19 05:44:03 +03:00
|
|
|
theRadix=16;
|
2014-05-05 21:30:46 +04:00
|
|
|
break;
|
2004-02-19 05:44:03 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} //switch
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//integer found
|
2004-02-19 05:44:03 +03:00
|
|
|
*aErrorCode = NS_OK;
|
|
|
|
|
|
|
|
if (aRadix!=kAutoDetect) theRadix = aRadix; // override
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//now iterate the numeric chars and build our result
|
2004-02-19 05:44:03 +03:00
|
|
|
CharT* first=--cp; //in case we have to back up.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool haveValue = false;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
while(cp<endcp){
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t oldresult = result;
|
2009-01-15 21:44:46 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
theChar=*cp++;
|
|
|
|
if(('0'<=theChar) && (theChar<='9')){
|
|
|
|
result = (theRadix * result) + (theChar-'0');
|
2011-10-17 18:59:28 +04:00
|
|
|
haveValue = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
else if((theChar>='A') && (theChar<='F')) {
|
|
|
|
if(10==theRadix) {
|
|
|
|
if(kAutoDetect==aRadix){
|
|
|
|
theRadix=16;
|
|
|
|
cp=first; //backup
|
|
|
|
result=0;
|
2011-10-17 18:59:28 +04:00
|
|
|
haveValue = false;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = (theRadix * result) + ((theChar-'A')+10);
|
2011-10-17 18:59:28 +04:00
|
|
|
haveValue = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((theChar>='a') && (theChar<='f')) {
|
|
|
|
if(10==theRadix) {
|
|
|
|
if(kAutoDetect==aRadix){
|
|
|
|
theRadix=16;
|
|
|
|
cp=first; //backup
|
|
|
|
result=0;
|
2011-10-17 18:59:28 +04:00
|
|
|
haveValue = false;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = (theRadix * result) + ((theChar-'a')+10);
|
2011-10-17 18:59:28 +04:00
|
|
|
haveValue = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if((('#'==theChar) || ('+'==theChar)) && !haveValue) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//we've encountered a char that's not a legal number or sign
|
|
|
|
break;
|
|
|
|
}
|
2009-01-15 21:44:46 +03:00
|
|
|
|
|
|
|
if (result < oldresult) {
|
|
|
|
// overflow!
|
|
|
|
*aErrorCode = NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
} //while
|
|
|
|
if(negate)
|
|
|
|
result=-result;
|
|
|
|
} //if
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::ToInteger64
|
|
|
|
*/
|
2012-09-18 05:10:50 +04:00
|
|
|
int64_t
|
|
|
|
nsTString_CharT::ToInteger64( nsresult* aErrorCode, uint32_t aRadix ) const
|
|
|
|
{
|
|
|
|
CharT* cp=mData;
|
|
|
|
int32_t theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect)
|
|
|
|
int64_t result=0;
|
|
|
|
bool negate=false;
|
|
|
|
CharT theChar=0;
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//initial value, override if we find an integer
|
2012-09-18 05:10:50 +04:00
|
|
|
*aErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2012-09-18 05:10:50 +04:00
|
|
|
if(cp) {
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2012-09-18 05:10:50 +04:00
|
|
|
//begin by skipping over leading chars that shouldn't be part of the number...
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2012-09-18 05:10:50 +04:00
|
|
|
CharT* endcp=cp+mLength;
|
|
|
|
bool done=false;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2012-09-18 05:10:50 +04:00
|
|
|
while((cp<endcp) && (!done)){
|
|
|
|
switch(*cp++) {
|
|
|
|
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
|
|
|
|
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
|
|
|
|
theRadix=16;
|
|
|
|
done=true;
|
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
done=true;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
negate=true; //fall through...
|
|
|
|
break;
|
|
|
|
case 'X': case 'x':
|
|
|
|
theRadix=16;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
} //switch
|
|
|
|
}
|
|
|
|
|
|
|
|
if (done) {
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//integer found
|
2012-09-18 05:10:50 +04:00
|
|
|
*aErrorCode = NS_OK;
|
|
|
|
|
|
|
|
if (aRadix!=kAutoDetect) theRadix = aRadix; // override
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
//now iterate the numeric chars and build our result
|
2012-09-18 05:10:50 +04:00
|
|
|
CharT* first=--cp; //in case we have to back up.
|
|
|
|
bool haveValue = false;
|
|
|
|
|
|
|
|
while(cp<endcp){
|
|
|
|
int64_t oldresult = result;
|
|
|
|
|
|
|
|
theChar=*cp++;
|
|
|
|
if(('0'<=theChar) && (theChar<='9')){
|
|
|
|
result = (theRadix * result) + (theChar-'0');
|
|
|
|
haveValue = true;
|
|
|
|
}
|
|
|
|
else if((theChar>='A') && (theChar<='F')) {
|
|
|
|
if(10==theRadix) {
|
|
|
|
if(kAutoDetect==aRadix){
|
|
|
|
theRadix=16;
|
|
|
|
cp=first; //backup
|
|
|
|
result=0;
|
|
|
|
haveValue = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = (theRadix * result) + ((theChar-'A')+10);
|
|
|
|
haveValue = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((theChar>='a') && (theChar<='f')) {
|
|
|
|
if(10==theRadix) {
|
|
|
|
if(kAutoDetect==aRadix){
|
|
|
|
theRadix=16;
|
|
|
|
cp=first; //backup
|
|
|
|
result=0;
|
|
|
|
haveValue = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*aErrorCode=NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = (theRadix * result) + ((theChar-'a')+10);
|
|
|
|
haveValue = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if((('#'==theChar) || ('+'==theChar)) && !haveValue) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//we've encountered a char that's not a legal number or sign
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result < oldresult) {
|
|
|
|
// overflow!
|
|
|
|
*aErrorCode = NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
result = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} //while
|
|
|
|
if(negate)
|
|
|
|
result=-result;
|
|
|
|
} //if
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-02-19 05:44:03 +03:00
|
|
|
* nsTString::Mid
|
|
|
|
*/
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2004-02-19 05:44:03 +03:00
|
|
|
nsTString_CharT::Mid( self_type& aResult, index_type aStartPos, size_type aLengthToCopy ) const
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (aStartPos == 0 && aLengthToCopy >= mLength)
|
|
|
|
aResult = *this;
|
|
|
|
else
|
|
|
|
aResult = Substring(*this, aStartPos, aLengthToCopy);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
return aResult.mLength;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::SetCharAt
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-01-04 19:02:17 +04:00
|
|
|
nsTString_CharT::SetCharAt( char16_t aChar, uint32_t aIndex )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (aIndex >= mLength)
|
|
|
|
return false;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (!EnsureMutable())
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(mLength);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
mData[aIndex] = CharT(aChar);
|
|
|
|
return true;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* nsTString::StripChars,StripChar,StripWhitespace
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsTString_CharT::StripChars( const char* aSet )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (!EnsureMutable())
|
2014-11-12 11:13:44 +03:00
|
|
|
AllocFailed(mLength);
|
2012-05-08 20:42:27 +04:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
mLength = nsBufferRoutines<CharT>::strip_chars(mData, mLength, aSet);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsTString_CharT::StripWhitespace()
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
StripChars(kWhitespace);
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::ReplaceChar,ReplaceSubstring
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsTString_CharT::ReplaceChar( char_type aOldChar, char_type 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-05-05 21:30:46 +04:00
|
|
|
|
|
|
|
for (uint32_t i=0; i<mLength; ++i)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
if (mData[i] == aOldChar)
|
|
|
|
mData[i] = aNewChar;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsTString_CharT::ReplaceChar( const char* aSet, char_type 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);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
char_type* data = mData;
|
|
|
|
uint32_t lenRemaining = mLength;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
while (lenRemaining)
|
|
|
|
{
|
|
|
|
int32_t i = ::FindCharInSet(data, lenRemaining, aSet);
|
|
|
|
if (i == kNotFound)
|
|
|
|
break;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
data[i++] = aNewChar;
|
|
|
|
data += i;
|
|
|
|
lenRemaining -= i;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-12-13 02:57:09 +03:00
|
|
|
void ReleaseData(void* aData, uint32_t aFlags);
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2015-01-08 04:14:01 +03:00
|
|
|
nsTString_CharT::ReplaceSubstring(const char_type* aTarget,
|
|
|
|
const char_type* aNewValue)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
ReplaceSubstring(nsTDependentString_CharT(aTarget),
|
|
|
|
nsTDependentString_CharT(aNewValue));
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2015-01-08 04:14:01 +03:00
|
|
|
bool
|
|
|
|
nsTString_CharT::ReplaceSubstring(const char_type* aTarget,
|
|
|
|
const char_type* aNewValue,
|
2015-01-28 12:00:40 +03:00
|
|
|
const fallible_t& aFallible)
|
2015-01-08 04:14:01 +03:00
|
|
|
{
|
|
|
|
return ReplaceSubstring(nsTDependentString_CharT(aTarget),
|
|
|
|
nsTDependentString_CharT(aNewValue),
|
2015-01-28 12:00:40 +03:00
|
|
|
aFallible);
|
2015-01-08 04:14:01 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
2015-01-08 04:14:01 +03:00
|
|
|
nsTString_CharT::ReplaceSubstring(const self_type& aTarget,
|
|
|
|
const self_type& aNewValue)
|
|
|
|
{
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!ReplaceSubstring(aTarget, aNewValue, mozilla::fallible)) {
|
2015-01-08 04:14:01 +03:00
|
|
|
// Note that this may wildly underestimate the allocation that failed, as
|
|
|
|
// we could have been replacing multiple copies of aTarget.
|
|
|
|
AllocFailed(mLength + (aNewValue.Length() - aTarget.Length()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsTString_CharT::ReplaceSubstring(const self_type& aTarget,
|
|
|
|
const self_type& aNewValue,
|
|
|
|
const fallible_t&)
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
if (aTarget.Length() == 0)
|
2015-01-08 04:14:01 +03:00
|
|
|
return true;
|
2014-05-05 21:30:46 +04:00
|
|
|
|
2014-12-13 02:57:09 +03:00
|
|
|
// Remember all of the non-matching parts.
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<Segment, 16> nonMatching;
|
2014-05-05 21:30:46 +04:00
|
|
|
uint32_t i = 0;
|
2016-11-29 00:29:04 +03:00
|
|
|
mozilla::CheckedUint32 newLength;
|
2014-12-13 02:57:09 +03:00
|
|
|
while (true)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
int32_t r = FindSubstring(mData + i, mLength - i, static_cast<const char_type*>(aTarget.Data()), aTarget.Length(), false);
|
2014-12-13 02:57:09 +03:00
|
|
|
int32_t until = (r == kNotFound) ? mLength - i : r;
|
|
|
|
nonMatching.AppendElement(Segment(i, until));
|
|
|
|
newLength += until;
|
|
|
|
if (r == kNotFound) {
|
2014-05-05 21:30:46 +04:00
|
|
|
break;
|
2014-12-13 02:57:09 +03:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-12-13 02:57:09 +03:00
|
|
|
newLength += aNewValue.Length();
|
|
|
|
i += r + aTarget.Length();
|
|
|
|
if (i >= mLength) {
|
|
|
|
// Add an auxiliary entry at the end of the list to help as an edge case
|
|
|
|
// for the algorithms below.
|
|
|
|
nonMatching.AppendElement(Segment(mLength, 0));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:29:04 +03:00
|
|
|
if (!newLength.isValid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-13 02:57:09 +03:00
|
|
|
// If there's only one non-matching segment, then the target string was not
|
|
|
|
// found, and there's nothing to do.
|
|
|
|
if (nonMatching.Length() == 1) {
|
|
|
|
MOZ_ASSERT(nonMatching[0].mBegin == 0 && nonMatching[0].mLength == mLength,
|
|
|
|
"We should have the correct non-matching segment.");
|
2015-01-08 04:14:01 +03:00
|
|
|
return true;
|
2014-12-13 02:57:09 +03:00
|
|
|
}
|
2014-12-12 21:04:44 +03:00
|
|
|
|
2014-12-13 02:57:09 +03:00
|
|
|
// Make sure that we can mutate our buffer.
|
2014-12-20 18:25:02 +03:00
|
|
|
// Note that we always allocate at least an mLength sized buffer, because the
|
|
|
|
// rest of the algorithm relies on having access to all of the original
|
|
|
|
// string. In other words, we over-allocate in the shrinking case.
|
2014-12-13 02:57:09 +03:00
|
|
|
char_type* oldData;
|
|
|
|
uint32_t oldFlags;
|
2016-11-29 00:29:04 +03:00
|
|
|
if (!MutatePrep(XPCOM_MAX(mLength, newLength.value()), &oldData, &oldFlags))
|
2015-01-08 04:14:01 +03:00
|
|
|
return false;
|
2014-12-13 02:57:09 +03:00
|
|
|
if (oldData) {
|
|
|
|
// Copy all of the old data to the new buffer.
|
2014-12-20 18:25:02 +03:00
|
|
|
char_traits::copy(mData, oldData, mLength);
|
2014-12-13 02:57:09 +03:00
|
|
|
::ReleaseData(oldData, oldFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTarget.Length() >= aNewValue.Length()) {
|
|
|
|
// In the shrinking case, start filling the buffer from the beginning.
|
|
|
|
const uint32_t delta = (aTarget.Length() - aNewValue.Length());
|
|
|
|
for (i = 1; i < nonMatching.Length(); ++i) {
|
|
|
|
// When we move the i'th non-matching segment into position, we need to
|
|
|
|
// account for the characters deleted by the previous |i| replacements by
|
|
|
|
// subtracting |i * delta|.
|
|
|
|
const char_type* sourceSegmentPtr = mData + nonMatching[i].mBegin;
|
|
|
|
char_type* destinationSegmentPtr = mData + nonMatching[i].mBegin - i * delta;
|
|
|
|
// Write the i'th replacement immediately before the new i'th non-matching
|
|
|
|
// segment.
|
|
|
|
char_traits::copy(destinationSegmentPtr - aNewValue.Length(),
|
|
|
|
aNewValue.Data(), aNewValue.Length());
|
|
|
|
char_traits::move(destinationSegmentPtr, sourceSegmentPtr,
|
|
|
|
nonMatching[i].mLength);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// In the growing case, start filling the buffer from the end.
|
|
|
|
const uint32_t delta = (aNewValue.Length() - aTarget.Length());
|
|
|
|
for (i = nonMatching.Length() - 1; i > 0; --i) {
|
|
|
|
// When we move the i'th non-matching segment into position, we need to
|
|
|
|
// account for the characters added by the previous |i| replacements by
|
|
|
|
// adding |i * delta|.
|
|
|
|
const char_type* sourceSegmentPtr = mData + nonMatching[i].mBegin;
|
|
|
|
char_type* destinationSegmentPtr = mData + nonMatching[i].mBegin + i * delta;
|
|
|
|
char_traits::move(destinationSegmentPtr, sourceSegmentPtr,
|
|
|
|
nonMatching[i].mLength);
|
|
|
|
// Write the i'th replacement immediately before the new i'th non-matching
|
|
|
|
// segment.
|
|
|
|
char_traits::copy(destinationSegmentPtr - aNewValue.Length(),
|
|
|
|
aNewValue.Data(), aNewValue.Length());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the length and make sure the string is null terminated.
|
2016-11-29 00:29:04 +03:00
|
|
|
mLength = newLength.value();
|
2014-12-13 02:57:09 +03:00
|
|
|
mData[mLength] = char_type(0);
|
2015-01-08 04:14:01 +03:00
|
|
|
|
|
|
|
return true;
|
2014-12-13 02:57:09 +03:00
|
|
|
}
|
2014-12-13 03:34:43 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
/**
|
|
|
|
* nsTString::Trim
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsTString_CharT::Trim( const char* aSet, bool aTrimLeading, bool aTrimTrailing, bool aIgnoreQuotes )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// the old implementation worried about aSet being null :-/
|
|
|
|
if (!aSet)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char_type* start = mData;
|
|
|
|
char_type* end = mData + mLength;
|
|
|
|
|
|
|
|
// skip over quotes if requested
|
|
|
|
if (aIgnoreQuotes && mLength > 2 && mData[0] == mData[mLength - 1] &&
|
|
|
|
(mData[0] == '\'' || mData[0] == '"'))
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
++start;
|
|
|
|
--end;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
uint32_t setLen = nsCharTraits<char>::length(aSet);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (aTrimLeading)
|
|
|
|
{
|
|
|
|
uint32_t cutStart = start - mData;
|
|
|
|
uint32_t cutLength = 0;
|
|
|
|
|
|
|
|
// walk forward from start to end
|
|
|
|
for (; start != end; ++start, ++cutLength)
|
|
|
|
{
|
|
|
|
int32_t pos = FindChar1(aSet, setLen, 0, *start, setLen);
|
|
|
|
if (pos == kNotFound)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cutLength)
|
|
|
|
{
|
|
|
|
Cut(cutStart, cutLength);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// reset iterators
|
|
|
|
start = mData + cutStart;
|
|
|
|
end = mData + mLength - cutStart;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (aTrimTrailing)
|
|
|
|
{
|
|
|
|
uint32_t cutEnd = end - mData;
|
|
|
|
uint32_t cutLength = 0;
|
|
|
|
|
|
|
|
// walk backward from end to start
|
|
|
|
--end;
|
|
|
|
for (; end >= start; --end, ++cutLength)
|
|
|
|
{
|
|
|
|
int32_t pos = FindChar1(aSet, setLen, 0, *end, setLen);
|
|
|
|
if (pos == kNotFound)
|
|
|
|
break;
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
if (cutLength)
|
|
|
|
Cut(cutEnd - cutLength, cutLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsTString::CompressWhitespace
|
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsTString_CharT::CompressWhitespace( bool aTrimLeading, bool aTrimTrailing )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
const char* set = kWhitespace;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
ReplaceChar(set, ' ');
|
|
|
|
Trim(set, aTrimLeading, aTrimTrailing);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-05-05 21:30:46 +04:00
|
|
|
// this one does some questionable fu... just copying the old code!
|
|
|
|
mLength = nsBufferRoutines<char_type>::compress_chars(mData, mLength, set);
|
|
|
|
}
|
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
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTString_CharT::AssignWithConversion( const incompatible_char_type* aData, int32_t aLength )
|
2014-05-05 21:30:46 +04:00
|
|
|
{
|
|
|
|
// for compatibility with the old string implementation, we need to allow
|
|
|
|
// for a nullptr input buffer :-(
|
|
|
|
if (!aData)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2014-05-05 21:30:46 +04:00
|
|
|
Truncate();
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2014-05-05 21:30:46 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (aLength < 0)
|
|
|
|
aLength = nsCharTraits<incompatible_char_type>::length(aData);
|
|
|
|
|
|
|
|
AssignWithConversion(Substring(aData, aLength));
|
|
|
|
}
|
|
|
|
}
|