aboutsummaryrefslogtreecommitdiff
path: root/strbuf.h
diff options
context:
space:
mode:
Diffstat (limited to 'strbuf.h')
-rw-r--r--strbuf.h46
1 files changed, 19 insertions, 27 deletions
diff --git a/strbuf.h b/strbuf.h
index a98ee22..d77e0f4 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -32,15 +32,13 @@
32 32
33/* Size: Total bytes allocated to *buf 33/* Size: Total bytes allocated to *buf
34 * Length: String length, excluding optional NULL terminator. 34 * Length: String length, excluding optional NULL terminator.
35 * Increment: Allocation increments when resizing the string buffer.
36 * Dynamic: True if created via strbuf_new() 35 * Dynamic: True if created via strbuf_new()
37 */ 36 */
38 37
39typedef struct { 38typedef struct {
40 char *buf; 39 char *buf;
41 int size; 40 size_t size;
42 int length; 41 size_t length;
43 int increment;
44 int dynamic; 42 int dynamic;
45 int reallocs; 43 int reallocs;
46 int debug; 44 int debug;
@@ -49,33 +47,27 @@ typedef struct {
49#ifndef STRBUF_DEFAULT_SIZE 47#ifndef STRBUF_DEFAULT_SIZE
50#define STRBUF_DEFAULT_SIZE 1023 48#define STRBUF_DEFAULT_SIZE 1023
51#endif 49#endif
52#ifndef STRBUF_DEFAULT_INCREMENT
53#define STRBUF_DEFAULT_INCREMENT -2
54#endif
55 50
56/* Initialise */ 51/* Initialise */
57extern strbuf_t *strbuf_new(int len); 52extern strbuf_t *strbuf_new(size_t len);
58extern void strbuf_init(strbuf_t *s, int len); 53extern void strbuf_init(strbuf_t *s, size_t len);
59extern void strbuf_set_increment(strbuf_t *s, int increment);
60 54
61/* Release */ 55/* Release */
62extern void strbuf_free(strbuf_t *s); 56extern void strbuf_free(strbuf_t *s);
63extern char *strbuf_free_to_string(strbuf_t *s, int *len); 57extern char *strbuf_free_to_string(strbuf_t *s, size_t *len);
64 58
65/* Management */ 59/* Management */
66extern void strbuf_resize(strbuf_t *s, int len); 60extern void strbuf_resize(strbuf_t *s, size_t len);
67static int strbuf_empty_length(strbuf_t *s); 61static size_t strbuf_empty_length(strbuf_t *s);
68static int strbuf_length(strbuf_t *s); 62static size_t strbuf_length(strbuf_t *s);
69static char *strbuf_string(strbuf_t *s, int *len); 63static char *strbuf_string(strbuf_t *s, size_t *len);
70static void strbuf_ensure_empty_length(strbuf_t *s, int len); 64static void strbuf_ensure_empty_length(strbuf_t *s, size_t len);
71static char *strbuf_empty_ptr(strbuf_t *s); 65static char *strbuf_empty_ptr(strbuf_t *s);
72static void strbuf_extend_length(strbuf_t *s, int len); 66static void strbuf_extend_length(strbuf_t *s, size_t len);
73static void strbuf_set_length(strbuf_t *s, int len); 67static void strbuf_set_length(strbuf_t *s, int len);
74 68
75/* Update */ 69/* Update */
76extern void strbuf_append_fmt(strbuf_t *s, int len, const char *fmt, ...); 70static void strbuf_append_mem(strbuf_t *s, const char *c, size_t len);
77extern void strbuf_append_fmt_retry(strbuf_t *s, const char *format, ...);
78static void strbuf_append_mem(strbuf_t *s, const char *c, int len);
79extern void strbuf_append_string(strbuf_t *s, const char *str); 71extern void strbuf_append_string(strbuf_t *s, const char *str);
80static void strbuf_append_char(strbuf_t *s, const char c); 72static void strbuf_append_char(strbuf_t *s, const char c);
81static void strbuf_ensure_null(strbuf_t *s); 73static void strbuf_ensure_null(strbuf_t *s);
@@ -93,12 +85,12 @@ static inline int strbuf_allocated(strbuf_t *s)
93 85
94/* Return bytes remaining in the string buffer 86/* Return bytes remaining in the string buffer
95 * Ensure there is space for a NULL terminator. */ 87 * Ensure there is space for a NULL terminator. */
96static inline int strbuf_empty_length(strbuf_t *s) 88static inline size_t strbuf_empty_length(strbuf_t *s)
97{ 89{
98 return s->size - s->length - 1; 90 return s->size - s->length - 1;
99} 91}
100 92
101static inline void strbuf_ensure_empty_length(strbuf_t *s, int len) 93static inline void strbuf_ensure_empty_length(strbuf_t *s, size_t len)
102{ 94{
103 if (len > strbuf_empty_length(s)) 95 if (len > strbuf_empty_length(s))
104 strbuf_resize(s, s->length + len); 96 strbuf_resize(s, s->length + len);
@@ -114,12 +106,12 @@ static inline void strbuf_set_length(strbuf_t *s, int len)
114 s->length = len; 106 s->length = len;
115} 107}
116 108
117static inline void strbuf_extend_length(strbuf_t *s, int len) 109static inline void strbuf_extend_length(strbuf_t *s, size_t len)
118{ 110{
119 s->length += len; 111 s->length += len;
120} 112}
121 113
122static inline int strbuf_length(strbuf_t *s) 114static inline size_t strbuf_length(strbuf_t *s)
123{ 115{
124 return s->length; 116 return s->length;
125} 117}
@@ -135,14 +127,14 @@ static inline void strbuf_append_char_unsafe(strbuf_t *s, const char c)
135 s->buf[s->length++] = c; 127 s->buf[s->length++] = c;
136} 128}
137 129
138static inline void strbuf_append_mem(strbuf_t *s, const char *c, int len) 130static inline void strbuf_append_mem(strbuf_t *s, const char *c, size_t len)
139{ 131{
140 strbuf_ensure_empty_length(s, len); 132 strbuf_ensure_empty_length(s, len);
141 memcpy(s->buf + s->length, c, len); 133 memcpy(s->buf + s->length, c, len);
142 s->length += len; 134 s->length += len;
143} 135}
144 136
145static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, int len) 137static inline void strbuf_append_mem_unsafe(strbuf_t *s, const char *c, size_t len)
146{ 138{
147 memcpy(s->buf + s->length, c, len); 139 memcpy(s->buf + s->length, c, len);
148 s->length += len; 140 s->length += len;
@@ -153,7 +145,7 @@ static inline void strbuf_ensure_null(strbuf_t *s)
153 s->buf[s->length] = 0; 145 s->buf[s->length] = 0;
154} 146}
155 147
156static inline char *strbuf_string(strbuf_t *s, int *len) 148static inline char *strbuf_string(strbuf_t *s, size_t *len)
157{ 149{
158 if (len) 150 if (len)
159 *len = s->length; 151 *len = s->length;