diff options
author | crupest <crupest@outlook.com> | 2020-10-28 17:44:06 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-10-28 17:44:06 +0800 |
commit | 864b031211322dc276b220ec0a6e11483503a0e9 (patch) | |
tree | c5c0aa1a046b443dc001445f39870cb440b6fe38 /src/common/StringUtil.cpp | |
parent | 2df47ffbfff02fb6b64d19e404adc41a93677afe (diff) | |
download | cru-864b031211322dc276b220ec0a6e11483503a0e9.tar.gz cru-864b031211322dc276b220ec0a6e11483503a0e9.tar.bz2 cru-864b031211322dc276b220ec0a6e11483503a0e9.zip |
...
Diffstat (limited to 'src/common/StringUtil.cpp')
-rw-r--r-- | src/common/StringUtil.cpp | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/common/StringUtil.cpp b/src/common/StringUtil.cpp index fc6d6349..3c312d49 100644 --- a/src/common/StringUtil.cpp +++ b/src/common/StringUtil.cpp @@ -1,4 +1,5 @@ #include "cru/common/StringUtil.hpp" +#include "gsl/gsl_util" namespace cru { namespace { @@ -191,8 +192,8 @@ void Utf8EncodeCodePointAppend(CodePoint code_point, std::string& str) { } void Utf16EncodeCodePointAppend(CodePoint code_point, std::u16string& str) { - if (code_point >= 0 && code_point <= 0xD7FF || - code_point >= 0xE000 && code_point <= 0xFFFF) { + if ((code_point >= 0 && code_point <= 0xD7FF) || + (code_point >= 0xE000 && code_point <= 0xFFFF)) { str.push_back(static_cast<char16_t>(code_point)); } else if (code_point >= 0x10000 && code_point <= 0x10FFFF) { std::uint32_t u = code_point - 0x10000; @@ -220,4 +221,12 @@ std::u16string ToUtf16(std::string_view s) { } return result; } + +bool Utf16IsValidInsertPosition(std::u16string_view s, gsl::index position) { + if (position < 0) return false; + if (position > static_cast<gsl::index>(s.size())) return false; + if (position == 0) return true; + if (position == static_cast<gsl::index>(s.size())) return true; + return !IsUtf16SurrogatePairTrailing(s[position]); +} } // namespace cru |