diff options
author | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-05-05 12:53:53 +0200 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-05-05 13:59:23 +0200 |
commit | fc96e5c0536ae6d9d689a373b696f4fd3659f7d3 (patch) | |
tree | dcd2abb7deafeeaff8efd704f4574737e47a2455 /src/test/test-utf8.c | |
parent | basic/escape: flagsify xescape_full() (diff) | |
download | systemd-fc96e5c0536ae6d9d689a373b696f4fd3659f7d3.tar.xz systemd-fc96e5c0536ae6d9d689a373b696f4fd3659f7d3.zip |
basic/escape: allow truncation mode where "…" is always appended
So far we would append "…" or "..." when the string was wider than the specified
output width. But let's add a mode where the caller knows that the string being
passed is already truncated.
The condition for jumping back in utf8_escape_non_printable_full() was
off-by-one. But we only jumped to that label after doing a check with a
stronger condition, so I think it didn't matter. Now it matters because we'd
output the forced ellipsis one column too early.
Diffstat (limited to 'src/test/test-utf8.c')
-rw-r--r-- | src/test/test-utf8.c | 49 |
1 files changed, 23 insertions, 26 deletions
diff --git a/src/test/test-utf8.c b/src/test/test-utf8.c index 042b94634b..cdbdfcb054 100644 --- a/src/test/test-utf8.c +++ b/src/test/test-utf8.c @@ -136,32 +136,29 @@ static void test_utf8_escape_non_printable(void) { static void test_utf8_escape_non_printable_full(void) { log_info("/* %s */", __func__); - for (size_t i = 0; i < 20; i++) { - _cleanup_free_ char *p; - - p = utf8_escape_non_printable_full("goo goo goo", i); - puts(p); - assert_se(utf8_is_valid(p)); - assert_se(utf8_console_width(p) <= i); - } - - for (size_t i = 0; i < 20; i++) { - _cleanup_free_ char *p; - - p = utf8_escape_non_printable_full("\001 \019\20\a", i); - puts(p); - assert_se(utf8_is_valid(p)); - assert_se(utf8_console_width(p) <= i); - } - - for (size_t i = 0; i < 20; i++) { - _cleanup_free_ char *p; - - p = utf8_escape_non_printable_full("\xef\xbf\x30\x13", i); - puts(p); - assert_se(utf8_is_valid(p)); - assert_se(utf8_console_width(p) <= i); - } + const char *s; + FOREACH_STRING(s, + "goo goo goo", /* ASCII */ + "\001 \019\20\a", /* control characters */ + "\xef\xbf\x30\x13") /* misplaced continuation bytes followed by a digit and cc */ + for (size_t cw = 0; cw < 22; cw++) { + _cleanup_free_ char *p, *q; + size_t ew; + + p = utf8_escape_non_printable_full(s, cw, false); + ew = utf8_console_width(p); + log_debug("%02zu \"%s\" (%zu wasted)", cw, p, cw - ew); + assert_se(utf8_is_valid(p)); + assert_se(ew <= cw); + + q = utf8_escape_non_printable_full(s, cw, true); + ew = utf8_console_width(q); + log_debug(" \"%s\" (%zu wasted)", q, cw - ew); + assert_se(utf8_is_valid(q)); + assert_se(ew <= cw); + if (cw > 0) + assert_se(endswith(q, "…")); + } } static void test_utf16_to_utf8(void) { |