diff --git a/src/color.cc b/src/color.cc index dfe2e955..145a5381 100644 --- a/src/color.cc +++ b/src/color.cc @@ -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 diff --git a/src/json.cc b/src/json.cc index 3661ac80..82fc0f28 100644 --- a/src/json.cc +++ b/src/json.cc @@ -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>{{"foo", {1,2,3}}, {"\033", {3, 4, 5}}}) == R"({"foo": [1, 2, 3],"\u001b": [3, 4, 5]})"); +}}; + } diff --git a/src/json.hh b/src/json.hh index 1f76739c..84c14d36 100644 --- a/src/json.hh +++ b/src/json.hh @@ -11,6 +11,10 @@ namespace Kakoune using JsonArray = Vector; using JsonObject = HashMap; +String to_json(int i); +String to_json(bool b); +String to_json(StringView str); + template String to_json(ArrayView array) { @@ -27,10 +31,6 @@ String to_json(const HashMap& 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); diff --git a/src/json_ui.cc b/src/json_ui.cc index 1200ed60..01768897 100644 --- a/src/json_ui.cc +++ b/src/json_ui.cc @@ -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)); diff --git a/src/regex_impl.cc b/src/regex_impl.cc index a380e7f2..a02554c7 100644 --- a/src/regex_impl.cc +++ b/src/regex_impl.cc @@ -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; } diff --git a/src/string_utils.cc b/src/string_utils.cc index bc368978..53d2d455 100644 --- a/src/string_utils.cc +++ b/src/string_utils.cc @@ -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 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");