summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 19247e5)
raw | patch | inline | side by side (parent: 19247e5)
author | Pierre Habouzit <madcoder@debian.org> | |
Wed, 19 Sep 2007 22:42:12 +0000 (00:42 +0200) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Fri, 21 Sep 2007 06:17:40 +0000 (23:17 -0700) |
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>
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>
commit.c | patch | blob | history | |
fast-import.c | patch | blob | history | |
strbuf.c | patch | blob | history | |
strbuf.h | patch | blob | history |
diff --git a/commit.c b/commit.c
index f86fa776c05b27d0e353cc70ee2b49d29a8de2e7..55b08ec0b93040785792afbe0c4013fe410a9d40 100644 (file)
--- a/commit.c
+++ b/commit.c
strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1);
if (is_encoding_utf8(encoding)) {
/* we have re-coded to UTF-8; drop the header */
- strbuf_splice(&tmp, start, len, NULL, 0);
+ strbuf_remove(&tmp, start, len);
} else {
/* just replaces XXXX in 'encoding XXXX\n' */
strbuf_splice(&tmp, start + strlen("encoding "),
diff --git a/fast-import.c b/fast-import.c
index f9906586ee252519e65f6d9c4e6a2cff417262bc..eddae22ea11c6045f2e7ed12b98163364715575a 100644 (file)
--- a/fast-import.c
+++ b/fast-import.c
if (last->no_swap) {
last->data = *dat;
} else {
- struct strbuf tmp = *dat;
- *dat = last->data;
- last->data = tmp;
+ strbuf_swap(&last->data, dat);
}
last->offset = e->offset;
}
diff --git a/strbuf.c b/strbuf.c
index 59383ac7763dd58ba8da1c1daea6906740affea0..dcb725dcdd2f8edb04502fed425bd3b7a7204ebf 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
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,
const void *data, size_t dlen)
{
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)
{
strbuf_grow(sb, len);
diff --git a/strbuf.h b/strbuf.h
index b2cbd976f4d93a326a292e95c245eaeeaa4b73dc..567e2d17d1a5300b35b010c73ed68a376c6001cb 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
extern void strbuf_reset(struct strbuf *);
extern char *strbuf_detach(struct strbuf *);
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 -----*/
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) {
- assert (len < sb->alloc);
- sb->len = len;
- sb->buf[len] = '\0';
+ assert (len < sb->alloc);
+ sb->len = len;
+ sb->buf[len] = '\0';
}
extern void strbuf_grow(struct strbuf *, size_t);
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_remove(struct strbuf *, size_t pos, size_t len);
/* splice pos..pos+len with given data */
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);
static inline void strbuf_addstr(struct strbuf *sb, const char *s) {