nano

nano with my custom patches
git clone git://bsandro.tech/nano
Log | Files | Refs | README | LICENSE

commit f03c87c300f3626d45a0dc1af2ba6ea2f3820d26
parent 45b1a38b8280b81271f309e74f1901ba85059931
Author: Benno Schulenberg <bensberg@telfort.nl>
Date:   Sat,  8 Jun 2019 16:12:14 +0200

tweaks: squeeze excess spaces out of a line in situ

There is no need to make a copy of the line, because if there are some
bytes that are moved, they are always moved toward the left -- the line
can only shrink, never expand.

Diffstat:
Msrc/text.c | 20+++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/src/text.c b/src/text.c @@ -1601,8 +1601,12 @@ void copy_character(char **from, char **to) { int charlen = parse_mbchar(*from, NULL, NULL); - while (--charlen >= 0) - *((*to)++) = *((*from)++); + if (*from == *to) { + *from += charlen; + *to += charlen; + } else + while (--charlen >= 0) + *((*to)++) = *((*from)++); } /* In the given line, replace any series of blanks with a single space, @@ -1611,12 +1615,8 @@ void copy_character(char **from, char **to) * number of characters untreated. */ void squeeze(linestruct *line, size_t skip) { - char *from, *to, *newdata; - - newdata = charalloc(strlen(line->data) + 1); - strncpy(newdata, line->data, skip); - from = line->data + skip; - to = newdata + skip; + char *start = line->data + skip; + char *from = start, *to = start; /* For each character, 1) when a blank, change it to a space, and pass over * all blanks after it; 2) if it is punctuation, copy it plus a possible @@ -1651,12 +1651,10 @@ void squeeze(linestruct *line, size_t skip) } /* If there are spaces at the end of the line, remove them. */ - while (to > newdata + skip && *(to - 1) == ' ') + while (to > start && *(to - 1) == ' ') to--; *to = '\0'; - free(line->data); - line->data = newdata; } /* Return the length of the quote part of the given line. The "quote part"