| OLD | NEW |
| 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 "SkFixed.h" | |
| 11 #include "SkString.h" | 10 #include "SkString.h" |
| 12 #include "SkUtils.h" | 11 #include "SkUtils.h" |
| 13 #include <stdarg.h> | 12 #include <stdarg.h> |
| 14 #include <stdio.h> | 13 #include <stdio.h> |
| 15 | 14 |
| 16 // number of bytes (on the stack) to receive the printf result | 15 // number of bytes (on the stack) to receive the printf result |
| 17 static const size_t kBufferSize = 1024; | 16 static const size_t kBufferSize = 1024; |
| 18 | 17 |
| 19 #ifdef SK_BUILD_FOR_WIN | 18 #ifdef SK_BUILD_FOR_WIN |
| 20 #define VSNPRINTF(buffer, size, format, args) \ | 19 #define VSNPRINTF(buffer, size, format, args) \ |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 // since floats have at most 8 significant digits, we limit our %g to that. | 135 // since floats have at most 8 significant digits, we limit our %g to that. |
| 137 static const char gFormat[] = "%.8g"; | 136 static const char gFormat[] = "%.8g"; |
| 138 // make it 1 larger for the terminating 0 | 137 // make it 1 larger for the terminating 0 |
| 139 char buffer[SkStrAppendScalar_MaxSize + 1]; | 138 char buffer[SkStrAppendScalar_MaxSize + 1]; |
| 140 int len = SNPRINTF(buffer, sizeof(buffer), gFormat, value); | 139 int len = SNPRINTF(buffer, sizeof(buffer), gFormat, value); |
| 141 memcpy(string, buffer, len); | 140 memcpy(string, buffer, len); |
| 142 SkASSERT(len <= SkStrAppendScalar_MaxSize); | 141 SkASSERT(len <= SkStrAppendScalar_MaxSize); |
| 143 return string + len; | 142 return string + len; |
| 144 } | 143 } |
| 145 | 144 |
| 146 char* SkStrAppendFixed(char string[], SkFixed x) { | |
| 147 SkDEBUGCODE(char* start = string;) | |
| 148 if (x < 0) { | |
| 149 *string++ = '-'; | |
| 150 x = -x; | |
| 151 } | |
| 152 | |
| 153 unsigned frac = x & 0xFFFF; | |
| 154 x >>= 16; | |
| 155 if (frac == 0xFFFF) { | |
| 156 // need to do this to "round up", since 65535/65536 is closer to 1 than
to .9999 | |
| 157 x += 1; | |
| 158 frac = 0; | |
| 159 } | |
| 160 string = SkStrAppendS32(string, x); | |
| 161 | |
| 162 // now handle the fractional part (if any) | |
| 163 if (frac) { | |
| 164 static const uint16_t gTens[] = { 1000, 100, 10, 1 }; | |
| 165 const uint16_t* tens = gTens; | |
| 166 | |
| 167 x = SkFixedRoundToInt(frac * 10000); | |
| 168 SkASSERT(x <= 10000); | |
| 169 if (x == 10000) { | |
| 170 x -= 1; | |
| 171 } | |
| 172 *string++ = '.'; | |
| 173 do { | |
| 174 unsigned powerOfTen = *tens++; | |
| 175 *string++ = SkToU8('0' + x / powerOfTen); | |
| 176 x %= powerOfTen; | |
| 177 } while (x != 0); | |
| 178 } | |
| 179 | |
| 180 SkASSERT(string - start <= SkStrAppendScalar_MaxSize); | |
| 181 return string; | |
| 182 } | |
| 183 | |
| 184 /////////////////////////////////////////////////////////////////////////////// | 145 /////////////////////////////////////////////////////////////////////////////// |
| 185 | 146 |
| 186 // the 3 values are [length] [refcnt] [terminating zero data] | 147 // the 3 values are [length] [refcnt] [terminating zero data] |
| 187 const SkString::Rec SkString::gEmptyRec = { 0, 0, 0 }; | 148 const SkString::Rec SkString::gEmptyRec = { 0, 0, 0 }; |
| 188 | 149 |
| 189 #define SizeOfRec() (gEmptyRec.data() - (const char*)&gEmptyRec) | 150 #define SizeOfRec() (gEmptyRec.data() - (const char*)&gEmptyRec) |
| 190 | 151 |
| 191 static uint32_t trim_size_t_to_u32(size_t value) { | 152 static uint32_t trim_size_t_to_u32(size_t value) { |
| 192 if (sizeof(size_t) > sizeof(uint32_t)) { | 153 if (sizeof(size_t) > sizeof(uint32_t)) { |
| 193 if (value > SK_MaxU32) { | 154 if (value > SK_MaxU32) { |
| (...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 665 str += strspn(str, delimiters); | 626 str += strspn(str, delimiters); |
| 666 } else { | 627 } else { |
| 667 // Skip one delimiter. | 628 // Skip one delimiter. |
| 668 str += 1; | 629 str += 1; |
| 669 } | 630 } |
| 670 } | 631 } |
| 671 } | 632 } |
| 672 | 633 |
| 673 #undef VSNPRINTF | 634 #undef VSNPRINTF |
| 674 #undef SNPRINTF | 635 #undef SNPRINTF |
| OLD | NEW |