diff --git a/src/common/stringutils.cpp b/src/common/stringutils.cpp index 3e99a7b7..1e854c31 100644 --- a/src/common/stringutils.cpp +++ b/src/common/stringutils.cpp @@ -197,3 +197,7 @@ std::size_t StrUtils::Utf8StringLength(const std::string &str) return result; } +bool StrUtils::isUtf8ContinuationByte(char c) +{ + return (c & 0b11'000000) == 0b10'000000; +} diff --git a/src/common/stringutils.h b/src/common/stringutils.h index bdb24049..b6eca93c 100644 --- a/src/common/stringutils.h +++ b/src/common/stringutils.h @@ -87,5 +87,8 @@ int Utf8CharSizeAt(const std::string &str, unsigned int pos); //! Returns the length in characters of UTF-8 string \a str std::size_t Utf8StringLength(const std::string &str); +//! Returns true if char is continuation UTF-8 byte +bool isUtf8ContinuationByte(char c); + } // namespace StrUtil diff --git a/src/ui/controls/edit.cpp b/src/ui/controls/edit.cpp index 037a8d06..168a97b8 100644 --- a/src/ui/controls/edit.cpp +++ b/src/ui/controls/edit.cpp @@ -27,6 +27,7 @@ #include "common/logger.h" #include "common/make_unique.h" +#include "common/stringutils.h" #include "common/resources/inputstream.h" #include "common/resources/outputstream.h" @@ -2309,7 +2310,10 @@ void CEdit::MoveChar(int move, bool bWord, bool bSelect) if ( m_cursor1 > 0 ) { m_cursor1 --; - while ( m_cursor1 > 0 && (m_text[m_cursor1] & 0xC0) == 0x80 ) m_cursor1 --; + while ( m_cursor1 > 0 && StrUtils::isUtf8ContinuationByte(m_text[m_cursor1]) ) + { + m_cursor1 --; + } } } } @@ -2362,7 +2366,10 @@ void CEdit::MoveChar(int move, bool bWord, bool bSelect) if ( m_cursor1 < m_len ) { m_cursor1 ++; - while ( m_cursor1 < m_len && (m_text[m_cursor1] & 0xC0) == 0x80 ) m_cursor1 ++; + while ( m_cursor1 < m_len && StrUtils::isUtf8ContinuationByte(m_text[m_cursor1]) ) + { + m_cursor1 ++; + } } } } @@ -2795,8 +2802,8 @@ void CEdit::DeleteOne(int dir) if ( m_cursor1 > m_cursor2 ) Math::Swap(m_cursor1, m_cursor2); // Expands selection to delete integer number of UTF-8 symbols - while ( m_cursor1 > 0 && (m_text[m_cursor1] & 0xC0) == 0x80 ) m_cursor1 --; - while ( m_cursor2 < m_len && (m_text[m_cursor2] & 0xC0) == 0x80 ) m_cursor2 ++; + while ( m_cursor1 > 0 && StrUtils::isUtf8ContinuationByte(m_text[m_cursor1]) ) m_cursor1 --; + while ( m_cursor2 < m_len && StrUtils::isUtf8ContinuationByte(m_text[m_cursor2]) ) m_cursor2 ++; hole = m_cursor2-m_cursor1; end = m_len-hole;