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

@ -1,8 +1,10 @@
#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) {
@ -18,7 +20,7 @@ void strbuf_reset(struct strbuf *sb) {
char *strbuf_detach(struct strbuf *sb) {
char *res = sb->buf;
strbuf_init(sb);
strbuf_init(sb, 0);
return res;
}
@ -28,6 +30,24 @@ void strbuf_grow(struct strbuf *sb, size_t extra) {
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);
@ -44,12 +64,12 @@ void strbuf_addf(struct strbuf *sb, const char *fmt, ...) {
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");
}
}
@ -67,14 +87,14 @@ size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f) {
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);
@ -83,6 +103,7 @@ ssize_t strbuf_read(struct strbuf *sb, int fd)
if (!cnt)
break;
sb->len += cnt;
strbuf_grow(sb, 8192);
}
sb->buf[sb->len] = '\0';