std::string_view

This commit is contained in:
Jinhao
2018-06-07 03:41:08 +08:00
parent 549b3d4353
commit 3427181405
12 changed files with 251 additions and 44 deletions

View File

@@ -28,6 +28,42 @@
namespace nana
{
#ifdef _nana_std_has_string_view
bool is_utf8(std::string_view str)
{
auto ustr = reinterpret_cast<const unsigned char*>(str.data());
auto end = ustr + str.size();
while (ustr < end)
{
const auto uv = *ustr;
if (uv < 0x80)
{
++ustr;
continue;
}
if (uv < 0xC0)
return false;
if ((uv < 0xE0) && (end - ustr > 1))
ustr += 2;
else if ((uv < 0xF0) && (end - ustr > 2))
ustr += 3;
else if ((uv < 0x1F) && (end - ustr > 3))
ustr += 4;
else
return false;
}
return true;
}
void throw_not_utf8(std::string_view str)
{
if (!is_utf8(str))
return utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + std::string(str.data(), str.size())).emit();
}
#else
bool is_utf8(const char* str, std::size_t len)
{
auto ustr = reinterpret_cast<const unsigned char*>(str);
@@ -57,6 +93,23 @@ namespace nana
return true;
}
void throw_not_utf8(const std::string& text)
{
throw_not_utf8(text.c_str(), text.size());
}
void throw_not_utf8(const char* text)
{
throw_not_utf8(text, std::strlen(text));
}
void throw_not_utf8(const char* text, std::size_t len)
{
if (!is_utf8(text, len))
return utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + std::string(text, len)).emit();
}
#endif
//class utf8_Error
#if defined(_MSC_VER)
@@ -79,22 +132,6 @@ namespace nana
bool utf8_Error::use_throw{ false };
//end class utf8_Error
void throw_not_utf8(const std::string& text)
{
throw_not_utf8(text.c_str(), text.size());
}
void throw_not_utf8(const char* text)
{
throw_not_utf8(text, std::strlen(text));
}
void throw_not_utf8(const char* text, std::size_t len)
{
if (!is_utf8(text, len))
return utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + std::string(text, len) ).emit();
}
std::string recode_to_utf8(std::string no_utf8)
{
return nana::charset(std::move(no_utf8)).to_bytes(nana::unicode::utf8);
@@ -103,7 +140,11 @@ namespace nana
/// this text needed change, it needed review ??
bool review_utf8(const std::string& text)
{
#ifdef _nana_std_has_string_view
if (!is_utf8(text))
#else
if (!is_utf8(text.c_str(), text.length()))
#endif
{
utf8_Error(std::string("\nThe const text is not encoded in UTF8: ") + text).emit();
return true; /// it needed change, it needed review !!
@@ -115,7 +156,11 @@ namespace nana
/// this text needed change, it needed review ??
bool review_utf8(std::string& text)
{
#ifdef _nana_std_has_string_view
if(!is_utf8(text))
#else
if (!is_utf8(text.c_str(), text.length()))
#endif
{
utf8_Error(std::string("\nThe text is not encoded in UTF8: ") + text).emit();
text=recode_to_utf8(text);
@@ -135,10 +180,21 @@ namespace nana
return ::nana::charset(text).to_bytes(::nana::unicode::utf8);
}
#ifdef _nana_std_has_string_view
std::wstring to_wstring(std::string_view utf8_str)
{
if (utf8_str.empty())
return{};
return ::nana::charset(std::string{ utf8_str.data(), utf8_str.size() }, unicode::utf8);
}
#else
std::wstring to_wstring(const std::string& utf8_str)
{
return ::nana::charset(utf8_str, ::nana::unicode::utf8);
}
#endif
const std::wstring& to_wstring(const std::wstring& wstr)
{