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" |
11 #include "SkFixed.h" | 11 #include "SkFixed.h" |
12 #include "SkThread.h" | 12 #include "SkThread.h" |
13 #include "SkUtils.h" | 13 #include "SkUtils.h" |
14 #include <stdarg.h> | 14 #include <stdarg.h> |
15 #include <stdio.h> | 15 #include <stdio.h> |
16 | 16 |
17 // number of bytes (on the stack) to receive the printf result | 17 // number of bytes (on the stack) to receive the printf result |
18 static const size_t kBufferSize = 1024; | 18 static const size_t kBufferSize = 1024; |
19 | 19 |
20 #ifdef SK_BUILD_FOR_WIN | 20 #ifdef SK_BUILD_FOR_WIN |
21 #define VSNPRINTF(buffer, size, format, args) \ | 21 #define VSNPRINTF(buffer, size, format, args) \ |
22 _vsnprintf_s(buffer, size, _TRUNCATE, format, args) | 22 _vsnprintf_s(buffer, size, _TRUNCATE, format, args) |
23 #define SNPRINTF _snprintf | 23 #define SNPRINTF _snprintf |
24 #else | 24 #else |
25 #define VSNPRINTF vsnprintf | 25 #define VSNPRINTF vsnprintf |
26 #define SNPRINTF snprintf | 26 #define SNPRINTF snprintf |
27 #endif | 27 #endif |
28 | 28 |
29 #define ARGS_TO_BUFFER(format, buffer, size) \ | 29 #define ARGS_TO_BUFFER(format, buffer, size, written) \ |
30 do { \ | 30 do { \ |
31 va_list args; \ | 31 va_list args; \ |
32 va_start(args, format); \ | 32 va_start(args, format); \ |
33 VSNPRINTF(buffer, size, format, args); \ | 33 written = VSNPRINTF(buffer, size, format, args); \ |
34 va_end(args); \ | 34 SkASSERT(written >= 0 && written < SkToInt(size)); \ |
| 35 va_end(args); \ |
35 } while (0) | 36 } while (0) |
36 | 37 |
37 /////////////////////////////////////////////////////////////////////////////// | 38 /////////////////////////////////////////////////////////////////////////////// |
38 | 39 |
39 bool SkStrEndsWith(const char string[], const char suffixStr[]) { | 40 bool SkStrEndsWith(const char string[], const char suffixStr[]) { |
40 SkASSERT(string); | 41 SkASSERT(string); |
41 SkASSERT(suffixStr); | 42 SkASSERT(suffixStr); |
42 size_t strLen = strlen(string); | 43 size_t strLen = strlen(string); |
43 size_t suffixLen = strlen(suffixStr); | 44 size_t suffixLen = strlen(suffixStr); |
44 return strLen >= suffixLen && | 45 return strLen >= suffixLen && |
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
550 } | 551 } |
551 | 552 |
552 void SkString::insertScalar(size_t offset, SkScalar value) { | 553 void SkString::insertScalar(size_t offset, SkScalar value) { |
553 char buffer[SkStrAppendScalar_MaxSize]; | 554 char buffer[SkStrAppendScalar_MaxSize]; |
554 char* stop = SkStrAppendScalar(buffer, value); | 555 char* stop = SkStrAppendScalar(buffer, value); |
555 this->insert(offset, buffer, stop - buffer); | 556 this->insert(offset, buffer, stop - buffer); |
556 } | 557 } |
557 | 558 |
558 void SkString::printf(const char format[], ...) { | 559 void SkString::printf(const char format[], ...) { |
559 char buffer[kBufferSize]; | 560 char buffer[kBufferSize]; |
560 ARGS_TO_BUFFER(format, buffer, kBufferSize); | 561 int length; |
| 562 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); |
561 | 563 |
562 this->set(buffer, strlen(buffer)); | 564 this->set(buffer, length); |
563 } | 565 } |
564 | 566 |
565 void SkString::appendf(const char format[], ...) { | 567 void SkString::appendf(const char format[], ...) { |
566 char buffer[kBufferSize]; | 568 char buffer[kBufferSize]; |
567 ARGS_TO_BUFFER(format, buffer, kBufferSize); | 569 int length; |
| 570 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); |
568 | 571 |
569 this->append(buffer, strlen(buffer)); | 572 this->append(buffer, length); |
570 } | 573 } |
571 | 574 |
572 void SkString::appendVAList(const char format[], va_list args) { | 575 void SkString::appendVAList(const char format[], va_list args) { |
573 char buffer[kBufferSize]; | 576 char buffer[kBufferSize]; |
574 VSNPRINTF(buffer, kBufferSize, format, args); | 577 int length = VSNPRINTF(buffer, kBufferSize, format, args); |
| 578 SkASSERT(length >= 0 && length < SkToInt(kBufferSize)); |
575 | 579 |
576 this->append(buffer, strlen(buffer)); | 580 this->append(buffer, length); |
577 } | 581 } |
578 | 582 |
579 void SkString::prependf(const char format[], ...) { | 583 void SkString::prependf(const char format[], ...) { |
580 char buffer[kBufferSize]; | 584 char buffer[kBufferSize]; |
581 ARGS_TO_BUFFER(format, buffer, kBufferSize); | 585 int length; |
| 586 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); |
582 | 587 |
583 this->prepend(buffer, strlen(buffer)); | 588 this->prepend(buffer, length); |
584 } | 589 } |
585 | 590 |
586 void SkString::prependVAList(const char format[], va_list args) { | 591 void SkString::prependVAList(const char format[], va_list args) { |
587 char buffer[kBufferSize]; | 592 char buffer[kBufferSize]; |
588 VSNPRINTF(buffer, kBufferSize, format, args); | 593 int length = VSNPRINTF(buffer, kBufferSize, format, args); |
| 594 SkASSERT(length >= 0 && length < SkToInt(kBufferSize)); |
589 | 595 |
590 this->prepend(buffer, strlen(buffer)); | 596 this->prepend(buffer, length); |
591 } | 597 } |
592 | 598 |
593 | 599 |
594 /////////////////////////////////////////////////////////////////////////////// | 600 /////////////////////////////////////////////////////////////////////////////// |
595 | 601 |
596 void SkString::remove(size_t offset, size_t length) { | 602 void SkString::remove(size_t offset, size_t length) { |
597 size_t size = this->size(); | 603 size_t size = this->size(); |
598 | 604 |
599 if (offset < size) { | 605 if (offset < size) { |
600 if (length > size - offset) { | 606 if (length > size - offset) { |
(...skipping 27 matching lines...) Expand all Loading... |
628 #ifdef SK_DEBUG | 634 #ifdef SK_DEBUG |
629 SkTSwap<const char*>(fStr, other.fStr); | 635 SkTSwap<const char*>(fStr, other.fStr); |
630 #endif | 636 #endif |
631 } | 637 } |
632 | 638 |
633 /////////////////////////////////////////////////////////////////////////////// | 639 /////////////////////////////////////////////////////////////////////////////// |
634 | 640 |
635 SkString SkStringPrintf(const char* format, ...) { | 641 SkString SkStringPrintf(const char* format, ...) { |
636 SkString formattedOutput; | 642 SkString formattedOutput; |
637 char buffer[kBufferSize]; | 643 char buffer[kBufferSize]; |
638 ARGS_TO_BUFFER(format, buffer, kBufferSize); | 644 SK_UNUSED int length; |
| 645 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); |
639 formattedOutput.set(buffer); | 646 formattedOutput.set(buffer); |
640 return formattedOutput; | 647 return formattedOutput; |
641 } | 648 } |
642 | 649 |
643 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out
) { | 650 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out
) { |
644 const char* end = str + strlen(str); | 651 const char* end = str + strlen(str); |
645 while (str != end) { | 652 while (str != end) { |
646 // Find a token. | 653 // Find a token. |
647 const size_t len = strcspn(str, delimiters); | 654 const size_t len = strcspn(str, delimiters); |
648 out->push_back().set(str, len); | 655 out->push_back().set(str, len); |
649 str += len; | 656 str += len; |
650 // Skip any delimiters. | 657 // Skip any delimiters. |
651 str += strspn(str, delimiters); | 658 str += strspn(str, delimiters); |
652 } | 659 } |
653 } | 660 } |
654 | 661 |
655 #undef VSNPRINTF | 662 #undef VSNPRINTF |
656 #undef SNPRINTF | 663 #undef SNPRINTF |
OLD | NEW |