* Utility function to check if a string is a valid JSON number. * * str is of length len, and need not be null-terminated. */
| 112 | * str is of length len, and need not be null-terminated. |
| 113 | */ |
| 114 | bool |
| 115 | IsValidJsonNumber(const char *str, int len) |
| 116 | { |
| 117 | bool numeric_error; |
| 118 | int total_len; |
| 119 | JsonLexContext dummy_lex; |
| 120 | |
| 121 | if (len <= 0) |
| 122 | return false; |
| 123 | |
| 124 | /* |
| 125 | * json_lex_number expects a leading '-' to have been eaten already. |
| 126 | * |
| 127 | * having to cast away the constness of str is ugly, but there's not much |
| 128 | * easy alternative. |
| 129 | */ |
| 130 | if (*str == '-') |
| 131 | { |
| 132 | dummy_lex.input = unconstify(char *, str) + 1; |
| 133 | dummy_lex.input_length = len - 1; |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | dummy_lex.input = unconstify(char *, str); |
| 138 | dummy_lex.input_length = len; |
| 139 | } |
| 140 | |
| 141 | json_lex_number(&dummy_lex, dummy_lex.input, &numeric_error, &total_len); |
| 142 | |
| 143 | return (!numeric_error) && (total_len == dummy_lex.input_length); |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * makeJsonLexContextCstringLen |
no test coverage detected