Fix my strlcpy() adventure with a much simpler version of the code that should

also perform better for the usual case of inserting one char at a time. Using
strpbrk() on non-terminated strings was probably also not too healthy.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@36187 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2010-04-12 14:41:44 +00:00
parent 99e658c3e9
commit 846cb93278
1 changed files with 13 additions and 18 deletions

View File

@ -18,6 +18,7 @@
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include <LayoutUtils.h> #include <LayoutUtils.h>
#include <Message.h> #include <Message.h>
#include <String.h>
#include <TextControl.h> #include <TextControl.h>
#include <TextView.h> #include <TextView.h>
#include <Window.h> #include <Window.h>
@ -204,28 +205,22 @@ void
_BTextInput_::InsertText(const char* inText, int32 inLength, _BTextInput_::InsertText(const char* inText, int32 inLength,
int32 inOffset, const text_run_array* inRuns) int32 inOffset, const text_run_array* inRuns)
{ {
char* buffer = NULL; // Filter all line breaks, note that inText is not terminated.
if (inLength == 1) {
if (strpbrk(inText, "\r\n") && inLength <= 1024) { if (*inText == '\n' || *inText == '\r')
buffer = (char*)malloc(inLength + 1); BTextView::InsertText(" ", 1, inOffset, inRuns);
else
if (buffer) { BTextView::InsertText(inText, 1, inOffset, inRuns);
strlcpy(buffer, inText, inLength + 1); } else {
BString filteredText(inText, inLength);
for (int32 i = 0; i < inLength; i++) { filteredText.ReplaceAll('\n', ' ');
if (buffer[i] == '\r' || buffer[i] == '\n') filteredText.ReplaceAll('\r', ' ');
buffer[i] = ' '; BTextView::InsertText(filteredText.String(), inLength, inOffset,
}
}
}
BTextView::InsertText(buffer ? buffer : inText, inLength, inOffset,
inRuns); inRuns);
}
TextControl()->InvokeNotify(TextControl()->ModificationMessage(), TextControl()->InvokeNotify(TextControl()->ModificationMessage(),
B_CONTROL_MODIFIED); B_CONTROL_MODIFIED);
free(buffer);
} }