Skip to content

Commit 9b20bbd

Browse files
committed
(libretro-db) query_parse_integer: no longer use sscanf. Up to 5-10x
faster
1 parent eb3d848 commit 9b20bbd

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

libretro-db/query.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,29 @@ static struct buffer query_parse_integer(
388388
struct rmsgpack_dom_value *value,
389389
const char **err)
390390
{
391+
int64_t result = 0;
392+
int sign = 1;
393+
bool has_digit = false;
394+
size_t idx = buff.offset;
395+
391396
value->type = RDT_INT;
392-
if (sscanf(buff.data + buff.offset,
393-
STRING_REP_INT64,
394-
(int64_t*)&value->val.int_) == 0)
397+
398+
if (idx < buff.len && buff.data[idx] == '-')
399+
{
400+
sign = -1;
401+
idx++;
402+
}
403+
else if (idx < buff.len && buff.data[idx] == '+')
404+
idx++;
405+
406+
while (idx < buff.len && ISDIGIT((int)buff.data[idx]))
407+
{
408+
has_digit = true;
409+
result = result * 10 + (buff.data[idx] - '0');
410+
idx++;
411+
}
412+
413+
if (!has_digit)
395414
{
396415
snprintf(s, len,
397416
"%" PRIu64 "::Expected number",
@@ -400,8 +419,8 @@ static struct buffer query_parse_integer(
400419
}
401420
else
402421
{
403-
while (ISDIGIT((int)buff.data[buff.offset]))
404-
buff.offset++;
422+
value->val.int_ = result * sign;
423+
buff.offset = idx;
405424
}
406425

407426
return buff;

0 commit comments

Comments
 (0)