SkParse.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #include "include/utils/SkParse.h"
  8. #include <stdlib.h>
  9. static inline bool is_between(int c, int min, int max)
  10. {
  11. return (unsigned)(c - min) <= (unsigned)(max - min);
  12. }
  13. static inline bool is_ws(int c)
  14. {
  15. return is_between(c, 1, 32);
  16. }
  17. static inline bool is_digit(int c)
  18. {
  19. return is_between(c, '0', '9');
  20. }
  21. static inline bool is_sep(int c)
  22. {
  23. return is_ws(c) || c == ',' || c == ';';
  24. }
  25. static int to_hex(int c)
  26. {
  27. if (is_digit(c))
  28. return c - '0';
  29. c |= 0x20; // make us lower-case
  30. if (is_between(c, 'a', 'f'))
  31. return c + 10 - 'a';
  32. else
  33. return -1;
  34. }
  35. static inline bool is_hex(int c)
  36. {
  37. return to_hex(c) >= 0;
  38. }
  39. static const char* skip_ws(const char str[])
  40. {
  41. SkASSERT(str);
  42. while (is_ws(*str))
  43. str++;
  44. return str;
  45. }
  46. static const char* skip_sep(const char str[])
  47. {
  48. SkASSERT(str);
  49. while (is_sep(*str))
  50. str++;
  51. return str;
  52. }
  53. int SkParse::Count(const char str[])
  54. {
  55. char c;
  56. int count = 0;
  57. goto skipLeading;
  58. do {
  59. count++;
  60. do {
  61. if ((c = *str++) == '\0')
  62. goto goHome;
  63. } while (is_sep(c) == false);
  64. skipLeading:
  65. do {
  66. if ((c = *str++) == '\0')
  67. goto goHome;
  68. } while (is_sep(c));
  69. } while (true);
  70. goHome:
  71. return count;
  72. }
  73. int SkParse::Count(const char str[], char separator)
  74. {
  75. char c;
  76. int count = 0;
  77. goto skipLeading;
  78. do {
  79. count++;
  80. do {
  81. if ((c = *str++) == '\0')
  82. goto goHome;
  83. } while (c != separator);
  84. skipLeading:
  85. do {
  86. if ((c = *str++) == '\0')
  87. goto goHome;
  88. } while (c == separator);
  89. } while (true);
  90. goHome:
  91. return count;
  92. }
  93. const char* SkParse::FindHex(const char str[], uint32_t* value)
  94. {
  95. SkASSERT(str);
  96. str = skip_ws(str);
  97. if (!is_hex(*str))
  98. return nullptr;
  99. uint32_t n = 0;
  100. int max_digits = 8;
  101. int digit;
  102. while ((digit = to_hex(*str)) >= 0)
  103. {
  104. if (--max_digits < 0)
  105. return nullptr;
  106. n = (n << 4) | digit;
  107. str += 1;
  108. }
  109. if (*str == 0 || is_ws(*str))
  110. {
  111. if (value)
  112. *value = n;
  113. return str;
  114. }
  115. return nullptr;
  116. }
  117. const char* SkParse::FindS32(const char str[], int32_t* value)
  118. {
  119. SkASSERT(str);
  120. str = skip_ws(str);
  121. int sign = 0;
  122. if (*str == '-')
  123. {
  124. sign = -1;
  125. str += 1;
  126. }
  127. if (!is_digit(*str))
  128. return nullptr;
  129. int n = 0;
  130. while (is_digit(*str))
  131. {
  132. n = 10*n + *str - '0';
  133. str += 1;
  134. }
  135. if (value)
  136. *value = (n ^ sign) - sign;
  137. return str;
  138. }
  139. const char* SkParse::FindMSec(const char str[], SkMSec* value)
  140. {
  141. SkASSERT(str);
  142. str = skip_ws(str);
  143. int sign = 0;
  144. if (*str == '-')
  145. {
  146. sign = -1;
  147. str += 1;
  148. }
  149. if (!is_digit(*str))
  150. return nullptr;
  151. int n = 0;
  152. while (is_digit(*str))
  153. {
  154. n = 10*n + *str - '0';
  155. str += 1;
  156. }
  157. int remaining10s = 3;
  158. if (*str == '.') {
  159. str++;
  160. while (is_digit(*str))
  161. {
  162. n = 10*n + *str - '0';
  163. str += 1;
  164. if (--remaining10s == 0)
  165. break;
  166. }
  167. }
  168. while (--remaining10s >= 0)
  169. n *= 10;
  170. if (value)
  171. *value = (n ^ sign) - sign;
  172. return str;
  173. }
  174. const char* SkParse::FindScalar(const char str[], SkScalar* value) {
  175. SkASSERT(str);
  176. str = skip_ws(str);
  177. char* stop;
  178. float v = (float)strtod(str, &stop);
  179. if (str == stop) {
  180. return nullptr;
  181. }
  182. if (value) {
  183. *value = v;
  184. }
  185. return stop;
  186. }
  187. const char* SkParse::FindScalars(const char str[], SkScalar value[], int count)
  188. {
  189. SkASSERT(count >= 0);
  190. if (count > 0)
  191. {
  192. for (;;)
  193. {
  194. str = SkParse::FindScalar(str, value);
  195. if (--count == 0 || str == nullptr)
  196. break;
  197. // keep going
  198. str = skip_sep(str);
  199. if (value)
  200. value += 1;
  201. }
  202. }
  203. return str;
  204. }
  205. static bool lookup_str(const char str[], const char** table, int count)
  206. {
  207. while (--count >= 0)
  208. if (!strcmp(str, table[count]))
  209. return true;
  210. return false;
  211. }
  212. bool SkParse::FindBool(const char str[], bool* value)
  213. {
  214. static const char* gYes[] = { "yes", "1", "true" };
  215. static const char* gNo[] = { "no", "0", "false" };
  216. if (lookup_str(str, gYes, SK_ARRAY_COUNT(gYes)))
  217. {
  218. if (value) *value = true;
  219. return true;
  220. }
  221. else if (lookup_str(str, gNo, SK_ARRAY_COUNT(gNo)))
  222. {
  223. if (value) *value = false;
  224. return true;
  225. }
  226. return false;
  227. }
  228. int SkParse::FindList(const char target[], const char list[])
  229. {
  230. size_t len = strlen(target);
  231. int index = 0;
  232. for (;;)
  233. {
  234. const char* end = strchr(list, ',');
  235. size_t entryLen;
  236. if (end == nullptr) // last entry
  237. entryLen = strlen(list);
  238. else
  239. entryLen = end - list;
  240. if (entryLen == len && memcmp(target, list, len) == 0)
  241. return index;
  242. if (end == nullptr)
  243. break;
  244. list = end + 1; // skip the ','
  245. index += 1;
  246. }
  247. return -1;
  248. }