Skip to content

Commit

Permalink
compat: restore needed functions in strbuf
Browse files Browse the repository at this point in the history
Downstream changes depend on strbuf_swap(), strbuf_remove() and strbuf_addstr().

Signed-off-by: David Barr <davidbarr@google.com>
  • Loading branch information
barrbrain committed May 17, 2012
1 parent 4ab7bd1 commit 5c34dc4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
31 changes: 31 additions & 0 deletions compat/strbuf.c
Expand Up @@ -7,6 +7,8 @@
* - use compat-util.h in place of cache.h.
* - include strbuf.h directly instead of through refs.h.
* - remove unneeded functions.
* Modifications (2012-05-18):
* - restore functions needed for downstream changes.
*/

#include "compat-util.h"
Expand Down Expand Up @@ -64,3 +66,32 @@ size_t strbuf_fread(struct strbuf *sb, size_t size, FILE *f)
strbuf_release(sb);
return res;
}

void strbuf_splice(struct strbuf *sb, size_t pos, size_t len,
const void *data, size_t dlen)
{
if (unsigned_add_overflows(pos, len))
die("you want to use way too much memory");
if (pos > sb->len)
die("`pos' is too far after the end of the buffer");
if (pos + len > sb->len)
die("`pos + len' is too far after the end of the buffer");

if (dlen >= len)
strbuf_grow(sb, dlen - len);
memmove(sb->buf + pos + dlen,
sb->buf + pos + len,
sb->len - pos - len);
memcpy(sb->buf + pos, data, dlen);
strbuf_setlen(sb, sb->len + dlen - len);
}

void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len)
{
strbuf_splice(sb, pos, 0, data, len);
}

void strbuf_remove(struct strbuf *sb, size_t pos, size_t len)
{
strbuf_splice(sb, pos, len, NULL, 0);
}
17 changes: 17 additions & 0 deletions compat/strbuf.h
Expand Up @@ -5,6 +5,8 @@
* Modifications (2010-10-19):
* - add license header.
* - remove unneeded functions.
* Modifications (2012-05-18):
* - restore functions needed for downstream changes.
*/

#ifndef STRBUF_H
Expand Down Expand Up @@ -57,6 +59,11 @@ struct strbuf {
/*----- strbuf life cycle -----*/
extern void strbuf_init(struct strbuf *, size_t);
extern void strbuf_release(struct strbuf *);
static inline void strbuf_swap(struct strbuf *a, struct strbuf *b) {
struct strbuf tmp = *a;
*a = *b;
*b = tmp;
}

/*----- strbuf size related -----*/
extern void strbuf_grow(struct strbuf *, size_t);
Expand All @@ -78,6 +85,16 @@ static inline void strbuf_addch(struct strbuf *sb, int c) {
}

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 void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
extern void strbuf_remove(struct strbuf *, size_t pos, size_t len);

/* splice pos..pos+len with given data */
extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
const void *, size_t);

extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);

Expand Down

0 comments on commit 5c34dc4

Please sign in to comment.