improved decimal handling in lib/json.c

This commit is contained in:
hyung-hwan 2020-06-17 13:36:49 +00:00
parent e98b3a37c0
commit 044120a4d9
3 changed files with 56 additions and 18 deletions

View File

@ -129,7 +129,7 @@ static int on_json_inst (mio_json_t* json, mio_json_inst_t inst, mio_oow_t level
static int write_json_element (mio_jsonwr_t* jsonwr, const mio_bch_t* dptr, mio_oow_t dlen, void* ctx)
{
write (1, dptr, dlen);
fwrite (dptr, 1, dlen, stdout);
return 0;
}
@ -148,6 +148,7 @@ int main (int argc, char* argv[])
mio_json_t* json = MIO_NULL;
char buf[128];
mio_oow_t rem;
size_t size;
json = mio_json_open(mio, 0);
@ -157,18 +158,21 @@ int main (int argc, char* argv[])
while (!feof(stdin) || rem > 0)
{
int x;
size_t size;
if (!feof(stdin))
{
size = fread(&buf[rem], 1, sizeof(buf) - rem, stdin);
if (size <= 0) break;
}
else size = 0;
else
{
size = rem;
rem = 0;
}
if ((x = mio_json_feed(json, buf, size + rem, &rem, 1)) <= -1)
{
printf ("**** ERROR ****\n");
mio_logbfmt (mio, MIO_LOG_STDOUT, "**** ERROR - %js ****\n", mio_geterrmsg(mio));
break;
}
@ -178,7 +182,7 @@ int main (int argc, char* argv[])
mio_logbfmt (mio, MIO_LOG_STDOUT, "\n-----------------------------------\n");
}
//printf ("--> x %d input %d left-over %d\n", (int)x, (int)size, (int)rem);
/*printf ("--> x %d input %d left-over %d => [%.*s]\n", (int)x, (int)size, (int)rem, (int)rem, &buf[size - rem]);*/
if (rem > 0) memcpy (buf, &buf[size - rem], rem);
}

View File

@ -330,17 +330,51 @@ static int handle_string_value_char (mio_json_t* json, mio_ooci_t c)
static int handle_numeric_value_char (mio_json_t* json, mio_ooci_t c)
{
if (mio_is_ooch_digit(c) || (json->tok.len == 0 && (c == '+' || c == '-')))
switch (json->state_stack->u.nv.progress)
{
if (add_char_to_token(json, c, 0) <= -1) return -1;
return 1;
}
else if (!json->state_stack->u.nv.dotted && c == '.' &&
json->tok.len > 0 && mio_is_ooch_digit(json->tok.ptr[json->tok.len - 1]))
{
if (add_char_to_token(json, c, 0) <= -1) return -1;
json->state_stack->u.nv.dotted = 1;
return 1;
case 0: /* integer part */
if (mio_is_ooch_digit(c) || (json->tok.len == 0 && (c == '+' || c == '-')))
{
if (add_char_to_token(json, c, 0) <= -1) return -1;
return 1;
}
else if ((c == '.' || c == 'e' || c == 'E') && json->tok.len > 0 && mio_is_ooch_digit(json->tok.ptr[json->tok.len - 1]))
{
if (add_char_to_token(json, c, 0) <= -1) return -1;
json->state_stack->u.nv.progress = (c == '.'? 1: 2);
return 1;
}
break;
case 1: /* decimal part */
if (mio_is_ooch_digit(c))
{
if (add_char_to_token(json, c, 0) <= -1) return -1;
return 1;
}
else if (c == 'e' || c == 'E')
{
if (add_char_to_token(json, c, 0) <= -1) return -1;
json->state_stack->u.nv.progress = 2;
return 1;
}
break;
case 2: /* exponent part (ok to have a sign) */
if (c == '+' || c == '-')
{
if (add_char_to_token(json, c, 0) <= -1) return -1;
json->state_stack->u.nv.progress = 3;
return 1;
}
/* fall thru */
case 3: /* exponent part (no sign expected) */
if (mio_is_ooch_digit(c))
{
if (add_char_to_token(json, c, 0) <= -1) return -1;
return 1;
}
break;
}
pop_read_state (json);
@ -453,7 +487,7 @@ static int handle_char_in_array (mio_json_t* json, mio_ooci_t c)
{
if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1;
clear_token (json);
json->state_stack->u.nv.dotted = 0;
json->state_stack->u.nv.progress = 0;
return 0; /* start over */
}
else if (mio_is_ooch_alpha(c))
@ -539,7 +573,7 @@ static int handle_char_in_object (mio_json_t* json, mio_ooci_t c)
{
if (push_read_state(json, MIO_JSON_STATE_IN_NUMERIC_VALUE) <= -1) return -1;
clear_token (json);
json->state_stack->u.nv.dotted = 0;
json->state_stack->u.nv.progress = 0;
return 0; /* start over */
}
else if (mio_is_ooch_alpha(c))

View File

@ -114,7 +114,7 @@ struct mio_json_state_node_t
} cv;
struct
{
int dotted;
int progress;
} nv;
} u;
mio_json_state_node_t* next;