Smaller sizeof for in place strings

This commit is contained in:
Maxime Coste 2015-08-18 21:06:53 +01:00
parent 386f41e1c5
commit a33c8d9677
2 changed files with 20 additions and 18 deletions

View File

@ -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);

View File

@ -263,21 +263,23 @@ Optional<int> str_to_int_ifp(StringView str);
template<size_t N>
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<typename RealType, typename ValueType>
decltype(to_string(std::declval<ValueType>()))