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

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: 2016-04-25 (Monday) 11:33:51 EDT 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 SkAutoTMalloc<char> autoTMalloc((size_t)length + 1); \
53 va_start(args, format); \
54 SkDEBUGCODE(int check = ) _vsnprintf_s(autoTMalloc.get(), \
55 length + 1, _TRUNCATE, \
56 format, args); \
57 va_end(args); \
58 SkASSERT(check == length); \
59 output.set(autoTMalloc.get(), length); \
60 SkASSERT(output[length] == '\0'); \
61 } while (false)
62 #else
63 #define V_SKSTRING_PRINTF(output, format) \
64 do { \
65 va_list args; \
66 va_start(args, format); \
67 char buffer[kBufferSize]; \
68 int length = vsnprintf(buffer, sizeof(buffer), format, args); \
69 va_end(args); \
70 if (length < 0) { \
71 break; \
72 } \
73 if (length < (int)sizeof(buffer)) { \
74 output.set(buffer, length); \
75 break; \
76 } \
77 SkAutoTMalloc<char> autoTMalloc((size_t)length + 1); \
78 va_start(args, format); \
79 SkDEBUGCODE(int check = ) vsnprintf(autoTMalloc.get(), \
80 length + 1, format, args); \
81 va_end(args); \
82 SkASSERT(check == length); \
83 output.set(autoTMalloc.get(), length); \
84 SkASSERT(output[length] == '\0'); \
85 } while (false)
86 #endif
87
36 /////////////////////////////////////////////////////////////////////////////// 88 ///////////////////////////////////////////////////////////////////////////////
37 89
38 bool SkStrEndsWith(const char string[], const char suffixStr[]) { 90 bool SkStrEndsWith(const char string[], const char suffixStr[]) {
39 SkASSERT(string); 91 SkASSERT(string);
40 SkASSERT(suffixStr); 92 SkASSERT(suffixStr);
41 size_t strLen = strlen(string); 93 size_t strLen = strlen(string);
42 size_t suffixLen = strlen(suffixStr); 94 size_t suffixLen = strlen(suffixStr);
43 return strLen >= suffixLen && 95 return strLen >= suffixLen &&
44 !strncmp(string + strLen - suffixLen, suffixStr, suffixLen); 96 !strncmp(string + strLen - suffixLen, suffixStr, suffixLen);
45 } 97 }
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 this->insert(offset, p, buffer + sizeof(buffer) - p); 558 this->insert(offset, p, buffer + sizeof(buffer) - p);
507 } 559 }
508 560
509 void SkString::insertScalar(size_t offset, SkScalar value) { 561 void SkString::insertScalar(size_t offset, SkScalar value) {
510 char buffer[SkStrAppendScalar_MaxSize]; 562 char buffer[SkStrAppendScalar_MaxSize];
511 char* stop = SkStrAppendScalar(buffer, value); 563 char* stop = SkStrAppendScalar(buffer, value);
512 this->insert(offset, buffer, stop - buffer); 564 this->insert(offset, buffer, stop - buffer);
513 } 565 }
514 566
515 void SkString::printf(const char format[], ...) { 567 void SkString::printf(const char format[], ...) {
516 char buffer[kBufferSize]; 568 V_SKSTRING_PRINTF((*this), format);
517 int length;
518 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
519
520 this->set(buffer, length);
521 } 569 }
522 570
523 void SkString::appendf(const char format[], ...) { 571 void SkString::appendf(const char format[], ...) {
524 char buffer[kBufferSize]; 572 char buffer[kBufferSize];
525 int length; 573 int length;
526 ARGS_TO_BUFFER(format, buffer, kBufferSize, length); 574 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
527 575
528 this->append(buffer, length); 576 this->append(buffer, length);
529 } 577 }
530 578
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 this->validate(); 634 this->validate();
587 other.validate(); 635 other.validate();
588 636
589 SkTSwap<Rec*>(fRec, other.fRec); 637 SkTSwap<Rec*>(fRec, other.fRec);
590 } 638 }
591 639
592 /////////////////////////////////////////////////////////////////////////////// 640 ///////////////////////////////////////////////////////////////////////////////
593 641
594 SkString SkStringPrintf(const char* format, ...) { 642 SkString SkStringPrintf(const char* format, ...) {
595 SkString formattedOutput; 643 SkString formattedOutput;
596 char buffer[kBufferSize]; 644 V_SKSTRING_PRINTF(formattedOutput, format);
597 SK_UNUSED int length;
598 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
599 formattedOutput.set(buffer);
600 return formattedOutput; 645 return formattedOutput;
601 } 646 }
602 647
603 void SkStrSplit(const char* str, const char* delimiters, SkStrSplitMode splitMod e, 648 void SkStrSplit(const char* str, const char* delimiters, SkStrSplitMode splitMod e,
604 SkTArray<SkString>* out) { 649 SkTArray<SkString>* out) {
605 if (splitMode == kCoalesce_SkStrSplitMode) { 650 if (splitMode == kCoalesce_SkStrSplitMode) {
606 // Skip any delimiters. 651 // Skip any delimiters.
607 str += strspn(str, delimiters); 652 str += strspn(str, delimiters);
608 } 653 }
609 if (!*str) { 654 if (!*str) {
(...skipping 16 matching lines...) Expand all
626 str += strspn(str, delimiters); 671 str += strspn(str, delimiters);
627 } else { 672 } else {
628 // Skip one delimiter. 673 // Skip one delimiter.
629 str += 1; 674 str += 1;
630 } 675 }
631 } 676 }
632 } 677 }
633 678
634 #undef VSNPRINTF 679 #undef VSNPRINTF
635 #undef SNPRINTF 680 #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