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.)
This commit is contained in:
Benno Schulenberg 2022-09-28 12:21:52 +02:00
parent 1dc2a75cb6
commit ceb305a780
4 changed files with 13 additions and 14 deletions

View File

@ -988,9 +988,7 @@ void send_data(const linestruct *line, int fd)
/* Send each line, except a final empty line. */ /* Send each line, except a final empty line. */
while (line != NULL && (line->next != NULL || line->data[0] != '\0')) { while (line != NULL && (line->next != NULL || line->data[0] != '\0')) {
size_t length = strlen(line->data); size_t length = recode_LF_to_NUL(line->data);
recode_LF_to_NUL(line->data);
if (fwrite(line->data, sizeof(char), length, tube) < length) if (fwrite(line->data, sizeof(char), length, tube) < length)
exit(5); exit(5);
@ -1865,11 +1863,10 @@ bool write_file(const char *name, FILE *thefile, bool normal,
statusbar(_("Writing...")); statusbar(_("Writing..."));
while (TRUE) { while (TRUE) {
size_t data_len = strlen(line->data); size_t data_len, wrote;
size_t wrote;
/* Decode LFs as the NULs that they are, before writing to disk. */ /* 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); wrote = fwrite(line->data, sizeof(char), data_len, thefile);

View File

@ -304,10 +304,8 @@ bool write_list(const linestruct *head, FILE *histfile)
const linestruct *item; const linestruct *item;
for (item = head; item != NULL; item = item->next) { for (item = head; item != NULL; item = item->next) {
size_t length = strlen(item->data);
/* Decode 0x0A bytes as embedded NULs. */ /* 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) if (fwrite(item->data, sizeof(char), length, histfile) < length)
return FALSE; return FALSE;
@ -451,10 +449,9 @@ void save_poshistory(void)
path_and_place = nmalloc(strlen(item->filename) + 44); path_and_place = nmalloc(strlen(item->filename) + 44);
sprintf(path_and_place, "%s %zd %zd\n", sprintf(path_and_place, "%s %zd %zd\n",
item->filename, item->linenumber, item->columnnumber); item->filename, item->linenumber, item->columnnumber);
length = strlen(path_and_place);
/* Encode newlines in filenames as NULs. */ /* 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. */ /* Restore the terminating newline. */
path_and_place[length - 1] = '\n'; path_and_place[length - 1] = '\n';

View File

@ -544,7 +544,7 @@ int digits(ssize_t n);
bool parse_num(const char *str, ssize_t *result); bool parse_num(const char *str, ssize_t *result);
bool parse_line_column(const char *str, ssize_t *line, ssize_t *column); 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_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) #if !defined(ENABLE_TINY) || defined(ENABLE_TABCOMP) || defined(ENABLE_BROWSER)
void free_chararray(char **array, size_t len); void free_chararray(char **array, size_t len);
#endif #endif

View File

@ -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. */ /* In the given string, recode each embedded newline as a NUL,
void recode_LF_to_NUL(char *string) * and return the number of bytes in the string. */
size_t recode_LF_to_NUL(char *string)
{ {
char *beginning = string;
while (*string != '\0') { while (*string != '\0') {
if (*string == '\n') if (*string == '\n')
*string = '\0'; *string = '\0';
string++; string++;
} }
return (string - beginning);
} }
#if !defined(ENABLE_TINY) || defined(ENABLE_TABCOMP) || defined(ENABLE_BROWSER) #if !defined(ENABLE_TINY) || defined(ENABLE_TABCOMP) || defined(ENABLE_BROWSER)