Refactor: Create function isUtf8ContinuationByte

fix-squashed-planets
Evgeny Pestov 2021-12-22 21:52:00 +07:00
parent 69ea470a26
commit 05b68a4b80
3 changed files with 18 additions and 4 deletions

View File

@ -197,3 +197,7 @@ std::size_t StrUtils::Utf8StringLength(const std::string &str)
return result; return result;
} }
bool StrUtils::isUtf8ContinuationByte(char c)
{
return (c & 0b11'000000) == 0b10'000000;
}

View File

@ -87,5 +87,8 @@ int Utf8CharSizeAt(const std::string &str, unsigned int pos);
//! Returns the length in characters of UTF-8 string \a str //! Returns the length in characters of UTF-8 string \a str
std::size_t Utf8StringLength(const std::string &str); std::size_t Utf8StringLength(const std::string &str);
//! Returns true if char is continuation UTF-8 byte
bool isUtf8ContinuationByte(char c);
} // namespace StrUtil } // namespace StrUtil

View File

@ -27,6 +27,7 @@
#include "common/logger.h" #include "common/logger.h"
#include "common/make_unique.h" #include "common/make_unique.h"
#include "common/stringutils.h"
#include "common/resources/inputstream.h" #include "common/resources/inputstream.h"
#include "common/resources/outputstream.h" #include "common/resources/outputstream.h"
@ -2309,7 +2310,10 @@ void CEdit::MoveChar(int move, bool bWord, bool bSelect)
if ( m_cursor1 > 0 ) if ( m_cursor1 > 0 )
{ {
m_cursor1 --; 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 ) if ( m_cursor1 < m_len )
{ {
m_cursor1 ++; 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); if ( m_cursor1 > m_cursor2 ) Math::Swap(m_cursor1, m_cursor2);
// Expands selection to delete integer number of UTF-8 symbols // Expands selection to delete integer number of UTF-8 symbols
while ( m_cursor1 > 0 && (m_text[m_cursor1] & 0xC0) == 0x80 ) m_cursor1 --; while ( m_cursor1 > 0 && StrUtils::isUtf8ContinuationByte(m_text[m_cursor1]) ) m_cursor1 --;
while ( m_cursor2 < m_len && (m_text[m_cursor2] & 0xC0) == 0x80 ) m_cursor2 ++; while ( m_cursor2 < m_len && StrUtils::isUtf8ContinuationByte(m_text[m_cursor2]) ) m_cursor2 ++;
hole = m_cursor2-m_cursor1; hole = m_cursor2-m_cursor1;
end = m_len-hole; end = m_len-hole;