also remove trailing spaces.

This commit is contained in:
Enno Rehling 2016-11-11 01:34:59 +01:00
parent 97e91fd8eb
commit e0add2275f
2 changed files with 19 additions and 10 deletions

View File

@ -34,8 +34,8 @@
int unicode_utf8_trim(utf8_t *buf) int unicode_utf8_trim(utf8_t *buf)
{ {
int result = 0; int result = 0, ts = 0;
utf8_t *op = buf, *ip = buf; utf8_t *op = buf, *ip = buf, *lc = buf;
while (*ip) { while (*ip) {
ucs4_t ucs = *ip; ucs4_t ucs = *ip;
size_t size = 1; size_t size = 1;
@ -46,20 +46,25 @@ int unicode_utf8_trim(utf8_t *buf)
} }
} }
if (op == buf && iswspace(ucs)) { if (op == buf && iswspace(ucs)) {
++result; ++result;
} }
else if (iswprint(ucs)) { else if (iswprint(ucs)) {
if (op != ip) { if (op != ip) {
memcpy(op, ip, size); memcpy(op, ip, size);
} }
op += size; op += size;
if (iswspace(ucs)) ++ts;
else {
lc = op;
ts = 0;
}
} else { } else {
++result; ++result;
} }
ip += size; ip += size;
} }
*op = '\0'; *lc = '\0';
return result; return result + ts;
} }
int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip) int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip)

View File

@ -13,6 +13,10 @@ static void test_unicode_trim(CuTest * tc)
CuAssertIntEquals(tc, 0, unicode_utf8_trim(buffer)); CuAssertIntEquals(tc, 0, unicode_utf8_trim(buffer));
CuAssertStrEquals(tc, "Hello Word", 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"); strcpy(buffer, "Hello Word\n");
CuAssertIntEquals(tc, 1, unicode_utf8_trim(buffer)); CuAssertIntEquals(tc, 1, unicode_utf8_trim(buffer));
CuAssertStrEquals(tc, "Hello Word", buffer); CuAssertStrEquals(tc, "Hello Word", buffer);