[apps/code] autocompletionText uses the right text length

Before, there was no notion that the autocompleted text might not be a
null terminated string.
This commit is contained in:
Léa Saviot
2020-03-24 18:03:46 +01:00
committed by Émilie Feral
parent 12bb77cd9f
commit eeb42f2544
10 changed files with 31 additions and 35 deletions

View File

@@ -117,7 +117,7 @@ protected:
const char * editedText() const override { return m_text.text(); }
size_t editedTextLength() const override { return m_text.textLength(); }
const Text * getText() const { return &m_text; }
bool insertTextAtLocation(const char * text, char * location) override;
bool insertTextAtLocation(const char * text, char * location, int textLength = -1) override;
void moveCursorGeo(int deltaX, int deltaY);
bool removePreviousGlyph() override;
bool removeEndOfLine() override;

View File

@@ -74,7 +74,7 @@ protected:
/* If the text to be appended is too long to be added without overflowing the
* buffer, nothing is done (not even adding few letters from the text to reach
* the maximum buffer capacity) and false is returned. */
bool insertTextAtLocation(const char * text, char * location) override;
bool insertTextAtLocation(const char * text, char * location, int textLength = -1) override;
KDSize minimalSizeForOptimalDisplay() const override;
bool removePreviousGlyph() override;
bool removeEndOfLine() override;

View File

@@ -49,7 +49,7 @@ protected:
// Virtual text get/add/remove
virtual const char * text() const = 0;
virtual bool insertTextAtLocation(const char * text, char * location) = 0;
virtual bool insertTextAtLocation(const char * text, char * location, int textLength = -1) = 0;
virtual bool removePreviousGlyph() = 0;
virtual bool removeEndOfLine() = 0;

View File

@@ -458,26 +458,19 @@ void TextArea::ContentView::setText(char * textBuffer, size_t textBufferSize) {
m_cursorLocation = text();
}
bool TextArea::ContentView::insertTextAtLocation(const char * text, char * location) {
int textSize = strlen(text);
if (m_text.textLength() + textSize >= m_text.bufferSize() || textSize == 0) {
bool TextArea::ContentView::insertTextAtLocation(const char * text, char * location, int textLength) {
int textLen = textLength < 0 ? strlen(text) : textLength;
assert(textLen < 0 || textLen <= strlen(text));
if (m_text.textLength() + textLen >= m_text.bufferSize() || textLen == 0) {
return false;
}
bool lineBreak = false;
// Scan for \n and 0
const char * nullLocation = UTF8Helper::PerformAtCodePoints(
text, '\n',
[](int codePointOffset, void * lineBreak, int context1, int context2) {
*((bool *)lineBreak) = true;
},
[](int c1, void * c2, int c3, int c4) { },
&lineBreak, 0);
// Scan for \n
bool lineBreak = UTF8Helper::HasCodePoint(text, '\n', text + textLen);
assert(UTF8Helper::CodePointIs(nullLocation, 0));
m_text.insertText(text, nullLocation - text, location);
m_text.insertText(text, textLen, location);
// Replace System parentheses (used to keep layout tree structure) by normal parentheses
Poincare::SerializationHelper::ReplaceSystemParenthesesByUserParentheses(location, nullLocation - text);
Poincare::SerializationHelper::ReplaceSystemParenthesesByUserParentheses(location, textLen);
reloadRectFromPosition(location, lineBreak);
return true;
}

View File

@@ -119,10 +119,10 @@ void TextField::ContentView::reinitDraftTextBuffer() {
setCursorLocation(s_draftTextBuffer);
}
bool TextField::ContentView::insertTextAtLocation(const char * text, char * location) {
bool TextField::ContentView::insertTextAtLocation(const char * text, char * location, int textLen) {
assert(m_isEditing);
int textLength = strlen(text);
int textLength = textLen < 0 ? strlen(text) : textLen;
if (m_currentDraftTextLength + textLength >= m_draftTextBufferSize || textLength == 0) {
return false;
}
@@ -130,12 +130,12 @@ bool TextField::ContentView::insertTextAtLocation(const char * text, char * loca
memmove(location + textLength, location, (s_draftTextBuffer + m_currentDraftTextLength + 1) - location);
// Caution! One byte will be overridden by the null-terminating char of strlcpy
char * overridenByteLocation = location + textLength;
size_t copySize = std::min(textLength, (s_draftTextBuffer + m_draftTextBufferSize) - location);
char * overridenByteLocation = location + copySize;
char overridenByte = *overridenByteLocation;
strlcpy(location, text, (s_draftTextBuffer + m_draftTextBufferSize) - location);
assert(overridenByteLocation < s_draftTextBuffer + m_draftTextBufferSize);
strlcpy(location, text, copySize);
*overridenByteLocation = overridenByte;
m_currentDraftTextLength += textLength;
m_currentDraftTextLength += copySize;
reloadRectFromPosition(m_horizontalAlignment == 0.0f ? location : s_draftTextBuffer);
return true;