From 4cfafe0883da22cc020bb4e92d652ce16500b104 Mon Sep 17 00:00:00 2001 From: Mark Pulford Date: Tue, 3 May 2011 19:47:50 +0930 Subject: Add strbuf_reset() to reset string length Add strbuf_reset() to reset string length and hide the string implementation. --- lua_cjson.c | 4 ++-- strbuf.h | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lua_cjson.c b/lua_cjson.c index df3b71c..39e22ad 100644 --- a/lua_cjson.c +++ b/lua_cjson.c @@ -558,7 +558,7 @@ static int json_encode(lua_State *l) /* Reset persistent encode_buf. Avoids temporary allocation * for a single call. */ - cfg->encode_buf.length = 0; + strbuf_reset(&cfg->encode_buf); json_append_data(l, cfg, &cfg->encode_buf); json = strbuf_string(&cfg->encode_buf, &len); @@ -683,7 +683,7 @@ static void json_next_string_token(json_parse_t *json, json_token_t *token) /* json->tmp is the temporary strbuf used to accumulate the * decoded string value. */ - json->tmp->length = 0; + strbuf_reset(json->tmp); while ((ch = json->data[json->index]) != '"') { if (!ch) { /* Premature end of the string */ diff --git a/strbuf.h b/strbuf.h index 46fad33..85f3261 100644 --- a/strbuf.h +++ b/strbuf.h @@ -72,6 +72,12 @@ extern void strbuf_append_number(strbuf_t *s, double number); static void strbuf_append_char(strbuf_t *s, const char c); static void strbuf_ensure_null(strbuf_t *s); +/* Reset string for before use */ +static inline void strbuf_reset(strbuf_t *s) +{ + s->length = 0; +} + /* Return bytes remaining in the string buffer * Ensure there is space for a NULL terminator. */ static inline int strbuf_empty_length(strbuf_t *s) -- cgit v1.2.3-55-g6feb