xpcom/string/src/nsTStringObsolete.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7
michael@0 8
michael@0 9 /**
michael@0 10 * nsTString::Find
michael@0 11 *
michael@0 12 * aOffset specifies starting index
michael@0 13 * aCount specifies number of string compares (iterations)
michael@0 14 */
michael@0 15
michael@0 16 int32_t
michael@0 17 nsTString_CharT::Find( const nsCString& aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
michael@0 18 {
michael@0 19 // this method changes the meaning of aOffset and aCount:
michael@0 20 Find_ComputeSearchRange(mLength, aString.Length(), aOffset, aCount);
michael@0 21
michael@0 22 int32_t result = FindSubstring(mData + aOffset, aCount, aString.get(), aString.Length(), aIgnoreCase);
michael@0 23 if (result != kNotFound)
michael@0 24 result += aOffset;
michael@0 25 return result;
michael@0 26 }
michael@0 27
michael@0 28 int32_t
michael@0 29 nsTString_CharT::Find( const char* aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
michael@0 30 {
michael@0 31 return Find(nsDependentCString(aString), aIgnoreCase, aOffset, aCount);
michael@0 32 }
michael@0 33
michael@0 34
michael@0 35 /**
michael@0 36 * nsTString::RFind
michael@0 37 *
michael@0 38 * aOffset specifies starting index
michael@0 39 * aCount specifies number of string compares (iterations)
michael@0 40 */
michael@0 41
michael@0 42 int32_t
michael@0 43 nsTString_CharT::RFind( const nsCString& aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
michael@0 44 {
michael@0 45 // this method changes the meaning of aOffset and aCount:
michael@0 46 RFind_ComputeSearchRange(mLength, aString.Length(), aOffset, aCount);
michael@0 47
michael@0 48 int32_t result = RFindSubstring(mData + aOffset, aCount, aString.get(), aString.Length(), aIgnoreCase);
michael@0 49 if (result != kNotFound)
michael@0 50 result += aOffset;
michael@0 51 return result;
michael@0 52 }
michael@0 53
michael@0 54 int32_t
michael@0 55 nsTString_CharT::RFind( const char* aString, bool aIgnoreCase, int32_t aOffset, int32_t aCount) const
michael@0 56 {
michael@0 57 return RFind(nsDependentCString(aString), aIgnoreCase, aOffset, aCount);
michael@0 58 }
michael@0 59
michael@0 60
michael@0 61 /**
michael@0 62 * nsTString::RFindChar
michael@0 63 */
michael@0 64
michael@0 65 int32_t
michael@0 66 nsTString_CharT::RFindChar( char16_t aChar, int32_t aOffset, int32_t aCount) const
michael@0 67 {
michael@0 68 return nsBufferRoutines<CharT>::rfind_char(mData, mLength, aOffset, aChar, aCount);
michael@0 69 }
michael@0 70
michael@0 71
michael@0 72 /**
michael@0 73 * nsTString::FindCharInSet
michael@0 74 */
michael@0 75
michael@0 76 int32_t
michael@0 77 nsTString_CharT::FindCharInSet( const char* aSet, int32_t aOffset ) const
michael@0 78 {
michael@0 79 if (aOffset < 0)
michael@0 80 aOffset = 0;
michael@0 81 else if (aOffset >= int32_t(mLength))
michael@0 82 return kNotFound;
michael@0 83
michael@0 84 int32_t result = ::FindCharInSet(mData + aOffset, mLength - aOffset, aSet);
michael@0 85 if (result != kNotFound)
michael@0 86 result += aOffset;
michael@0 87 return result;
michael@0 88 }
michael@0 89
michael@0 90
michael@0 91 /**
michael@0 92 * nsTString::RFindCharInSet
michael@0 93 */
michael@0 94
michael@0 95 int32_t
michael@0 96 nsTString_CharT::RFindCharInSet( const CharT* aSet, int32_t aOffset ) const
michael@0 97 {
michael@0 98 // We want to pass a "data length" to ::RFindCharInSet
michael@0 99 if (aOffset < 0 || aOffset > int32_t(mLength))
michael@0 100 aOffset = mLength;
michael@0 101 else
michael@0 102 ++aOffset;
michael@0 103
michael@0 104 return ::RFindCharInSet(mData, aOffset, aSet);
michael@0 105 }
michael@0 106
michael@0 107
michael@0 108 // it's a shame to replicate this code. it was done this way in the past
michael@0 109 // to help performance. this function also gets to keep the rickg style
michael@0 110 // indentation :-/
michael@0 111 int32_t
michael@0 112 nsTString_CharT::ToInteger( nsresult* aErrorCode, uint32_t aRadix ) const
michael@0 113 {
michael@0 114 CharT* cp=mData;
michael@0 115 int32_t theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect)
michael@0 116 int32_t result=0;
michael@0 117 bool negate=false;
michael@0 118 CharT theChar=0;
michael@0 119
michael@0 120 //initial value, override if we find an integer
michael@0 121 *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
michael@0 122
michael@0 123 if(cp) {
michael@0 124
michael@0 125 //begin by skipping over leading chars that shouldn't be part of the number...
michael@0 126
michael@0 127 CharT* endcp=cp+mLength;
michael@0 128 bool done=false;
michael@0 129
michael@0 130 while((cp<endcp) && (!done)){
michael@0 131 switch(*cp++) {
michael@0 132 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
michael@0 133 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
michael@0 134 theRadix=16;
michael@0 135 done=true;
michael@0 136 break;
michael@0 137 case '0': case '1': case '2': case '3': case '4':
michael@0 138 case '5': case '6': case '7': case '8': case '9':
michael@0 139 done=true;
michael@0 140 break;
michael@0 141 case '-':
michael@0 142 negate=true; //fall through...
michael@0 143 break;
michael@0 144 case 'X': case 'x':
michael@0 145 theRadix=16;
michael@0 146 break;
michael@0 147 default:
michael@0 148 break;
michael@0 149 } //switch
michael@0 150 }
michael@0 151
michael@0 152 if (done) {
michael@0 153
michael@0 154 //integer found
michael@0 155 *aErrorCode = NS_OK;
michael@0 156
michael@0 157 if (aRadix!=kAutoDetect) theRadix = aRadix; // override
michael@0 158
michael@0 159 //now iterate the numeric chars and build our result
michael@0 160 CharT* first=--cp; //in case we have to back up.
michael@0 161 bool haveValue = false;
michael@0 162
michael@0 163 while(cp<endcp){
michael@0 164 int32_t oldresult = result;
michael@0 165
michael@0 166 theChar=*cp++;
michael@0 167 if(('0'<=theChar) && (theChar<='9')){
michael@0 168 result = (theRadix * result) + (theChar-'0');
michael@0 169 haveValue = true;
michael@0 170 }
michael@0 171 else if((theChar>='A') && (theChar<='F')) {
michael@0 172 if(10==theRadix) {
michael@0 173 if(kAutoDetect==aRadix){
michael@0 174 theRadix=16;
michael@0 175 cp=first; //backup
michael@0 176 result=0;
michael@0 177 haveValue = false;
michael@0 178 }
michael@0 179 else {
michael@0 180 *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
michael@0 181 result=0;
michael@0 182 break;
michael@0 183 }
michael@0 184 }
michael@0 185 else {
michael@0 186 result = (theRadix * result) + ((theChar-'A')+10);
michael@0 187 haveValue = true;
michael@0 188 }
michael@0 189 }
michael@0 190 else if((theChar>='a') && (theChar<='f')) {
michael@0 191 if(10==theRadix) {
michael@0 192 if(kAutoDetect==aRadix){
michael@0 193 theRadix=16;
michael@0 194 cp=first; //backup
michael@0 195 result=0;
michael@0 196 haveValue = false;
michael@0 197 }
michael@0 198 else {
michael@0 199 *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
michael@0 200 result=0;
michael@0 201 break;
michael@0 202 }
michael@0 203 }
michael@0 204 else {
michael@0 205 result = (theRadix * result) + ((theChar-'a')+10);
michael@0 206 haveValue = true;
michael@0 207 }
michael@0 208 }
michael@0 209 else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) {
michael@0 210 continue;
michael@0 211 }
michael@0 212 else if((('#'==theChar) || ('+'==theChar)) && !haveValue) {
michael@0 213 continue;
michael@0 214 }
michael@0 215 else {
michael@0 216 //we've encountered a char that's not a legal number or sign
michael@0 217 break;
michael@0 218 }
michael@0 219
michael@0 220 if (result < oldresult) {
michael@0 221 // overflow!
michael@0 222 *aErrorCode = NS_ERROR_ILLEGAL_VALUE;
michael@0 223 result = 0;
michael@0 224 break;
michael@0 225 }
michael@0 226 } //while
michael@0 227 if(negate)
michael@0 228 result=-result;
michael@0 229 } //if
michael@0 230 }
michael@0 231 return result;
michael@0 232 }
michael@0 233
michael@0 234
michael@0 235 /**
michael@0 236 * nsTString::ToInteger64
michael@0 237 */
michael@0 238 int64_t
michael@0 239 nsTString_CharT::ToInteger64( nsresult* aErrorCode, uint32_t aRadix ) const
michael@0 240 {
michael@0 241 CharT* cp=mData;
michael@0 242 int32_t theRadix=10; // base 10 unless base 16 detected, or overriden (aRadix != kAutoDetect)
michael@0 243 int64_t result=0;
michael@0 244 bool negate=false;
michael@0 245 CharT theChar=0;
michael@0 246
michael@0 247 //initial value, override if we find an integer
michael@0 248 *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
michael@0 249
michael@0 250 if(cp) {
michael@0 251
michael@0 252 //begin by skipping over leading chars that shouldn't be part of the number...
michael@0 253
michael@0 254 CharT* endcp=cp+mLength;
michael@0 255 bool done=false;
michael@0 256
michael@0 257 while((cp<endcp) && (!done)){
michael@0 258 switch(*cp++) {
michael@0 259 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
michael@0 260 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
michael@0 261 theRadix=16;
michael@0 262 done=true;
michael@0 263 break;
michael@0 264 case '0': case '1': case '2': case '3': case '4':
michael@0 265 case '5': case '6': case '7': case '8': case '9':
michael@0 266 done=true;
michael@0 267 break;
michael@0 268 case '-':
michael@0 269 negate=true; //fall through...
michael@0 270 break;
michael@0 271 case 'X': case 'x':
michael@0 272 theRadix=16;
michael@0 273 break;
michael@0 274 default:
michael@0 275 break;
michael@0 276 } //switch
michael@0 277 }
michael@0 278
michael@0 279 if (done) {
michael@0 280
michael@0 281 //integer found
michael@0 282 *aErrorCode = NS_OK;
michael@0 283
michael@0 284 if (aRadix!=kAutoDetect) theRadix = aRadix; // override
michael@0 285
michael@0 286 //now iterate the numeric chars and build our result
michael@0 287 CharT* first=--cp; //in case we have to back up.
michael@0 288 bool haveValue = false;
michael@0 289
michael@0 290 while(cp<endcp){
michael@0 291 int64_t oldresult = result;
michael@0 292
michael@0 293 theChar=*cp++;
michael@0 294 if(('0'<=theChar) && (theChar<='9')){
michael@0 295 result = (theRadix * result) + (theChar-'0');
michael@0 296 haveValue = true;
michael@0 297 }
michael@0 298 else if((theChar>='A') && (theChar<='F')) {
michael@0 299 if(10==theRadix) {
michael@0 300 if(kAutoDetect==aRadix){
michael@0 301 theRadix=16;
michael@0 302 cp=first; //backup
michael@0 303 result=0;
michael@0 304 haveValue = false;
michael@0 305 }
michael@0 306 else {
michael@0 307 *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
michael@0 308 result=0;
michael@0 309 break;
michael@0 310 }
michael@0 311 }
michael@0 312 else {
michael@0 313 result = (theRadix * result) + ((theChar-'A')+10);
michael@0 314 haveValue = true;
michael@0 315 }
michael@0 316 }
michael@0 317 else if((theChar>='a') && (theChar<='f')) {
michael@0 318 if(10==theRadix) {
michael@0 319 if(kAutoDetect==aRadix){
michael@0 320 theRadix=16;
michael@0 321 cp=first; //backup
michael@0 322 result=0;
michael@0 323 haveValue = false;
michael@0 324 }
michael@0 325 else {
michael@0 326 *aErrorCode=NS_ERROR_ILLEGAL_VALUE;
michael@0 327 result=0;
michael@0 328 break;
michael@0 329 }
michael@0 330 }
michael@0 331 else {
michael@0 332 result = (theRadix * result) + ((theChar-'a')+10);
michael@0 333 haveValue = true;
michael@0 334 }
michael@0 335 }
michael@0 336 else if((('X'==theChar) || ('x'==theChar)) && (!haveValue || result == 0)) {
michael@0 337 continue;
michael@0 338 }
michael@0 339 else if((('#'==theChar) || ('+'==theChar)) && !haveValue) {
michael@0 340 continue;
michael@0 341 }
michael@0 342 else {
michael@0 343 //we've encountered a char that's not a legal number or sign
michael@0 344 break;
michael@0 345 }
michael@0 346
michael@0 347 if (result < oldresult) {
michael@0 348 // overflow!
michael@0 349 *aErrorCode = NS_ERROR_ILLEGAL_VALUE;
michael@0 350 result = 0;
michael@0 351 break;
michael@0 352 }
michael@0 353 } //while
michael@0 354 if(negate)
michael@0 355 result=-result;
michael@0 356 } //if
michael@0 357 }
michael@0 358 return result;
michael@0 359 }
michael@0 360
michael@0 361
michael@0 362 /**
michael@0 363 * nsTString::Mid
michael@0 364 */
michael@0 365
michael@0 366 uint32_t
michael@0 367 nsTString_CharT::Mid( self_type& aResult, index_type aStartPos, size_type aLengthToCopy ) const
michael@0 368 {
michael@0 369 if (aStartPos == 0 && aLengthToCopy >= mLength)
michael@0 370 aResult = *this;
michael@0 371 else
michael@0 372 aResult = Substring(*this, aStartPos, aLengthToCopy);
michael@0 373
michael@0 374 return aResult.mLength;
michael@0 375 }
michael@0 376
michael@0 377
michael@0 378 /**
michael@0 379 * nsTString::SetCharAt
michael@0 380 */
michael@0 381
michael@0 382 bool
michael@0 383 nsTString_CharT::SetCharAt( char16_t aChar, uint32_t aIndex )
michael@0 384 {
michael@0 385 if (aIndex >= mLength)
michael@0 386 return false;
michael@0 387
michael@0 388 if (!EnsureMutable())
michael@0 389 NS_ABORT_OOM(mLength);
michael@0 390
michael@0 391 mData[aIndex] = CharT(aChar);
michael@0 392 return true;
michael@0 393 }
michael@0 394
michael@0 395
michael@0 396 /**
michael@0 397 * nsTString::StripChars,StripChar,StripWhitespace
michael@0 398 */
michael@0 399
michael@0 400 void
michael@0 401 nsTString_CharT::StripChars( const char* aSet )
michael@0 402 {
michael@0 403 if (!EnsureMutable())
michael@0 404 NS_ABORT_OOM(mLength);
michael@0 405
michael@0 406 mLength = nsBufferRoutines<CharT>::strip_chars(mData, mLength, aSet);
michael@0 407 }
michael@0 408
michael@0 409 void
michael@0 410 nsTString_CharT::StripWhitespace()
michael@0 411 {
michael@0 412 StripChars(kWhitespace);
michael@0 413 }
michael@0 414
michael@0 415
michael@0 416 /**
michael@0 417 * nsTString::ReplaceChar,ReplaceSubstring
michael@0 418 */
michael@0 419
michael@0 420 void
michael@0 421 nsTString_CharT::ReplaceChar( char_type aOldChar, char_type aNewChar )
michael@0 422 {
michael@0 423 if (!EnsureMutable()) // XXX do this lazily?
michael@0 424 NS_ABORT_OOM(mLength);
michael@0 425
michael@0 426 for (uint32_t i=0; i<mLength; ++i)
michael@0 427 {
michael@0 428 if (mData[i] == aOldChar)
michael@0 429 mData[i] = aNewChar;
michael@0 430 }
michael@0 431 }
michael@0 432
michael@0 433 void
michael@0 434 nsTString_CharT::ReplaceChar( const char* aSet, char_type aNewChar )
michael@0 435 {
michael@0 436 if (!EnsureMutable()) // XXX do this lazily?
michael@0 437 NS_ABORT_OOM(mLength);
michael@0 438
michael@0 439 char_type* data = mData;
michael@0 440 uint32_t lenRemaining = mLength;
michael@0 441
michael@0 442 while (lenRemaining)
michael@0 443 {
michael@0 444 int32_t i = ::FindCharInSet(data, lenRemaining, aSet);
michael@0 445 if (i == kNotFound)
michael@0 446 break;
michael@0 447
michael@0 448 data[i++] = aNewChar;
michael@0 449 data += i;
michael@0 450 lenRemaining -= i;
michael@0 451 }
michael@0 452 }
michael@0 453
michael@0 454 void
michael@0 455 nsTString_CharT::ReplaceSubstring( const char_type* aTarget, const char_type* aNewValue )
michael@0 456 {
michael@0 457 ReplaceSubstring(nsTDependentString_CharT(aTarget),
michael@0 458 nsTDependentString_CharT(aNewValue));
michael@0 459 }
michael@0 460
michael@0 461 void
michael@0 462 nsTString_CharT::ReplaceSubstring( const self_type& aTarget, const self_type& aNewValue )
michael@0 463 {
michael@0 464 if (aTarget.Length() == 0)
michael@0 465 return;
michael@0 466
michael@0 467 uint32_t i = 0;
michael@0 468 while (i < mLength)
michael@0 469 {
michael@0 470 int32_t r = FindSubstring(mData + i, mLength - i, static_cast<const char_type*>(aTarget.Data()), aTarget.Length(), false);
michael@0 471 if (r == kNotFound)
michael@0 472 break;
michael@0 473
michael@0 474 Replace(i + r, aTarget.Length(), aNewValue);
michael@0 475 i += r + aNewValue.Length();
michael@0 476 }
michael@0 477 }
michael@0 478
michael@0 479
michael@0 480 /**
michael@0 481 * nsTString::Trim
michael@0 482 */
michael@0 483
michael@0 484 void
michael@0 485 nsTString_CharT::Trim( const char* aSet, bool aTrimLeading, bool aTrimTrailing, bool aIgnoreQuotes )
michael@0 486 {
michael@0 487 // the old implementation worried about aSet being null :-/
michael@0 488 if (!aSet)
michael@0 489 return;
michael@0 490
michael@0 491 char_type* start = mData;
michael@0 492 char_type* end = mData + mLength;
michael@0 493
michael@0 494 // skip over quotes if requested
michael@0 495 if (aIgnoreQuotes && mLength > 2 && mData[0] == mData[mLength - 1] &&
michael@0 496 (mData[0] == '\'' || mData[0] == '"'))
michael@0 497 {
michael@0 498 ++start;
michael@0 499 --end;
michael@0 500 }
michael@0 501
michael@0 502 uint32_t setLen = nsCharTraits<char>::length(aSet);
michael@0 503
michael@0 504 if (aTrimLeading)
michael@0 505 {
michael@0 506 uint32_t cutStart = start - mData;
michael@0 507 uint32_t cutLength = 0;
michael@0 508
michael@0 509 // walk forward from start to end
michael@0 510 for (; start != end; ++start, ++cutLength)
michael@0 511 {
michael@0 512 int32_t pos = FindChar1(aSet, setLen, 0, *start, setLen);
michael@0 513 if (pos == kNotFound)
michael@0 514 break;
michael@0 515 }
michael@0 516
michael@0 517 if (cutLength)
michael@0 518 {
michael@0 519 Cut(cutStart, cutLength);
michael@0 520
michael@0 521 // reset iterators
michael@0 522 start = mData + cutStart;
michael@0 523 end = mData + mLength - cutStart;
michael@0 524 }
michael@0 525 }
michael@0 526
michael@0 527 if (aTrimTrailing)
michael@0 528 {
michael@0 529 uint32_t cutEnd = end - mData;
michael@0 530 uint32_t cutLength = 0;
michael@0 531
michael@0 532 // walk backward from end to start
michael@0 533 --end;
michael@0 534 for (; end >= start; --end, ++cutLength)
michael@0 535 {
michael@0 536 int32_t pos = FindChar1(aSet, setLen, 0, *end, setLen);
michael@0 537 if (pos == kNotFound)
michael@0 538 break;
michael@0 539 }
michael@0 540
michael@0 541 if (cutLength)
michael@0 542 Cut(cutEnd - cutLength, cutLength);
michael@0 543 }
michael@0 544 }
michael@0 545
michael@0 546
michael@0 547 /**
michael@0 548 * nsTString::CompressWhitespace
michael@0 549 */
michael@0 550
michael@0 551 void
michael@0 552 nsTString_CharT::CompressWhitespace( bool aTrimLeading, bool aTrimTrailing )
michael@0 553 {
michael@0 554 const char* set = kWhitespace;
michael@0 555
michael@0 556 ReplaceChar(set, ' ');
michael@0 557 Trim(set, aTrimLeading, aTrimTrailing);
michael@0 558
michael@0 559 // this one does some questionable fu... just copying the old code!
michael@0 560 mLength = nsBufferRoutines<char_type>::compress_chars(mData, mLength, set);
michael@0 561 }
michael@0 562
michael@0 563
michael@0 564 /**
michael@0 565 * nsTString::AssignWithConversion
michael@0 566 */
michael@0 567
michael@0 568 void
michael@0 569 nsTString_CharT::AssignWithConversion( const incompatible_char_type* aData, int32_t aLength )
michael@0 570 {
michael@0 571 // for compatibility with the old string implementation, we need to allow
michael@0 572 // for a nullptr input buffer :-(
michael@0 573 if (!aData)
michael@0 574 {
michael@0 575 Truncate();
michael@0 576 }
michael@0 577 else
michael@0 578 {
michael@0 579 if (aLength < 0)
michael@0 580 aLength = nsCharTraits<incompatible_char_type>::length(aData);
michael@0 581
michael@0 582 AssignWithConversion(Substring(aData, aLength));
michael@0 583 }
michael@0 584 }

mercurial