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