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:

committed by
Junio C Hamano

parent
ddb95de33e
commit
f1696ee398
10
strbuf.h
10
strbuf.h
@ -51,7 +51,7 @@ struct strbuf {
|
||||
#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 *);
|
||||
@ -68,6 +68,9 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
|
||||
|
||||
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);
|
||||
@ -75,6 +78,9 @@ static inline void strbuf_addch(struct strbuf *sb, int c) {
|
||||
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));
|
||||
@ -88,7 +94,7 @@ extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
|
||||
|
||||
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);
|
||||
|
||||
|
Reference in New Issue
Block a user