summaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-10-01 11:58:15 +0200
committernulltoken <emeric.fermas@gmail.com>2012-10-25 17:42:35 +0200
commit3a14d3e2bca4f1af7de978decda1c7ca74ffd3bf (patch)
treecf8e77e397586dad5eec5879afc0bbd1e33f93fa /src/buffer.c
parentfb39b3a54cfedd1e414dc86f6ff5f9af9190c97b (diff)
downloadlibgit2-3a14d3e2bca4f1af7de978decda1c7ca74ffd3bf.tar.gz
buf: introduce git_buf_splice()
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c
index b40b16b66..e55b0a230 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -549,3 +549,31 @@ void git_buf_unescape(git_buf *buf)
{
buf->size = git__unescape(buf->ptr);
}
+
+int git_buf_splice(
+ git_buf *buf,
+ size_t where,
+ size_t nb_to_remove,
+ const char *data,
+ size_t nb_to_insert)
+{
+ assert(buf &&
+ where <= git_buf_len(buf) &&
+ where + nb_to_remove <= git_buf_len(buf));
+
+ /* Ported from git.git
+ * https://github.com/git/git/blob/16eed7c/strbuf.c#L159-176
+ */
+ if (git_buf_grow(buf, git_buf_len(buf) + nb_to_insert - nb_to_remove) < 0)
+ return -1;
+
+ memmove(buf->ptr + where + nb_to_insert,
+ buf->ptr + where + nb_to_remove,
+ buf->size - where - nb_to_remove);
+
+ memcpy(buf->ptr + where, data, nb_to_insert);
+
+ buf->size = buf->size + nb_to_insert - nb_to_remove;
+ buf->ptr[buf->size] = '\0';
+ return 0;
+}