Cleanup: clang-tidy utfconv
This commit is contained in:
parent
93c6b12df5
commit
e04631f44a
|
@ -74,8 +74,9 @@ size_t count_utf_16_from_8(const char *string8)
|
|||
char type = 0;
|
||||
unsigned int u32 = 0;
|
||||
|
||||
if (!string8)
|
||||
if (!string8) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (; (u = *string8); string8++) {
|
||||
if (type == 0) {
|
||||
|
@ -101,22 +102,22 @@ size_t count_utf_16_from_8(const char *string8)
|
|||
} // 4 utf-8 char
|
||||
continue;
|
||||
}
|
||||
if ((u & 0xC0) == 0x80) {
|
||||
u32 = (u32 << 6) | (u & 0x3F);
|
||||
type--;
|
||||
}
|
||||
else {
|
||||
if ((u & 0xC0) == 0x80) {
|
||||
u32 = (u32 << 6) | (u & 0x3F);
|
||||
type--;
|
||||
}
|
||||
else {
|
||||
u32 = 0;
|
||||
type = 0;
|
||||
}
|
||||
u32 = 0;
|
||||
type = 0;
|
||||
}
|
||||
|
||||
if (type == 0) {
|
||||
if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000))
|
||||
if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) {
|
||||
count++;
|
||||
else if (0x10000 <= u32 && u32 < 0x110000)
|
||||
}
|
||||
else if (0x10000 <= u32 && u32 < 0x110000) {
|
||||
count += 2;
|
||||
}
|
||||
u32 = 0;
|
||||
}
|
||||
}
|
||||
|
@ -129,8 +130,9 @@ int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
|
|||
char *out8end = out8 + size8;
|
||||
wchar_t u = 0;
|
||||
int err = 0;
|
||||
if (!size8 || !in16 || !out8)
|
||||
if (!size8 || !in16 || !out8) {
|
||||
return UTF_ERROR_NULL_IN;
|
||||
}
|
||||
out8end--;
|
||||
|
||||
for (; out8 < out8end && (u = *in16); in16++, out8++) {
|
||||
|
@ -138,14 +140,16 @@ int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
|
|||
*out8 = u;
|
||||
}
|
||||
else if (u < 0x0800) {
|
||||
if (out8 + 1 >= out8end)
|
||||
if (out8 + 1 >= out8end) {
|
||||
break;
|
||||
}
|
||||
*out8++ = (0x3 << 6) | (0x1F & (u >> 6));
|
||||
*out8 = (0x1 << 7) | (0x3F & (u));
|
||||
}
|
||||
else if (u < 0xD800 || u >= 0xE000) {
|
||||
if (out8 + 2 >= out8end)
|
||||
if (out8 + 2 >= out8end) {
|
||||
break;
|
||||
}
|
||||
*out8++ = (0x7 << 5) | (0xF & (u >> 12));
|
||||
*out8++ = (0x1 << 7) | (0x3F & (u >> 6));
|
||||
*out8 = (0x1 << 7) | (0x3F & (u));
|
||||
|
@ -153,19 +157,19 @@ int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
|
|||
else if (u < 0xDC00) {
|
||||
wchar_t u2 = *++in16;
|
||||
|
||||
if (!u2)
|
||||
if (!u2) {
|
||||
break;
|
||||
}
|
||||
if (u2 >= 0xDC00 && u2 < 0xE000) {
|
||||
if (out8 + 3 >= out8end)
|
||||
if (out8 + 3 >= out8end) {
|
||||
break;
|
||||
else {
|
||||
unsigned int uc = 0x10000 + (u2 - 0xDC00) + ((u - 0xD800) << 10);
|
||||
|
||||
*out8++ = (0xF << 4) | (0x7 & (uc >> 18));
|
||||
*out8++ = (0x1 << 7) | (0x3F & (uc >> 12));
|
||||
*out8++ = (0x1 << 7) | (0x3F & (uc >> 6));
|
||||
*out8 = (0x1 << 7) | (0x3F & (uc));
|
||||
}
|
||||
unsigned int uc = 0x10000 + (u2 - 0xDC00) + ((u - 0xD800) << 10);
|
||||
|
||||
*out8++ = (0xF << 4) | (0x7 & (uc >> 18));
|
||||
*out8++ = (0x1 << 7) | (0x3F & (uc >> 12));
|
||||
*out8++ = (0x1 << 7) | (0x3F & (uc >> 6));
|
||||
*out8 = (0x1 << 7) | (0x3F & (uc));
|
||||
}
|
||||
else {
|
||||
out8--;
|
||||
|
@ -180,8 +184,9 @@ int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8)
|
|||
|
||||
*out8 = *out8end = 0;
|
||||
|
||||
if (*in16)
|
||||
if (*in16) {
|
||||
err |= UTF_ERROR_SMALL;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
@ -193,8 +198,9 @@ int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
|
|||
unsigned int u32 = 0;
|
||||
wchar_t *out16end = out16 + size16;
|
||||
int err = 0;
|
||||
if (!size16 || !in8 || !out16)
|
||||
if (!size16 || !in8 || !out16) {
|
||||
return UTF_ERROR_NULL_IN;
|
||||
}
|
||||
out16end--;
|
||||
|
||||
for (; out16 < out16end && (u = *in8); in8++) {
|
||||
|
@ -223,25 +229,25 @@ int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
|
|||
err |= UTF_ERROR_ILLCHAR;
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
if ((u & 0xC0) == 0x80) {
|
||||
u32 = (u32 << 6) | (u & 0x3F);
|
||||
type--;
|
||||
}
|
||||
else {
|
||||
u32 = 0;
|
||||
type = 0;
|
||||
err |= UTF_ERROR_ILLSEQ;
|
||||
}
|
||||
if ((u & 0xC0) == 0x80) {
|
||||
u32 = (u32 << 6) | (u & 0x3F);
|
||||
type--;
|
||||
}
|
||||
else {
|
||||
u32 = 0;
|
||||
type = 0;
|
||||
err |= UTF_ERROR_ILLSEQ;
|
||||
}
|
||||
|
||||
if (type == 0) {
|
||||
if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) {
|
||||
*out16 = u32;
|
||||
out16++;
|
||||
}
|
||||
else if (0x10000 <= u32 && u32 < 0x110000) {
|
||||
if (out16 + 1 >= out16end)
|
||||
if (out16 + 1 >= out16end) {
|
||||
break;
|
||||
}
|
||||
u32 -= 0x10000;
|
||||
*out16 = 0xD800 + (u32 >> 10);
|
||||
out16++;
|
||||
|
@ -254,8 +260,9 @@ int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16)
|
|||
|
||||
*out16 = *out16end = 0;
|
||||
|
||||
if (*in8)
|
||||
if (*in8) {
|
||||
err |= UTF_ERROR_SMALL;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
@ -286,8 +293,9 @@ char *alloc_utf_8_from_16(const wchar_t *in16, size_t add)
|
|||
{
|
||||
size_t bsize = count_utf_8_from_16(in16);
|
||||
char *out8 = NULL;
|
||||
if (!bsize)
|
||||
if (!bsize) {
|
||||
return NULL;
|
||||
}
|
||||
out8 = (char *)malloc(sizeof(char) * (bsize + add));
|
||||
conv_utf_16_to_8(in16, out8, bsize);
|
||||
return out8;
|
||||
|
@ -297,8 +305,9 @@ wchar_t *alloc_utf16_from_8(const char *in8, size_t add)
|
|||
{
|
||||
size_t bsize = count_utf_16_from_8(in8);
|
||||
wchar_t *out16 = NULL;
|
||||
if (!bsize)
|
||||
if (!bsize) {
|
||||
return NULL;
|
||||
}
|
||||
out16 = (wchar_t *)malloc(sizeof(wchar_t) * (bsize + add));
|
||||
conv_utf_8_to_16(in8, out16, bsize);
|
||||
return out16;
|
||||
|
|
Loading…
Reference in New Issue