From f9c3f741172374599a6bb4749d188eeae52a42c3 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Sun, 7 Jun 2020 12:16:23 +0200 Subject: [PATCH] Bug 2670 trim   and others from strings. --- src/util/unicode.c | 2 +- src/util/unicode.test.c | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/util/unicode.c b/src/util/unicode.c index e87c27fc3..c7fada910 100644 --- a/src/util/unicode.c +++ b/src/util/unicode.c @@ -25,7 +25,7 @@ #define B00000001 0x01 static bool char_trimmed(wint_t wc) { - if (wc >= 0x2000 && wc <= 0x200f) { + if (wc == 0xa0 || wc == 0x202f || (wc >= 0x2000 && wc <= 0x200f)) { /* only weird stuff here */ return true; } diff --git a/src/util/unicode.test.c b/src/util/unicode.test.c index 8a5d77488..f746c77da 100644 --- a/src/util/unicode.test.c +++ b/src/util/unicode.test.c @@ -154,7 +154,7 @@ static void test_unicode_compare(CuTest *tc) } static void test_unicode_trim_zwnj(CuTest *tc) { - const char zwnj[] = { 0xe2, 0x80, 0x8c, 0x00 }; + const char zwnj[] = { 0xe2, 0x80, 0x8c, 0 }; char name[64]; char expect[64]; snprintf(name, sizeof(name), "%sA%sB%s ", zwnj, zwnj, zwnj); @@ -163,8 +163,38 @@ static void test_unicode_trim_zwnj(CuTest *tc) { CuAssertStrEquals(tc, expect, name); } +static void test_unicode_trim_nbsp(CuTest *tc) { + const char code[] = { 0xc2, 0xa0, 0 }; + char name[64]; + char expect[64]; + snprintf(name, sizeof(name), "%sA%sB%s ", code, code, code); + snprintf(expect, sizeof(expect), "A%sB", code); + CuAssertIntEquals(tc, 6, unicode_utf8_trim(name)); + CuAssertStrEquals(tc, expect, name); +} + +static void test_unicode_trim_nnbsp(CuTest *tc) { + const char code[] = { 0xe2, 0x80, 0xaf, 0 }; + char name[64]; + char expect[64]; + snprintf(name, sizeof(name), "%sA%sB%s ", code, code, code); + snprintf(expect, sizeof(expect), "A%sB", code); + CuAssertIntEquals(tc, 8, unicode_utf8_trim(name)); + CuAssertStrEquals(tc, expect, name); +} + +static void test_unicode_trim_figure_space(CuTest *tc) { + const char code[] = { 0xe2, 0x80, 0x87, 0 }; + char name[64]; + char expect[64]; + snprintf(name, sizeof(name), "%sA%sB%s ", code, code, code); + snprintf(expect, sizeof(expect), "A%sB", code); + CuAssertIntEquals(tc, 8, unicode_utf8_trim(name)); + CuAssertStrEquals(tc, expect, name); +} + static void test_unicode_trim_ltrm(CuTest *tc) { - const char ltrm[] = { 0xe2, 0x80, 0x8e, 0x00 }; + const char ltrm[] = { 0xe2, 0x80, 0x8e, 0 }; char name[64]; char expect[64]; snprintf(name, sizeof(name), "%sBrot%szeit%s ", ltrm, ltrm, ltrm); @@ -188,6 +218,9 @@ CuSuite *get_unicode_suite(void) CuSuite *suite = CuSuiteNew(); SUITE_ADD_TEST(suite, test_unicode_trim); SUITE_ADD_TEST(suite, test_unicode_trim_zwnj); + SUITE_ADD_TEST(suite, test_unicode_trim_nbsp); + SUITE_ADD_TEST(suite, test_unicode_trim_nnbsp); + SUITE_ADD_TEST(suite, test_unicode_trim_figure_space); SUITE_ADD_TEST(suite, test_unicode_trim_ltrm); SUITE_ADD_TEST(suite, test_unicode_trim_emoji); SUITE_ADD_TEST(suite, test_unicode_utf8_to_other);