From 846cb93278fc9f1ed8c34b4b8fa954cb82614f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Mon, 12 Apr 2010 14:41:44 +0000 Subject: [PATCH] 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 --- src/kits/interface/TextInput.cpp | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/kits/interface/TextInput.cpp b/src/kits/interface/TextInput.cpp index 781214f451..c1d8ce1857 100644 --- a/src/kits/interface/TextInput.cpp +++ b/src/kits/interface/TextInput.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -204,28 +205,22 @@ void _BTextInput_::InsertText(const char* inText, int32 inLength, int32 inOffset, const text_run_array* inRuns) { - char* buffer = NULL; - - if (strpbrk(inText, "\r\n") && inLength <= 1024) { - buffer = (char*)malloc(inLength + 1); - - if (buffer) { - strlcpy(buffer, inText, inLength + 1); - - for (int32 i = 0; i < inLength; i++) { - if (buffer[i] == '\r' || buffer[i] == '\n') - buffer[i] = ' '; - } - } + // Filter all line breaks, note that inText is not terminated. + if (inLength == 1) { + if (*inText == '\n' || *inText == '\r') + BTextView::InsertText(" ", 1, inOffset, inRuns); + else + BTextView::InsertText(inText, 1, inOffset, inRuns); + } else { + BString filteredText(inText, inLength); + filteredText.ReplaceAll('\n', ' '); + filteredText.ReplaceAll('\r', ' '); + BTextView::InsertText(filteredText.String(), inLength, inOffset, + inRuns); } - BTextView::InsertText(buffer ? buffer : inText, inLength, inOffset, - inRuns); - TextControl()->InvokeNotify(TextControl()->ModificationMessage(), B_CONTROL_MODIFIED); - - free(buffer); }