| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #include "SkString.h" | 10 #include "SkString.h" |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 // make sure know one has written over our global | 240 // make sure know one has written over our global |
| 241 SkASSERT(0 == gEmptyRec.fLength); | 241 SkASSERT(0 == gEmptyRec.fLength); |
| 242 SkASSERT(0 == gEmptyRec.fRefCnt); | 242 SkASSERT(0 == gEmptyRec.fRefCnt); |
| 243 SkASSERT(0 == gEmptyRec.data()[0]); | 243 SkASSERT(0 == gEmptyRec.data()[0]); |
| 244 | 244 |
| 245 if (fRec != &gEmptyRec) { | 245 if (fRec != &gEmptyRec) { |
| 246 SkASSERT(fRec->fLength > 0); | 246 SkASSERT(fRec->fLength > 0); |
| 247 SkASSERT(fRec->fRefCnt > 0); | 247 SkASSERT(fRec->fRefCnt > 0); |
| 248 SkASSERT(0 == fRec->data()[fRec->fLength]); | 248 SkASSERT(0 == fRec->data()[fRec->fLength]); |
| 249 } | 249 } |
| 250 SkASSERT(fStr == c_str()); | |
| 251 } | 250 } |
| 252 #endif | 251 #endif |
| 253 | 252 |
| 254 /////////////////////////////////////////////////////////////////////////////// | 253 /////////////////////////////////////////////////////////////////////////////// |
| 255 | 254 |
| 256 SkString::SkString() : fRec(const_cast<Rec*>(&gEmptyRec)) { | 255 SkString::SkString() : fRec(const_cast<Rec*>(&gEmptyRec)) { |
| 257 #ifdef SK_DEBUG | |
| 258 fStr = fRec->data(); | |
| 259 #endif | |
| 260 } | 256 } |
| 261 | 257 |
| 262 SkString::SkString(size_t len) { | 258 SkString::SkString(size_t len) { |
| 263 fRec = AllocRec(NULL, len); | 259 fRec = AllocRec(NULL, len); |
| 264 #ifdef SK_DEBUG | |
| 265 fStr = fRec->data(); | |
| 266 #endif | |
| 267 } | 260 } |
| 268 | 261 |
| 269 SkString::SkString(const char text[]) { | 262 SkString::SkString(const char text[]) { |
| 270 size_t len = text ? strlen(text) : 0; | 263 size_t len = text ? strlen(text) : 0; |
| 271 | 264 |
| 272 fRec = AllocRec(text, len); | 265 fRec = AllocRec(text, len); |
| 273 #ifdef SK_DEBUG | |
| 274 fStr = fRec->data(); | |
| 275 #endif | |
| 276 } | 266 } |
| 277 | 267 |
| 278 SkString::SkString(const char text[], size_t len) { | 268 SkString::SkString(const char text[], size_t len) { |
| 279 fRec = AllocRec(text, len); | 269 fRec = AllocRec(text, len); |
| 280 #ifdef SK_DEBUG | |
| 281 fStr = fRec->data(); | |
| 282 #endif | |
| 283 } | 270 } |
| 284 | 271 |
| 285 SkString::SkString(const SkString& src) { | 272 SkString::SkString(const SkString& src) { |
| 286 src.validate(); | 273 src.validate(); |
| 287 | 274 |
| 288 fRec = RefRec(src.fRec); | 275 fRec = RefRec(src.fRec); |
| 289 #ifdef SK_DEBUG | |
| 290 fStr = fRec->data(); | |
| 291 #endif | |
| 292 } | 276 } |
| 293 | 277 |
| 294 SkString::~SkString() { | 278 SkString::~SkString() { |
| 295 this->validate(); | 279 this->validate(); |
| 296 | 280 |
| 297 if (fRec->fLength) { | 281 if (fRec->fLength) { |
| 298 SkASSERT(fRec->fRefCnt > 0); | 282 SkASSERT(fRec->fRefCnt > 0); |
| 299 if (sk_atomic_dec(&fRec->fRefCnt) == 1) { | 283 if (sk_atomic_dec(&fRec->fRefCnt) == 1) { |
| 300 sk_free(fRec); | 284 sk_free(fRec); |
| 301 } | 285 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 this->validate(); | 323 this->validate(); |
| 340 | 324 |
| 341 if (fRec->fLength) { | 325 if (fRec->fLength) { |
| 342 SkASSERT(fRec->fRefCnt > 0); | 326 SkASSERT(fRec->fRefCnt > 0); |
| 343 if (sk_atomic_dec(&fRec->fRefCnt) == 1) { | 327 if (sk_atomic_dec(&fRec->fRefCnt) == 1) { |
| 344 sk_free(fRec); | 328 sk_free(fRec); |
| 345 } | 329 } |
| 346 } | 330 } |
| 347 | 331 |
| 348 fRec = const_cast<Rec*>(&gEmptyRec); | 332 fRec = const_cast<Rec*>(&gEmptyRec); |
| 349 #ifdef SK_DEBUG | |
| 350 fStr = fRec->data(); | |
| 351 #endif | |
| 352 } | 333 } |
| 353 | 334 |
| 354 char* SkString::writable_str() { | 335 char* SkString::writable_str() { |
| 355 this->validate(); | 336 this->validate(); |
| 356 | 337 |
| 357 if (fRec->fLength) { | 338 if (fRec->fLength) { |
| 358 if (fRec->fRefCnt > 1) { | 339 if (fRec->fRefCnt > 1) { |
| 359 Rec* rec = AllocRec(fRec->data(), fRec->fLength); | 340 Rec* rec = AllocRec(fRec->data(), fRec->fLength); |
| 360 if (sk_atomic_dec(&fRec->fRefCnt) == 1) { | 341 if (sk_atomic_dec(&fRec->fRefCnt) == 1) { |
| 361 // In this case after our check of fRecCnt > 1, we suddenly | 342 // In this case after our check of fRecCnt > 1, we suddenly |
| 362 // did become the only owner, so now we have two copies of the | 343 // did become the only owner, so now we have two copies of the |
| 363 // data (fRec and rec), so we need to delete one of them. | 344 // data (fRec and rec), so we need to delete one of them. |
| 364 sk_free(fRec); | 345 sk_free(fRec); |
| 365 } | 346 } |
| 366 fRec = rec; | 347 fRec = rec; |
| 367 #ifdef SK_DEBUG | |
| 368 fStr = fRec->data(); | |
| 369 #endif | |
| 370 } | 348 } |
| 371 } | 349 } |
| 372 return fRec->data(); | 350 return fRec->data(); |
| 373 } | 351 } |
| 374 | 352 |
| 375 void SkString::set(const char text[]) { | 353 void SkString::set(const char text[]) { |
| 376 this->set(text, text ? strlen(text) : 0); | 354 this->set(text, text ? strlen(text) : 0); |
| 377 } | 355 } |
| 378 | 356 |
| 379 void SkString::set(const char text[], size_t len) { | 357 void SkString::set(const char text[], size_t len) { |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 626 this->swap(tmp); | 604 this->swap(tmp); |
| 627 } | 605 } |
| 628 } | 606 } |
| 629 } | 607 } |
| 630 | 608 |
| 631 void SkString::swap(SkString& other) { | 609 void SkString::swap(SkString& other) { |
| 632 this->validate(); | 610 this->validate(); |
| 633 other.validate(); | 611 other.validate(); |
| 634 | 612 |
| 635 SkTSwap<Rec*>(fRec, other.fRec); | 613 SkTSwap<Rec*>(fRec, other.fRec); |
| 636 #ifdef SK_DEBUG | |
| 637 SkTSwap<const char*>(fStr, other.fStr); | |
| 638 #endif | |
| 639 } | 614 } |
| 640 | 615 |
| 641 /////////////////////////////////////////////////////////////////////////////// | 616 /////////////////////////////////////////////////////////////////////////////// |
| 642 | 617 |
| 643 SkString SkStringPrintf(const char* format, ...) { | 618 SkString SkStringPrintf(const char* format, ...) { |
| 644 SkString formattedOutput; | 619 SkString formattedOutput; |
| 645 char buffer[kBufferSize]; | 620 char buffer[kBufferSize]; |
| 646 SK_UNUSED int length; | 621 SK_UNUSED int length; |
| 647 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); | 622 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); |
| 648 formattedOutput.set(buffer); | 623 formattedOutput.set(buffer); |
| 649 return formattedOutput; | 624 return formattedOutput; |
| 650 } | 625 } |
| 651 | 626 |
| 652 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out
) { | 627 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out
) { |
| 653 const char* end = str + strlen(str); | 628 const char* end = str + strlen(str); |
| 654 while (str != end) { | 629 while (str != end) { |
| 655 // Find a token. | 630 // Find a token. |
| 656 const size_t len = strcspn(str, delimiters); | 631 const size_t len = strcspn(str, delimiters); |
| 657 out->push_back().set(str, len); | 632 out->push_back().set(str, len); |
| 658 str += len; | 633 str += len; |
| 659 // Skip any delimiters. | 634 // Skip any delimiters. |
| 660 str += strspn(str, delimiters); | 635 str += strspn(str, delimiters); |
| 661 } | 636 } |
| 662 } | 637 } |
| 663 | 638 |
| 664 #undef VSNPRINTF | 639 #undef VSNPRINTF |
| 665 #undef SNPRINTF | 640 #undef SNPRINTF |
| OLD | NEW |