ed

simple ed
git clone git://git.2f30.org/ed.git
Log | Files | Refs | LICENSE

commit dcb15d26b930278fd5aee15468ffc6aab53e445a
parent 513fd592d1efb2b23572a894fe2eb02d9b31bcfb
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  3 Dec 2015 10:18:43 +0100

Implement join()

Join had the problem that addchar() is used in inject,
so we could not use the same buffer, but after the
last change, addchar() receives all the parameters,
so we can use it without problems. This function
could be implemented without dynamic memory, writing
directly to the scratch file, but lines are usually
smalls, and the common use case is to join only
2 lines, so the efford and lost of simplicity is not
needed at all.

Diffstat:
ed.c | 23++++++++++++++---------
1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/ed.c b/ed.c @@ -633,17 +633,22 @@ move(int where) static void join(void) { - int first, last; + int i; + char *t, c; + size_t len = 0, cap = 0; + static char *s; - first = getindex(line1); - last = getindex(line2); - delete(line1, line2); + free(s); + for (s = NULL, i = line1; i <= line2; i = nextln(i)) { + for (t = gettxt(i); (c = *t) != '\n'; ++t) + s = addchar(*t, s, &len, &cap); + } - /* - * TODO: we have to join all the lines, - * but we cannot use addchar because it is - * used in gettxt(). - */ + s = addchar('\n', s, &len, &cap); + s = addchar('\0', s, &len, &cap); + delete(line1, line2); + inject(s); + free(s); } static void