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.
This commit is contained in:
parent
651e2b0ee2
commit
20794b570e
23
ed.c
23
ed.c
|
@ -20,6 +20,12 @@
|
||||||
#define NUMLINES 32
|
#define NUMLINES 32
|
||||||
#define CACHESIZ 4096
|
#define CACHESIZ 4096
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char *str;
|
||||||
|
size_t cap;
|
||||||
|
size_t siz;
|
||||||
|
} String;
|
||||||
|
|
||||||
struct hline {
|
struct hline {
|
||||||
off_t seek;
|
off_t seek;
|
||||||
char global;
|
char global;
|
||||||
|
@ -111,6 +117,23 @@ prevln(int line)
|
||||||
return (line < 0) ? lastln : line;
|
return (line < 0) ? lastln : 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 *
|
static char *
|
||||||
addchar(char c, char *t, size_t *capacity, size_t *size)
|
addchar(char c, char *t, size_t *capacity, size_t *size)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue
Block a user