Rewrite validateUTF8 using if/else

This commit is contained in:
Alex Beregszaszi 2017-06-22 13:37:38 +01:00
parent c45dbab00c
commit aea5f90ad3

View File

@ -33,41 +33,48 @@ namespace
/// Validate byte sequence against Unicode chapter 3 Table 3-7. /// Validate byte sequence against Unicode chapter 3 Table 3-7.
bool isWellFormed(unsigned char byte1, unsigned char byte2) bool isWellFormed(unsigned char byte1, unsigned char byte2)
{ {
switch (byte1) if (byte1 == 0xc0 || byte1 == 0xc1)
{
case 0xc0 ... 0xc1:
return false; return false;
case 0xc2 ... 0xdf: else if (byte1 >= 0xc2 && byte1 <= 0xdf)
break; return true;
case 0xe0: else if (byte1 == 0xe0)
{
if (byte2 < 0xa0) if (byte2 < 0xa0)
return false; return false;
break; else
case 0xe1 ... 0xec: return true;
break; }
case 0xed: else if (byte1 >= 0xe1 && byte1 <= 0xec)
return true;
else if (byte1 == 0xed)
{
if (byte2 > 0x9f) if (byte2 > 0x9f)
return false; return false;
break; else
case 0xee ... 0xef: return true;
break; }
case 0xf0: else if (byte1 == 0xee || byte1 == 0xef)
return true;
else if (byte1 == 0xf0)
{
if (byte2 < 0x90) if (byte2 < 0x90)
return false; return false;
break; else
case 0xf1 ... 0xf3: return true;
break; }
case 0xf4: else if (byte1 >= 0xf1 && byte1 <= 0xf3)
return true;
else if (byte1 == 0xf4)
{
if (byte2 > 0x8f) if (byte2 > 0x8f)
return false; return false;
break; else
case 0xf5 ... 0xf7: return true;
default:
/// Technically anything below 0xc0 or above 0xf7 is
/// not possible to encode using Table 3-6 anyway.
return false;
} }
return true; /// 0xf5 .. 0xf7 is disallowed
/// Technically anything below 0xc0 or above 0xf7 is
/// not possible to encode using Table 3-6 anyway.
return false;
} }
bool validateUTF8(const unsigned char *_input, size_t _length, size_t& _invalidPosition) bool validateUTF8(const unsigned char *_input, size_t _length, size_t& _invalidPosition)
@ -82,20 +89,12 @@ bool validateUTF8(const unsigned char *_input, size_t _length, size_t& _invalidP
continue; continue;
size_t count = 0; size_t count = 0;
switch ((unsigned char)_input[i]) if (_input[i] >= 0xc0 && _input[i] <= 0xdf)
{ count = 1;
case 0xc0 ... 0xdf: else if (_input[i] >= 0xe0 && _input[i] <= 0xef)
count = 1; count = 2;
break; else if (_input[i] >= 0xf0 && _input[i] <= 0xf7)
case 0xe0 ... 0xef: count = 3;
count = 2;
break;
case 0xf0 ... 0xf7:
count = 3;
break;
default:
break;
}
if (count == 0) if (count == 0)
{ {