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;
@ -53,13 +53,18 @@ int unicode_utf8_trim(utf8_t *buf)
memcpy(op, ip, size);
}
op += size;
if (iswspace(ucs)) ++ts;
else {
lc = op;
ts = 0;
}
} else {
++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);