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

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

Issue 1908423002: Revert of 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: Created 4 years, 8 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 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 8
9 #include "SkAtomics.h" 9 #include "SkAtomics.h"
10 #include "SkString.h" 10 #include "SkString.h"
(...skipping 15 matching lines...) Expand all
26 26
27 #define ARGS_TO_BUFFER(format, buffer, size, written) \ 27 #define ARGS_TO_BUFFER(format, buffer, size, written) \
28 do { \ 28 do { \
29 va_list args; \ 29 va_list args; \
30 va_start(args, format); \ 30 va_start(args, format); \
31 written = VSNPRINTF(buffer, size, format, args); \ 31 written = VSNPRINTF(buffer, size, format, args); \
32 SkASSERT(written >= 0 && written < SkToInt(size)); \ 32 SkASSERT(written >= 0 && written < SkToInt(size)); \
33 va_end(args); \ 33 va_end(args); \
34 } while (0) 34 } while (0)
35 35
36 #ifdef SK_BUILD_FOR_WIN
37 #define V_SKSTRING_PRINTF(output, format) \
38 do { \
39 va_list args; \
40 va_start(args, format); \
41 char buffer[kBufferSize]; \
42 int length = _vsnprintf_s(buffer, sizeof(buffer), \
43 _TRUNCATE, format, args); \
44 va_end(args); \
45 if (length >= 0 && length < (int)sizeof(buffer)) { \
46 output.set(buffer, length); \
47 break; \
48 } \
49 va_start(args, format); \
50 length = _vscprintf(format, args); \
51 va_end(args); \
52 output.resize((size_t)length); \
53 va_start(args, format); \
54 SkDEBUGCODE(int check = ) _vsnprintf_s(output.writable_str(), \
55 length + 1, _TRUNCATE, \
56 format, args); \
57 va_end(args); \
58 SkASSERT(check == length); \
59 SkASSERT(output[length] == '\0'); \
60 } while (false)
61 #else
62 #define V_SKSTRING_PRINTF(output, format) \
63 do { \
64 va_list args; \
65 va_start(args, format); \
66 char buffer[kBufferSize]; \
67 int length = vsnprintf(buffer, sizeof(buffer), format, args); \
68 va_end(args); \
69 if (length < 0) { \
70 break; \
71 } \
72 if (length < (int)sizeof(buffer)) { \
73 output.set(buffer, length); \
74 break; \
75 } \
76 output.resize((size_t)length); \
77 va_start(args, format); \
78 SkDEBUGCODE(int check = ) vsnprintf(output.writable_str(), \
79 length + 1, format, args); \
80 va_end(args); \
81 SkASSERT(check == length); \
82 SkASSERT(output[length] == '\0'); \
83 } while (false)
84 #endif
85
86 /////////////////////////////////////////////////////////////////////////////// 36 ///////////////////////////////////////////////////////////////////////////////
87 37
88 bool SkStrEndsWith(const char string[], const char suffixStr[]) { 38 bool SkStrEndsWith(const char string[], const char suffixStr[]) {
89 SkASSERT(string); 39 SkASSERT(string);
90 SkASSERT(suffixStr); 40 SkASSERT(suffixStr);
91 size_t strLen = strlen(string); 41 size_t strLen = strlen(string);
92 size_t suffixLen = strlen(suffixStr); 42 size_t suffixLen = strlen(suffixStr);
93 return strLen >= suffixLen && 43 return strLen >= suffixLen &&
94 !strncmp(string + strLen - suffixLen, suffixStr, suffixLen); 44 !strncmp(string + strLen - suffixLen, suffixStr, suffixLen);
95 } 45 }
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 this->insert(offset, p, buffer + sizeof(buffer) - p); 506 this->insert(offset, p, buffer + sizeof(buffer) - p);
557 } 507 }
558 508
559 void SkString::insertScalar(size_t offset, SkScalar value) { 509 void SkString::insertScalar(size_t offset, SkScalar value) {
560 char buffer[SkStrAppendScalar_MaxSize]; 510 char buffer[SkStrAppendScalar_MaxSize];
561 char* stop = SkStrAppendScalar(buffer, value); 511 char* stop = SkStrAppendScalar(buffer, value);
562 this->insert(offset, buffer, stop - buffer); 512 this->insert(offset, buffer, stop - buffer);
563 } 513 }
564 514
565 void SkString::printf(const char format[], ...) { 515 void SkString::printf(const char format[], ...) {
566 V_SKSTRING_PRINTF((*this), format); 516 char buffer[kBufferSize];
517 int length;
518 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
519
520 this->set(buffer, length);
567 } 521 }
568 522
569 void SkString::appendf(const char format[], ...) { 523 void SkString::appendf(const char format[], ...) {
570 char buffer[kBufferSize]; 524 char buffer[kBufferSize];
571 int length; 525 int length;
572 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); 526 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
573 527
574 this->append(buffer, length); 528 this->append(buffer, length);
575 } 529 }
576 530
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 this->validate(); 586 this->validate();
633 other.validate(); 587 other.validate();
634 588
635 SkTSwap<Rec*>(fRec, other.fRec); 589 SkTSwap<Rec*>(fRec, other.fRec);
636 } 590 }
637 591
638 /////////////////////////////////////////////////////////////////////////////// 592 ///////////////////////////////////////////////////////////////////////////////
639 593
640 SkString SkStringPrintf(const char* format, ...) { 594 SkString SkStringPrintf(const char* format, ...) {
641 SkString formattedOutput; 595 SkString formattedOutput;
642 V_SKSTRING_PRINTF(formattedOutput, format); 596 char buffer[kBufferSize];
597 SK_UNUSED int length;
598 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
599 formattedOutput.set(buffer);
643 return formattedOutput; 600 return formattedOutput;
644 } 601 }
645 602
646 void SkStrSplit(const char* str, const char* delimiters, SkStrSplitMode splitMod e, 603 void SkStrSplit(const char* str, const char* delimiters, SkStrSplitMode splitMod e,
647 SkTArray<SkString>* out) { 604 SkTArray<SkString>* out) {
648 if (splitMode == kCoalesce_SkStrSplitMode) { 605 if (splitMode == kCoalesce_SkStrSplitMode) {
649 // Skip any delimiters. 606 // Skip any delimiters.
650 str += strspn(str, delimiters); 607 str += strspn(str, delimiters);
651 } 608 }
652 if (!*str) { 609 if (!*str) {
(...skipping 16 matching lines...) Expand all
669 str += strspn(str, delimiters); 626 str += strspn(str, delimiters);
670 } else { 627 } else {
671 // Skip one delimiter. 628 // Skip one delimiter.
672 str += 1; 629 str += 1;
673 } 630 }
674 } 631 }
675 } 632 }
676 633
677 #undef VSNPRINTF 634 #undef VSNPRINTF
678 #undef SNPRINTF 635 #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