mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2026-03-21 05:33:49 -04:00
json: Use state machine and fix 1E10 parsing
Use a state machine to check for valid numbers and fix the 1E10 parsing case. With help from Claude.
This commit is contained in:
parent
c745582ef5
commit
f6939a1cf0
2 changed files with 24 additions and 34 deletions
|
|
@ -476,40 +476,29 @@ SPA_API_JSON bool spa_json_is_int(const char *val, int len)
|
||||||
|
|
||||||
SPA_API_JSON bool spa_json_is_json_number(const char *val, int len)
|
SPA_API_JSON bool spa_json_is_json_number(const char *val, int len)
|
||||||
{
|
{
|
||||||
int pos = 0, first;
|
static const int8_t trans[9][7] = {
|
||||||
/* integer */
|
/* '1-9' '0' '-' '+' '.' 'eE' other */
|
||||||
if (len == 0)
|
/* 0 */ {-1, -1, -1, -1, 6, 7, -1 }, /* after '0' */
|
||||||
return 0;
|
/* 1 */ { 1, 1, -1, -1, 6, 7, -1 }, /* in integer */
|
||||||
if (pos < len && val[pos] == '-')
|
/* 2 */ { 2, 2, -1, -1, -1, 7, -1 }, /* in fraction */
|
||||||
pos++;
|
/* 3 */ { 3, 3, -1, -1, -1, -1, -1 }, /* in exponent */
|
||||||
first = pos;
|
/* 4 */ { 1, 0, 5, -1, -1, -1, -1 }, /* start */
|
||||||
while(pos < len && val[pos] >= '0' && val[pos] <= '9')
|
/* 5 */ { 1, 0, -1, -1, -1, -1, -1 }, /* after '-' */
|
||||||
pos++;
|
/* 6 */ { 2, 2, -1, -1, -1, -1, -1 }, /* after '.' */
|
||||||
if (pos == first || (first + 1 < pos && val[first] == '0'))
|
/* 7 */ { 3, 3, 8, 8, -1, -1, -1 }, /* after 'e'/'E' */
|
||||||
return 0;
|
/* 8 */ { 3, 3, -1, -1, -1, -1, -1 }, /* after exp sign */
|
||||||
/* fraction */
|
};
|
||||||
if (pos == len)
|
int i, state = 4;
|
||||||
return 1;
|
|
||||||
if (val[pos++] != '.')
|
for (i = 0; i < len; i++) {
|
||||||
return 0;
|
char v = val[i];
|
||||||
first = pos;
|
int cls = (v >= '1' && v <= '9') ? 0 : v == '0' ? 1 :
|
||||||
while(pos < len && val[pos] >= '0' && val[pos] <= '9')
|
v == '-' ? 2 : v == '+' ? 3 : v == '.' ? 4 :
|
||||||
pos++;
|
(v == 'e' || v == 'E') ? 5 : 6;
|
||||||
if (pos == first)
|
if ((state = trans[state][cls]) < 0)
|
||||||
return 0;
|
return false;
|
||||||
/* exponent */
|
}
|
||||||
if (pos == len)
|
return state < 4;
|
||||||
return 1;
|
|
||||||
if (val[pos] != 'e' && val[pos] != 'E')
|
|
||||||
return 0;
|
|
||||||
pos++;
|
|
||||||
if (val[pos] == '-' || val[pos] == '+')
|
|
||||||
pos++;
|
|
||||||
while(pos < len && val[pos] >= '0' && val[pos] <= '9')
|
|
||||||
pos++;
|
|
||||||
if (pos != len)
|
|
||||||
return 0;
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* bool */
|
/* bool */
|
||||||
|
|
|
||||||
|
|
@ -740,6 +740,7 @@ PWTEST(json_float_check)
|
||||||
{ "-1.", 1, 0 },
|
{ "-1.", 1, 0 },
|
||||||
|
|
||||||
{ "-.0", 1, 0 },
|
{ "-.0", 1, 0 },
|
||||||
|
{ "-1E10", 1, 1 },
|
||||||
|
|
||||||
};
|
};
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue