add utf8 check function

This commit is contained in:
Jinhao 2015-11-26 00:53:50 +08:00
parent 243af14da7
commit 3b8e113745
2 changed files with 33 additions and 0 deletions

View File

@ -9,6 +9,9 @@ namespace nana
utf8, utf16, utf32
};
/// Checks whether a specified text is utf8 encoding
bool is_utf8(const char* str, unsigned len);
namespace detail
{
class charset_encoding_interface;

View File

@ -30,6 +30,36 @@
namespace nana
{
bool is_utf8(const char* str, unsigned len)
{
auto ustr = reinterpret_cast<const unsigned char*>(str);
auto end = ustr + len;
while (ustr < end)
{
const auto uv = *ustr;
if (uv < 0x80)
{
++ustr;
continue;
}
if (uv < 0xC0)
return false;
if ((uv < 0xE0) && (ustr + 1 < end))
ustr += 2;
else if (uv < 0xF0 && (ustr + 2 <= end))
ustr += 3;
else if (uv < 0x1F && (ustr + 3 <= end))
ustr += 4;
else
return false;
}
return true;
}
namespace detail
{
class locale_initializer