commit ceb305a780f8f744cd208d89cb797937972b3c8e
parent 1dc2a75cb61d4632d2acd8481b4e5476d6093598
Author: Benno Schulenberg <bensberg@telfort.nl>
Date: Wed, 28 Sep 2022 12:21:52 +0200
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.)
Diffstat:
4 files changed, 13 insertions(+), 14 deletions(-)
diff --git 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
@@ -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
@@ -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
@@ -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)