Add support for 0-padding in format and replace uses of sprintf

This commit is contained in:
Maxime Coste 2023-11-03 20:27:41 +11:00
parent 7577fa1b66
commit f7499ccf45
6 changed files with 27 additions and 14 deletions

View File

@ -71,9 +71,9 @@ String to_string(Color color)
{ {
char buffer[14]; char buffer[14];
if (color.a == 255) if (color.a == 255)
sprintf(buffer, "rgb:%02x%02x%02x", color.r, color.g, color.b); format_to(buffer, "rgb:{:02}{:02}{:02}", hex(color.r), hex(color.g), hex(color.b));
else else
sprintf(buffer, "rgba:%02x%02x%02x%02x", color.r, color.g, color.b, color.a); format_to(buffer, "rgba:{:02}{:02}{:02}{:02}", hex(color.r), hex(color.g), hex(color.b), hex(color.a));
return buffer; return buffer;
} }
else else

View File

@ -29,7 +29,7 @@ String to_json(StringView str)
char buf[7] = {'\\', *next, 0}; char buf[7] = {'\\', *next, 0};
if (*next >= 0 and *next <= 0x1F) if (*next >= 0 and *next <= 0x1F)
sprintf(buf, "\\u%04x", *next); format_to(buf, "\\u{:04}", hex(*next));
res += buf; res += buf;
it = next+1; it = next+1;
@ -188,4 +188,11 @@ UnitTest test_json_parser{[]()
} }
}}; }};
UnitTest test_to_json{[]()
{
kak_assert(to_json(true) == "true");
kak_assert(to_json(false) == "false");
kak_assert(to_json(HashMap<String, Vector<int>>{{"foo", {1,2,3}}, {"\033", {3, 4, 5}}}) == R"({"foo": [1, 2, 3],"\u001b": [3, 4, 5]})");
}};
} }

View File

@ -11,6 +11,10 @@ namespace Kakoune
using JsonArray = Vector<Value>; using JsonArray = Vector<Value>;
using JsonObject = HashMap<String, Value>; using JsonObject = HashMap<String, Value>;
String to_json(int i);
String to_json(bool b);
String to_json(StringView str);
template<typename T> template<typename T>
String to_json(ArrayView<const T> array) String to_json(ArrayView<const T> array)
{ {
@ -27,10 +31,6 @@ String to_json(const HashMap<K, V, D>& map)
',', false) + "}"; ',', false) + "}";
} }
String to_json(int i);
String to_json(bool b);
String to_json(StringView str);
struct JsonResult { Value value; const char* new_pos; }; struct JsonResult { Value value; const char* new_pos; };
JsonResult parse_json(const char* pos, const char* end); JsonResult parse_json(const char* pos, const char* end);

View File

@ -27,7 +27,7 @@ String to_json(Color color)
if (color.color == Kakoune::Color::RGB) if (color.color == Kakoune::Color::RGB)
{ {
char buffer[10]; char buffer[10];
sprintf(buffer, R"("#%02x%02x%02x")", color.r, color.g, color.b); format_to(buffer, R"("#{:02}{:02}{:02}")", hex(color.r), hex(color.g), hex(color.b));
return buffer; return buffer;
} }
return to_json(to_string(color)); return to_json(to_string(color));

View File

@ -1080,7 +1080,7 @@ String dump_regex(const CompiledRegex& program)
for (auto& inst : program.instructions) for (auto& inst : program.instructions)
{ {
char buf[20]; char buf[20];
sprintf(buf, " %03d ", count++); format_to(buf, " {:03} ", count++);
res += buf; res += buf;
switch (inst.op) switch (inst.op)
{ {
@ -1148,7 +1148,7 @@ String dump_regex(const CompiledRegex& program)
if (desc.map[c]) if (desc.map[c])
{ {
if (c < 32) if (c < 32)
res += format("<0x{}>", Hex{c}); res += format("<0x{}>", hex(c));
else else
res += (char)c; res += (char)c;
} }

View File

@ -215,7 +215,7 @@ InplaceString<23> to_string(float val)
return to_string_impl<23>(val, std::chars_format::general); return to_string_impl<23>(val, std::chars_format::general);
#else #else
InplaceString<23> res; InplaceString<23> res;
res.m_length = sprintf(res.m_data, "%f", val); res.m_length = snprintf(res.m_data, 23, "%f", val);
return res; return res;
#endif #endif
} }
@ -360,9 +360,15 @@ void format_impl(StringView fmt, ArrayView<const StringView> params, AppendFunc
if (format != closing) if (format != closing)
{ {
for (ColumnCount width = str_to_int({format+1, closing}), len = params[index].column_length(); char padding = ' ';
if (*(++format) == '0')
{
padding = '0';
++format;
}
for (ColumnCount width = str_to_int({format, closing}), len = params[index].column_length();
width > len; --width) width > len; --width)
append(' '); append(padding);
} }
append(params[index]); append(params[index]);
@ -475,7 +481,7 @@ UnitTest test_string{[]()
kak_assert(subsequence_match("tchou kanaky", "tchou kanaky")); kak_assert(subsequence_match("tchou kanaky", "tchou kanaky"));
kak_assert(not subsequence_match("tchou kanaky", "tchou kanaky")); kak_assert(not subsequence_match("tchou kanaky", "tchou kanaky"));
kak_assert(format("Youhou {1} {} '{0:4}' \\{}", 10, "hehe", 5) == "Youhou hehe 5 ' 10' {}"); kak_assert(format("Youhou {1} {} '{0:4}' {2:04} \\{}", 10, "hehe", 5) == "Youhou hehe 5 ' 10' 0005 {}");
char buffer[20]; char buffer[20];
kak_assert(format_to(buffer, "Hey {}", 15) == "Hey 15"); kak_assert(format_to(buffer, "Hey {}", 15) == "Hey 15");