From a33c8d9677ae81375f8e9567efc5b3e14ae026f5 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 18 Aug 2015 21:06:53 +0100 Subject: [PATCH] Smaller sizeof for in place strings --- src/string.cc | 20 ++++++++++---------- src/string.hh | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/string.cc b/src/string.cc index d96899382..682a69476 100644 --- a/src/string.cc +++ b/src/string.cc @@ -138,37 +138,37 @@ int str_to_int(StringView str) throw runtime_error{str + " is not a number"}; } -InplaceString<16> to_string(int val) +InplaceString<15> to_string(int val) { - InplaceString<16> res; + InplaceString<15> res; res.m_length = sprintf(res.m_data, "%i", val); return res; } -InplaceString<24> to_string(size_t val) +InplaceString<23> to_string(size_t val) { - InplaceString<24> res; + InplaceString<23> res; res.m_length = sprintf(res.m_data, "%zu", val); return res; } -InplaceString<24> to_string(Hex val) +InplaceString<23> to_string(Hex val) { - InplaceString<24> res; + InplaceString<23> res; res.m_length = sprintf(res.m_data, "%zx", val.val); return res; } -InplaceString<24> to_string(float val) +InplaceString<23> to_string(float val) { - InplaceString<24> res; + InplaceString<23> res; res.m_length = sprintf(res.m_data, "%f", val); return res; } -InplaceString<8> to_string(Codepoint c) +InplaceString<7> to_string(Codepoint c) { - InplaceString<8> res; + InplaceString<7> res; char* ptr = res.m_data; utf8::dump(ptr, c); res.m_length = (int)(ptr - res.m_data); diff --git a/src/string.hh b/src/string.hh index c853ebe48..6970e74a8 100644 --- a/src/string.hh +++ b/src/string.hh @@ -263,21 +263,23 @@ Optional str_to_int_ifp(StringView str); template struct InplaceString { - constexpr operator StringView() const { return {m_data, m_length}; } - operator String() const { return {m_data, m_length}; } + static_assert(N < 256, "InplaceString cannot handle sizes >= 256"); - ByteCount m_length; + constexpr operator StringView() const { return {m_data, ByteCount{m_length}}; } + operator String() const { return {m_data, ByteCount{m_length}}; } + + unsigned char m_length; char m_data[N]; }; struct Hex { size_t val; }; inline Hex hex(size_t val) { return {val}; } -InplaceString<16> to_string(int val); -InplaceString<24> to_string(size_t val); -InplaceString<24> to_string(Hex val); -InplaceString<24> to_string(float val); -InplaceString<8> to_string(Codepoint c); +InplaceString<15> to_string(int val); +InplaceString<23> to_string(size_t val); +InplaceString<23> to_string(Hex val); +InplaceString<23> to_string(float val); +InplaceString<7> to_string(Codepoint c); template decltype(to_string(std::declval()))