aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-05-03 19:47:50 +0930
committerMark Pulford <mark@kyne.com.au>2011-05-03 19:47:50 +0930
commit4cfafe0883da22cc020bb4e92d652ce16500b104 (patch)
treeda23a04ed07085641609f3a9e39aa708da3630a9
parentec18dfc358cc4391eec3a37bdb81fe8d5a8b3d69 (diff)
downloadlua-cjson-4cfafe0883da22cc020bb4e92d652ce16500b104.tar.gz
lua-cjson-4cfafe0883da22cc020bb4e92d652ce16500b104.tar.bz2
lua-cjson-4cfafe0883da22cc020bb4e92d652ce16500b104.zip
Add strbuf_reset() to reset string length
Add strbuf_reset() to reset string length and hide the string implementation.
-rw-r--r--lua_cjson.c4
-rw-r--r--strbuf.h6
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)
558 558
559 /* Reset persistent encode_buf. Avoids temporary allocation 559 /* Reset persistent encode_buf. Avoids temporary allocation
560 * for a single call. */ 560 * for a single call. */
561 cfg->encode_buf.length = 0; 561 strbuf_reset(&cfg->encode_buf);
562 json_append_data(l, cfg, &cfg->encode_buf); 562 json_append_data(l, cfg, &cfg->encode_buf);
563 json = strbuf_string(&cfg->encode_buf, &len); 563 json = strbuf_string(&cfg->encode_buf, &len);
564 564
@@ -683,7 +683,7 @@ static void json_next_string_token(json_parse_t *json, json_token_t *token)
683 683
684 /* json->tmp is the temporary strbuf used to accumulate the 684 /* json->tmp is the temporary strbuf used to accumulate the
685 * decoded string value. */ 685 * decoded string value. */
686 json->tmp->length = 0; 686 strbuf_reset(json->tmp);
687 while ((ch = json->data[json->index]) != '"') { 687 while ((ch = json->data[json->index]) != '"') {
688 if (!ch) { 688 if (!ch) {
689 /* Premature end of the string */ 689 /* 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);
72static void strbuf_append_char(strbuf_t *s, const char c); 72static void strbuf_append_char(strbuf_t *s, const char c);
73static void strbuf_ensure_null(strbuf_t *s); 73static void strbuf_ensure_null(strbuf_t *s);
74 74
75/* Reset string for before use */
76static inline void strbuf_reset(strbuf_t *s)
77{
78 s->length = 0;
79}
80
75/* Return bytes remaining in the string buffer 81/* Return bytes remaining in the string buffer
76 * Ensure there is space for a NULL terminator. */ 82 * Ensure there is space for a NULL terminator. */
77static inline int strbuf_empty_length(strbuf_t *s) 83static inline int strbuf_empty_length(strbuf_t *s)