forked from github/server
also remove trailing spaces.
This commit is contained in:
parent
97e91fd8eb
commit
e0add2275f
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue