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>
This commit is contained in:
Pierre Habouzit 2007-09-20 00:42:12 +02:00 committed by Junio C Hamano
parent 19247e5510
commit c76689df6c
4 changed files with 23 additions and 20 deletions

View File

@ -656,7 +656,7 @@ static char *replace_encoding_header(char *buf, const char *encoding)
strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1); strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
if (is_encoding_utf8(encoding)) { if (is_encoding_utf8(encoding)) {
/* we have re-coded to UTF-8; drop the header */ /* we have re-coded to UTF-8; drop the header */
strbuf_splice(&tmp, start, len, NULL, 0); strbuf_remove(&tmp, start, len);
} else { } else {
/* just replaces XXXX in 'encoding XXXX\n' */ /* just replaces XXXX in 'encoding XXXX\n' */
strbuf_splice(&tmp, start + strlen("encoding "), strbuf_splice(&tmp, start + strlen("encoding "),

View File

@ -1111,9 +1111,7 @@ static int store_object(
if (last->no_swap) { if (last->no_swap) {
last->data = *dat; last->data = *dat;
} else { } else {
struct strbuf tmp = *dat; strbuf_swap(&last->data, dat);
*dat = last->data;
last->data = tmp;
} }
last->offset = e->offset; last->offset = e->offset;
} }

View File

@ -50,16 +50,6 @@ void strbuf_rtrim(struct strbuf *sb)
sb->buf[sb->len] = '\0'; sb->buf[sb->len] = '\0';
} }
void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
{
strbuf_grow(sb, len);
if (pos > sb->len)
die("`pos' is too far after the end of the buffer");
memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
memcpy(sb->buf + pos, data, len);
strbuf_setlen(sb, sb->len + len);
}
void strbuf_splice(struct strbuf *sb, size_t pos, size_t len, void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
const void *data, size_t dlen) const void *data, size_t dlen)
{ {
@ -79,6 +69,16 @@ void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
strbuf_setlen(sb, sb->len + dlen - len); strbuf_setlen(sb, sb->len + dlen - len);
} }
void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
{
strbuf_splice(sb, pos, 0, data, len);
}
void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
{
strbuf_splice(sb, pos, len, NULL, 0);
}
void strbuf_add(struct strbuf *sb, const void *data, size_t len) void strbuf_add(struct strbuf *sb, const void *data, size_t len)
{ {
strbuf_grow(sb, len); strbuf_grow(sb, len);

View File

@ -55,15 +55,20 @@ extern void strbuf_release(struct strbuf *);
extern void strbuf_reset(struct strbuf *); extern void strbuf_reset(struct strbuf *);
extern char *strbuf_detach(struct strbuf *); extern char *strbuf_detach(struct strbuf *);
extern void strbuf_attach(struct strbuf *, void *, size_t, size_t); extern void strbuf_attach(struct strbuf *, void *, size_t, size_t);
static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
struct strbuf tmp = *a;
*a = *b;
*b = tmp;
}
/*----- strbuf size related -----*/ /*----- strbuf size related -----*/
static inline size_t strbuf_avail(struct strbuf *sb) { static inline size_t strbuf_avail(struct strbuf *sb) {
return sb->alloc ? sb->alloc - sb->len - 1 : 0; return sb->alloc ? sb->alloc - sb->len - 1 : 0;
} }
static inline void strbuf_setlen(struct strbuf *sb, size_t len) { static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
assert (len < sb->alloc); assert (len < sb->alloc);
sb->len = len; sb->len = len;
sb->buf[len] = '\0'; sb->buf[len] = '\0';
} }
extern void strbuf_grow(struct strbuf *, size_t); extern void strbuf_grow(struct strbuf *, size_t);
@ -78,12 +83,12 @@ static inline void strbuf_addch(struct strbuf *sb, int c) {
sb->buf[sb->len] = '\0'; sb->buf[sb->len] = '\0';
} }
/* inserts after pos, or appends if pos >= sb->len */
extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t); extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);
/* splice pos..pos+len with given data */ /* splice pos..pos+len with given data */
extern void strbuf_splice(struct strbuf *, size_t pos, size_t len, extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
const void *, size_t); const void *, size_t);
extern void strbuf_add(struct strbuf *, const void *, size_t); extern void strbuf_add(struct strbuf *, const void *, size_t);
static inline void strbuf_addstr(struct strbuf *sb, const char *s) { static inline void strbuf_addstr(struct strbuf *sb, const char *s) {