1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-09-11 13:00:41 +03:00

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];
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
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;
}
else

View File

@ -29,7 +29,7 @@ String to_json(StringView str)
char buf[7] = {'\\', *next, 0};
if (*next >= 0 and *next <= 0x1F)
sprintf(buf, "\\u%04x", *next);
format_to(buf, "\\u{:04}", hex(*next));
res += buf;
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 JsonObject = HashMap<String, Value>;
String to_json(int i);
String to_json(bool b);
String to_json(StringView str);
template<typename T>
String to_json(ArrayView<const T> array)
{
@ -27,10 +31,6 @@ String to_json(const HashMap<K, V, D>& map)
',', false) + "}";
}
String to_json(int i);
String to_json(bool b);
String to_json(StringView str);
struct JsonResult { Value value; const char* new_pos; };
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)
{
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 to_json(to_string(color));

View File

@ -1080,7 +1080,7 @@ String dump_regex(const CompiledRegex& program)
for (auto& inst : program.instructions)
{
char buf[20];
sprintf(buf, " %03d ", count++);
format_to(buf, " {:03} ", count++);
res += buf;
switch (inst.op)
{
@ -1148,7 +1148,7 @@ String dump_regex(const CompiledRegex& program)
if (desc.map[c])
{
if (c < 32)
res += format("<0x{}>", Hex{c});
res += format("<0x{}>", hex(c));
else
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);
#else
InplaceString<23> res;
res.m_length = sprintf(res.m_data, "%f", val);
res.m_length = snprintf(res.m_data, 23, "%f", val);
return res;
#endif
}
@ -360,9 +360,15 @@ void format_impl(StringView fmt, ArrayView<const StringView> params, AppendFunc
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)
append(' ');
append(padding);
}
append(params[index]);
@ -475,7 +481,7 @@ UnitTest test_string{[]()
kak_assert(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];
kak_assert(format_to(buffer, "Hey {}", 15) == "Hey 15");