Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mpx/lua-cjson.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Pulford <mark@kyne.com.au>2011-05-02 18:53:16 +0400
committerMark Pulford <mark@kyne.com.au>2011-05-02 18:53:16 +0400
commitb07c4162f4e1c5056e73500147a17f4e63abaaf4 (patch)
tree8af0d3575c205efbe4582eb5aa18e8075c535110 /strbuf.c
parentf89fb30058a7d2d6a896ace242fc612bfe4e2c34 (diff)
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.
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/strbuf.c b/strbuf.c
index b6227fc..7a53a52 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -175,13 +175,6 @@ void strbuf_resize(strbuf_t *s, int len)
s->reallocs++;
}
-void strbuf_append_mem(strbuf_t *s, const char *c, int len)
-{
- strbuf_ensure_empty_length(s, len);
- memcpy(s->buf + s->length, c, len);
- s->length += len;
-}
-
void strbuf_append_string(strbuf_t *s, const char *str)
{
int space, i;
@@ -200,6 +193,24 @@ void strbuf_append_string(strbuf_t *s, const char *str)
}
}
+void strbuf_append_number(strbuf_t *s, double number)
+{
+ int len;
+
+ /* Lowest double printed with %.14g is 21 characters long:
+ * -1.7976931348623e+308
+ *
+ * Use 32 to include the \0, and a few extra just in case..
+ */
+ strbuf_ensure_empty_length(s, 32);
+
+ len = sprintf(s->buf + s->length, "%.14g", number);
+ if (len < 0)
+ die("BUG: Unable to convert number"); /* This should never happen.. */
+
+ s->length += len;
+}
+
void strbuf_append_fmt(strbuf_t *s, const char *fmt, ...)
{
va_list arg;