xpcom/string/src/nsTStringObsolete.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/string/src/nsTStringObsolete.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,584 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et cindent: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +
    1.11 +
    1.12 +  /**
    1.13 +   * nsTString::Find
    1.14 +   *
    1.15 +   * aOffset specifies starting index
    1.16 +   * aCount specifies number of string compares (iterations)
    1.17 +   */
    1.18 +
    1.19 +int32_t
    1.20 +nsTString_CharT::Find( const nsCString& aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
    1.21 +  {
    1.22 +    // this method changes the meaning of aOffset and aCount:
    1.23 +    Find_ComputeSearchRange(mLength, aString.Length(), aOffset, aCount);
    1.24 +
    1.25 +    int32_t result = FindSubstring(mData + aOffset, aCount, aString.get(), aString.Length(), aIgnoreCase);
    1.26 +    if (result != kNotFound)
    1.27 +      result += aOffset;
    1.28 +    return result;
    1.29 +  }
    1.30 +
    1.31 +int32_t
    1.32 +nsTString_CharT::Find( const char* aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
    1.33 +  {
    1.34 +    return Find(nsDependentCString(aString), aIgnoreCase, aOffset, aCount);
    1.35 +  }
    1.36 +
    1.37 +
    1.38 +  /**
    1.39 +   * nsTString::RFind
    1.40 +   *
    1.41 +   * aOffset specifies starting index
    1.42 +   * aCount specifies number of string compares (iterations)
    1.43 +   */
    1.44 +
    1.45 +int32_t
    1.46 +nsTString_CharT::RFind( const nsCString& aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
    1.47 +  {
    1.48 +    // this method changes the meaning of aOffset and aCount:
    1.49 +    RFind_ComputeSearchRange(mLength, aString.Length(), aOffset, aCount);
    1.50 +
    1.51 +    int32_t result = RFindSubstring(mData + aOffset, aCount, aString.get(), aString.Length(), aIgnoreCase);
    1.52 +    if (result != kNotFound)
    1.53 +      result += aOffset;
    1.54 +    return result;
    1.55 +  }
    1.56 +
    1.57 +int32_t
    1.58 +nsTString_CharT::RFind( const char* aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
    1.59 +  {
    1.60 +    return RFind(nsDependentCString(aString), aIgnoreCase, aOffset, aCount);
    1.61 +  }
    1.62 +
    1.63 +
    1.64 +  /**
    1.65 +   * nsTString::RFindChar
    1.66 +   */
    1.67 +
    1.68 +int32_t
    1.69 +nsTString_CharT::RFindChar( char16_t aChar, int32_t aOffset, int32_t aCount) const
    1.70 +  {
    1.71 +    return nsBufferRoutines<CharT>::rfind_char(mData, mLength, aOffset, aChar, aCount);
    1.72 +  }
    1.73 +
    1.74 +
    1.75 +  /**
    1.76 +   * nsTString::FindCharInSet
    1.77 +   */
    1.78 +
    1.79 +int32_t
    1.80 +nsTString_CharT::FindCharInSet( const char* aSet, int32_t aOffset ) const
    1.81 +  {
    1.82 +    if (aOffset < 0)
    1.83 +      aOffset = 0;
    1.84 +    else if (aOffset >= int32_t(mLength))
    1.85 +      return kNotFound;
    1.86 +    
    1.87 +    int32_t result = ::FindCharInSet(mData + aOffset, mLength - aOffset, aSet);
    1.88 +    if (result != kNotFound)
    1.89 +      result += aOffset;
    1.90 +    return result;
    1.91 +  }
    1.92 +
    1.93 +
    1.94 +  /**
    1.95 +   * nsTString::RFindCharInSet
    1.96 +   */
    1.97 +
    1.98 +int32_t
    1.99 +nsTString_CharT::RFindCharInSet( const CharT* aSet, int32_t aOffset ) const
   1.100 +  {
   1.101 +    // We want to pass a "data length" to ::RFindCharInSet
   1.102 +    if (aOffset < 0 || aOffset > int32_t(mLength))
   1.103 +      aOffset = mLength;
   1.104 +    else
   1.105 +      ++aOffset;
   1.106 +
   1.107 +    return ::RFindCharInSet(mData, aOffset, aSet);
   1.108 +  }
   1.109 +
   1.110 +
   1.111 +  // it's a shame to replicate this code.  it was done this way in the past
   1.112 +  // to help performance.  this function also gets to keep the rickg style
   1.113 +  // indentation :-/
   1.114 +int32_t
   1.115 +nsTString_CharT::ToInteger( nsresult* aErrorCode, uint32_t aRadix ) const
   1.116 +{
   1.117 +  CharT*  cp=mData;
   1.118 +  int32_t theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect)
   1.119 +  int32_t result=0;
   1.120 +  bool    negate=false;
   1.121 +  CharT   theChar=0;
   1.122 +
   1.123 +    //initial value, override if we find an integer
   1.124 +  *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
   1.125 +  
   1.126 +  if(cp) {
   1.127 +  
   1.128 +    //begin by skipping over leading chars that shouldn't be part of the number...
   1.129 +    
   1.130 +    CharT*  endcp=cp+mLength;
   1.131 +    bool    done=false;
   1.132 +    
   1.133 +    while((cp<endcp) && (!done)){
   1.134 +      switch(*cp++) {
   1.135 +        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
   1.136 +        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
   1.137 +          theRadix=16;
   1.138 +          done=true;
   1.139 +          break;
   1.140 +        case '0': case '1': case '2': case '3': case '4': 
   1.141 +        case '5': case '6': case '7': case '8': case '9':
   1.142 +          done=true;
   1.143 +          break;
   1.144 +        case '-': 
   1.145 +          negate=true; //fall through...
   1.146 +          break;
   1.147 +        case 'X': case 'x': 
   1.148 +          theRadix=16;
   1.149 +          break; 
   1.150 +        default:
   1.151 +          break;
   1.152 +      } //switch
   1.153 +    }
   1.154 +
   1.155 +    if (done) {
   1.156 +
   1.157 +        //integer found
   1.158 +      *aErrorCode = NS_OK;
   1.159 +
   1.160 +      if (aRadix!=kAutoDetect) theRadix = aRadix; // override
   1.161 +
   1.162 +        //now iterate the numeric chars and build our result
   1.163 +      CharT* first=--cp;  //in case we have to back up.
   1.164 +      bool haveValue = false;
   1.165 +
   1.166 +      while(cp<endcp){
   1.167 +        int32_t oldresult = result;
   1.168 +
   1.169 +        theChar=*cp++;
   1.170 +        if(('0'<=theChar) && (theChar<='9')){
   1.171 +          result = (theRadix * result) + (theChar-'0');
   1.172 +          haveValue = true;
   1.173 +        }
   1.174 +        else if((theChar>='A') && (theChar<='F')) {
   1.175 +          if(10==theRadix) {
   1.176 +            if(kAutoDetect==aRadix){
   1.177 +              theRadix=16;
   1.178 +              cp=first; //backup
   1.179 +              result=0;
   1.180 +              haveValue = false;
   1.181 +            }
   1.182 +            else {
   1.183 +              *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
   1.184 +              result=0;
   1.185 +              break;
   1.186 +            }
   1.187 +          }
   1.188 +          else {
   1.189 +            result = (theRadix * result) + ((theChar-'A')+10);
   1.190 +            haveValue = true;
   1.191 +          }
   1.192 +        }
   1.193 +        else if((theChar>='a') && (theChar<='f')) {
   1.194 +          if(10==theRadix) {
   1.195 +            if(kAutoDetect==aRadix){
   1.196 +              theRadix=16;
   1.197 +              cp=first; //backup
   1.198 +              result=0;
   1.199 +              haveValue = false;
   1.200 +            }
   1.201 +            else {
   1.202 +              *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
   1.203 +              result=0;
   1.204 +              break;
   1.205 +            }
   1.206 +          }
   1.207 +          else {
   1.208 +            result = (theRadix * result) + ((theChar-'a')+10);
   1.209 +            haveValue = true;
   1.210 +          }
   1.211 +        }
   1.212 +        else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) {
   1.213 +          continue;
   1.214 +        }
   1.215 +        else if((('#'==theChar) || ('+'==theChar)) && !haveValue) {
   1.216 +          continue;
   1.217 +        }
   1.218 +        else {
   1.219 +          //we've encountered a char that's not a legal number or sign
   1.220 +          break;
   1.221 +        }
   1.222 +
   1.223 +        if (result < oldresult) {
   1.224 +          // overflow!
   1.225 +          *aErrorCode = NS_ERROR_ILLEGAL_VALUE;
   1.226 +          result = 0;
   1.227 +          break;
   1.228 +        }
   1.229 +      } //while
   1.230 +      if(negate)
   1.231 +        result=-result;
   1.232 +    } //if
   1.233 +  }
   1.234 +  return result;
   1.235 +}
   1.236 +
   1.237 +
   1.238 +  /**
   1.239 +   * nsTString::ToInteger64
   1.240 +   */
   1.241 +int64_t
   1.242 +nsTString_CharT::ToInteger64( nsresult* aErrorCode, uint32_t aRadix ) const
   1.243 +{
   1.244 +  CharT*  cp=mData;
   1.245 +  int32_t theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect)
   1.246 +  int64_t result=0;
   1.247 +  bool    negate=false;
   1.248 +  CharT   theChar=0;
   1.249 +
   1.250 +    //initial value, override if we find an integer
   1.251 +  *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
   1.252 + 
   1.253 +  if(cp) {
   1.254 + 
   1.255 +    //begin by skipping over leading chars that shouldn't be part of the number...
   1.256 +   
   1.257 +    CharT*  endcp=cp+mLength;
   1.258 +    bool    done=false;
   1.259 +   
   1.260 +    while((cp<endcp) && (!done)){
   1.261 +      switch(*cp++) {
   1.262 +        case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
   1.263 +        case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
   1.264 +          theRadix=16;
   1.265 +          done=true;
   1.266 +          break;
   1.267 +        case '0': case '1': case '2': case '3': case '4':
   1.268 +        case '5': case '6': case '7': case '8': case '9':
   1.269 +          done=true;
   1.270 +          break;
   1.271 +        case '-':
   1.272 +          negate=true; //fall through...
   1.273 +          break;
   1.274 +        case 'X': case 'x':
   1.275 +          theRadix=16;
   1.276 +          break;
   1.277 +        default:
   1.278 +          break;
   1.279 +      } //switch
   1.280 +    }
   1.281 +
   1.282 +    if (done) {
   1.283 +
   1.284 +        //integer found
   1.285 +      *aErrorCode = NS_OK;
   1.286 +
   1.287 +      if (aRadix!=kAutoDetect) theRadix = aRadix; // override
   1.288 +
   1.289 +        //now iterate the numeric chars and build our result
   1.290 +      CharT* first=--cp;  //in case we have to back up.
   1.291 +      bool haveValue = false;
   1.292 +
   1.293 +      while(cp<endcp){
   1.294 +        int64_t oldresult = result;
   1.295 +
   1.296 +        theChar=*cp++;
   1.297 +        if(('0'<=theChar) && (theChar<='9')){
   1.298 +          result = (theRadix * result) + (theChar-'0');
   1.299 +          haveValue = true;
   1.300 +        }
   1.301 +        else if((theChar>='A') && (theChar<='F')) {
   1.302 +          if(10==theRadix) {
   1.303 +            if(kAutoDetect==aRadix){
   1.304 +              theRadix=16;
   1.305 +              cp=first; //backup
   1.306 +              result=0;
   1.307 +              haveValue = false;
   1.308 +            }
   1.309 +            else {
   1.310 +              *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
   1.311 +              result=0;
   1.312 +              break;
   1.313 +            }
   1.314 +          }
   1.315 +          else {
   1.316 +            result = (theRadix * result) + ((theChar-'A')+10);
   1.317 +            haveValue = true;
   1.318 +          }
   1.319 +        }
   1.320 +        else if((theChar>='a') && (theChar<='f')) {
   1.321 +          if(10==theRadix) {
   1.322 +            if(kAutoDetect==aRadix){
   1.323 +              theRadix=16;
   1.324 +              cp=first; //backup
   1.325 +              result=0;
   1.326 +              haveValue = false;
   1.327 +            }
   1.328 +            else {
   1.329 +              *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
   1.330 +              result=0;
   1.331 +              break;
   1.332 +            }
   1.333 +          }
   1.334 +          else {
   1.335 +            result = (theRadix * result) + ((theChar-'a')+10);
   1.336 +            haveValue = true;
   1.337 +          }
   1.338 +        }
   1.339 +        else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) {
   1.340 +          continue;
   1.341 +        }
   1.342 +        else if((('#'==theChar) || ('+'==theChar)) && !haveValue) {
   1.343 +          continue;
   1.344 +        }
   1.345 +        else {
   1.346 +          //we've encountered a char that's not a legal number or sign
   1.347 +          break;
   1.348 +        }
   1.349 +
   1.350 +        if (result < oldresult) {
   1.351 +          // overflow!
   1.352 +          *aErrorCode = NS_ERROR_ILLEGAL_VALUE;
   1.353 +          result = 0;
   1.354 +          break;
   1.355 +        }
   1.356 +      } //while
   1.357 +      if(negate)
   1.358 +        result=-result;
   1.359 +    } //if
   1.360 +  }
   1.361 +  return result;
   1.362 +}
   1.363 +
   1.364 +
   1.365 +/**
   1.366 +   * nsTString::Mid
   1.367 +   */
   1.368 +
   1.369 +uint32_t
   1.370 +nsTString_CharT::Mid( self_type& aResult, index_type aStartPos, size_type aLengthToCopy ) const
   1.371 +  {
   1.372 +    if (aStartPos == 0 && aLengthToCopy >= mLength)
   1.373 +      aResult = *this;
   1.374 +    else
   1.375 +      aResult = Substring(*this, aStartPos, aLengthToCopy);
   1.376 +
   1.377 +    return aResult.mLength;
   1.378 +  }
   1.379 +
   1.380 +
   1.381 +  /**
   1.382 +   * nsTString::SetCharAt
   1.383 +   */
   1.384 +
   1.385 +bool
   1.386 +nsTString_CharT::SetCharAt( char16_t aChar, uint32_t aIndex )
   1.387 +  {
   1.388 +    if (aIndex >= mLength)
   1.389 +      return false;
   1.390 +
   1.391 +    if (!EnsureMutable())
   1.392 +      NS_ABORT_OOM(mLength);
   1.393 +
   1.394 +    mData[aIndex] = CharT(aChar);
   1.395 +    return true;
   1.396 +  }
   1.397 +
   1.398 + 
   1.399 +  /**
   1.400 +   * nsTString::StripChars,StripChar,StripWhitespace
   1.401 +   */
   1.402 +
   1.403 +void
   1.404 +nsTString_CharT::StripChars( const char* aSet )
   1.405 +  {
   1.406 +    if (!EnsureMutable())
   1.407 +      NS_ABORT_OOM(mLength);
   1.408 +
   1.409 +    mLength = nsBufferRoutines<CharT>::strip_chars(mData, mLength, aSet);
   1.410 +  }
   1.411 +
   1.412 +void
   1.413 +nsTString_CharT::StripWhitespace()
   1.414 +  {
   1.415 +    StripChars(kWhitespace);
   1.416 +  }
   1.417 +
   1.418 +
   1.419 +  /**
   1.420 +   * nsTString::ReplaceChar,ReplaceSubstring
   1.421 +   */
   1.422 +
   1.423 +void
   1.424 +nsTString_CharT::ReplaceChar( char_type aOldChar, char_type aNewChar )
   1.425 +  {
   1.426 +    if (!EnsureMutable()) // XXX do this lazily?
   1.427 +      NS_ABORT_OOM(mLength);
   1.428 +
   1.429 +    for (uint32_t i=0; i<mLength; ++i)
   1.430 +      {
   1.431 +        if (mData[i] == aOldChar)
   1.432 +          mData[i] = aNewChar;
   1.433 +      }
   1.434 +  }
   1.435 +
   1.436 +void
   1.437 +nsTString_CharT::ReplaceChar( const char* aSet, char_type aNewChar )
   1.438 +  {
   1.439 +    if (!EnsureMutable()) // XXX do this lazily?
   1.440 +      NS_ABORT_OOM(mLength);
   1.441 +
   1.442 +    char_type* data = mData;
   1.443 +    uint32_t lenRemaining = mLength;
   1.444 +
   1.445 +    while (lenRemaining)
   1.446 +      {
   1.447 +        int32_t i = ::FindCharInSet(data, lenRemaining, aSet);
   1.448 +        if (i == kNotFound)
   1.449 +          break;
   1.450 +
   1.451 +        data[i++] = aNewChar;
   1.452 +        data += i;
   1.453 +        lenRemaining -= i;
   1.454 +      }
   1.455 +  }
   1.456 +
   1.457 +void
   1.458 +nsTString_CharT::ReplaceSubstring( const char_type* aTarget, const char_type* aNewValue )
   1.459 +  {
   1.460 +    ReplaceSubstring(nsTDependentString_CharT(aTarget),
   1.461 +                     nsTDependentString_CharT(aNewValue));
   1.462 +  }
   1.463 +
   1.464 +void
   1.465 +nsTString_CharT::ReplaceSubstring( const self_type& aTarget, const self_type& aNewValue )
   1.466 +  {
   1.467 +    if (aTarget.Length() == 0)
   1.468 +      return;
   1.469 +
   1.470 +    uint32_t i = 0;
   1.471 +    while (i < mLength)
   1.472 +      {
   1.473 +        int32_t r = FindSubstring(mData + i, mLength - i, static_cast<const char_type*>(aTarget.Data()), aTarget.Length(), false);
   1.474 +        if (r == kNotFound)
   1.475 +          break;
   1.476 +
   1.477 +        Replace(i + r, aTarget.Length(), aNewValue);
   1.478 +        i += r + aNewValue.Length();
   1.479 +      }
   1.480 +  }
   1.481 +
   1.482 +
   1.483 +  /**
   1.484 +   * nsTString::Trim
   1.485 +   */
   1.486 +
   1.487 +void
   1.488 +nsTString_CharT::Trim( const char* aSet, bool aTrimLeading, bool aTrimTrailing, bool aIgnoreQuotes )
   1.489 +  {
   1.490 +      // the old implementation worried about aSet being null :-/
   1.491 +    if (!aSet)
   1.492 +      return;
   1.493 +
   1.494 +    char_type* start = mData;
   1.495 +    char_type* end   = mData + mLength;
   1.496 +
   1.497 +      // skip over quotes if requested
   1.498 +    if (aIgnoreQuotes && mLength > 2 && mData[0] == mData[mLength - 1] &&
   1.499 +          (mData[0] == '\'' || mData[0] == '"'))
   1.500 +      {
   1.501 +        ++start;
   1.502 +        --end;
   1.503 +      }
   1.504 +
   1.505 +    uint32_t setLen = nsCharTraits<char>::length(aSet);
   1.506 +
   1.507 +    if (aTrimLeading)
   1.508 +      {
   1.509 +        uint32_t cutStart = start - mData;
   1.510 +        uint32_t cutLength = 0;
   1.511 +
   1.512 +          // walk forward from start to end
   1.513 +        for (; start != end; ++start, ++cutLength)
   1.514 +          {
   1.515 +            int32_t pos = FindChar1(aSet, setLen, 0, *start, setLen);
   1.516 +            if (pos == kNotFound)
   1.517 +              break;
   1.518 +          }
   1.519 +
   1.520 +        if (cutLength)
   1.521 +          {
   1.522 +            Cut(cutStart, cutLength);
   1.523 +
   1.524 +              // reset iterators
   1.525 +            start = mData + cutStart;
   1.526 +            end   = mData + mLength - cutStart;
   1.527 +          }
   1.528 +      }
   1.529 +
   1.530 +    if (aTrimTrailing)
   1.531 +      {
   1.532 +        uint32_t cutEnd = end - mData;
   1.533 +        uint32_t cutLength = 0;
   1.534 +
   1.535 +          // walk backward from end to start
   1.536 +        --end;
   1.537 +        for (; end >= start; --end, ++cutLength)
   1.538 +          {
   1.539 +            int32_t pos = FindChar1(aSet, setLen, 0, *end, setLen);
   1.540 +            if (pos == kNotFound)
   1.541 +              break;
   1.542 +          }
   1.543 +
   1.544 +        if (cutLength)
   1.545 +          Cut(cutEnd - cutLength, cutLength);
   1.546 +      }
   1.547 +  }
   1.548 +
   1.549 +
   1.550 +  /**
   1.551 +   * nsTString::CompressWhitespace
   1.552 +   */
   1.553 +
   1.554 +void
   1.555 +nsTString_CharT::CompressWhitespace( bool aTrimLeading, bool aTrimTrailing )
   1.556 +  {
   1.557 +    const char* set = kWhitespace;
   1.558 +
   1.559 +    ReplaceChar(set, ' ');
   1.560 +    Trim(set, aTrimLeading, aTrimTrailing);
   1.561 +
   1.562 +      // this one does some questionable fu... just copying the old code!
   1.563 +    mLength = nsBufferRoutines<char_type>::compress_chars(mData, mLength, set);
   1.564 +  }
   1.565 +
   1.566 +
   1.567 +  /**
   1.568 +   * nsTString::AssignWithConversion
   1.569 +   */
   1.570 +
   1.571 +void
   1.572 +nsTString_CharT::AssignWithConversion( const incompatible_char_type* aData, int32_t aLength )
   1.573 +  {
   1.574 +      // for compatibility with the old string implementation, we need to allow
   1.575 +      // for a nullptr input buffer :-(
   1.576 +    if (!aData)
   1.577 +      {
   1.578 +        Truncate();
   1.579 +      }
   1.580 +    else
   1.581 +      {
   1.582 +        if (aLength < 0)
   1.583 +          aLength = nsCharTraits<incompatible_char_type>::length(aData);
   1.584 +
   1.585 +        AssignWithConversion(Substring(aData, aLength));
   1.586 +      }
   1.587 +  }

mercurial