Strbuf API extensions and fixes.

* 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>
This commit is contained in:
Pierre Habouzit
2007-09-10 12:35:04 +02:00
committed by Junio C Hamano
parent ddb95de33e
commit f1696ee398
14 changed files with 58 additions and 34 deletions

View File

@ -293,8 +293,7 @@ static int update_one(struct cache_tree *it,
/*
* 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];
@ -419,7 +418,7 @@ void *cache_tree_write(struct cache_tree *root, unsigned long *size_p)
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);