tostring cleanup

master
Daniel Kolesa 2015-05-28 01:44:21 +01:00
parent 324bdd154c
commit 84336cabef
1 changed files with 15 additions and 12 deletions

View File

@ -166,18 +166,21 @@ namespace octa {
String operator()(const T &) { return ""; } String operator()(const T &) { return ""; }
}; };
template<typename S, typename T, typename U> template<typename T>
void __octa_str_printf(S *s, const T *fmt, U v) { void __octa_str_printf(Vector<char> *s, const char *fmt, T v) {
char buf[256]; char buf[256];
auto p_buf = (Vector<typename S::ValType> *)s; int n = snprintf(buf, sizeof(buf), fmt, v);
size_t n = snprintf(buf, sizeof(buf), fmt, v); s->clear();
p_buf->clear(); s->reserve(n + 1);
p_buf->reserve(n + 1); if (n >= (int)sizeof(buf))
if (n >= sizeof(buf)) snprintf(s->data(), n + 1, fmt, v);
snprintf(p_buf->data(), n + 1, fmt, v); else if (n > 0)
else memcpy(s->data(), buf, n + 1);
memcpy(p_buf->data(), buf, n + 1); else {
*(((size_t *)p_buf) + 1) = n + 1; n = 0;
*(s->data()) = '\0';
}
*(((size_t *)s) + 1) = n + 1;
} }
template<> struct ToString<char> { template<> struct ToString<char> {
@ -196,7 +199,7 @@ namespace octa {
typedef String ResultType; \ typedef String ResultType; \
String operator()(T v) { \ String operator()(T v) { \
String ret; \ String ret; \
__octa_str_printf(&ret, fmt, v); \ __octa_str_printf((Vector<char> *)&ret, fmt, v); \
return move(ret); \ return move(ret); \
} \ } \
}; };