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

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

Issue 17448012: Add SkString::appendU32() and SkString::appendU64() (Closed) Base URL: http://skia.googlecode.com/svn/trunk/
Patch Set: add_cornercase_test_that_works_now Created 7 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « include/core/SkString.h ('k') | tests/StringTest.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 "SkString.h" 10 #include "SkString.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 const char* limit = strchr(prefixes, '\0'); 61 const char* limit = strchr(prefixes, '\0');
62 if (!strncmp(string, prefixes, limit - prefixes)) { 62 if (!strncmp(string, prefixes, limit - prefixes)) {
63 return index; 63 return index;
64 } 64 }
65 prefixes = limit + 1; 65 prefixes = limit + 1;
66 index++; 66 index++;
67 } while (prefixes[0]); 67 } while (prefixes[0]);
68 return -1; 68 return -1;
69 } 69 }
70 70
71 char* SkStrAppendS32(char string[], int32_t dec) { 71 char* SkStrAppendU32(char string[], uint32_t dec) {
72 SkDEBUGCODE(char* start = string;) 72 SkDEBUGCODE(char* start = string;)
73 73
74 char buffer[SkStrAppendS32_MaxSize]; 74 char buffer[SkStrAppendU32_MaxSize];
75 char* p = buffer + sizeof(buffer); 75 char* p = buffer + sizeof(buffer);
76 bool neg = false;
77
78 if (dec < 0) {
79 neg = true;
80 dec = -dec;
81 }
82 76
83 do { 77 do {
84 *--p = SkToU8('0' + dec % 10); 78 *--p = SkToU8('0' + dec % 10);
85 dec /= 10; 79 dec /= 10;
86 } while (dec != 0); 80 } while (dec != 0);
87 81
88 if (neg) {
89 *--p = '-';
90 }
91
92 SkASSERT(p >= buffer); 82 SkASSERT(p >= buffer);
93 char* stop = buffer + sizeof(buffer); 83 char* stop = buffer + sizeof(buffer);
94 while (p < stop) { 84 while (p < stop) {
95 *string++ = *p++; 85 *string++ = *p++;
96 } 86 }
97 SkASSERT(string - start <= SkStrAppendS32_MaxSize); 87 SkASSERT(string - start <= SkStrAppendU32_MaxSize);
98 return string; 88 return string;
99 } 89 }
100 90
101 char* SkStrAppendS64(char string[], int64_t dec, int minDigits) { 91 char* SkStrAppendS32(char string[], int32_t dec) {
92 if (dec < 0) {
93 *string++ = '-';
94 dec = -dec;
95 }
96 return SkStrAppendU32(string, static_cast<uint32_t>(dec));
97 }
98
99 char* SkStrAppendU64(char string[], uint64_t dec, int minDigits) {
102 SkDEBUGCODE(char* start = string;) 100 SkDEBUGCODE(char* start = string;)
103 101
104 char buffer[SkStrAppendS64_MaxSize]; 102 char buffer[SkStrAppendU64_MaxSize];
105 char* p = buffer + sizeof(buffer); 103 char* p = buffer + sizeof(buffer);
106 bool neg = false;
107
108 if (dec < 0) {
109 neg = true;
110 dec = -dec;
111 }
112 104
113 do { 105 do {
114 *--p = SkToU8('0' + (int32_t) (dec % 10)); 106 *--p = SkToU8('0' + (int32_t) (dec % 10));
115 dec /= 10; 107 dec /= 10;
116 minDigits--; 108 minDigits--;
117 } while (dec != 0); 109 } while (dec != 0);
118 110
119 while (minDigits > 0) { 111 while (minDigits > 0) {
120 *--p = '0'; 112 *--p = '0';
121 minDigits--; 113 minDigits--;
122 } 114 }
123 115
124 if (neg) {
125 *--p = '-';
126 }
127 SkASSERT(p >= buffer); 116 SkASSERT(p >= buffer);
128 size_t cp_len = buffer + sizeof(buffer) - p; 117 size_t cp_len = buffer + sizeof(buffer) - p;
129 memcpy(string, p, cp_len); 118 memcpy(string, p, cp_len);
130 string += cp_len; 119 string += cp_len;
131 120
132 SkASSERT(string - start <= SkStrAppendS64_MaxSize); 121 SkASSERT(string - start <= SkStrAppendU64_MaxSize);
133 return string; 122 return string;
134 } 123 }
135 124
125 char* SkStrAppendS64(char string[], int64_t dec, int minDigits) {
126 if (dec < 0) {
127 *string++ = '-';
128 dec = -dec;
129 }
130 return SkStrAppendU64(string, static_cast<uint64_t>(dec), minDigits);
131 }
132
136 char* SkStrAppendFloat(char string[], float value) { 133 char* SkStrAppendFloat(char string[], float value) {
137 // since floats have at most 8 significant digits, we limit our %g to that. 134 // since floats have at most 8 significant digits, we limit our %g to that.
138 static const char gFormat[] = "%.8g"; 135 static const char gFormat[] = "%.8g";
139 // make it 1 larger for the terminating 0 136 // make it 1 larger for the terminating 0
140 char buffer[SkStrAppendScalar_MaxSize + 1]; 137 char buffer[SkStrAppendScalar_MaxSize + 1];
141 int len = SNPRINTF(buffer, sizeof(buffer), gFormat, value); 138 int len = SNPRINTF(buffer, sizeof(buffer), gFormat, value);
142 memcpy(string, buffer, len); 139 memcpy(string, buffer, len);
143 SkASSERT(len <= SkStrAppendScalar_MaxSize); 140 SkASSERT(len <= SkStrAppendScalar_MaxSize);
144 return string + len; 141 return string + len;
145 } 142 }
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
511 char* stop = SkStrAppendS32(buffer, dec); 508 char* stop = SkStrAppendS32(buffer, dec);
512 this->insert(offset, buffer, stop - buffer); 509 this->insert(offset, buffer, stop - buffer);
513 } 510 }
514 511
515 void SkString::insertS64(size_t offset, int64_t dec, int minDigits) { 512 void SkString::insertS64(size_t offset, int64_t dec, int minDigits) {
516 char buffer[SkStrAppendS64_MaxSize]; 513 char buffer[SkStrAppendS64_MaxSize];
517 char* stop = SkStrAppendS64(buffer, dec, minDigits); 514 char* stop = SkStrAppendS64(buffer, dec, minDigits);
518 this->insert(offset, buffer, stop - buffer); 515 this->insert(offset, buffer, stop - buffer);
519 } 516 }
520 517
518 void SkString::insertU32(size_t offset, uint32_t dec) {
519 char buffer[SkStrAppendU32_MaxSize];
520 char* stop = SkStrAppendU32(buffer, dec);
521 this->insert(offset, buffer, stop - buffer);
522 }
523
524 void SkString::insertU64(size_t offset, uint64_t dec, int minDigits) {
525 char buffer[SkStrAppendU64_MaxSize];
526 char* stop = SkStrAppendU64(buffer, dec, minDigits);
527 this->insert(offset, buffer, stop - buffer);
528 }
529
521 void SkString::insertHex(size_t offset, uint32_t hex, int minDigits) { 530 void SkString::insertHex(size_t offset, uint32_t hex, int minDigits) {
522 minDigits = SkPin32(minDigits, 0, 8); 531 minDigits = SkPin32(minDigits, 0, 8);
523 532
524 static const char gHex[] = "0123456789ABCDEF"; 533 static const char gHex[] = "0123456789ABCDEF";
525 534
526 char buffer[8]; 535 char buffer[8];
527 char* p = buffer + sizeof(buffer); 536 char* p = buffer + sizeof(buffer);
528 537
529 do { 538 do {
530 *--p = gHex[hex & 0xF]; 539 *--p = gHex[hex & 0xF];
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 SkString SkStringPrintf(const char* format, ...) { 650 SkString SkStringPrintf(const char* format, ...) {
642 SkString formattedOutput; 651 SkString formattedOutput;
643 char buffer[kBufferSize]; 652 char buffer[kBufferSize];
644 ARGS_TO_BUFFER(format, buffer, kBufferSize); 653 ARGS_TO_BUFFER(format, buffer, kBufferSize);
645 formattedOutput.set(buffer); 654 formattedOutput.set(buffer);
646 return formattedOutput; 655 return formattedOutput;
647 } 656 }
648 657
649 #undef VSNPRINTF 658 #undef VSNPRINTF
650 #undef SNPRINTF 659 #undef SNPRINTF
OLDNEW
« no previous file with comments | « include/core/SkString.h ('k') | tests/StringTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698