New strbuf APIs: splice and attach.
* strbuf_splice replace a portion of the buffer with another. * strbuf_attach replace a strbuf buffer with the given one, that should be malloc'ed. Then it enforces strbuf's invariants. If alloc > len, then this function has negligible cost, else it will perform a realloc, possibly with a cost. Also some style issues are fixed now. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pierre Habouzit committed
Sep 15, 2007 at 15:56 UTC
917c9a713397b16671ed5b1f1c159515bcfa389e
2 files changed
+57
-15
strbuf.c
+52
-15
@@ -1,30 +1,45 @@
1
#include "cache.h"
2
#include "strbuf.h"
3
4
-void strbuf_init(struct strbuf *sb, size_t hint) {
4
+void strbuf_init(struct strbuf *sb, size_t hint)
5
+{
6
memset(sb, 0, sizeof(*sb));
7
if (hint)
8
strbuf_grow(sb, hint);
9
}
10
10
-void strbuf_release(struct strbuf *sb) {
11
+void strbuf_release(struct strbuf *sb)
12
+{
13
free(sb->buf);
14
memset(sb, 0, sizeof(*sb));
15
}
16
15
-void strbuf_reset(struct strbuf *sb) {
17
+void strbuf_reset(struct strbuf *sb)
18
+{
19
if (sb->len)
20
strbuf_setlen(sb, 0);
21
sb->eof = 0;
22
}
23
21
-char *strbuf_detach(struct strbuf *sb) {
24
+char *strbuf_detach(struct strbuf *sb)
25
+{
26
char *res = sb->buf;
27
strbuf_init(sb, 0);
28
return res;
29
}
30
27
-void strbuf_grow(struct strbuf *sb, size_t extra) {
31
+void strbuf_attach(struct strbuf *sb, void *buf, size_t len, size_t alloc)
32
+{
33
+ strbuf_release(sb);
34
+ sb->buf = buf;
35
+ sb->len = len;
36
+ sb->alloc = alloc;
37
+ strbuf_grow(sb, 0);
38
+ sb->buf[sb->len] = '\0';
39
+}
40
+
41
+void strbuf_grow(struct strbuf *sb, size_t extra)
42
+{
43
if (sb->len + extra + 1 <= sb->len)
44
die("you want to use way too much memory");
45
ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
@@ -37,24 +52,44 @@ void strbuf_rtrim(struct strbuf *sb)
52
sb->buf[sb->len] = '\0';
53
}
54
40
-void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len) {
55
+void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
56
+{
57
strbuf_grow(sb, len);
42
- if (pos >= sb->len) {
43
- pos = sb->len;
44
- } else {
45
- memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
46
- }
58
+ if (pos > sb->len)
59
+ die("`pos' is too far after the end of the buffer");
60
+ memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
61
memcpy(sb->buf + pos, data, len);
62
strbuf_setlen(sb, sb->len + len);
63
}
64
51
-void strbuf_add(struct strbuf *sb, const void *data, size_t len) {
65
+void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
66
+ const void *data, size_t dlen)
67
+{
68
+ if (pos + len < pos)
69
+ die("you want to use way too much memory");
70
+ if (pos > sb->len)
71
+ die("`pos' is too far after the end of the buffer");
72
+ if (pos + len > sb->len)
73
+ die("`pos + len' is too far after the end of the buffer");
74
+
75
+ if (dlen >= len)
76
+ strbuf_grow(sb, dlen - len);
77
+ memmove(sb->buf + pos + dlen,
78
+ sb->buf + pos + len,
79
+ sb->len - pos - len);
80
+ memcpy(sb->buf + pos, data, dlen);
81
+ strbuf_setlen(sb, sb->len + dlen - len);
82
+}
83
+
84
+void strbuf_add(struct strbuf *sb, const void *data, size_t len)
85
+{
86
strbuf_grow(sb, len);
87
memcpy(sb->buf + sb->len, data, len);
88
strbuf_setlen(sb, sb->len + len);
89
}
90
57
-void strbuf_addf(struct strbuf *sb, const char *fmt, ...) {
91
+void strbuf_addf(struct strbuf *sb, const char *fmt, ...)
92
+{
93
int len;
94
va_list ap;
95
@@ -76,7 +111,8 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...) {
111
strbuf_setlen(sb, sb->len + len);
112
}
113
79
-size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) {
114
+size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
115
+{
116
size_t res;
117
118
strbuf_grow(sb, size);
@@ -110,7 +146,8 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
146
return sb->len - oldlen;
147
}
148
113
-void read_line(struct strbuf *sb, FILE *fp, int term) {
149
+void read_line(struct strbuf *sb, FILE *fp, int term)
150
+{
151
int ch;
152
if (feof(fp)) {
153
strbuf_release(sb);
strbuf.h
+5
@@ -55,6 +55,7 @@ extern void strbuf_init(struct strbuf *, size_t);
55
extern void strbuf_release(struct strbuf *);
56
extern void strbuf_reset(struct strbuf *);
57
extern char *strbuf_detach(struct strbuf *);
58
+extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
59
60
/*----- strbuf size related -----*/
61
static inline size_t strbuf_avail(struct strbuf *sb) {
@@ -81,6 +82,10 @@ static inline void strbuf_addch(struct strbuf *sb, int c) {
82
/* inserts after pos, or appends if pos >= sb->len */
83
extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
84
85
+/* splice pos..pos+len with given data */
86
+extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
87
+ const void *, size_t);
88
+
89
extern void strbuf_add(struct strbuf *, const void *, size_t);
90
static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
91
strbuf_add(sb, s, strlen(s));