diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-05-03 00:23:16 +0930 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-05-03 00:23:16 +0930 |
commit | b07c4162f4e1c5056e73500147a17f4e63abaaf4 (patch) | |
tree | 8af0d3575c205efbe4582eb5aa18e8075c535110 | |
parent | f89fb30058a7d2d6a896ace242fc612bfe4e2c34 (diff) | |
download | lua-cjson-b07c4162f4e1c5056e73500147a17f4e63abaaf4.tar.gz lua-cjson-b07c4162f4e1c5056e73500147a17f4e63abaaf4.tar.bz2 lua-cjson-b07c4162f4e1c5056e73500147a17f4e63abaaf4.zip |
Add strbuf_append_number()
The separate strbuf_append_number() function avoids a potential double
call to the slow vsnprintf() function required by strbuf_append_fmt().
Also inline strbuf_append_mem() since it is very simple and will
be used often.
-rw-r--r-- | strbuf.c | 25 | ||||
-rw-r--r-- | strbuf.h | 10 |
2 files changed, 27 insertions, 8 deletions
@@ -175,13 +175,6 @@ void strbuf_resize(strbuf_t *s, int len) | |||
175 | s->reallocs++; | 175 | s->reallocs++; |
176 | } | 176 | } |
177 | 177 | ||
178 | void strbuf_append_mem(strbuf_t *s, const char *c, int len) | ||
179 | { | ||
180 | strbuf_ensure_empty_length(s, len); | ||
181 | memcpy(s->buf + s->length, c, len); | ||
182 | s->length += len; | ||
183 | } | ||
184 | |||
185 | void strbuf_append_string(strbuf_t *s, const char *str) | 178 | void strbuf_append_string(strbuf_t *s, const char *str) |
186 | { | 179 | { |
187 | int space, i; | 180 | int space, i; |
@@ -200,6 +193,24 @@ void strbuf_append_string(strbuf_t *s, const char *str) | |||
200 | } | 193 | } |
201 | } | 194 | } |
202 | 195 | ||
196 | void strbuf_append_number(strbuf_t *s, double number) | ||
197 | { | ||
198 | int len; | ||
199 | |||
200 | /* Lowest double printed with %.14g is 21 characters long: | ||
201 | * -1.7976931348623e+308 | ||
202 | * | ||
203 | * Use 32 to include the \0, and a few extra just in case.. | ||
204 | */ | ||
205 | strbuf_ensure_empty_length(s, 32); | ||
206 | |||
207 | len = sprintf(s->buf + s->length, "%.14g", number); | ||
208 | if (len < 0) | ||
209 | die("BUG: Unable to convert number"); /* This should never happen.. */ | ||
210 | |||
211 | s->length += len; | ||
212 | } | ||
213 | |||
203 | void strbuf_append_fmt(strbuf_t *s, const char *fmt, ...) | 214 | void strbuf_append_fmt(strbuf_t *s, const char *fmt, ...) |
204 | { | 215 | { |
205 | va_list arg; | 216 | va_list arg; |
@@ -66,8 +66,9 @@ static void strbuf_ensure_empty_length(strbuf_t *s, int len); | |||
66 | 66 | ||
67 | /* Update */ | 67 | /* Update */ |
68 | extern void strbuf_append_fmt(strbuf_t *s, const char *format, ...); | 68 | extern void strbuf_append_fmt(strbuf_t *s, const char *format, ...); |
69 | extern void strbuf_append_mem(strbuf_t *s, const char *c, int len); | 69 | static void strbuf_append_mem(strbuf_t *s, const char *c, int len); |
70 | extern void strbuf_append_string(strbuf_t *s, const char *str); | 70 | extern void strbuf_append_string(strbuf_t *s, const char *str); |
71 | extern void strbuf_append_number(strbuf_t *s, double number); | ||
71 | static void strbuf_append_char(strbuf_t *s, const char c); | 72 | static void strbuf_append_char(strbuf_t *s, const char c); |
72 | static void strbuf_ensure_null(strbuf_t *s); | 73 | static void strbuf_ensure_null(strbuf_t *s); |
73 | 74 | ||
@@ -100,6 +101,13 @@ static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c) | |||
100 | s->buf[s->length++] = c; | 101 | s->buf[s->length++] = c; |
101 | } | 102 | } |
102 | 103 | ||
104 | static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len) | ||
105 | { | ||
106 | strbuf_ensure_empty_length(s, len); | ||
107 | memcpy(s->buf + s->length, c, len); | ||
108 | s->length += len; | ||
109 | } | ||
110 | |||
103 | static inline void strbuf_ensure_null(strbuf_t *s) | 111 | static inline void strbuf_ensure_null(strbuf_t *s) |
104 | { | 112 | { |
105 | s->buf[s->length] = 0; | 113 | s->buf[s->length] = 0; |