Rewrite validateUTF8 to use char

This commit is contained in:
Alex Beregszaszi 2017-06-22 11:24:15 +01:00
parent 6488f7e079
commit c45dbab00c

View File

@ -70,18 +70,15 @@ bool isWellFormed(unsigned char byte1, unsigned char byte2)
return true;
}
}
bool validateUTF8(std::string const& _input, size_t& _invalidPosition)
bool validateUTF8(const unsigned char *_input, size_t _length, size_t& _invalidPosition)
{
const size_t length = _input.length();
bool valid = true;
size_t i = 0;
for (; i < length; i++)
for (; i < _length; i++)
{
// Check for Unicode Chapter 3 Table 3-6 conformity.
if ((unsigned char)_input[i] < 0x80)
if (_input[i] < 0x80)
continue;
size_t count = 0;
@ -106,7 +103,7 @@ bool validateUTF8(std::string const& _input, size_t& _invalidPosition)
break;
}
if ((i + count) >= length)
if ((i + count) >= _length)
{
valid = false;
break;
@ -138,3 +135,10 @@ bool validateUTF8(std::string const& _input, size_t& _invalidPosition)
}
}
bool validateUTF8(std::string const& _input, size_t& _invalidPosition)
{
return validateUTF8(reinterpret_cast<unsigned char const*>(_input.c_str()), _input.length(), _invalidPosition);
}
}