Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkParse.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
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
12
13#include <cstdint>
14#include <cstdlib>
15#include <cstring>
16#include <limits>
17#include <string>
18
19static inline bool is_between(int c, int min, int max)
20{
21 return (unsigned)(c - min) <= (unsigned)(max - min);
22}
23
24static inline bool is_ws(int c)
25{
26 return is_between(c, 1, 32);
27}
28
29static inline bool is_digit(int c)
30{
31 return is_between(c, '0', '9');
32}
33
34static inline bool is_sep(int c)
35{
36 return is_ws(c) || c == ',' || c == ';';
37}
38
39static int to_hex(int c)
40{
41 if (is_digit(c))
42 return c - '0';
43
44 c |= 0x20; // make us lower-case
45 if (is_between(c, 'a', 'f'))
46 return c + 10 - 'a';
47 else
48 return -1;
49}
50
51static inline bool is_hex(int c)
52{
53 return to_hex(c) >= 0;
54}
55
56static const char* skip_ws(const char str[])
57{
58 SkASSERT(str);
59 while (is_ws(*str))
60 str++;
61 return str;
62}
63
64static const char* skip_sep(const char str[])
65{
66 SkASSERT(str);
67 while (is_sep(*str))
68 str++;
69 return str;
70}
71
72int SkParse::Count(const char str[])
73{
74 char c;
75 int count = 0;
76 goto skipLeading;
77 do {
78 count++;
79 do {
80 if ((c = *str++) == '\0')
81 goto goHome;
82 } while (is_sep(c) == false);
83skipLeading:
84 do {
85 if ((c = *str++) == '\0')
86 goto goHome;
87 } while (is_sep(c));
88 } while (true);
89goHome:
90 return count;
91}
92
93int SkParse::Count(const char str[], char separator)
94{
95 char c;
96 int count = 0;
97 goto skipLeading;
98 do {
99 count++;
100 do {
101 if ((c = *str++) == '\0')
102 goto goHome;
103 } while (c != separator);
104skipLeading:
105 do {
106 if ((c = *str++) == '\0')
107 goto goHome;
108 } while (c == separator);
109 } while (true);
110goHome:
111 return count;
112}
113
114const char* SkParse::FindHex(const char str[], uint32_t* value)
115{
116 SkASSERT(str);
117 str = skip_ws(str);
118
119 if (!is_hex(*str))
120 return nullptr;
121
122 uint32_t n = 0;
123 int max_digits = 8;
124 int digit;
125
126 while ((digit = to_hex(*str)) >= 0)
127 {
128 if (--max_digits < 0)
129 return nullptr;
130 n = (n << 4) | digit;
131 str += 1;
132 }
133
134 if (*str == 0 || is_ws(*str))
135 {
136 if (value)
137 *value = n;
138 return str;
139 }
140 return nullptr;
141}
142
143const char* SkParse::FindS32(const char str[], int32_t* value)
144{
145 SkASSERT(str);
146 str = skip_ws(str);
147
148 int sign = 1;
149 int64_t maxAbsValue = std::numeric_limits<int>::max();
150 if (*str == '-')
151 {
152 sign = -1;
153 maxAbsValue = -static_cast<int64_t>(std::numeric_limits<int>::min());
154 str += 1;
155 }
156
157 if (!is_digit(*str)) {
158 return nullptr;
159 }
160
161 int64_t n = 0;
162 while (is_digit(*str))
163 {
164 n = 10*n + *str - '0';
165 if (n > maxAbsValue) {
166 return nullptr;
167 }
168
169 str += 1;
170 }
171 if (value) {
172 *value = SkToS32(sign*n);
173 }
174 return str;
175}
176
177const char* SkParse::FindMSec(const char str[], SkMSec* value)
178{
179 SkASSERT(str);
180 str = skip_ws(str);
181
182 int sign = 0;
183 if (*str == '-')
184 {
185 sign = -1;
186 str += 1;
187 }
188
189 if (!is_digit(*str))
190 return nullptr;
191
192 int n = 0;
193 while (is_digit(*str))
194 {
195 n = 10*n + *str - '0';
196 str += 1;
197 }
198 int remaining10s = 3;
199 if (*str == '.') {
200 str++;
201 while (is_digit(*str))
202 {
203 n = 10*n + *str - '0';
204 str += 1;
205 if (--remaining10s == 0)
206 break;
207 }
208 }
209 while (--remaining10s >= 0)
210 n *= 10;
211 if (value)
212 *value = (n ^ sign) - sign;
213 return str;
214}
215
216const char* SkParse::FindScalar(const char str[], SkScalar* value) {
217 SkASSERT(str);
218 str = skip_ws(str);
219
220 char* stop;
221 float v = (float)strtod(str, &stop);
222 if (str == stop) {
223 return nullptr;
224 }
225 if (value) {
226 *value = v;
227 }
228 return stop;
229}
230
231const char* SkParse::FindScalars(const char str[], SkScalar value[], int count)
232{
233 SkASSERT(count >= 0);
234
235 if (count > 0)
236 {
237 for (;;)
238 {
239 str = SkParse::FindScalar(str, value);
240 if (--count == 0 || str == nullptr)
241 break;
242
243 // keep going
244 str = skip_sep(str);
245 if (value)
246 value += 1;
247 }
248 }
249 return str;
250}
251
252static bool lookup_str(const char str[], const char** table, int count)
253{
254 while (--count >= 0)
255 if (!strcmp(str, table[count]))
256 return true;
257 return false;
258}
259
260bool SkParse::FindBool(const char str[], bool* value)
261{
262 static const char* gYes[] = { "yes", "1", "true" };
263 static const char* gNo[] = { "no", "0", "false" };
264
265 if (lookup_str(str, gYes, std::size(gYes)))
266 {
267 if (value) *value = true;
268 return true;
269 }
270 else if (lookup_str(str, gNo, std::size(gNo)))
271 {
272 if (value) *value = false;
273 return true;
274 }
275 return false;
276}
277
278int SkParse::FindList(const char target[], const char list[])
279{
280 size_t len = strlen(target);
281 int index = 0;
282
283 for (;;)
284 {
285 const char* end = strchr(list, ',');
286 size_t entryLen;
287
288 if (end == nullptr) // last entry
289 entryLen = strlen(list);
290 else
291 entryLen = end - list;
292
293 if (entryLen == len && memcmp(target, list, len) == 0)
294 return index;
295 if (end == nullptr)
296 break;
297
298 list = end + 1; // skip the ','
299 index += 1;
300 }
301 return -1;
302}
int count
#define SkASSERT(cond)
Definition SkAssert.h:116
static bool is_ws(int c)
Definition SkParse.cpp:24
static bool lookup_str(const char str[], const char **table, int count)
Definition SkParse.cpp:252
static bool is_sep(int c)
Definition SkParse.cpp:34
static const char * skip_sep(const char str[])
Definition SkParse.cpp:64
static bool is_digit(int c)
Definition SkParse.cpp:29
static bool is_hex(int c)
Definition SkParse.cpp:51
static const char * skip_ws(const char str[])
Definition SkParse.cpp:56
static bool is_between(int c, int min, int max)
Definition SkParse.cpp:19
static int to_hex(int c)
Definition SkParse.cpp:39
static int sign(SkScalar x)
Definition SkPath.cpp:2141
constexpr int32_t SkToS32(S x)
Definition SkTo.h:25
uint32_t SkMSec
Definition SkTypes.h:184
SI F table(const skcms_Curve *curve, F v)
static const char * FindScalar(const char str[], SkScalar *value)
Definition SkParse.cpp:216
static const char * FindHex(const char str[], uint32_t *value)
Definition SkParse.cpp:114
static const char * FindS32(const char str[], int32_t *value)
Definition SkParse.cpp:143
static int Count(const char str[])
Definition SkParse.cpp:72
static const char * FindScalars(const char str[], SkScalar value[], int count)
Definition SkParse.cpp:231
static int FindList(const char str[], const char list[])
Definition SkParse.cpp:278
static bool FindBool(const char str[], bool *value)
Definition SkParse.cpp:260
static const char * FindMSec(const char str[], SkMSec *value)
Definition SkParse.cpp:177
float SkScalar
Definition extension.cpp:12
glong glong end
uint8_t value
uint32_t * target
static float max(float r, float g, float b)
Definition hsl.cpp:49
static float min(float r, float g, float b)
Definition hsl.cpp:48