2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "include/utils/SkParse.h"
13 static inline bool is_between(int c, int min, int max)
15 return (unsigned)(c - min) <= (unsigned)(max - min);
18 static inline bool is_ws(int c)
20 return is_between(c, 1, 32);
23 static inline bool is_digit(int c)
25 return is_between(c, '0', '9');
28 static inline bool is_sep(int c)
30 return is_ws(c) || c == ',' || c == ';';
33 static int to_hex(int c)
38 c |= 0x20; // make us lower-case
39 if (is_between(c, 'a', 'f'))
45 static inline bool is_hex(int c)
47 return to_hex(c) >= 0;
50 static const char* skip_ws(const char str[])
58 static const char* skip_sep(const char str[])
66 int SkParse::Count(const char str[])
74 if ((c = *str++) == '\0')
76 } while (is_sep(c) == false);
79 if ((c = *str++) == '\0')
87 int SkParse::Count(const char str[], char separator)
95 if ((c = *str++) == '\0')
97 } while (c != separator);
100 if ((c = *str++) == '\0')
102 } while (c == separator);
108 const char* SkParse::FindHex(const char str[], uint32_t* value)
120 while ((digit = to_hex(*str)) >= 0)
122 if (--max_digits < 0)
124 n = (n << 4) | digit;
128 if (*str == 0 || is_ws(*str))
137 const char* SkParse::FindS32(const char str[], int32_t* value)
153 while (is_digit(*str))
155 n = 10*n + *str - '0';
159 *value = (n ^ sign) - sign;
163 const char* SkParse::FindMSec(const char str[], SkMSec* value)
179 while (is_digit(*str))
181 n = 10*n + *str - '0';
184 int remaining10s = 3;
187 while (is_digit(*str))
189 n = 10*n + *str - '0';
191 if (--remaining10s == 0)
195 while (--remaining10s >= 0)
198 *value = (n ^ sign) - sign;
202 const char* SkParse::FindScalar(const char str[], SkScalar* value) {
207 float v = (float)strtod(str, &stop);
217 const char* SkParse::FindScalars(const char str[], SkScalar value[], int count)
219 SkASSERT(count >= 0);
225 str = SkParse::FindScalar(str, value);
226 if (--count == 0 || str == nullptr)
238 static bool lookup_str(const char str[], const char** table, int count)
241 if (!strcmp(str, table[count]))
246 bool SkParse::FindBool(const char str[], bool* value)
248 static const char* gYes[] = { "yes", "1", "true" };
249 static const char* gNo[] = { "no", "0", "false" };
251 if (lookup_str(str, gYes, SK_ARRAY_COUNT(gYes)))
253 if (value) *value = true;
256 else if (lookup_str(str, gNo, SK_ARRAY_COUNT(gNo)))
258 if (value) *value = false;
264 int SkParse::FindList(const char target[], const char list[])
266 size_t len = strlen(target);
271 const char* end = strchr(list, ',');
274 if (end == nullptr) // last entry
275 entryLen = strlen(list);
277 entryLen = end - list;
279 if (entryLen == len && memcmp(target, list, len) == 0)
284 list = end + 1; // skip the ','