Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: src/core/SkString.cpp

Issue 1403803002: SkStringPrintf and SkString::printf now are no longer limted by a static buffer (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-10-12 (Monday) 15:52:58 EDT Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/pdf/SkPDFMetadata.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "SkAtomics.h" 10 #include "SkAtomics.h"
(...skipping 17 matching lines...) Expand all
28 28
29 #define ARGS_TO_BUFFER(format, buffer, size, written) \ 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 written = VSNPRINTF(buffer, size, format, args); \ 33 written = VSNPRINTF(buffer, size, format, args); \
34 SkASSERT(written >= 0 && written < SkToInt(size)); \ 34 SkASSERT(written >= 0 && written < SkToInt(size)); \
35 va_end(args); \ 35 va_end(args); \
36 } while (0) 36 } while (0)
37 37
38 static SkString v_skstring_printf(const char* format, va_list args) {
39 #ifdef SK_BUILD_FOR_WIN
40 va_list args_copy;
41 va_copy(args_copy, args);
42 char buffer[1024];
43 int length = _vsnprintf_s(buffer, sizeof(buffer), _TRUNCATE, format, args_co py);
44 va_end(args_copy);
45 if (length >= 0 && length < (int)sizeof(buffer)) {
46 return SkString(buffer, length);
47 }
48 va_copy(args_copy, args);
49 length = _vscprintf(format, args);
50 va_end(args_copy);
51
52 SkString formattedOutput((size_t)length);
53 SkDEBUGCODE(int check = ) _vsnprintf_s(formattedOutput.writable_str(),
54 length + 1, _TRUNCATE, format, args);
55 SkASSERT(check == length);
56 SkASSERT(formattedOutput[length] == '\0');
57 return skstd::move(formattedOutput);
58 #else // C99/C++11 standard vsnprintf
59 // TODO: When all compilers support this, remove windows-specific code.
60 va_list args_copy;
61 va_copy(args_copy, args);
62 char buffer[1024];
63 int length = vsnprintf(buffer, sizeof(buffer), format, args);
64 va_end(args_copy);
65 if (length < 0) {
66 return SkString();
67 }
68 if (length < (int)sizeof(buffer)) {
69 return SkString(buffer, length);
70 }
71 SkString formattedOutput((size_t)length);
72 SkDEBUGCODE(int check = )
73 vsnprintf(formattedOutput.writable_str(), length + 1, format, args);
74 SkASSERT(check == length);
75 SkASSERT(formattedOutput[length] == '\0');
76 return skstd::move(formattedOutput);
77 #endif
78 }
79
38 /////////////////////////////////////////////////////////////////////////////// 80 ///////////////////////////////////////////////////////////////////////////////
39 81
40 bool SkStrEndsWith(const char string[], const char suffixStr[]) { 82 bool SkStrEndsWith(const char string[], const char suffixStr[]) {
41 SkASSERT(string); 83 SkASSERT(string);
42 SkASSERT(suffixStr); 84 SkASSERT(suffixStr);
43 size_t strLen = strlen(string); 85 size_t strLen = strlen(string);
44 size_t suffixLen = strlen(suffixStr); 86 size_t suffixLen = strlen(suffixStr);
45 return strLen >= suffixLen && 87 return strLen >= suffixLen &&
46 !strncmp(string + strLen - suffixLen, suffixStr, suffixLen); 88 !strncmp(string + strLen - suffixLen, suffixStr, suffixLen);
47 } 89 }
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 this->insert(offset, p, buffer + sizeof(buffer) - p); 572 this->insert(offset, p, buffer + sizeof(buffer) - p);
531 } 573 }
532 574
533 void SkString::insertScalar(size_t offset, SkScalar value) { 575 void SkString::insertScalar(size_t offset, SkScalar value) {
534 char buffer[SkStrAppendScalar_MaxSize]; 576 char buffer[SkStrAppendScalar_MaxSize];
535 char* stop = SkStrAppendScalar(buffer, value); 577 char* stop = SkStrAppendScalar(buffer, value);
536 this->insert(offset, buffer, stop - buffer); 578 this->insert(offset, buffer, stop - buffer);
537 } 579 }
538 580
539 void SkString::printf(const char format[], ...) { 581 void SkString::printf(const char format[], ...) {
540 char buffer[kBufferSize]; 582 va_list args;
541 int length; 583 va_start(args, format);
542 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); 584 *this = v_skstring_printf(format, args);
543 585 va_end(args);
544 this->set(buffer, length);
545 } 586 }
546 587
547 void SkString::appendf(const char format[], ...) { 588 void SkString::appendf(const char format[], ...) {
548 char buffer[kBufferSize]; 589 char buffer[kBufferSize];
549 int length; 590 int length;
550 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); 591 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
551 592
552 this->append(buffer, length); 593 this->append(buffer, length);
553 } 594 }
554 595
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 649
609 void SkString::swap(SkString& other) { 650 void SkString::swap(SkString& other) {
610 this->validate(); 651 this->validate();
611 other.validate(); 652 other.validate();
612 653
613 SkTSwap<Rec*>(fRec, other.fRec); 654 SkTSwap<Rec*>(fRec, other.fRec);
614 } 655 }
615 656
616 /////////////////////////////////////////////////////////////////////////////// 657 ///////////////////////////////////////////////////////////////////////////////
617 658
659 // Improvement on SkStringPrintf to allow for arbitrarily long output.
tomhudson 2015/10/12 20:21:01 This feels like a comment that belongs in the comm
hal.canary 2015/10/12 21:38:24 Done.
660 // TODO: replace SkStringPrintf.
618 SkString SkStringPrintf(const char* format, ...) { 661 SkString SkStringPrintf(const char* format, ...) {
619 SkString formattedOutput; 662 va_list args;
620 char buffer[kBufferSize]; 663 va_start(args, format);
621 SK_UNUSED int length; 664 SkString formattedOutput(v_skstring_printf(format, args));
622 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); 665 va_end(args);
623 formattedOutput.set(buffer); 666 return skstd::move(formattedOutput);
624 return formattedOutput;
625 } 667 }
626 668
627 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out ) { 669 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out ) {
628 const char* end = str + strlen(str); 670 const char* end = str + strlen(str);
629 while (str != end) { 671 while (str != end) {
630 // Find a token. 672 // Find a token.
631 const size_t len = strcspn(str, delimiters); 673 const size_t len = strcspn(str, delimiters);
632 out->push_back().set(str, len); 674 out->push_back().set(str, len);
633 str += len; 675 str += len;
634 // Skip any delimiters. 676 // Skip any delimiters.
635 str += strspn(str, delimiters); 677 str += strspn(str, delimiters);
636 } 678 }
637 } 679 }
638 680
639 #undef VSNPRINTF 681 #undef VSNPRINTF
640 #undef SNPRINTF 682 #undef SNPRINTF
OLDNEW
« no previous file with comments | « no previous file | src/pdf/SkPDFMetadata.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698