return an error code if the string had any characters removed.

This commit is contained in:
Enno Rehling 2016-11-09 14:58:51 +01:00
parent 7d79822aa4
commit f03e8f3b62
2 changed files with 9 additions and 3 deletions

View File

@ -34,13 +34,14 @@
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)
{ {
int ret = 0;
while (*ip) { while (*ip) {
ucs4_t ucs = *ip; ucs4_t ucs = *ip;
size_t size = 1; size_t size = 1;
bool isp = false; bool isp = false;
// bool iss = false; // bool iss = false;
if (ucs & 0x80) { if (ucs & 0x80) {
int ret = unicode_utf8_to_ucs4(&ucs, ip, &size); ret = unicode_utf8_to_ucs4(&ucs, ip, &size);
if (ret !=0) { if (ret !=0) {
return ret; return ret;
} }
@ -57,6 +58,8 @@ int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip)
memcpy(op, ip, size); memcpy(op, ip, size);
op += size; op += size;
outlen -= size; outlen -= size;
} else {
ret = 1;
} }
ip += size; ip += size;
} }
@ -64,7 +67,7 @@ int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip)
return ENOMEM; return ENOMEM;
} }
*op = 0; *op = 0;
return 0; return ret;
} }
int unicode_utf8_tolower(utf8_t * op, size_t outlen, const utf8_t * ip) int unicode_utf8_tolower(utf8_t * op, size_t outlen, const utf8_t * ip)

View File

@ -8,7 +8,10 @@
static void test_unicode_mkname(CuTest * tc) static void test_unicode_mkname(CuTest * tc)
{ {
char buffer[32]; char buffer[32];
CuAssertIntEquals(tc, 0, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO\nW0Rld")); CuAssertIntEquals(tc, 0, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO W0Rld"));
CuAssertStrEquals(tc, "HeLlO W0Rld", buffer);
memset(buffer, 0, sizeof(buffer));
CuAssertIntEquals(tc, 1, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO\nW0Rld"));
CuAssertStrEquals(tc, "HeLlOW0Rld", buffer); CuAssertStrEquals(tc, "HeLlOW0Rld", buffer);
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
buffer[5] = 'X'; buffer[5] = 'X';