A utility for dealing with strings in C
git clone git://soophie.de/buf
diff --git a/src/buf.h b/src/buf.h
index c57b44e..13f1524 100644
--- a/src/buf.h
+++ b/src/buf.h
1
@@ -43,6 +43,9 @@ bool buf_write_len(buf_t *self, const void *bytes, size_t len);2
bool buf_write(buf_t *self, const void *bytes);3
void buf_writef(buf_t *self, const char *fmt, ...);4
bool buf_read(buf_t *self, void *bytes, size_t len);5
+void buf_concat(buf_t *self, buf_t buf);6
+void buf_repall_len(buf_t *self, const char *pat, const char *rep, size_t len);7
+void buf_repall(buf_t *self, const char *pat, const char *rep);9
#ifdef BUF_IMPL11
@@ -105,4 +108,38 @@ bool buf_read(buf_t *self, void *bytes, size_t len) {12
return true;13
}15
+void buf_concat(buf_t *self, buf_t buf) {16
+ buf_write_len(self, buf.bytes, buf.len);17
+}18
+19
+void buf_repall_len(buf_t *self, const char *pat, const char *rep, size_t len) {20
+ if (!self || !self->bytes || !pat || !rep) return;21
+ const char *bytes = (const char *) self->bytes;22
+ size_t pat_len = strlen(pat);23
+ size_t rep_len = strlen(rep);24
+ if (pat_len == 0) return;25
+ buf_t out = BUF_NULL;26
+ size_t idx = 0;27
+ while (idx < len && *bytes) {28
+ size_t rem = len - idx;29
+ if (rem >= pat_len && strncmp(bytes, pat, pat_len) == 0) {30
+ buf_write_len(&out, rep, rep_len);31
+ bytes += pat_len;32
+ idx += pat_len;33
+ }34
+ else {35
+ buf_write_len(&out, bytes, 1);36
+ bytes++;37
+ idx++;38
+ }39
+ }40
+ buf_free(self);41
+ buf_write_len(self, out.bytes, out.len);42
+ buf_free(&out);43
+}44
+45
+void buf_repall(buf_t *self, const char *pat, const char *rep) {46
+ buf_repall_len(self, pat, rep, self->len);47
+}48
+49
#endif /* BUF_IMPL */