2 * resizable string buffer
4 * (c) 2017-2020 Steve Bennett <steveb@workware.net.au>
6 * See utf8.c for licence details.
15 #include "stringbuf.h"
21 #define SB_INCREMENT 200
23 stringbuf *sb_alloc(void)
25 stringbuf *sb = (stringbuf *)malloc(sizeof(*sb));
36 void sb_free(stringbuf *sb)
44 void sb_realloc(stringbuf *sb, int newlen)
46 sb->data = (char *)realloc(sb->data, newlen);
47 sb->remaining = newlen - sb->last;
50 void sb_append(stringbuf *sb, const char *str)
52 sb_append_len(sb, str, strlen(str));
55 void sb_append_len(stringbuf *sb, const char *str, int len)
57 if (sb->remaining < len + 1) {
58 sb_realloc(sb, sb->last + len + 1 + SB_INCREMENT);
60 memcpy(sb->data + sb->last, str, len);
61 sb->data[sb->last + len] = 0;
66 sb->chars += utf8_strlen(str, len);
70 char *sb_to_string(stringbuf *sb)
72 if (sb->data == NULL) {
73 /* Return an allocated empty string, not null */
77 /* Just return the data and free the stringbuf structure */
84 /* Insert and delete operations */
86 /* Moves up all the data at position 'pos' and beyond by 'len' bytes
87 * to make room for new data
89 * Note: Does *not* update sb->chars
91 static void sb_insert_space(stringbuf *sb, int pos, int len)
93 assert(pos <= sb->last);
95 /* Make sure there is enough space */
96 if (sb->remaining < len) {
97 sb_realloc(sb, sb->last + len + SB_INCREMENT);
100 memmove(sb->data + pos + len, sb->data + pos, sb->last - pos);
102 sb->remaining -= len;
103 /* And null terminate */
104 sb->data[sb->last] = 0;
108 * Move down all the data from pos + len, effectively
109 * deleting the data at position 'pos' of length 'len'
111 static void sb_delete_space(stringbuf *sb, int pos, int len)
113 assert(pos < sb->last);
114 assert(pos + len <= sb->last);
117 sb->chars -= utf8_strlen(sb->data + pos, len);
121 memmove(sb->data + pos, sb->data + pos + len, sb->last - pos - len);
123 sb->remaining += len;
124 /* And null terminate */
125 sb->data[sb->last] = 0;
128 void sb_insert(stringbuf *sb, int index, const char *str)
130 if (index >= sb->last) {
131 /* Inserting after the end of the list appends. */
135 int len = strlen(str);
137 sb_insert_space(sb, index, len);
138 memcpy(sb->data + index, str, len);
140 sb->chars += utf8_strlen(str, len);
146 * Delete the bytes at index 'index' for length 'len'
147 * Has no effect if the index is past the end of the list.
149 void sb_delete(stringbuf *sb, int index, int len)
151 if (index < sb->last) {
152 char *pos = sb->data + index;
157 sb_delete_space(sb, pos - sb->data, len);
161 void sb_clear(stringbuf *sb)