[escher/text_field] Fix removing of \n in insertTextAtLocation

Scenario: Copy a text with \n (for instance from a script), then paste
it in a script name -> if \n are replaed with \0 instead of just being
removed, there are problems with the extension
This commit is contained in:
Léa Saviot
2020-01-29 09:10:10 +01:00
parent ad665fba73
commit b29d014695
3 changed files with 19 additions and 4 deletions

View File

@@ -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<char *>(codePointPointer)) = 0;
m_currentDraftTextLength = codePointPointer - s_draftTextBuffer;
break;
strlcpy(const_cast<char *>(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();
}

View File

@@ -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;
};

View File

@@ -1,4 +1,5 @@
#include <ion/unicode/utf8_decoder.h>
#include <string.h>
#include <assert.h>
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;
}