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-29 12:33:22 +0400
committerMark Pulford <mark@kyne.com.au>2011-05-29 12:33:22 +0400
commit3d1c5e19f45cf484774926ba6e2555d1c8e4c39b (patch)
treef6cd7cc888ec643d025a37810fe7f15cc91d7381 /strbuf.c
parentc0b473a8e974407dc308ce0fd0058136b9faa90c (diff)
Add support for runtime number precision config
Add cjson.encode_number_precision(). Reducing the number precision from 14 to 3 can increase performance up to 50% with number heavy conversions.
Diffstat (limited to 'strbuf.c')
-rw-r--r--strbuf.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/strbuf.c b/strbuf.c
index c59b6f6..976925a 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -197,25 +197,28 @@ void strbuf_append_string(strbuf_t *s, const char *str)
}
}
-void strbuf_append_number(strbuf_t *s, double number)
+/* strbuf_append_fmt() should only be used when an upper bound
+ * is known for the output string. */
+void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...)
{
- int len;
+ va_list arg;
+ int fmt_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);
+ strbuf_ensure_empty_length(s, len);
- len = sprintf(s->buf + s->length, "%.14g", number);
- if (len < 0)
+ va_start(arg, fmt);
+ fmt_len = vsnprintf(s->buf + s->length, len, fmt, arg);
+ va_end(arg);
+
+ if (fmt_len < 0)
die("BUG: Unable to convert number"); /* This should never happen.. */
- s->length += len;
+ s->length += fmt_len;
}
-void strbuf_append_fmt(strbuf_t *s, const char *fmt, ...)
+/* strbuf_append_fmt_retry() can be used when the there is no known
+ * upper bound for the output string. */
+void strbuf_append_fmt_retry(strbuf_t *s, const char *fmt, ...)
{
va_list arg;
int fmt_len, try;