Making the calculation of the new line size more efficient and symmetrical,

and renaming two variables in the process.


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5323 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
This commit is contained in:
Benno Schulenberg 2015-07-26 17:29:34 +00:00
parent 17ab9a2dba
commit c7f5691637
2 changed files with 16 additions and 18 deletions

View File

@ -7,6 +7,8 @@
the last search command in a fixed direction without prompting. the last search command in a fixed direction without prompting.
* src/global.c (shortcut_init): Tweak a string. * src/global.c (shortcut_init): Tweak a string.
* src/search.c, src/move.c: Improve a few of the comments. * src/search.c, src/move.c: Improve a few of the comments.
* src/search.c (replace_regexp, replace_line): Rename two variables,
and make the calculation of the new line size more symmetrical.
2015-07-25 Benno Schulenberg <bensberg@justemail.net> 2015-07-25 Benno Schulenberg <bensberg@justemail.net>
* src/global.c (shortcut_init, strtosc), src/files.c (savefile), * src/global.c (shortcut_init, strtosc), src/files.c (savefile),

View File

@ -547,16 +547,13 @@ void do_research(void)
#endif /* !NANO_TINY */ #endif /* !NANO_TINY */
#ifdef HAVE_REGEX_H #ifdef HAVE_REGEX_H
/* Calculate the size of the replacement line, taking possible /* Calculate the size of the replacement text, taking possible
* subexpressions \1 to \9 into account. Return the replacement * subexpressions \1 to \9 into account. Return the replacement
* text in the passed string only when create is TRUE. */ * text in the passed string only when create is TRUE. */
int replace_regexp(char *string, bool create) int replace_regexp(char *string, bool create)
{ {
const char *c = last_replace; const char *c = last_replace;
size_t search_match_count = regmatches[0].rm_eo - size_t replacement_size = 0;
regmatches[0].rm_so;
size_t new_line_size = strlen(openfile->current->data) + 1 -
search_match_count;
/* Iterate through the replacement text to handle subexpression /* Iterate through the replacement text to handle subexpression
* replacement using \1, \2, \3, etc. */ * replacement using \1, \2, \3, etc. */
@ -568,7 +565,7 @@ int replace_regexp(char *string, bool create)
if (create) if (create)
*string++ = *c; *string++ = *c;
c++; c++;
new_line_size++; replacement_size++;
} else { } else {
size_t i = regmatches[num].rm_eo - regmatches[num].rm_so; size_t i = regmatches[num].rm_eo - regmatches[num].rm_so;
@ -576,7 +573,7 @@ int replace_regexp(char *string, bool create)
c += 2; c += 2;
/* But add the length of the subexpression to new_size. */ /* But add the length of the subexpression to new_size. */
new_line_size += i; replacement_size += i;
/* And if create is TRUE, append the result of the /* And if create is TRUE, append the result of the
* subexpression match to the new line. */ * subexpression match to the new line. */
@ -591,7 +588,7 @@ int replace_regexp(char *string, bool create)
if (create) if (create)
*string = '\0'; *string = '\0';
return new_line_size; return replacement_size;
} }
#endif /* HAVE_REGEX_H */ #endif /* HAVE_REGEX_H */
@ -599,18 +596,18 @@ int replace_regexp(char *string, bool create)
char *replace_line(const char *needle) char *replace_line(const char *needle)
{ {
char *copy; char *copy;
size_t new_line_size, search_match_count; size_t match_len;
size_t new_line_size = strlen(openfile->current->data) + 1;
/* First calculate the size of the new line. */ /* First adjust the size of the new line for the change. */
#ifdef HAVE_REGEX_H #ifdef HAVE_REGEX_H
if (ISSET(USE_REGEXP)) { if (ISSET(USE_REGEXP)) {
search_match_count = regmatches[0].rm_eo - regmatches[0].rm_so; match_len = regmatches[0].rm_eo - regmatches[0].rm_so;
new_line_size = replace_regexp(NULL, FALSE); new_line_size += replace_regexp(NULL, FALSE) - match_len;
} else { } else {
#endif #endif
search_match_count = strlen(needle); match_len = strlen(needle);
new_line_size = strlen(openfile->current->data) - new_line_size += strlen(answer) - match_len;
search_match_count + strlen(answer) + 1;
#ifdef HAVE_REGEX_H #ifdef HAVE_REGEX_H
} }
#endif #endif
@ -629,11 +626,10 @@ char *replace_line(const char *needle)
#endif #endif
strcpy(copy + openfile->current_x, answer); strcpy(copy + openfile->current_x, answer);
assert(openfile->current_x + search_match_count <= strlen(openfile->current->data)); assert(openfile->current_x + match_len <= strlen(openfile->current->data));
/* Copy the tail of the original line. */ /* Copy the tail of the original line. */
strcat(copy, openfile->current->data + openfile->current_x + strcat(copy, openfile->current->data + openfile->current_x + match_len);
search_match_count);
return copy; return copy;
} }