sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 20794b570eeb5a9d55a4869a20888d46a1c9d4fd
parent 651e2b0ee2bae4826c9cc9f1215cd57965676bdf
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun,  4 Mar 2018 13:59:48 +0100

Define new String type

Current handling of strings is a bit messy. This type is copied
from the sed implementation. Addchar_ is added to be able to live
with String and old style chars based in 3 different variables.

Diffstat:
Med.c | 23+++++++++++++++++++++++
1 file changed, 23 insertions(+), 0 deletions(-)

diff --git a/ed.c b/ed.c @@ -20,6 +20,12 @@ #define NUMLINES 32 #define CACHESIZ 4096 +typedef struct { + char *str; + size_t cap; + size_t siz; +} String; + struct hline { off_t seek; char global; @@ -112,6 +118,23 @@ prevln(int line) } static char * +addchar_(char c, String *s) +{ + size_t cap = s->cap, siz = s->siz; + char *t = s->str; + + if (siz >= cap && + (cap > SIZE_MAX - LINESIZE || + (t = realloc(t, cap += LINESIZE)) == NULL)) + error("out of memory"); + t[siz++] = c; + s->siz = siz; + s->cap = cap; + s->str = t; + return t; +} + +static char * addchar(char c, char *t, size_t *capacity, size_t *size) { size_t cap = *capacity, siz = *size;