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"
11 #include <algorithm>
12 #include <errno.h>
13 #include <limits.h>
14 #include <locale>
15 #include <sstream>
16 #include <string>
17
18 namespace SkSL {
19
printf(const char * fmt,...)20 String String::printf(const char* fmt, ...) {
21 va_list args;
22 va_start(args, fmt);
23 String result;
24 result.vappendf(fmt, args);
25 va_end(args);
26 return result;
27 }
28
29 #ifdef SKSL_USE_STD_STRING
appendf(const char * fmt,...)30 void String::appendf(const char* fmt, ...) {
31 va_list args;
32 va_start(args, fmt);
33 this->vappendf(fmt, args);
34 va_end(args);
35 }
36
reset()37 void String::reset() {
38 this->clear();
39 }
40
findLastOf(const char c) const41 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 }
46 #endif
47
vappendf(const char * fmt,va_list args)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];
56 va_list reuse;
57 va_copy(reuse, args);
58 size_t size = VSNPRINTF(buffer, BUFFER_SIZE, fmt, args);
59 if (BUFFER_SIZE >= size) {
60 this->append(buffer, size);
61 } else {
62 auto newBuffer = std::unique_ptr<char[]>(new char[size + 1]);
63 VSNPRINTF(newBuffer.get(), size + 1, fmt, reuse);
64 this->append(newBuffer.get(), size);
65 }
66 va_end(reuse);
67 }
68
69
startsWith(const char * s) const70 bool String::startsWith(const char* s) const {
71 return !strncmp(c_str(), s, strlen(s));
72 }
73
endsWith(const char * s) const74 bool String::endsWith(const char* s) const {
75 size_t len = strlen(s);
76 if (size() < len) {
77 return false;
78 }
79 return !strncmp(c_str() + size() - len, s, len);
80 }
81
find(const String & substring,int fromPos) const82 int String::find(const String& substring, int fromPos) const {
83 return find(substring.c_str(), fromPos);
84 }
85
find(const char * substring,int fromPos) const86 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
operator +(const char * s) const110 String String::operator+(const char* s) const {
111 String result(*this);
112 result.append(s);
113 return result;
114 }
115
operator +(const String & s) const116 String String::operator+(const String& s) const {
117 String result(*this);
118 result.append(s);
119 return result;
120 }
121
operator +(StringFragment s) const122 String String::operator+(StringFragment s) const {
123 String result(*this);
124 result.append(s.fChars, s.fLength);
125 return result;
126 }
127
operator +=(char c)128 String& String::operator+=(char c) {
129 INHERITED::operator+=(c);
130 return *this;
131 }
132
operator +=(const char * s)133 String& String::operator+=(const char* s) {
134 INHERITED::operator+=(s);
135 return *this;
136 }
137
operator +=(const String & s)138 String& String::operator+=(const String& s) {
139 INHERITED::operator+=(s);
140 return *this;
141 }
142
operator +=(StringFragment s)143 String& String::operator+=(StringFragment s) {
144 this->append(s.fChars, s.fLength);
145 return *this;
146 }
147
operator ==(const String & s) const148 bool String::operator==(const String& s) const {
149 return this->size() == s.size() && !memcmp(c_str(), s.c_str(), this->size());
150 }
151
operator !=(const String & s) const152 bool String::operator!=(const String& s) const {
153 return !(*this == s);
154 }
155
operator ==(const char * s) const156 bool String::operator==(const char* s) const {
157 return this->size() == strlen(s) && !memcmp(c_str(), s, this->size());
158 }
159
operator !=(const char * s) const160 bool String::operator!=(const char* s) const {
161 return !(*this == s);
162 }
163
operator +(const char * s1,const String & s2)164 String operator+(const char* s1, const String& s2) {
165 String result(s1);
166 result.append(s2);
167 return result;
168 }
169
operator ==(const char * s1,const String & s2)170 bool operator==(const char* s1, const String& s2) {
171 return s2 == s1;
172 }
173
operator !=(const char * s1,const String & s2)174 bool operator!=(const char* s1, const String& s2) {
175 return s2 != s1;
176 }
177
operator ==(StringFragment s) const178 bool StringFragment::operator==(StringFragment s) const {
179 if (fLength != s.fLength) {
180 return false;
181 }
182 return !memcmp(fChars, s.fChars, fLength);
183 }
184
operator !=(StringFragment s) const185 bool StringFragment::operator!=(StringFragment s) const {
186 if (fLength != s.fLength) {
187 return true;
188 }
189 return memcmp(fChars, s.fChars, fLength);
190 }
191
operator ==(const char * s) const192 bool StringFragment::operator==(const char* s) const {
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];
199 }
200
operator !=(const char * s) const201 bool StringFragment::operator!=(const char* s) const {
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];
208 }
209
operator <(StringFragment other) const210 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
operator ==(const char * s1,StringFragment s2)218 bool operator==(const char* s1, StringFragment s2) {
219 return s2 == s1;
220 }
221
operator !=(const char * s1,StringFragment s2)222 bool operator!=(const char* s1, StringFragment s2) {
223 return s2 != s1;
224 }
225
to_string(int32_t value)226 String to_string(int32_t value) {
227 return SkSL::String::printf("%d", value);
228 }
229
to_string(uint32_t value)230 String to_string(uint32_t value) {
231 return SkSL::String::printf("%u", value);
232 }
233
to_string(int64_t value)234 String to_string(int64_t value) {
235 std::stringstream buffer;
236 buffer << value;
237 return String(buffer.str().c_str());
238 }
239
to_string(uint64_t value)240 String to_string(uint64_t value) {
241 std::stringstream buffer;
242 buffer << value;
243 return String(buffer.str().c_str());
244 }
245
to_string(double value)246 String to_string(double value) {
247 std::stringstream buffer;
248 buffer.imbue(std::locale::classic());
249 buffer.precision(17);
250 buffer << value;
251 bool needsDotZero = true;
252 const std::string str = buffer.str();
253 for (int i = str.size() - 1; i >= 0; --i) {
254 char c = str[i];
255 if (c == '.' || c == 'e') {
256 needsDotZero = false;
257 break;
258 }
259 }
260 if (needsDotZero) {
261 buffer << ".0";
262 }
263 return String(buffer.str().c_str());
264 }
265
stoi(const String & s)266 int stoi(const String& s) {
267 char* p;
268 SkDEBUGCODE(errno = 0;)
269 long result = strtoul(s.c_str(), &p, 0);
270 SkASSERT(*p == 0);
271 SkASSERT(!errno);
272 return (int) result;
273 }
274
stod(const String & s)275 double stod(const String& s) {
276 double result;
277 std::string str(s.c_str(), s.size());
278 std::stringstream buffer(str);
279 buffer.imbue(std::locale::classic());
280 buffer >> result;
281 SkASSERT(!buffer.fail());
282 return result;
283 }
284
stol(const String & s)285 long stol(const String& s) {
286 char* p;
287 SkDEBUGCODE(errno = 0;)
288 long result = strtoul(s.c_str(), &p, 0);
289 SkASSERT(*p == 0);
290 SkASSERT(!errno);
291 return result;
292 }
293
294 } // namespace
295