summaryrefslogtreecommitdiff
path: root/src/string_utils.cc
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2023-11-03 20:27:41 +1100
committerMaxime Coste <mawww@kakoune.org>2023-11-03 20:27:41 +1100
commitf7499ccf45315a57b65f7c176ba23a8265ae01cf (patch)
treef52003b20b462c00e50faa2dee37997eb11bbd17 /src/string_utils.cc
parent7577fa1b668ea81eb9b7b9af690a4161187129dd (diff)
Add support for 0-padding in format and replace uses of sprintf
Diffstat (limited to 'src/string_utils.cc')
-rw-r--r--src/string_utils.cc14
1 files changed, 10 insertions, 4 deletions
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<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");