From e0add2275ffea20eec0c7af3369eb51ccd2bf498 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Fri, 11 Nov 2016 01:34:59 +0100 Subject: [PATCH] also remove trailing spaces. --- src/util/unicode.c | 25 +++++++++++++++---------- src/util/unicode.test.c | 4 ++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/util/unicode.c b/src/util/unicode.c index 15b914fac..0e6aedb9f 100644 --- a/src/util/unicode.c +++ b/src/util/unicode.c @@ -34,8 +34,8 @@ int unicode_utf8_trim(utf8_t *buf) { - int result = 0; - utf8_t *op = buf, *ip = buf; + int result = 0, ts = 0; + utf8_t *op = buf, *ip = buf, *lc = buf; while (*ip) { ucs4_t ucs = *ip; size_t size = 1; @@ -46,20 +46,25 @@ int unicode_utf8_trim(utf8_t *buf) } } if (op == buf && iswspace(ucs)) { - ++result; - } + ++result; + } else if (iswprint(ucs)) { - if (op != ip) { - memcpy(op, ip, size); + if (op != ip) { + memcpy(op, ip, size); } op += size; + if (iswspace(ucs)) ++ts; + else { + lc = op; + ts = 0; + } } else { - ++result; - } + ++result; + } ip += size; } - *op = '\0'; - return result; + *lc = '\0'; + return result + ts; } int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip) diff --git a/src/util/unicode.test.c b/src/util/unicode.test.c index db3fb0ccf..167538a3f 100644 --- a/src/util/unicode.test.c +++ b/src/util/unicode.test.c @@ -13,6 +13,10 @@ static void test_unicode_trim(CuTest * tc) CuAssertIntEquals(tc, 0, unicode_utf8_trim(buffer)); CuAssertStrEquals(tc, "Hello Word", buffer); + strcpy(buffer, " Hello Word "); + CuAssertIntEquals(tc, 4, unicode_utf8_trim(buffer)); + CuAssertStrEquals(tc, "Hello Word", buffer); + strcpy(buffer, "Hello Word\n"); CuAssertIntEquals(tc, 1, unicode_utf8_trim(buffer)); CuAssertStrEquals(tc, "Hello Word", buffer);