blob: effb3dbe2f360537fd53e643f7ee9f69104ba34c [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}
Michael Ludwige84fb192018-08-21 16:32:20 -040036
37void String::reset() {
38 this->clear();
39}
40
41int 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 Nicholas0df1b042017-03-31 13:56:23 -040046#endif
47
48void 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 Nicholas762466e2017-06-29 10:03:38 -040056 va_list reuse;
57 va_copy(reuse, args);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040058 size_t size = VSNPRINTF(buffer, BUFFER_SIZE, fmt, args);
59 if (BUFFER_SIZE >= size) {
60 this->append(buffer, size);
61 } else {
Ethan Nicholas762466e2017-06-29 10:03:38 -040062 auto newBuffer = std::unique_ptr<char[]>(new char[size + 1]);
63 VSNPRINTF(newBuffer.get(), size + 1, fmt, reuse);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040064 this->append(newBuffer.get(), size);
65 }
z102.zhangd74f2c82018-08-10 09:08:47 +080066 va_end(reuse);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040067}
68
69
70bool String::startsWith(const char* s) const {
Ethan Nicholas985febf2017-05-19 14:03:45 -040071 return !strncmp(c_str(), s, strlen(s));
Ethan Nicholas0df1b042017-03-31 13:56:23 -040072}
73
74bool String::endsWith(const char* s) const {
75 size_t len = strlen(s);
76 if (size() < len) {
77 return false;
78 }
Ethan Nicholas985febf2017-05-19 14:03:45 -040079 return !strncmp(c_str() + size() - len, s, len);
Ethan Nicholas0df1b042017-03-31 13:56:23 -040080}
81
Michael Ludwiga4275592018-08-31 10:52:47 -040082int String::find(const String& substring, int fromPos) const {
83 return find(substring.c_str(), fromPos);
84}
85
86int 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 Nicholas0df1b042017-03-31 13:56:23 -0400110String String::operator+(const char* s) const {
111 String result(*this);
112 result.append(s);
113 return result;
114}
115
116String String::operator+(const String& s) const {
117 String result(*this);
118 result.append(s);
119 return result;
120}
121
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700122String String::operator+(StringFragment s) const {
123 String result(*this);
124 result.append(s.fChars, s.fLength);
125 return result;
126}
127
128String& String::operator+=(char c) {
129 INHERITED::operator+=(c);
130 return *this;
131}
132
133String& String::operator+=(const char* s) {
134 INHERITED::operator+=(s);
135 return *this;
136}
137
138String& String::operator+=(const String& s) {
139 INHERITED::operator+=(s);
140 return *this;
141}
142
143String& String::operator+=(StringFragment s) {
144 this->append(s.fChars, s.fLength);
145 return *this;
146}
147
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400148bool String::operator==(const String& s) const {
149 return this->size() == s.size() && !memcmp(c_str(), s.c_str(), this->size());
150}
151
152bool String::operator!=(const String& s) const {
153 return !(*this == s);
154}
155
156bool String::operator==(const char* s) const {
157 return this->size() == strlen(s) && !memcmp(c_str(), s, this->size());
158}
159
160bool String::operator!=(const char* s) const {
161 return !(*this == s);
162}
163
164String operator+(const char* s1, const String& s2) {
165 String result(s1);
166 result.append(s2);
167 return result;
168}
169
170bool operator==(const char* s1, const String& s2) {
171 return s2 == s1;
172}
173
174bool operator!=(const char* s1, const String& s2) {
175 return s2 != s1;
176}
177
Ethan Nicholas5b5f0962017-09-11 13:50:14 -0700178bool StringFragment::operator==(StringFragment s) const {
179 if (fLength != s.fLength) {
180 return false;
181 }
182 return !memcmp(fChars, s.fChars, fLength);
183}
184
185bool StringFragment::operator!=(StringFragment s) const {
186 if (fLength != s.fLength) {
187 return true;
188 }
189 return memcmp(fChars, s.fChars, fLength);
190}
191
192bool StringFragment::operator==(const char* s) const {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500193 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 Nicholas5b5f0962017-09-11 13:50:14 -0700199}
200
201bool StringFragment::operator!=(const char* s) const {
Ethan Nicholase9d172a2017-11-20 12:12:24 -0500202 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 Nicholas5b5f0962017-09-11 13:50:14 -0700208}
209
Ethan Nicholas2c33e3e2018-01-08 15:36:28 -0500210bool 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 Nicholascc305772017-10-13 16:17:45 -0400218bool operator==(const char* s1, StringFragment s2) {
219 return s2 == s1;
220}
221
222bool operator!=(const char* s1, StringFragment s2) {
223 return s2 != s1;
224}
225
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400226String to_string(int32_t value) {
227 return SkSL::String::printf("%d", value);
228}
229
230String to_string(uint32_t value) {
231 return SkSL::String::printf("%u", value);
232}
233
234String to_string(int64_t value) {
Ethan Nicholasc8c17602017-03-31 18:53:05 -0400235 std::stringstream buffer;
236 buffer << value;
237 return String(buffer.str().c_str());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400238}
239
240String to_string(uint64_t value) {
Ethan Nicholasc8c17602017-03-31 18:53:05 -0400241 std::stringstream buffer;
242 buffer << value;
243 return String(buffer.str().c_str());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400244}
245
246String 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 Nicholas00543112018-07-31 09:44:36 -0400254 int len = SNPRINTF(buffer, sizeof(buffer), "%.17g", value);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400255 SkASSERT(len < MAX_DOUBLE_CHARS);
Ethan Nicholas00543112018-07-31 09:44:36 -0400256 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 Nicholas0df1b042017-03-31 13:56:23 -0400268 String result(buffer);
Ethan Nicholas00543112018-07-31 09:44:36 -0400269 if (needsDotZero) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400270 result += ".0";
271 }
272 return result;
273#undef SNPRINTF
274#undef MAX_DOUBLE_CHARS
275}
276
Brian Osman634624a2017-08-15 11:14:30 -0400277int stoi(const String& s) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400278 char* p;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400279 SkDEBUGCODE(errno = 0;)
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400280 long result = strtoul(s.c_str(), &p, 0);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400281 SkASSERT(*p == 0);
282 SkASSERT(!errno);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400283 return (int) result;
284}
285
Brian Osman634624a2017-08-15 11:14:30 -0400286double stod(const String& s) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400287 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 Nicholasd9d33c32018-06-12 11:05:59 -0400292 SkASSERT(!buffer.fail());
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400293 return result;
294}
295
Brian Osman634624a2017-08-15 11:14:30 -0400296long stol(const String& s) {
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400297 char* p;
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400298 SkDEBUGCODE(errno = 0;)
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400299 long result = strtoul(s.c_str(), &p, 0);
Ethan Nicholasd9d33c32018-06-12 11:05:59 -0400300 SkASSERT(*p == 0);
301 SkASSERT(!errno);
Ethan Nicholas0df1b042017-03-31 13:56:23 -0400302 return result;
303}
304
305} // namespace