summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: ddb95de)
raw | patch | inline | side by side (parent: ddb95de)
author | Pierre Habouzit <madcoder@debian.org> | |
Mon, 10 Sep 2007 10:35:04 +0000 (12:35 +0200) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Mon, 10 Sep 2007 19:48:24 +0000 (12:48 -0700) |
* Add strbuf_rtrim to remove trailing spaces.
* Add strbuf_insert to insert data at a given position.
* Off-by one fix in strbuf_addf: strbuf_avail() does not counts the final
\0 so the overflow test for snprintf is the strict comparison. This is
not critical as the growth mechanism chosen will always allocate _more_
memory than asked, so the second test will not fail. It's some kind of
miracle though.
* Add size extension hints for strbuf_init and strbuf_read. If 0, default
applies, else:
+ initial buffer has the given size for strbuf_init.
+ first growth checks it has at least this size rather than the
default 8192.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
* Add strbuf_insert to insert data at a given position.
* Off-by one fix in strbuf_addf: strbuf_avail() does not counts the final
\0 so the overflow test for snprintf is the strict comparison. This is
not critical as the growth mechanism chosen will always allocate _more_
memory than asked, so the second test will not fail. It's some kind of
miracle though.
* Add size extension hints for strbuf_init and strbuf_read. If 0, default
applies, else:
+ initial buffer has the given size for strbuf_init.
+ first growth checks it has at least this size rather than the
default 8192.
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
14 files changed:
archive-tar.c | patch | blob | history | |
builtin-apply.c | patch | blob | history | |
builtin-blame.c | patch | blob | history | |
builtin-checkout-index.c | patch | blob | history | |
builtin-commit-tree.c | patch | blob | history | |
builtin-rerere.c | patch | blob | history | |
builtin-update-index.c | patch | blob | history | |
cache-tree.c | patch | blob | history | |
diff.c | patch | blob | history | |
fast-import.c | patch | blob | history | |
fetch.c | patch | blob | history | |
mktree.c | patch | blob | history | |
strbuf.c | patch | blob | history | |
strbuf.h | patch | blob | history |
diff --git a/archive-tar.c b/archive-tar.c
index 0612bb6051e1c60d5151b0c65d097fe51ba93f2e..cc94cf3672cfe9e15afd74d8156fe1cf5df9e8f6 100644 (file)
--- a/archive-tar.c
+++ b/archive-tar.c
struct strbuf ext_header;
memset(&header, 0, sizeof(header));
- strbuf_init(&ext_header);
+ strbuf_init(&ext_header, 0);
if (!sha1) {
*header.typeflag = TYPEFLAG_GLOBAL_HEADER;
{
struct strbuf ext_header;
- strbuf_init(&ext_header);
+ strbuf_init(&ext_header, 0);
strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
strbuf_release(&ext_header);
diff --git a/builtin-apply.c b/builtin-apply.c
index 90e328ef917cfa591e6621966b5b3bc2ead24075..988e85f1e5d730bdc3b237580fa92e2d9fe3e520 100644 (file)
--- a/builtin-apply.c
+++ b/builtin-apply.c
{
struct strbuf buf;
- strbuf_init(&buf);
- if (strbuf_read(&buf, fd) < 0)
+ strbuf_init(&buf, 0);
+ if (strbuf_read(&buf, fd, 0) < 0)
die("git-apply: read returned %s", strerror(errno));
*sizep = buf.len;
diff --git a/builtin-blame.c b/builtin-blame.c
index 1b1e6da85317200bd73e44071498fbf83d127487..b004f06cd8c247a299dc399bb29bd7f12da17ef9 100644 (file)
--- a/builtin-blame.c
+++ b/builtin-blame.c
@@ -2023,7 +2023,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
origin = make_origin(commit, path);
- strbuf_init(&buf);
+ strbuf_init(&buf, 0);
if (!contents_from || strcmp("-", contents_from)) {
struct stat st;
const char *read_from;
@@ -2046,7 +2046,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
fd = open(read_from, O_RDONLY);
if (fd < 0)
die("cannot open %s", read_from);
- if (strbuf_read(&buf, fd) != xsize_t(st.st_size))
+ if (strbuf_read(&buf, fd, 0) != xsize_t(st.st_size))
die("cannot read %s", read_from);
break;
case S_IFLNK:
@@ -2062,7 +2062,7 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
/* Reading from stdin */
contents_from = "standard input";
mode = 0;
- if (strbuf_read(&buf, 0) < 0)
+ if (strbuf_read(&buf, 0, 0) < 0)
die("read error %s from stdin", strerror(errno));
}
origin->file.ptr = buf.buf;
index 75377b9cab75ac75c6d5d7f79fdcf64bdb27cff2..153ba7dc129d54f09268aa70ab523946007c2821 100644 (file)
--- a/builtin-checkout-index.c
+++ b/builtin-checkout-index.c
struct strbuf buf;
if (all)
die("git-checkout-index: don't mix '--all' and '--stdin'");
- strbuf_init(&buf);
+ strbuf_init(&buf, 0);
while (1) {
char *path_name;
const char *p;
diff --git a/builtin-commit-tree.c b/builtin-commit-tree.c
index bc9502c13525759f92c39e77f18edbb355305ab1..325334fd654696f177c82d045447bc3697af9494 100644 (file)
--- a/builtin-commit-tree.c
+++ b/builtin-commit-tree.c
/* Not having i18n.commitencoding is the same as having utf-8 */
encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
- strbuf_init(&buffer);
- strbuf_grow(&buffer, 8192); /* should avoid reallocs for the headers */
+ strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */
strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree_sha1));
/*
strbuf_addch(&buffer, '\n');
/* And add the comment */
- if (strbuf_read(&buffer, 0) < 0)
+ if (strbuf_read(&buffer, 0, 0) < 0)
die("git-commit-tree: read returned %s", strerror(errno));
/* And check the encoding */
diff --git a/builtin-rerere.c b/builtin-rerere.c
index 98d7702168c03b148b0029f4d8d2e7d161ca80cb..826d346fbcd9c015e027f31ce92e8294081cbdda 100644 (file)
--- a/builtin-rerere.c
+++ b/builtin-rerere.c
FILE *f = fopen(path, "r");
FILE *out;
- strbuf_init(&minus);
- strbuf_init(&plus);
+ strbuf_init(&minus, 0);
+ strbuf_init(&plus, 0);
if (!f)
return error("Could not open %s", path);
diff --git a/builtin-update-index.c b/builtin-update-index.c
index a7a4574f2bff2a7db4a1c25aa4a514ad99760381..9240a288a78239067dc71b5669f7136a4c0c4a0c 100644 (file)
--- a/builtin-update-index.c
+++ b/builtin-update-index.c
static void read_index_info(int line_termination)
{
struct strbuf buf;
- strbuf_init(&buf);
+ strbuf_init(&buf, 0);
while (1) {
char *ptr, *tab;
char *path_name;
}
if (read_from_stdin) {
struct strbuf buf;
- strbuf_init(&buf);
+ strbuf_init(&buf, 0);
while (1) {
char *path_name;
const char *p;
diff --git a/cache-tree.c b/cache-tree.c
index 76af6f5d992d54f4abaafd0a7ea32c82f2d4ab89..8f53c99f154ba2a717d7b99eea9249b5e6ee2fba 100644 (file)
--- a/cache-tree.c
+++ b/cache-tree.c
/*
* Then write out the tree object for this level.
*/
- strbuf_init(&buffer);
- strbuf_grow(&buffer, 8192);
+ strbuf_init(&buffer, 8192);
for (i = 0; i < entries; i++) {
struct cache_entry *ce = cache[i];
struct strbuf buffer;
path[0] = 0;
- strbuf_init(&buffer);
+ strbuf_init(&buffer, 0);
write_one(root, path, 0, &buffer);
*size_p = buffer.len;
return strbuf_detach(&buffer);
index 26d7bb96bcaa52b4a104a2dfa17da388e3032c49..7290309ea9c01a6800aaa25d7a74d789adfd31c7 100644 (file)
--- a/diff.c
+++ b/diff.c
{
struct strbuf buf;
- strbuf_init(&buf);
- if (strbuf_read(&buf, 0) < 0)
+ strbuf_init(&buf, 0);
+ if (strbuf_read(&buf, 0, 0) < 0)
return error("error while reading from stdin %s",
strerror(errno));
diff --git a/fast-import.c b/fast-import.c
index 74ff0fdadd2d8c42191e5a52d68270c92288b528..2c0bfb9fabb114305df2bbe6fc6b15284197951d 100644 (file)
--- a/fast-import.c
+++ b/fast-import.c
{
struct strbuf buffer;
- strbuf_init(&buffer);
+ strbuf_init(&buffer, 0);
if (prefixcmp(command_buf.buf, "data "))
die("Expected 'data n' command, found: %s", command_buf.buf);
git_config(git_default_config);
alloc_objects(object_entry_alloc);
- strbuf_init(&command_buf);
+ strbuf_init(&command_buf, 0);
atom_table = xcalloc(atom_table_sz, sizeof(struct atom_str*));
branch_table = xcalloc(branch_table_sz, sizeof(struct branch*));
avail_tree_table = xcalloc(avail_tree_table_sz, sizeof(struct avail_tree_content*));
index 811be87a3c1e0d14d9f2b37650d56575b49caa22..dd6ed9e41cf7ebbd304568f422da0b14a5e01d27 100644 (file)
--- a/fetch.c
+++ b/fetch.c
int targets = 0, targets_alloc = 0;
struct strbuf buf;
*target = NULL; *write_ref = NULL;
- strbuf_init(&buf);
+ strbuf_init(&buf, 0);
while (1) {
char *rf_one = NULL;
char *tg_one;
diff --git a/mktree.c b/mktree.c
index 2e84889c02dd110caff0e896a6899f877ca863f1..3891cd9fb31bf9aa84a339a70c474c38569affa5 100644 (file)
--- a/mktree.c
+++ b/mktree.c
qsort(entries, used, sizeof(*entries), ent_compare);
for (size = i = 0; i < used; i++)
size += 32 + entries[i]->len;
- strbuf_init(&buf);
- strbuf_grow(&buf, size);
+ strbuf_init(&buf, size);
for (i = 0; i < used; i++) {
struct treeent *ent = entries[i];
strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
av++;
}
- strbuf_init(&sb);
+ strbuf_init(&sb, 0);
while (1) {
char *ptr, *ntr;
unsigned mode;
diff --git a/strbuf.c b/strbuf.c
index 7136de14c63b0a93c271156fcef86ca26cecea7a..d9190477e6dfdea558aa10771c2f643f7c2977d7 100644 (file)
--- a/strbuf.c
+++ b/strbuf.c
#include "cache.h"
#include "strbuf.h"
-void strbuf_init(struct strbuf *sb) {
+void strbuf_init(struct strbuf *sb, size_t hint) {
memset(sb, 0, sizeof(*sb));
+ if (hint)
+ strbuf_grow(sb, hint);
}
void strbuf_release(struct strbuf *sb) {
char *strbuf_detach(struct strbuf *sb) {
char *res = sb->buf;
- strbuf_init(sb);
+ strbuf_init(sb, 0);
return res;
}
ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
}
+void strbuf_rtrim(struct strbuf *sb)
+{
+ while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
+ sb->len--;
+ 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) {
+ pos = sb->len;
+ } else {
+ 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_add(struct strbuf *sb, const void *data, size_t len) {
strbuf_grow(sb, len);
memcpy(sb->buf + sb->len, data, len);
if (len < 0) {
len = 0;
}
- if (len >= strbuf_avail(sb)) {
+ if (len > strbuf_avail(sb)) {
strbuf_grow(sb, len);
va_start(ap, fmt);
len = vsnprintf(sb->buf + sb->len, sb->alloc - sb->len, fmt, ap);
va_end(ap);
- if (len >= strbuf_avail(sb)) {
+ if (len > strbuf_avail(sb)) {
die("this should not happen, your snprintf is broken");
}
}
return res;
}
-ssize_t strbuf_read(struct strbuf *sb, int fd)
+ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
{
size_t oldlen = sb->len;
+ strbuf_grow(sb, hint ? hint : 8192);
for (;;) {
ssize_t cnt;
- strbuf_grow(sb, 8192);
cnt = xread(fd, sb->buf + sb->len, sb->alloc - sb->len - 1);
if (cnt < 0) {
strbuf_setlen(sb, oldlen);
if (!cnt)
break;
sb->len += cnt;
+ strbuf_grow(sb, 8192);
}
sb->buf[sb->len] = '\0';
diff --git a/strbuf.h b/strbuf.h
index b40dc99fd0ac2b052d43bb47401409c7939653fc..21fc111f9d489560348193ad99aa2ac52d9ea7e1 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
#define STRBUF_INIT { 0, 0, 0, NULL }
/*----- strbuf life cycle -----*/
-extern void strbuf_init(struct strbuf *);
+extern void strbuf_init(struct strbuf *, size_t);
extern void strbuf_release(struct strbuf *);
extern void strbuf_reset(struct strbuf *);
extern char *strbuf_detach(struct strbuf *);
extern void strbuf_grow(struct strbuf *, size_t);
+/*----- content related -----*/
+extern void strbuf_rtrim(struct strbuf *);
+
/*----- add data in your buffer -----*/
static inline void strbuf_addch(struct strbuf *sb, int c) {
strbuf_grow(sb, 1);
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_add(struct strbuf *, const void *, size_t);
static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
strbuf_add(sb, s, strlen(s));
extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
/* XXX: if read fails, any partial read is undone */
-extern ssize_t strbuf_read(struct strbuf *, int fd);
+extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
extern void read_line(struct strbuf *, FILE *, int);