1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
17 static inline void dstr_zero (dstring_t
*s
) {
20 s
->size
= sizeof(s
->sbuf
);
25 void dstr_init (dstring_t
*s
) {
30 void dstr_init_cstr (dstring_t
*s
, const void *str
) {
32 dstr_push_cstr(s
, str
);
36 void dstr_init_buf (dstring_t
*s
, const void *start
, int len
) {
38 dstr_push_buf(s
, start
, len
);
42 void dstr_init_memrange (dstring_t
*s
, const void *start
, const void *finish
) {
44 dstr_push_memrange(s
, start
, finish
);
48 void dstr_done (dstring_t
*s
) {
49 if (s
->str
!= s
->sbuf
) free(s
->str
);
54 void dstr_clear (dstring_t
*s
) {
59 void dstr_empty (dstring_t
*s
) {
60 if (s
->str
!= s
->sbuf
) free(s
->str
);
65 static void dstr_resv (dstring_t
*s
, int size
) {
66 if (size
< 0) size
= 0;
68 if (s
->str
== s
->sbuf
) {
69 s
->str
= malloc(size
);
70 if (s
->len
> 0) memcpy(s
->str
, s
->sbuf
, s
->len
);
73 char *ns
= realloc(s
->str
, size
);
74 if (ns
== NULL
) { fprintf(stderr
, "FATAL: out of memory!\n"); abort(); }
82 void dstr_reserve (dstring_t
*s
, int size
) {
83 if (size
> s
->size
) dstr_resv(s
, size
);
87 // make room for at least `spc` chars
88 static inline void dstr_expand (dstring_t
*s
, int spc
) {
89 if (spc
>= 0) dstr_reserve(s
, ((s
->len
+spc
+1)|0x3ff)+1);
93 void dstr_push_buf (dstring_t
*s
, const void *start
, int len
) {
94 if (len
< 0) len
= (start
!= NULL
? strlen((const char *)start
) : 0);
97 if (start
== NULL
) memset(s
->str
+s
->len
, 32, len
); else memmove(s
->str
+s
->len
, start
, len
);
98 s
->str
[s
->len
+=len
] = 0;
103 void dstr_push_cstr (dstring_t
*s
, const void *str
) {
104 dstr_push_buf(s
, str
, -1);
108 void dstr_push_memrange (dstring_t
*s
, const void *start
, const void *finish
) {
109 if (finish
> start
) dstr_push_buf(s
, start
, ((const char *)finish
)-((const char *)start
));
113 void dstr_push_char (dstring_t
*s
, int x
) {
115 s
->str
[s
->len
++] = x
&0xff;
120 void dstr_set_cstr (dstring_t
*s
, const void *cstr
) {
122 dstr_push_cstr(s
, cstr
);
126 void dstr_set_buf (dstring_t
*s
, const void *start
, int len
) {
128 dstr_push_buf(s
, start
, len
);
131 void dstr_set_memrange (dstring_t
*s
, const void *start
, const void *finish
) {
133 dstr_push_memrange(s
, start
, finish
);
137 void dstr_chop (dstring_t
*s
, int n
) {
138 if (n
< 0) n
= 0; else if (n
> s
->len
) n
= s
->len
;
139 s
->str
[s
->len
=n
] = 0;
143 int dstr_pop_char (dstring_t
*s
) {
144 int res
= (s
->len
> 0 ? (unsigned char)(s
->str
[--s
->len
]) : 0);
150 char dstr_last_char (dstring_t
*s
) {
151 return (s
->len
> 0 ? s
->str
[s
->len
-1] : 0);
155 char *dstr_cstr (dstring_t
*s
) {
160 int dstr_len (dstring_t
*s
) {