diff --git a/jsonpull.c b/jsonpull.c index 5977f8fb..e2ce043f 100644 --- a/jsonpull.c +++ b/jsonpull.c @@ -16,10 +16,21 @@ json_pull *json_begin(int (*read)(struct json_pull *), int (*peek)(struct json_p j->read = read; j->peek = peek; j->source = source; + j->peeked = j->read(j); return j; } +static inline int peek(json_pull *j) { + return j->peeked; +} + +static inline int next(json_pull *j) { + int ret = j->peeked; + j->peeked = j->read(j); + return ret; +} + static int read_file(json_pull *j) { return fgetc(j->source); } @@ -60,8 +71,8 @@ void json_end(json_pull *p) { free(p); } -static int read_wrap(json_pull *j) { - int c = j->read(j); +static inline int read_wrap(json_pull *j) { + int c = next(j); if (c == '\n') { j->line++; @@ -372,34 +383,34 @@ again: string_append(&val, c); } else if (c >= '1' && c <= '9') { string_append(&val, c); - c = j->peek(j); + c = peek(j); while (c >= '0' && c <= '9') { string_append(&val, read_wrap(j)); - c = j->peek(j); + c = peek(j); } } - if (j->peek(j) == '.') { + if (peek(j) == '.') { string_append(&val, read_wrap(j)); - c = j->peek(j); + c = peek(j); while (c >= '0' && c <= '9') { string_append(&val, read_wrap(j)); - c = j->peek(j); + c = peek(j); } } - c = j->peek(j); + c = peek(j); if (c == 'e' || c == 'E') { string_append(&val, read_wrap(j)); - c = j->peek(j); + c = peek(j); if (c == '+' || c == '-') { string_append(&val, read_wrap(j)); } - c = j->peek(j); + c = peek(j); if (c < '0' || c > '9') { j->error = "Exponent without digits"; string_free(&val); @@ -407,7 +418,7 @@ again: } while (c >= '0' && c <= '9') { string_append(&val, read_wrap(j)); - c = j->peek(j); + c = peek(j); } } diff --git a/jsonpull.h b/jsonpull.h index 1a52f4cf..36bf3c87 100644 --- a/jsonpull.h +++ b/jsonpull.h @@ -30,6 +30,7 @@ typedef struct json_pull { int (*read)(struct json_pull *); int (*peek)(struct json_pull *); + int peeked; void *source; json_object *container;