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" |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 11 | #include <errno.h> |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 12 | #include <limits.h> |
| 13 | #include <locale> |
| 14 | #include <sstream> |
| 15 | #include <string> |
| 16 | |
| 17 | namespace SkSL { |
| 18 | |
| 19 | String String::printf(const char* fmt, ...) { |
| 20 | va_list args; |
| 21 | va_start(args, fmt); |
| 22 | String result; |
| 23 | result.vappendf(fmt, args); |
| 24 | return result; |
| 25 | } |
| 26 | |
Brian Osman | 93ba0a4 | 2017-08-14 14:48:10 -0400 | [diff] [blame] | 27 | #ifdef SKSL_USE_STD_STRING |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 28 | void String::appendf(const char* fmt, ...) { |
| 29 | va_list args; |
| 30 | va_start(args, fmt); |
| 31 | this->vappendf(fmt, args); |
| 32 | } |
| 33 | #endif |
| 34 | |
| 35 | void String::vappendf(const char* fmt, va_list args) { |
| 36 | #ifdef SKSL_BUILD_FOR_WIN |
| 37 | #define VSNPRINTF _vsnprintf |
| 38 | #else |
| 39 | #define VSNPRINTF vsnprintf |
| 40 | #endif |
| 41 | #define BUFFER_SIZE 256 |
| 42 | char buffer[BUFFER_SIZE]; |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 43 | va_list reuse; |
| 44 | va_copy(reuse, args); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 45 | size_t size = VSNPRINTF(buffer, BUFFER_SIZE, fmt, args); |
| 46 | if (BUFFER_SIZE >= size) { |
| 47 | this->append(buffer, size); |
| 48 | } else { |
Ethan Nicholas | 762466e | 2017-06-29 10:03:38 -0400 | [diff] [blame] | 49 | auto newBuffer = std::unique_ptr<char[]>(new char[size + 1]); |
| 50 | VSNPRINTF(newBuffer.get(), size + 1, fmt, reuse); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 51 | this->append(newBuffer.get(), size); |
| 52 | } |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | bool String::startsWith(const char* s) const { |
Ethan Nicholas | 985febf | 2017-05-19 14:03:45 -0400 | [diff] [blame] | 57 | return !strncmp(c_str(), s, strlen(s)); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool String::endsWith(const char* s) const { |
| 61 | size_t len = strlen(s); |
| 62 | if (size() < len) { |
| 63 | return false; |
| 64 | } |
Ethan Nicholas | 985febf | 2017-05-19 14:03:45 -0400 | [diff] [blame] | 65 | return !strncmp(c_str() + size() - len, s, len); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | String String::operator+(const char* s) const { |
| 69 | String result(*this); |
| 70 | result.append(s); |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | String String::operator+(const String& s) const { |
| 75 | String result(*this); |
| 76 | result.append(s); |
| 77 | return result; |
| 78 | } |
| 79 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 80 | String String::operator+(StringFragment s) const { |
| 81 | String result(*this); |
| 82 | result.append(s.fChars, s.fLength); |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | String& String::operator+=(char c) { |
| 87 | INHERITED::operator+=(c); |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | String& String::operator+=(const char* s) { |
| 92 | INHERITED::operator+=(s); |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | String& String::operator+=(const String& s) { |
| 97 | INHERITED::operator+=(s); |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | String& String::operator+=(StringFragment s) { |
| 102 | this->append(s.fChars, s.fLength); |
| 103 | return *this; |
| 104 | } |
| 105 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 106 | bool String::operator==(const String& s) const { |
| 107 | return this->size() == s.size() && !memcmp(c_str(), s.c_str(), this->size()); |
| 108 | } |
| 109 | |
| 110 | bool String::operator!=(const String& s) const { |
| 111 | return !(*this == s); |
| 112 | } |
| 113 | |
| 114 | bool String::operator==(const char* s) const { |
| 115 | return this->size() == strlen(s) && !memcmp(c_str(), s, this->size()); |
| 116 | } |
| 117 | |
| 118 | bool String::operator!=(const char* s) const { |
| 119 | return !(*this == s); |
| 120 | } |
| 121 | |
| 122 | String operator+(const char* s1, const String& s2) { |
| 123 | String result(s1); |
| 124 | result.append(s2); |
| 125 | return result; |
| 126 | } |
| 127 | |
| 128 | bool operator==(const char* s1, const String& s2) { |
| 129 | return s2 == s1; |
| 130 | } |
| 131 | |
| 132 | bool operator!=(const char* s1, const String& s2) { |
| 133 | return s2 != s1; |
| 134 | } |
| 135 | |
Ethan Nicholas | 5b5f096 | 2017-09-11 13:50:14 -0700 | [diff] [blame] | 136 | bool StringFragment::operator==(StringFragment s) const { |
| 137 | if (fLength != s.fLength) { |
| 138 | return false; |
| 139 | } |
| 140 | return !memcmp(fChars, s.fChars, fLength); |
| 141 | } |
| 142 | |
| 143 | bool StringFragment::operator!=(StringFragment s) const { |
| 144 | if (fLength != s.fLength) { |
| 145 | return true; |
| 146 | } |
| 147 | return memcmp(fChars, s.fChars, fLength); |
| 148 | } |
| 149 | |
| 150 | bool StringFragment::operator==(const char* s) const { |
| 151 | return !strncmp(fChars, s, fLength); |
| 152 | } |
| 153 | |
| 154 | bool StringFragment::operator!=(const char* s) const { |
| 155 | return strncmp(fChars, s, fLength); |
| 156 | } |
| 157 | |
Ethan Nicholas | cc30577 | 2017-10-13 16:17:45 -0400 | [diff] [blame] | 158 | bool operator==(const char* s1, StringFragment s2) { |
| 159 | return s2 == s1; |
| 160 | } |
| 161 | |
| 162 | bool operator!=(const char* s1, StringFragment s2) { |
| 163 | return s2 != s1; |
| 164 | } |
| 165 | |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 166 | String to_string(int32_t value) { |
| 167 | return SkSL::String::printf("%d", value); |
| 168 | } |
| 169 | |
| 170 | String to_string(uint32_t value) { |
| 171 | return SkSL::String::printf("%u", value); |
| 172 | } |
| 173 | |
| 174 | String to_string(int64_t value) { |
Ethan Nicholas | c8c1760 | 2017-03-31 18:53:05 -0400 | [diff] [blame] | 175 | std::stringstream buffer; |
| 176 | buffer << value; |
| 177 | return String(buffer.str().c_str()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | String to_string(uint64_t value) { |
Ethan Nicholas | c8c1760 | 2017-03-31 18:53:05 -0400 | [diff] [blame] | 181 | std::stringstream buffer; |
| 182 | buffer << value; |
| 183 | return String(buffer.str().c_str()); |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | String to_string(double value) { |
| 187 | #ifdef SKSL_BUILD_FOR_WIN |
| 188 | #define SNPRINTF _snprintf |
| 189 | #else |
| 190 | #define SNPRINTF snprintf |
| 191 | #endif |
| 192 | #define MAX_DOUBLE_CHARS 25 |
| 193 | char buffer[MAX_DOUBLE_CHARS]; |
| 194 | SKSL_DEBUGCODE(int len = )SNPRINTF(buffer, sizeof(buffer), "%.17g", value); |
| 195 | ASSERT(len < MAX_DOUBLE_CHARS); |
| 196 | String result(buffer); |
| 197 | if (!strchr(buffer, '.') && !strchr(buffer, 'e')) { |
| 198 | result += ".0"; |
| 199 | } |
| 200 | return result; |
| 201 | #undef SNPRINTF |
| 202 | #undef MAX_DOUBLE_CHARS |
| 203 | } |
| 204 | |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 205 | int stoi(const String& s) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 206 | char* p; |
| 207 | SKSL_DEBUGCODE(errno = 0;) |
| 208 | long result = strtoul(s.c_str(), &p, 0); |
| 209 | ASSERT(*p == 0); |
| 210 | ASSERT(!errno); |
| 211 | return (int) result; |
| 212 | } |
| 213 | |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 214 | double stod(const String& s) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 215 | double result; |
| 216 | std::string str(s.c_str(), s.size()); |
| 217 | std::stringstream buffer(str); |
| 218 | buffer.imbue(std::locale::classic()); |
| 219 | buffer >> result; |
| 220 | ASSERT(!buffer.fail()); |
| 221 | return result; |
| 222 | } |
| 223 | |
Brian Osman | 634624a | 2017-08-15 11:14:30 -0400 | [diff] [blame] | 224 | long stol(const String& s) { |
Ethan Nicholas | 0df1b04 | 2017-03-31 13:56:23 -0400 | [diff] [blame] | 225 | char* p; |
| 226 | SKSL_DEBUGCODE(errno = 0;) |
| 227 | long result = strtoul(s.c_str(), &p, 0); |
| 228 | ASSERT(*p == 0); |
| 229 | ASSERT(!errno); |
| 230 | return result; |
| 231 | } |
| 232 | |
| 233 | } // namespace |