From ceb305a780f8f744cd208d89cb797937972b3c8e Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Wed, 28 Sep 2022 12:21:52 +0200 Subject: [PATCH] tweaks: avoid iterating over the same string twice in a row The function recode_LF_to_NUL() has to iterate over the string anyway (to replace each \n with \0), so... instead of calling strlen() right before it, just let recode_LF_to_NUL() return the length of the string. (This is not speed-critical code, but... it saves one iteration over the entire buffer contents whenever writing out a file.) --- src/files.c | 9 +++------ src/history.c | 7 ++----- src/prototypes.h | 2 +- src/utils.c | 9 +++++++-- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/files.c b/src/files.c index 1950de30..c40b389d 100644 --- a/src/files.c +++ b/src/files.c @@ -988,9 +988,7 @@ void send_data(const linestruct *line, int fd) /* Send each line, except a final empty line. */ while (line != NULL && (line->next != NULL || line->data[0] != '\0')) { - size_t length = strlen(line->data); - - recode_LF_to_NUL(line->data); + size_t length = recode_LF_to_NUL(line->data); if (fwrite(line->data, sizeof(char), length, tube) < length) exit(5); @@ -1865,11 +1863,10 @@ bool write_file(const char *name, FILE *thefile, bool normal, statusbar(_("Writing...")); while (TRUE) { - size_t data_len = strlen(line->data); - size_t wrote; + size_t data_len, wrote; /* Decode LFs as the NULs that they are, before writing to disk. */ - recode_LF_to_NUL(line->data); + data_len = recode_LF_to_NUL(line->data); wrote = fwrite(line->data, sizeof(char), data_len, thefile); diff --git a/src/history.c b/src/history.c index 5cf4cb93..0f8f2577 100644 --- a/src/history.c +++ b/src/history.c @@ -304,10 +304,8 @@ bool write_list(const linestruct *head, FILE *histfile) const linestruct *item; for (item = head; item != NULL; item = item->next) { - size_t length = strlen(item->data); - /* Decode 0x0A bytes as embedded NULs. */ - recode_LF_to_NUL(item->data); + size_t length = recode_LF_to_NUL(item->data); if (fwrite(item->data, sizeof(char), length, histfile) < length) return FALSE; @@ -451,10 +449,9 @@ void save_poshistory(void) path_and_place = nmalloc(strlen(item->filename) + 44); sprintf(path_and_place, "%s %zd %zd\n", item->filename, item->linenumber, item->columnnumber); - length = strlen(path_and_place); /* Encode newlines in filenames as NULs. */ - recode_LF_to_NUL(path_and_place); + length = recode_LF_to_NUL(path_and_place); /* Restore the terminating newline. */ path_and_place[length - 1] = '\n'; diff --git a/src/prototypes.h b/src/prototypes.h index d638a01a..530c4b9f 100644 --- a/src/prototypes.h +++ b/src/prototypes.h @@ -544,7 +544,7 @@ int digits(ssize_t n); bool parse_num(const char *str, ssize_t *result); bool parse_line_column(const char *str, ssize_t *line, ssize_t *column); void recode_NUL_to_LF(char *string, size_t length); -void recode_LF_to_NUL(char *string); +size_t recode_LF_to_NUL(char *string); #if !defined(ENABLE_TINY) || defined(ENABLE_TABCOMP) || defined(ENABLE_BROWSER) void free_chararray(char **array, size_t len); #endif diff --git a/src/utils.c b/src/utils.c index f1bd5c96..d6470ccc 100644 --- a/src/utils.c +++ b/src/utils.c @@ -168,14 +168,19 @@ void recode_NUL_to_LF(char *string, size_t length) } } -/* In the given string, recode each embedded newline as a NUL. */ -void recode_LF_to_NUL(char *string) +/* In the given string, recode each embedded newline as a NUL, + * and return the number of bytes in the string. */ +size_t recode_LF_to_NUL(char *string) { + char *beginning = string; + while (*string != '\0') { if (*string == '\n') *string = '\0'; string++; } + + return (string - beginning); } #if !defined(ENABLE_TINY) || defined(ENABLE_TABCOMP) || defined(ENABLE_BROWSER)