use memmove, not memcpy.

fix unicode_trim for windows.
remove unused unicode_mkname.
This commit is contained in:
Enno Rehling 2016-11-13 15:49:26 +01:00
parent 8bbd0e9e44
commit f63baddd51
3 changed files with 10 additions and 68 deletions

View File

@ -37,23 +37,25 @@ int unicode_utf8_trim(utf8_t *buf)
int result = 0, ts = 0;
utf8_t *op = buf, *ip = buf, *lc = buf;
while (*ip) {
ucs4_t ucs = *ip;
size_t size = 1;
if (ucs & 0x80) {
wint_t wc = *ip;
if (wc & 0x80) {
ucs4_t ucs;
int ret = unicode_utf8_to_ucs4(&ucs, ip, &size);
if (ret != 0) {
return ret;
}
wc = (wint_t)ucs;
}
if (op == buf && iswspace(ucs)) {
if (op == buf && iswspace(wc)) {
++result;
}
else if (iswprint(ucs)) {
else if (iswprint(wc)) {
if (op != ip) {
memcpy(op, ip, size);
memmove(op, ip, size);
}
op += size;
if (iswspace(ucs)) ++ts;
if (iswspace(wc)) ++ts;
else {
lc = op;
ts = 0;
@ -67,49 +69,6 @@ int unicode_utf8_trim(utf8_t *buf)
return result + ts;
}
int unicode_utf8_mkname(utf8_t * op, size_t outlen, const utf8_t * ip)
{
int ret = 0;
bool iss = true;
while (*ip) {
size_t size = 1;
bool isp = false;
do {
ucs4_t ucs = *ip;
if (ucs & 0x80) {
ret = unicode_utf8_to_ucs4(&ucs, ip, &size);
if (ret !=0) {
return ret;
}
isp = iswprint(ucs);
iss &= !!iswspace(ucs);
} else {
isp = isprint(ucs);
iss &= !!isspace(ucs);
}
if (iss) {
ip += size;
}
} while (iss);
if (size > outlen) {
return ENOMEM;
}
if (isp) {
memcpy(op, ip, size);
op += size;
outlen -= size;
} else {
ret = 1;
}
ip += size;
}
if (outlen <= 0) {
return ENOMEM;
}
*op = 0;
return ret;
}
int unicode_utf8_tolower(utf8_t * op, size_t outlen, const utf8_t * ip)
{
while (*ip) {
@ -128,7 +87,7 @@ int unicode_utf8_tolower(utf8_t * op, size_t outlen, const utf8_t * ip)
}
low = towlower((wint_t)ucs);
if (low == ucs) {
memcpy(op, ip, size);
memmove(op, ip, size);
ip += size;
op += size;
outlen -= size;

View File

@ -25,7 +25,7 @@ extern "C" {
#include <wchar.h>
#define USE_UNICODE
typedef unsigned long ucs4_t;
typedef long ucs4_t;
typedef char utf8_t;
int unicode_utf8_to_cp437(unsigned char *result, const utf8_t * utf8_string,
@ -42,7 +42,6 @@ extern "C" {
int unicode_latin1_to_utf8(utf8_t * out, size_t * outlen,
const char *in, size_t * inlen);
int unicode_utf8_tolower(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 unicode_utf8_trim(utf8_t *ip);
#ifdef __cplusplus

View File

@ -30,21 +30,6 @@ static void test_unicode_trim(CuTest * tc)
CuAssertStrEquals(tc, "Hello Word", buffer);
}
static void test_unicode_mkname(CuTest * tc)
{
char buffer[32];
CuAssertIntEquals(tc, 0, unicode_utf8_mkname(buffer, sizeof(buffer), " HeLlO W0Rld"));
CuAssertStrEquals(tc, "HeLlO W0Rld", buffer);
CuAssertIntEquals(tc, 0, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO W0Rld"));
CuAssertStrEquals(tc, "HeLlO W0Rld", buffer);
CuAssertIntEquals(tc, 1, unicode_utf8_mkname(buffer, sizeof(buffer), "HeLlO\nW0Rld"));
CuAssertStrEquals(tc, "HeLlOW0Rld", buffer);
memset(buffer, 0, sizeof(buffer));
buffer[5] = 'X';
CuAssertIntEquals(tc, ENOMEM, unicode_utf8_mkname(buffer, 5, "HeLl\n W0Rld"));
CuAssertStrEquals(tc, "HeLl X", buffer);
}
static void test_unicode_tolower(CuTest * tc)
{
char buffer[32];
@ -106,7 +91,6 @@ CuSuite *get_unicode_suite(void)
{
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_unicode_tolower);
SUITE_ADD_TEST(suite, test_unicode_mkname);
SUITE_ADD_TEST(suite, test_unicode_trim);
SUITE_ADD_TEST(suite, test_unicode_utf8_to_other);
return suite;