@matheusbsbs / matheus-bagliano / commits / c76689df6c
Fork 0

strbuf API additions and enhancements.

Add strbuf_remove, change strbuf_insert: As both are special cases of strbuf_splice, implement them as such. gcc is able to do the math and generate almost optimal code this way. Add strbuf_swap: Exchange the values of its arguments. Use it in fast-import.c Also fix spacing issues in strbuf.h Signed-off-by: Pierre Habouzit <madcoder@debian.org>

Pierre Habouzit committed Sep 20, 2007 at 00:42 UTC c76689df6c64a1e987bd779bd71a2042b5131fb9
4 files changed +23 -20
commit.c
+1 -1
@@ -656,7 +656,7 @@ static char *replace_encoding_header(char *buf, const char *encoding)
656 strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
657 if (is_encoding_utf8(encoding)) {
658 /* we have re-coded to UTF-8; drop the header */
659 - strbuf_splice(&tmp, start, len, NULL, 0);
659 + strbuf_remove(&tmp, start, len);
660 } else {
661 /* just replaces XXXX in 'encoding XXXX\n' */
662 strbuf_splice(&tmp, start + strlen("encoding "),
fast-import.c
+1 -3
@@ -1111,9 +1111,7 @@ static int store_object(
1111 if (last->no_swap) {
1112 last->data = *dat;
1113 } else {
1114 - struct strbuf tmp = *dat;
1115 - *dat = last->data;
1116 - last->data = tmp;
1114 + strbuf_swap(&last->data, dat);
1115 }
1116 last->offset = e->offset;
1117 }
strbuf.c
+10 -10
@@ -50,16 +50,6 @@ void strbuf_rtrim(struct strbuf *sb)
50 sb->buf[sb->len] = '\0';
51 }
52
53 -void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
54 -{
55 - strbuf_grow(sb, len);
56 - if (pos > sb->len)
57 - die("`pos' is too far after the end of the buffer");
58 - memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
59 - memcpy(sb->buf + pos, data, len);
60 - strbuf_setlen(sb, sb->len + len);
61 -}
62 -
53 void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
54 const void *data, size_t dlen)
55 {
@@ -79,6 +69,16 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
69 strbuf_setlen(sb, sb->len + dlen - len);
70 }
71
72 +void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
73 +{
74 + strbuf_splice(sb, pos, 0, data, len);
75 +}
76 +
77 +void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
78 +{
79 + strbuf_splice(sb, pos, len, NULL, 0);
80 +}
81 +
82 void strbuf_add(struct strbuf *sb, const void *data, size_t len)
83 {
84 strbuf_grow(sb, len);
strbuf.h
+11 -6
@@ -55,15 +55,20 @@ extern void strbuf_release(struct strbuf *);
55 extern void strbuf_reset(struct strbuf *);
56 extern char *strbuf_detach(struct strbuf *);
57 extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
58 +static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
59 + struct strbuf tmp = *a;
60 + *a = *b;
61 + *b = tmp;
62 +}
63
64 /*----- strbuf size related -----*/
65 static inline size_t strbuf_avail(struct strbuf *sb) {
61 - return sb->alloc ? sb->alloc - sb->len - 1 : 0;
66 + return sb->alloc ? sb->alloc - sb->len - 1 : 0;
67 }
68 static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
64 - assert (len < sb->alloc);
65 - sb->len = len;
66 - sb->buf[len] = '\0';
69 + assert (len < sb->alloc);
70 + sb->len = len;
71 + sb->buf[len] = '\0';
72 }
73
74 extern void strbuf_grow(struct strbuf *, size_t);
@@ -78,12 +83,12 @@ static inline void strbuf_addch(struct strbuf *sb, int c) {
83 sb->buf[sb->len] = '\0';
84 }
85
81 -/* inserts after pos, or appends if pos >= sb->len */
86 extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
87 +extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
88
89 /* splice pos..pos+len with given data */
90 extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
86 - const void *, size_t);
91 + const void *, size_t);
92
93 extern void strbuf_add(struct strbuf *, const void *, size_t);
94 static inline void strbuf_addstr(struct strbuf *sb, const char *s) {