* 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>
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);
{
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;
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;
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:
/* 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;
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;
/* 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 */
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);
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;
/*
* 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);
{
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));
{
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*));
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;
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;
#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';
#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);