From 68ca1732b8eecb1112ff0635560ad67b00844deb Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Thu, 13 Feb 2020 14:42:47 +0100 Subject: [PATCH] prompt: insert a burst of bytes in one go instead of characterwise There is no need to count characters, so just insert the whole batch of bytes at once. --- src/prompt.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/prompt.c b/src/prompt.c index 54ebb635..d2f0c093 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -176,24 +176,17 @@ int do_statusbar_input(bool *finished) /* Insert the given short burst of bytes into the anwer. */ void inject_into_answer(char *burst, size_t count) { - size_t charlen, index = 0; - - while (index < count) { - /* Encode any NUL byte as 0x0A. */ + /* First encode any embedded NUL byte as 0x0A. */ + for (size_t index = 0; index < count; index++) if (burst[index] == '\0') burst[index] = '\n'; - charlen = char_length(burst + index); - - /* Insert the typed character into the existing answer string. */ - answer = charealloc(answer, strlen(answer) + charlen + 1); - memmove(answer + typing_x + charlen, answer + typing_x, + answer = charealloc(answer, strlen(answer) + count + 1); + memmove(answer + typing_x + count, answer + typing_x, strlen(answer) - typing_x + 1); - strncpy(answer + typing_x, burst + index, charlen); + strncpy(answer + typing_x, burst , count); - typing_x += charlen; - index += charlen; - } + typing_x += count; } /* Move to the beginning of the answer. */