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

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

Issue 679433003: Possible optimization to SkString (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 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 | no next file » | 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 "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); \
mtklein 2014/10/23 23:01:36 Let's SkASSERT(written >= 0 && written < size) her
joshualitt 2014/10/23 23:13:46 Acknowledged.
34 va_end(args); \ 34 va_end(args); \
35 } while (0) 35 } while (0)
36 36
37 /////////////////////////////////////////////////////////////////////////////// 37 ///////////////////////////////////////////////////////////////////////////////
38 38
39 bool SkStrEndsWith(const char string[], const char suffixStr[]) { 39 bool SkStrEndsWith(const char string[], const char suffixStr[]) {
40 SkASSERT(string); 40 SkASSERT(string);
41 SkASSERT(suffixStr); 41 SkASSERT(suffixStr);
42 size_t strLen = strlen(string); 42 size_t strLen = strlen(string);
43 size_t suffixLen = strlen(suffixStr); 43 size_t suffixLen = strlen(suffixStr);
44 return strLen >= suffixLen && 44 return strLen >= suffixLen &&
(...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after
550 } 550 }
551 551
552 void SkString::insertScalar(size_t offset, SkScalar value) { 552 void SkString::insertScalar(size_t offset, SkScalar value) {
553 char buffer[SkStrAppendScalar_MaxSize]; 553 char buffer[SkStrAppendScalar_MaxSize];
554 char* stop = SkStrAppendScalar(buffer, value); 554 char* stop = SkStrAppendScalar(buffer, value);
555 this->insert(offset, buffer, stop - buffer); 555 this->insert(offset, buffer, stop - buffer);
556 } 556 }
557 557
558 void SkString::printf(const char format[], ...) { 558 void SkString::printf(const char format[], ...) {
559 char buffer[kBufferSize]; 559 char buffer[kBufferSize];
560 ARGS_TO_BUFFER(format, buffer, kBufferSize); 560 int length;
561 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
562 SkASSERT(length >= 0);
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);
571 SkASSERT(length >= 0);
568 572
569 this->append(buffer, strlen(buffer)); 573 this->append(buffer, length);
570 } 574 }
571 575
572 void SkString::appendVAList(const char format[], va_list args) { 576 void SkString::appendVAList(const char format[], va_list args) {
573 char buffer[kBufferSize]; 577 char buffer[kBufferSize];
574 VSNPRINTF(buffer, kBufferSize, format, args); 578 int length = VSNPRINTF(buffer, kBufferSize, format, args);
579 SkASSERT(length >= 0);
575 580
576 this->append(buffer, strlen(buffer)); 581 this->append(buffer, length);
577 } 582 }
578 583
579 void SkString::prependf(const char format[], ...) { 584 void SkString::prependf(const char format[], ...) {
580 char buffer[kBufferSize]; 585 char buffer[kBufferSize];
581 ARGS_TO_BUFFER(format, buffer, kBufferSize); 586 int length;
587 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
588 SkASSERT(length >= 0);
582 589
583 this->prepend(buffer, strlen(buffer)); 590 this->prepend(buffer, length);
584 } 591 }
585 592
586 void SkString::prependVAList(const char format[], va_list args) { 593 void SkString::prependVAList(const char format[], va_list args) {
587 char buffer[kBufferSize]; 594 char buffer[kBufferSize];
588 VSNPRINTF(buffer, kBufferSize, format, args); 595 int length = VSNPRINTF(buffer, kBufferSize, format, args);
596 SkASSERT(length >= 0);
589 597
590 this->prepend(buffer, strlen(buffer)); 598 this->prepend(buffer, length);
591 } 599 }
592 600
593 601
594 /////////////////////////////////////////////////////////////////////////////// 602 ///////////////////////////////////////////////////////////////////////////////
595 603
596 void SkString::remove(size_t offset, size_t length) { 604 void SkString::remove(size_t offset, size_t length) {
597 size_t size = this->size(); 605 size_t size = this->size();
598 606
599 if (offset < size) { 607 if (offset < size) {
600 if (length > size - offset) { 608 if (length > size - offset) {
(...skipping 27 matching lines...) Expand all
628 #ifdef SK_DEBUG 636 #ifdef SK_DEBUG
629 SkTSwap<const char*>(fStr, other.fStr); 637 SkTSwap<const char*>(fStr, other.fStr);
630 #endif 638 #endif
631 } 639 }
632 640
633 /////////////////////////////////////////////////////////////////////////////// 641 ///////////////////////////////////////////////////////////////////////////////
634 642
635 SkString SkStringPrintf(const char* format, ...) { 643 SkString SkStringPrintf(const char* format, ...) {
636 SkString formattedOutput; 644 SkString formattedOutput;
637 char buffer[kBufferSize]; 645 char buffer[kBufferSize];
638 ARGS_TO_BUFFER(format, buffer, kBufferSize); 646 SK_UNUSED int length;
647 ARGS_TO_BUFFER(format, buffer, kBufferSize, length);
639 formattedOutput.set(buffer); 648 formattedOutput.set(buffer);
640 return formattedOutput; 649 return formattedOutput;
641 } 650 }
642 651
643 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out ) { 652 void SkStrSplit(const char* str, const char* delimiters, SkTArray<SkString>* out ) {
644 const char* end = str + strlen(str); 653 const char* end = str + strlen(str);
645 while (str != end) { 654 while (str != end) {
646 // Find a token. 655 // Find a token.
647 const size_t len = strcspn(str, delimiters); 656 const size_t len = strcspn(str, delimiters);
648 out->push_back().set(str, len); 657 out->push_back().set(str, len);
649 str += len; 658 str += len;
650 // Skip any delimiters. 659 // Skip any delimiters.
651 str += strspn(str, delimiters); 660 str += strspn(str, delimiters);
652 } 661 }
653 } 662 }
654 663
655 #undef VSNPRINTF 664 #undef VSNPRINTF
656 #undef SNPRINTF 665 #undef SNPRINTF
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698