1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
8 #include "string-util.h"
10 #include "terminal-util.h"
14 static void test_ellipsize_mem_one(const char *s
, size_t old_length
, size_t new_length
) {
15 _cleanup_free_
char *n
= NULL
;
16 _cleanup_free_
char *t1
= NULL
, *t2
= NULL
, *t3
= NULL
;
21 n
= memdup_suffix0(s
, old_length
);
23 if (!utf8_is_valid(n
))
24 /* We don't support invalid sequences… */
27 /* Report out inputs. We duplicate the data so that cellescape
28 * can properly report truncated multibyte sequences. */
29 log_info("%s \"%s\" old_length=%zu/%zu new_length=%zu", __func__
,
30 cellescape(buf
, sizeof buf
, n
),
31 old_length
, utf8_console_width(n
),
34 /* To keep this test simple, any case with wide chars starts with this glyph */
35 has_wide_chars
= startswith(s
, "你");
36 max_width
= MIN(utf8_console_width(n
), new_length
);
38 t1
= ellipsize_mem(n
, old_length
, new_length
, 30);
39 log_info("30%% → %s utf8_console_width=%zu", t1
, utf8_console_width(t1
));
41 assert_se(utf8_console_width(t1
) == max_width
);
43 assert_se(utf8_console_width(t1
) <= max_width
);
45 t2
= ellipsize_mem(n
, old_length
, new_length
, 90);
46 log_info("90%% → %s utf8_console_width=%zu", t2
, utf8_console_width(t2
));
48 assert_se(utf8_console_width(t2
) == max_width
);
50 assert_se(utf8_console_width(t2
) <= max_width
);
52 t3
= ellipsize_mem(n
, old_length
, new_length
, 100);
53 log_info("100%% → %s utf8_console_width=%zu", t3
, utf8_console_width(t3
));
55 assert_se(utf8_console_width(t3
) == max_width
);
57 assert_se(utf8_console_width(t3
) <= max_width
);
59 if (new_length
>= old_length
) {
68 "_XXXXXXXXXXX_", /* ASCII */
69 "_aąęółśćńżźć_", /* two-byte utf-8 */
70 "გამარჯობა", /* multi-byte utf-8 */
71 "你好世界", /* wide characters */
72 "你გą世óoó界") /* a mix */
73 for (ssize_t l
= strlen(s
); l
>= 0; l
--)
74 for (ssize_t k
= strlen(s
) + 1; k
>= 0; k
--)
75 test_ellipsize_mem_one(s
, l
, k
);
78 static void test_ellipsize_one(const char *p
) {
79 _cleanup_free_
char *t
= NULL
;
80 t
= ellipsize(p
, columns(), 70);
83 t
= ellipsize(p
, columns(), 0);
86 t
= ellipsize(p
, columns(), 100);
89 t
= ellipsize(p
, 0, 50);
92 t
= ellipsize(p
, 1, 50);
95 t
= ellipsize(p
, 2, 50);
98 t
= ellipsize(p
, 3, 50);
101 t
= ellipsize(p
, 4, 50);
104 t
= ellipsize(p
, 5, 50);
109 test_ellipsize_one(DIGITS LETTERS DIGITS LETTERS
);
110 test_ellipsize_one("한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어한국어");
111 test_ellipsize_one("-日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国日本国");
112 test_ellipsize_one("中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国-中国中国中国中国中国中国中国中国中国中国中国中国中国");
113 test_ellipsize_one("sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd sÿstëmd");
114 test_ellipsize_one("🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮🐮");
115 test_ellipsize_one("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
116 test_ellipsize_one("shórt");
119 TEST(ellipsize_ansi
) {
120 const char *s
= ANSI_HIGHLIGHT_YELLOW_UNDERLINE
"yęllow"
121 ANSI_HIGHLIGHT_GREY_UNDERLINE
"grěy"
122 ANSI_HIGHLIGHT_BLUE_UNDERLINE
"blue"
123 ANSI_NORMAL
"nórmął";
124 size_t len
= strlen(s
);
126 for (unsigned percent
= 0; percent
<= 100; percent
+= 15)
127 for (ssize_t x
= 21; x
>= 0; x
--) {
128 _cleanup_free_
char *t
= ellipsize_mem(s
, len
, x
, percent
);
129 printf("%02zd: \"%s\"\n", x
, t
);
130 assert_se(utf8_is_valid(t
));
133 _cleanup_free_
char *e
= cescape(t
);
134 printf(" : \"%s\"\n", e
);
139 TEST(ellipsize_ansi_cats
) {
140 _cleanup_free_
char *e
= NULL
, *f
= NULL
, *g
= NULL
, *h
= NULL
;
142 /* Make sure we don't cut off in the middle of an ANSI escape sequence. */
144 e
= ellipsize("01" ANSI_NORMAL
"23", 4, 0);
146 ASSERT_STREQ(e
, "01" ANSI_NORMAL
"23");
147 f
= ellipsize("ab" ANSI_NORMAL
"cd", 4, 90);
149 ASSERT_STREQ(f
, "ab" ANSI_NORMAL
"cd");
151 g
= ellipsize("🐱🐱" ANSI_NORMAL
"🐱🐱" ANSI_NORMAL
, 5, 0);
153 ASSERT_STREQ(g
, "…" ANSI_NORMAL
"🐱🐱" ANSI_NORMAL
);
154 h
= ellipsize("🐱🐱" ANSI_NORMAL
"🐱🐱" ANSI_NORMAL
, 5, 90);
156 ASSERT_STREQ(h
, "🐱…" ANSI_NORMAL
"🐱" ANSI_NORMAL
);
159 DEFINE_TEST_MAIN(LOG_INFO
);