diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-05-17 10:55:21 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2018-05-31 14:27:07 +0200 |
commit | 8409f688589c41ac30a1d67f41b3ffab6ae175d3 (patch) | |
tree | 555d3fa10c05246fe20f79d89dbe42b42f557f71 /src/test/test-string-util.c | |
parent | basic/journal-importer: do not write non-unicode char to log (diff) | |
download | systemd-8409f688589c41ac30a1d67f41b3ffab6ae175d3.tar.xz systemd-8409f688589c41ac30a1d67f41b3ffab6ae175d3.zip |
basic/string-util: add a convenience function to cescape mostly-ascii fields
It's not supposed to be the most efficient, but instead fast and simple to use.
I kept the logic in ellipsize_mem() to use unicode ellipsis even in non-unicode
locales. I'm not quite convinced things should be this way, especially that with
this patch it'd actually be simpler to always use "…" in unicode locale and "..."
otherwise, but Lennart wanted it this way for some reason.
Diffstat (limited to '')
-rw-r--r-- | src/test/test-string-util.c | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/test/test-string-util.c b/src/test/test-string-util.c index eac12ac7af..413adfda7d 100644 --- a/src/test/test-string-util.c +++ b/src/test/test-string-util.c @@ -6,6 +6,7 @@ ***/ #include "alloc-util.h" +#include "locale-util.h" #include "macro.h" #include "string-util.h" #include "strv.h" @@ -77,6 +78,29 @@ static void test_ascii_strcasecmp_nn(void) { assert_se(ascii_strcasecmp_nn("BBbb", 4, "aaaa", 4) > 0); } +static void test_cellescape(void) { + char buf[40]; + + assert_se(streq(cellescape(buf, 10, "1"), "1")); + assert_se(streq(cellescape(buf, 10, "12"), "12")); + assert_se(streq(cellescape(buf, 10, "123"), is_locale_utf8() ? "1…" : "1...")); + + assert_se(streq(cellescape(buf, 10, "1\011"), "1\\t")); + assert_se(streq(cellescape(buf, 10, "1\020"), "1\\020")); + assert_se(streq(cellescape(buf, 10, "1\020x"), is_locale_utf8() ? "1…" : "1...")); + + assert_se(streq(cellescape(buf, 40, "1\020"), "1\\020")); + assert_se(streq(cellescape(buf, 40, "1\020x"), "1\\020x")); + + assert_se(streq(cellescape(buf, 40, "\a\b\f\n\r\t\v\\\"'"), "\\a\\b\\f\\n\\r\\t\\v\\\\\\\"\\'")); + assert_se(streq(cellescape(buf, 10, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a...")); + assert_se(streq(cellescape(buf, 11, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a…" : "\\a...")); + assert_se(streq(cellescape(buf, 12, "\a\b\f\n\r\t\v\\\"'"), is_locale_utf8() ? "\\a\\b…" : "\\a\\b...")); + + assert_se(streq(cellescape(buf, sizeof buf, "1\020"), "1\\020")); + assert_se(streq(cellescape(buf, sizeof buf, "1\020x"), "1\\020x")); +} + static void test_streq_ptr(void) { assert_se(streq_ptr(NULL, NULL)); assert_se(!streq_ptr("abc", "cdef")); @@ -422,6 +446,7 @@ int main(int argc, char *argv[]) { test_string_erase(); test_ascii_strcasecmp_n(); test_ascii_strcasecmp_nn(); + test_cellescape(); test_streq_ptr(); test_strstrip(); test_strextend(); |