Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkSLString.h" |
| 9 | |
| 10 | #include "SkSLUtil.h" |
Brian Osman | de7220f | 2018-02-09 09:32:36 -0500 | [diff] [blame] | 11 | #include <algorithm> |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 12 | #include <errno.h> |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | #include <locale> |
| 15 | #include <sstream> |
| 16 | #include <string> |
| 17 | |
| 18 | namespace SkSL { |
| 19 | |
| 20 | String String::printf(const char* fmt, ...) { |
| 21 | va_list args; |
| 22 | va_start(args, fmt); |
| 23 | String result; |
| 24 | result.vappendf(fmt, args); |
z102.zhang | d74f2c8 | 2018-08-10 09:08:47 +0800 | [diff] [blame] | 25 | va_end(args); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 26 | return result; |
| 27 | } |
| 28 | |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 29 | #ifdef SKSL_USE_STD_STRING |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 30 | void String::appendf(const char* fmt, ...) { |
| 31 | va_list args; |
| 32 | va_start(args, fmt); |
| 33 | this->vappendf(fmt, args); |
z102.zhang | d74f2c8 | 2018-08-10 09:08:47 +0800 | [diff] [blame] | 34 | va_end(args); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 35 | } |
Michael Ludwig | e84fb19 | 2018-08-21 16:32:20 -0400 | [diff] [blame] | 36 | |
| 37 | void String::reset() { |
| 38 | this->clear(); |
| 39 | } |
| 40 | |
| 41 | int String::findLastOf(const char c) const { |
| 42 | // Rely on find_last_of and remap the output |
| 43 | size_t index = this->find_last_of(c); |
| 44 | return (index == std::string::npos ? -1 : index); |
| 45 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 46 | #endif |
| 47 | |
| 48 | void String::vappendf(const char* fmt, va_list args) { |
| 49 | #ifdef SKSL_BUILD_FOR_WIN |
| 50 | #define VSNPRINTF _vsnprintf |
| 51 | #else |
| 52 | #define VSNPRINTF vsnprintf |
| 53 | #endif |
| 54 | #define BUFFER_SIZE 256 |
| 55 | char buffer[BUFFER_SIZE]; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 56 | va_list reuse; |
| 57 | va_copy(reuse, args); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 58 | size_t size = VSNPRINTF(buffer, BUFFER_SIZE, fmt, args); |
| 59 | if (BUFFER_SIZE >= size) { |
| 60 | this->append(buffer, size); |
| 61 | } else { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 62 | auto newBuffer = std::unique_ptr<char[]>(new char[size + 1]); |
| 63 | VSNPRINTF(newBuffer.get(), size + 1, fmt, reuse); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 64 | this->append(newBuffer.get(), size); |
| 65 | } |
z102.zhang | d74f2c8 | 2018-08-10 09:08:47 +0800 | [diff] [blame] | 66 | va_end(reuse); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | |
| 70 | bool String::startsWith(const char* s) const { |
Ethan Nicholas | 985febf | 2017-05-19 14:03:45 -0400 | [diff] [blame] | 71 | return !strncmp(c_str(), s, strlen(s)); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool String::endsWith(const char* s) const { |
| 75 | size_t len = strlen(s); |
| 76 | if (size() < len) { |
| 77 | return false; |
| 78 | } |
Ethan Nicholas | 985febf | 2017-05-19 14:03:45 -0400 | [diff] [blame] | 79 | return !strncmp(c_str() + size() - len, s, len); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Michael Ludwig | a427559 | 2018-08-31 10:52:47 -0400 | [diff] [blame] | 82 | int String::find(const String& substring, int fromPos) const { |
| 83 | return find(substring.c_str(), fromPos); |
| 84 | } |
| 85 | |
| 86 | int String::find(const char* substring, int fromPos) const { |
| 87 | SkASSERT(fromPos >= 0); |
| 88 | #ifdef SKSL_USE_STD_STRING |
| 89 | // use std::string find() and check it against npos for not found, and find() natively supports |
| 90 | // searching from a position |
| 91 | size_t found = INHERITED::find(substring, (size_t) fromPos); |
| 92 | return found == std::string::npos ? -1 : found; |
| 93 | #else |
| 94 | // use SkStrFind on the underlying c string, and pointer arithmetic to support the searching |
| 95 | // position |
| 96 | if (substring == nullptr) { |
| 97 | // Treat null as empty, and an empty string shows up immediately |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | size_t sublen = strlen(substring); |
| 102 | if (fromPos >= size() - sublen) { |
| 103 | // Can't find it if there aren't enough characters left |
| 104 | return -1; |
| 105 | } |
| 106 | return SkStrFind(c_str() + fromPos, substring); |
| 107 | #endif |
| 108 | } |
| 109 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 110 | String String::operator+(const char* s) const { |
| 111 | String result(*this); |
| 112 | result.append(s); |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | String String::operator+(const String& s) const { |
| 117 | String result(*this); |
| 118 | result.append(s); |
| 119 | return result; |
| 120 | } |
| 121 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 122 | String String::operator+(StringFragment s) const { |
| 123 | String result(*this); |
| 124 | result.append(s.fChars, s.fLength); |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | String& String::operator+=(char c) { |
| 129 | INHERITED::operator+=(c); |
| 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | String& String::operator+=(const char* s) { |
| 134 | INHERITED::operator+=(s); |
| 135 | return *this; |
| 136 | } |
| 137 | |
| 138 | String& String::operator+=(const String& s) { |
| 139 | INHERITED::operator+=(s); |
| 140 | return *this; |
| 141 | } |
| 142 | |
| 143 | String& String::operator+=(StringFragment s) { |
| 144 | this->append(s.fChars, s.fLength); |
| 145 | return *this; |
| 146 | } |
| 147 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 148 | bool String::operator==(const String& s) const { |
| 149 | return this->size() == s.size() && !memcmp(c_str(), s.c_str(), this->size()); |
| 150 | } |
| 151 | |
| 152 | bool String::operator!=(const String& s) const { |
| 153 | return !(*this == s); |
| 154 | } |
| 155 | |
| 156 | bool String::operator==(const char* s) const { |
| 157 | return this->size() == strlen(s) && !memcmp(c_str(), s, this->size()); |
| 158 | } |
| 159 | |
| 160 | bool String::operator!=(const char* s) const { |
| 161 | return !(*this == s); |
| 162 | } |
| 163 | |
| 164 | String operator+(const char* s1, const String& s2) { |
| 165 | String result(s1); |
| 166 | result.append(s2); |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | bool operator==(const char* s1, const String& s2) { |
| 171 | return s2 == s1; |
| 172 | } |
| 173 | |
| 174 | bool operator!=(const char* s1, const String& s2) { |
| 175 | return s2 != s1; |
| 176 | } |
| 177 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 178 | bool StringFragment::operator==(StringFragment s) const { |
| 179 | if (fLength != s.fLength) { |
| 180 | return false; |
| 181 | } |
| 182 | return !memcmp(fChars, s.fChars, fLength); |
| 183 | } |
| 184 | |
| 185 | bool StringFragment::operator!=(StringFragment s) const { |
| 186 | if (fLength != s.fLength) { |
| 187 | return true; |
| 188 | } |
| 189 | return memcmp(fChars, s.fChars, fLength); |
| 190 | } |
| 191 | |
| 192 | bool StringFragment::operator==(const char* s) const { |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 193 | for (size_t i = 0; i < fLength; ++i) { |
| 194 | if (fChars[i] != s[i]) { |
| 195 | return false; |
| 196 | } |
| 197 | } |
| 198 | return 0 == s[fLength]; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool StringFragment::operator!=(const char* s) const { |
Ethan Nicholas | e9d172a | 2017-11-20 12:12:24 -0500 | [diff] [blame] | 202 | for (size_t i = 0; i < fLength; ++i) { |
| 203 | if (fChars[i] != s[i]) { |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | return 0 != s[fLength]; |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Ethan Nicholas | 2c33e3e | 2018-01-08 15:36:28 -0500 | [diff] [blame] | 210 | bool StringFragment::operator<(StringFragment other) const { |
| 211 | int comparison = strncmp(fChars, other.fChars, std::min(fLength, other.fLength)); |
| 212 | if (comparison) { |
| 213 | return comparison < 0; |
| 214 | } |
| 215 | return fLength < other.fLength; |
| 216 | } |
| 217 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 218 | bool operator==(const char* s1, StringFragment s2) { |
| 219 | return s2 == s1; |
| 220 | } |
| 221 | |
| 222 | bool operator!=(const char* s1, StringFragment s2) { |
| 223 | return s2 != s1; |
| 224 | } |
| 225 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 226 | String to_string(int32_t value) { |
| 227 | return SkSL::String::printf("%d", value); |
| 228 | } |
| 229 | |
| 230 | String to_string(uint32_t value) { |
| 231 | return SkSL::String::printf("%u", value); |
| 232 | } |
| 233 | |
| 234 | String to_string(int64_t value) { |
Ethan Nicholas | c8c1760 | 2017-03-31 18:53:05 -0400 | [diff] [blame] | 235 | std::stringstream buffer; |
| 236 | buffer << value; |
| 237 | return String(buffer.str().c_str()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | String to_string(uint64_t value) { |
Ethan Nicholas | c8c1760 | 2017-03-31 18:53:05 -0400 | [diff] [blame] | 241 | std::stringstream buffer; |
| 242 | buffer << value; |
| 243 | return String(buffer.str().c_str()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | String to_string(double value) { |
| 247 | #ifdef SKSL_BUILD_FOR_WIN |
| 248 | #define SNPRINTF _snprintf |
| 249 | #else |
| 250 | #define SNPRINTF snprintf |
| 251 | #endif |
| 252 | #define MAX_DOUBLE_CHARS 25 |
| 253 | char buffer[MAX_DOUBLE_CHARS]; |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 254 | int len = SNPRINTF(buffer, sizeof(buffer), "%.17g", value); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 255 | SkASSERT(len < MAX_DOUBLE_CHARS); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 256 | bool needsDotZero = true; |
| 257 | for (int i = 0; i < len; ++i) { |
| 258 | char c = buffer[i]; |
| 259 | if (c == ',') { |
| 260 | buffer[i] = '.'; |
| 261 | needsDotZero = false; |
| 262 | break; |
| 263 | } else if (c == '.' || c == 'e') { |
| 264 | needsDotZero = false; |
| 265 | break; |
| 266 | } |
| 267 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 268 | String result(buffer); |
Ethan Nicholas | 0054311 | 2018-07-31 09:44:36 -0400 | [diff] [blame] | 269 | if (needsDotZero) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 270 | result += ".0"; |
| 271 | } |
| 272 | return result; |
| 273 | #undef SNPRINTF |
| 274 | #undef MAX_DOUBLE_CHARS |
| 275 | } |
| 276 | |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 277 | int stoi(const String& s) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 278 | char* p; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 279 | SkDEBUGCODE(errno = 0;) |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 280 | long result = strtoul(s.c_str(), &p, 0); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 281 | SkASSERT(*p == 0); |
| 282 | SkASSERT(!errno); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 283 | return (int) result; |
| 284 | } |
| 285 | |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 286 | double stod(const String& s) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 287 | double result; |
| 288 | std::string str(s.c_str(), s.size()); |
| 289 | std::stringstream buffer(str); |
| 290 | buffer.imbue(std::locale::classic()); |
| 291 | buffer >> result; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 292 | SkASSERT(!buffer.fail()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 293 | return result; |
| 294 | } |
| 295 | |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 296 | long stol(const String& s) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 297 | char* p; |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 298 | SkDEBUGCODE(errno = 0;) |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 299 | long result = strtoul(s.c_str(), &p, 0); |
Ethan Nicholas | d9d33c3 | 2018-06-12 11:05:59 -0400 | [diff] [blame] | 300 | SkASSERT(*p == 0); |
| 301 | SkASSERT(!errno); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 302 | return result; |
| 303 | } |
| 304 | |
| 305 | } // namespace |