From aea5f90ad38bb69e7ef4346c036815aaa16f0ab3 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 22 Jun 2017 13:37:38 +0100 Subject: Rewrite validateUTF8 using if/else --- libdevcore/UTF8.cpp | 77 ++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 39 deletions(-) (limited to 'libdevcore') diff --git a/libdevcore/UTF8.cpp b/libdevcore/UTF8.cpp index affe64b2..2ae720ec 100644 --- a/libdevcore/UTF8.cpp +++ b/libdevcore/UTF8.cpp @@ -33,41 +33,48 @@ namespace /// Validate byte sequence against Unicode chapter 3 Table 3-7. bool isWellFormed(unsigned char byte1, unsigned char byte2) { - switch (byte1) - { - case 0xc0 ... 0xc1: + if (byte1 == 0xc0 || byte1 == 0xc1) return false; - case 0xc2 ... 0xdf: - break; - case 0xe0: + else if (byte1 >= 0xc2 && byte1 <= 0xdf) + return true; + else if (byte1 == 0xe0) + { if (byte2 < 0xa0) return false; - break; - case 0xe1 ... 0xec: - break; - case 0xed: + else + return true; + } + else if (byte1 >= 0xe1 && byte1 <= 0xec) + return true; + else if (byte1 == 0xed) + { if (byte2 > 0x9f) return false; - break; - case 0xee ... 0xef: - break; - case 0xf0: + else + return true; + } + else if (byte1 == 0xee || byte1 == 0xef) + return true; + else if (byte1 == 0xf0) + { if (byte2 < 0x90) return false; - break; - case 0xf1 ... 0xf3: - break; - case 0xf4: + else + return true; + } + else if (byte1 >= 0xf1 && byte1 <= 0xf3) + return true; + else if (byte1 == 0xf4) + { if (byte2 > 0x8f) return false; - break; - case 0xf5 ... 0xf7: - default: - /// Technically anything below 0xc0 or above 0xf7 is - /// not possible to encode using Table 3-6 anyway. - return false; + else + return true; } - 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) @@ -82,20 +89,12 @@ bool validateUTF8(const unsigned char *_input, size_t _length, size_t& _invalidP continue; size_t count = 0; - switch ((unsigned char)_input[i]) - { - case 0xc0 ... 0xdf: - count = 1; - break; - case 0xe0 ... 0xef: - count = 2; - break; - case 0xf0 ... 0xf7: - count = 3; - break; - default: - break; - } + if (_input[i] >= 0xc0 && _input[i] <= 0xdf) + count = 1; + else if (_input[i] >= 0xe0 && _input[i] <= 0xef) + count = 2; + else if (_input[i] >= 0xf0 && _input[i] <= 0xf7) + count = 3; if (count == 0) { -- cgit v1.2.3