From 84336cabef7f26998616ee64920c9e8ddae236d8 Mon Sep 17 00:00:00 2001 From: q66 Date: Thu, 28 May 2015 01:44:21 +0100 Subject: [PATCH] tostring cleanup --- octa/string.h | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/octa/string.h b/octa/string.h index bd9afa0..212818b 100644 --- a/octa/string.h +++ b/octa/string.h @@ -166,18 +166,21 @@ namespace octa { String operator()(const T &) { return ""; } }; - template - void __octa_str_printf(S *s, const T *fmt, U v) { + template + void __octa_str_printf(Vector *s, const char *fmt, T v) { char buf[256]; - auto p_buf = (Vector *)s; - size_t n = snprintf(buf, sizeof(buf), fmt, v); - p_buf->clear(); - p_buf->reserve(n + 1); - if (n >= sizeof(buf)) - snprintf(p_buf->data(), n + 1, fmt, v); - else - memcpy(p_buf->data(), buf, n + 1); - *(((size_t *)p_buf) + 1) = n + 1; + int n = snprintf(buf, sizeof(buf), fmt, v); + s->clear(); + s->reserve(n + 1); + if (n >= (int)sizeof(buf)) + snprintf(s->data(), n + 1, fmt, v); + else if (n > 0) + memcpy(s->data(), buf, n + 1); + else { + n = 0; + *(s->data()) = '\0'; + } + *(((size_t *)s) + 1) = n + 1; } template<> struct ToString { @@ -196,7 +199,7 @@ namespace octa { typedef String ResultType; \ String operator()(T v) { \ String ret; \ - __octa_str_printf(&ret, fmt, v); \ + __octa_str_printf((Vector *)&ret, fmt, v); \ return move(ret); \ } \ };