tweaks: elide an intermediate copy of a character during injection, twice

This commit is contained in:
Benno Schulenberg 2020-02-13 14:12:02 +01:00
parent a0703ab62d
commit 78767b583d
2 changed files with 6 additions and 9 deletions

View File

@ -1638,10 +1638,9 @@ void process_a_keystroke(void)
/* The user typed output_len multibyte characters. Add them to the buffer. */
void inject(char *output, size_t output_len)
{
char onechar[MAXCHARLEN];
int charlen;
size_t current_len = strlen(openfile->current->data);
size_t index = 0;
int charlen;
#ifndef NANO_TINY
size_t original_row = 0, old_amount = 0;
@ -1657,8 +1656,7 @@ void inject(char *output, size_t output_len)
if (output[index] == '\0')
output[index] = '\n';
/* Get the next multibyte character. */
charlen = collect_char(output + index, onechar);
charlen = char_length(output + index);
/* Make room for the new character and copy it into the line. */
openfile->current->data = charealloc(openfile->current->data,
@ -1666,7 +1664,8 @@ void inject(char *output, size_t output_len)
memmove(openfile->current->data + openfile->current_x + charlen,
openfile->current->data + openfile->current_x,
current_len - openfile->current_x + 1);
strncpy(openfile->current->data + openfile->current_x, onechar, charlen);
strncpy(openfile->current->data + openfile->current_x,
output + index, charlen);
current_len += charlen;
index += charlen;

View File

@ -176,7 +176,6 @@ int do_statusbar_input(bool *finished)
/* The user typed input_len multibyte characters. Add them to the answer. */
void inject_into_answer(char *output, size_t input_len)
{
char onechar[MAXCHARLEN];
size_t charlen, index = 0;
while (index < input_len) {
@ -184,14 +183,13 @@ void inject_into_answer(char *output, size_t input_len)
if (output[index] == '\0')
output[index] = '\n';
/* Interpret the next multibyte character. */
charlen = collect_char(output + index, onechar);
charlen = char_length(output + 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,
strlen(answer) - typing_x + 1);
strncpy(answer + typing_x, onechar, charlen);
strncpy(answer + typing_x, output + index, charlen);
typing_x += charlen;
index += charlen;