blob: 11ae1c78ae64d6c6b3657e3e69293e81cf7b4020 [file] [log] [blame]
Ethan Nicholas0df1b042017-03-31 13:56:23 -04001/*
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 Osmande7220f2018-02-09 09:32:36 -050011#include <algorithm>
Ethan Nicholas0df1b042017-03-31 13:56:23 -040012#include <errno.h>
Ethan Nicholas0df1b042017-03-31 13:56:23 -040013#include <limits.h>
14#include <locale>
15#include <sstream>
16#include <string>
17
18namespace SkSL {
19
20String String::printf(const char* fmt, ...) {
21 va_list args;
22 va_start(args, fmt);
23 String result;
24 result.vappendf(fmt, args);
z102.zhangd74f2c82018-08-10 09:08:47 +080025 va_end(args);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040026 return result;
27}
28
Brian Osman93ba0a42017-08-14 14:48:10 -040029#ifdef SKSL_USE_STD_STRING
Ethan Nicholas0df1b042017-03-31 13:56:23 -040030void String::appendf(const char* fmt, ...) {
31 va_list args;
32 va_start(args, fmt);
33 this->vappendf(fmt, args);
z102.zhangd74f2c82018-08-10 09:08:47 +080034 va_end(args);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040035}
36#endif
37
38void String::vappendf(const char* fmt, va_list args) {
39#ifdef SKSL_BUILD_FOR_WIN
40 #define VSNPRINTF _vsnprintf
41#else
42 #define VSNPRINTF vsnprintf
43#endif
44 #define BUFFER_SIZE 256
45 char buffer[BUFFER_SIZE];
Ethan Nicholas762466e2017-06-29 10:03:38 -040046 va_list reuse;
47 va_copy(reuse, args);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040048 size_t size = VSNPRINTF(buffer, BUFFER_SIZE, fmt, args);
49 if (BUFFER_SIZE >= size) {
50 this->append(buffer, size);
51 } else {
Ethan Nicholas762466e2017-06-29 10:03:38 -040052 auto newBuffer = std::unique_ptr<char[]>(new char[size + 1]);
53 VSNPRINTF(newBuffer.get(), size + 1, fmt, reuse);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040054 this->append(newBuffer.get(), size);
55 }
z102.zhangd74f2c82018-08-10 09:08:47 +080056 va_end(reuse);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040057}
58
59
60bool String::startsWith(const char* s) const {
Ethan Nicholas985febf2017-05-19 14:03:45 -040061 return !strncmp(c_str(), s, strlen(s));
Ethan Nicholas0df1b042017-03-31 13:56:23 -040062}
63
64bool String::endsWith(const char* s) const {
65 size_t len = strlen(s);
66 if (size() < len) {
67 return false;
68 }
Ethan Nicholas985febf2017-05-19 14:03:45 -040069 return !strncmp(c_str() + size() - len, s, len);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040070}
71
72String String::operator+(const char* s) const {
73 String result(*this);
74 result.append(s);
75 return result;
76}
77
78String String::operator+(const String& s) const {
79 String result(*this);
80 result.append(s);
81 return result;
82}
83
Ethan Nicholas5b5f0962017-09-11 13:50:14 -070084String String::operator+(StringFragment s) const {
85 String result(*this);
86 result.append(s.fChars, s.fLength);
87 return result;
88}
89
90String& String::operator+=(char c) {
91 INHERITED::operator+=(c);
92 return *this;
93}
94
95String& String::operator+=(const char* s) {
96 INHERITED::operator+=(s);
97 return *this;
98}
99
100String& String::operator+=(const String& s) {
101 INHERITED::operator+=(s);
102 return *this;
103}
104
105String& String::operator+=(StringFragment s) {
106 this->append(s.fChars, s.fLength);
107 return *this;
108}
109
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400110bool String::operator==(const String& s) const {
111 return this->size() == s.size() && !memcmp(c_str(), s.c_str(), this->size());
112}
113
114bool String::operator!=(const String& s) const {
115 return !(*this == s);
116}
117
118bool String::operator==(const char* s) const {
119 return this->size() == strlen(s) && !memcmp(c_str(), s, this->size());
120}
121
122bool String::operator!=(const char* s) const {
123 return !(*this == s);
124}
125
126String operator+(const char* s1, const String& s2) {
127 String result(s1);
128 result.append(s2);
129 return result;
130}
131
132bool operator==(const char* s1, const String& s2) {
133 return s2 == s1;
134}
135
136bool operator!=(const char* s1, const String& s2) {
137 return s2 != s1;
138}
139
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700140bool StringFragment::operator==(StringFragment s) const {
141 if (fLength != s.fLength) {
142 return false;
143 }
144 return !memcmp(fChars, s.fChars, fLength);
145}
146
147bool StringFragment::operator!=(StringFragment s) const {
148 if (fLength != s.fLength) {
149 return true;
150 }
151 return memcmp(fChars, s.fChars, fLength);
152}
153
154bool StringFragment::operator==(const char* s) const {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500155 for (size_t i = 0; i < fLength; ++i) {
156 if (fChars[i] != s[i]) {
157 return false;
158 }
159 }
160 return 0 == s[fLength];
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700161}
162
163bool StringFragment::operator!=(const char* s) const {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500164 for (size_t i = 0; i < fLength; ++i) {
165 if (fChars[i] != s[i]) {
166 return true;
167 }
168 }
169 return 0 != s[fLength];
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700170}
171
Ethan Nicholas2c33e3e2018-01-08 15:36:28 -0500172bool StringFragment::operator<(StringFragment other) const {
173 int comparison = strncmp(fChars, other.fChars, std::min(fLength, other.fLength));
174 if (comparison) {
175 return comparison < 0;
176 }
177 return fLength < other.fLength;
178}
179
Ethan Nicholascc305772017-10-13 16:17:45 -0400180bool operator==(const char* s1, StringFragment s2) {
181 return s2 == s1;
182}
183
184bool operator!=(const char* s1, StringFragment s2) {
185 return s2 != s1;
186}
187
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400188String to_string(int32_t value) {
189 return SkSL::String::printf("%d", value);
190}
191
192String to_string(uint32_t value) {
193 return SkSL::String::printf("%u", value);
194}
195
196String to_string(int64_t value) {
Ethan Nicholasc8c17602017-03-31 18:53:05 -0400197 std::stringstream buffer;
198 buffer << value;
199 return String(buffer.str().c_str());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400200}
201
202String to_string(uint64_t value) {
Ethan Nicholasc8c17602017-03-31 18:53:05 -0400203 std::stringstream buffer;
204 buffer << value;
205 return String(buffer.str().c_str());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400206}
207
208String to_string(double value) {
209#ifdef SKSL_BUILD_FOR_WIN
210 #define SNPRINTF _snprintf
211#else
212 #define SNPRINTF snprintf
213#endif
214#define MAX_DOUBLE_CHARS 25
215 char buffer[MAX_DOUBLE_CHARS];
Ethan Nicholas00543112018-07-31 09:44:36 -0400216 int len = SNPRINTF(buffer, sizeof(buffer), "%.17g", value);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400217 SkASSERT(len < MAX_DOUBLE_CHARS);
Ethan Nicholas00543112018-07-31 09:44:36 -0400218 bool needsDotZero = true;
219 for (int i = 0; i < len; ++i) {
220 char c = buffer[i];
221 if (c == ',') {
222 buffer[i] = '.';
223 needsDotZero = false;
224 break;
225 } else if (c == '.' || c == 'e') {
226 needsDotZero = false;
227 break;
228 }
229 }
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400230 String result(buffer);
Ethan Nicholas00543112018-07-31 09:44:36 -0400231 if (needsDotZero) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400232 result += ".0";
233 }
234 return result;
235#undef SNPRINTF
236#undef MAX_DOUBLE_CHARS
237}
238
Brian Osman634624a2017-08-15 11:14:30 -0400239int stoi(const String& s) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400240 char* p;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400241 SkDEBUGCODE(errno = 0;)
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400242 long result = strtoul(s.c_str(), &p, 0);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400243 SkASSERT(*p == 0);
244 SkASSERT(!errno);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400245 return (int) result;
246}
247
Brian Osman634624a2017-08-15 11:14:30 -0400248double stod(const String& s) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400249 double result;
250 std::string str(s.c_str(), s.size());
251 std::stringstream buffer(str);
252 buffer.imbue(std::locale::classic());
253 buffer >> result;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400254 SkASSERT(!buffer.fail());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400255 return result;
256}
257
Brian Osman634624a2017-08-15 11:14:30 -0400258long stol(const String& s) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400259 char* p;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400260 SkDEBUGCODE(errno = 0;)
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400261 long result = strtoul(s.c_str(), &p, 0);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400262 SkASSERT(*p == 0);
263 SkASSERT(!errno);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400264 return result;
265}
266
267} // namespace