Fix Fl_Simple_Terminal::append(str, len) assumes a null terminated string (#728)

This commit is contained in:
ManoloFLTK 2023-05-19 18:57:18 +02:00
parent 033880673a
commit 89454508a2
3 changed files with 19 additions and 15 deletions

View File

@ -283,15 +283,17 @@ public:
/**
Inserts null-terminated string \p text at position \p pos.
\param pos insertion position as byte offset (must be UTF-8 character aligned)
\param text UTF-8 encoded and nul terminated text
\param text UTF-8 encoded text
\param insertedLength number of bytes to insert, or -1 to indicate \p text is null-terminated
*/
void insert(int pos, const char* text);
void insert(int pos, const char* text, int insertedLength = -1);
/**
Appends the text string to the end of the buffer.
\param t UTF-8 encoded and nul terminated text
\param t UTF-8 encoded text
\param addedLength number of bytes to append, or -1 to indicate \p t is null-terminated
*/
void append(const char* t) { insert(length(), t); }
void append(const char* t, int addedLength = -1) { insert(length(), t, addedLength); }
void vprintf(const char *fmt, va_list ap);
void printf(const char* fmt, ...);
@ -308,9 +310,10 @@ public:
null-terminated string \p text in their place in the buffer.
\param start byte offset to first character to be removed and new insert position
\param end byte offset to character after last character to be removed
\param text UTF-8 encoded and nul terminated text
\param text UTF-8 encoded text
\param insertedLength number of bytes to insert, or -1 to indicate \p text is null-terminated
*/
void replace(int start, int end, const char *text);
void replace(int start, int end, const char *text, int insertedLength = -1);
/**
Copies text from another Fl_Text_Buffer to this one.
@ -784,13 +787,14 @@ protected:
/**
Internal (non-redisplaying) version of insert().
Returns the length of text inserted (this is just strlen(\p text), however
Returns the length of text inserted (this is just strlen(\p text) if
\p insertedLength == -1, however
this calculation can be expensive and the length will be required by any
caller who will continue on to call redisplay). \p pos must be contiguous
with the existing text in the buffer (i.e. not past the end).
\return the number of bytes inserted
*/
int insert_(int pos, const char* text);
int insert_(int pos, const char* text, int insertedLength = -1);
/**
Internal (non-redisplaying) version of remove().

View File

@ -910,7 +910,7 @@ void Fl_Simple_Terminal::append(const char *s, int len) {
append_ansi(s, len);
} else {
// raw append
buf->append(s);
buf->append(s, len);
lines_ += ::strcnt(s, '\n'); // count total line feeds in string added
}
enforce_history_lines();

View File

@ -380,7 +380,7 @@ char Fl_Text_Buffer::byte_at(int pos) const {
Insert some text at the given index.
Pos must be at a character boundary.
*/
void Fl_Text_Buffer::insert(int pos, const char *text)
void Fl_Text_Buffer::insert(int pos, const char *text, int insertedLength)
{
IS_UTF8_ALIGNED2(this, (pos))
IS_UTF8_ALIGNED(text)
@ -399,7 +399,7 @@ void Fl_Text_Buffer::insert(int pos, const char *text)
call_predelete_callbacks(pos, 0);
/* insert and redisplay */
int nInserted = insert_(pos, text);
int nInserted = insert_(pos, text, insertedLength);
mCursorPosHint = pos + nInserted;
IS_UTF8_ALIGNED2(this, (mCursorPosHint))
call_modify_callbacks(pos, 0, nInserted, 0, NULL);
@ -454,7 +454,7 @@ void Fl_Text_Buffer::printf(const char *fmt, ...) {
Replace a range of text with new text.
Start and end must be at a character boundary.
*/
void Fl_Text_Buffer::replace(int start, int end, const char *text)
void Fl_Text_Buffer::replace(int start, int end, const char *text, int insertedLength)
{
// Range check...
if (!text)
@ -471,7 +471,7 @@ void Fl_Text_Buffer::replace(int start, int end, const char *text)
call_predelete_callbacks(start, end - start);
const char *deletedText = text_range(start, end);
remove_(start, end);
int nInserted = insert_(start, text);
int nInserted = insert_(start, text, insertedLength);
mCursorPosHint = start + nInserted;
call_modify_callbacks(start, end - start, nInserted, 0, deletedText);
free((void *) deletedText);
@ -1355,12 +1355,12 @@ int Fl_Text_Buffer::search_backward(int startPos, const char *searchString,
Insert a string into the buffer.
Pos must be at a character boundary. Text must be a correct UTF-8 string.
*/
int Fl_Text_Buffer::insert_(int pos, const char *text)
int Fl_Text_Buffer::insert_(int pos, const char *text, int insertedLength)
{
if (!text || !*text)
return 0;
int insertedLength = (int) strlen(text);
if (insertedLength == -1) insertedLength = (int) strlen(text);
/* Prepare the buffer to receive the new text. If the new text fits in
the current buffer, just move the gap (if necessary) to where