diff --git a/escher/src/text_field.cpp b/escher/src/text_field.cpp index 842d784cc..c330a42e4 100644 --- a/escher/src/text_field.cpp +++ b/escher/src/text_field.cpp @@ -131,6 +131,7 @@ bool TextField::ContentView::insertTextAtLocation(const char * text, char * loca *overridenByteLocation = overridenByte; m_currentDraftTextLength += textLength; + // Remove the \n code points UTF8Decoder decoder(s_draftTextBuffer); const char * codePointPointer = decoder.stringPosition(); CodePoint codePoint = decoder.nextCodePoint(); @@ -139,11 +140,12 @@ bool TextField::ContentView::insertTextAtLocation(const char * text, char * loca assert(codePointPointer < s_draftTextBuffer + m_draftTextBufferSize); if (codePoint == '\n') { assert(UTF8Decoder::CharSizeOfCodePoint('\n') == 1); - *(const_cast(codePointPointer)) = 0; - m_currentDraftTextLength = codePointPointer - s_draftTextBuffer; - break; + strlcpy(const_cast(codePointPointer), codePointPointer + 1, (s_draftTextBuffer + m_draftTextBufferSize) - codePointPointer); + // Put the decoder to the code point replacing \n + decoder.setPosition(codePointPointer); + } else { + codePointPointer = decoder.stringPosition(); } - codePointPointer = decoder.stringPosition(); codePoint = decoder.nextCodePoint(); } diff --git a/ion/include/ion/unicode/utf8_decoder.h b/ion/include/ion/unicode/utf8_decoder.h index b01a94e99..529eafbff 100644 --- a/ion/include/ion/unicode/utf8_decoder.h +++ b/ion/include/ion/unicode/utf8_decoder.h @@ -41,9 +41,11 @@ public: const char * nextGlyphPosition(); const char * previousGlyphPosition(); const char * stringPosition() const { return m_stringPosition; } + void setPosition(const char * position); static size_t CharSizeOfCodePoint(CodePoint c); static size_t CodePointToChars(CodePoint c, char * buffer, size_t bufferSize); // No null-terminating char private: + static bool IsInTheMiddleOfACodePoint(uint8_t value); const char * const m_string; const char * m_stringPosition; }; diff --git a/ion/src/shared/unicode/utf8_decoder.cpp b/ion/src/shared/unicode/utf8_decoder.cpp index 8e12b8f8f..c23569676 100644 --- a/ion/src/shared/unicode/utf8_decoder.cpp +++ b/ion/src/shared/unicode/utf8_decoder.cpp @@ -1,4 +1,5 @@ #include +#include #include static inline int leading_ones(uint8_t value) { @@ -87,6 +88,12 @@ const char * UTF8Decoder::previousGlyphPosition() { return resultGlyphPosition; } +void UTF8Decoder::setPosition(const char * position) { + assert(m_stringPosition >= m_string && m_stringPosition <= m_string + strlen(m_string)); + assert(!IsInTheMiddleOfACodePoint(*position)); + m_stringPosition = position; +} + size_t UTF8Decoder::CharSizeOfCodePoint(CodePoint c) { if (c <= 0x7F) { return 1; @@ -131,3 +138,7 @@ size_t UTF8Decoder::CodePointToChars(CodePoint c, char * buffer, size_t bufferSi assert(i == charCount); return charCount; } + +bool UTF8Decoder::IsInTheMiddleOfACodePoint(uint8_t value) { + return value >= 0b10000000 && value < 0b11000000; +}