mirror of
git://git.sv.gnu.org/nano.git
synced 2025-01-10 03:22:04 +03:00
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.
This commit is contained in:
parent
45b1a38b82
commit
f03c87c300
20
src/text.c
20
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"
|
||||
|
Loading…
Reference in New Issue
Block a user