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 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)

View File

@ -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);