diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/buffer.c | 28 | ||||
-rw-r--r-- | src/buffer.h | 25 |
2 files changed, 53 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; +} diff --git a/src/buffer.h b/src/buffer.h index 2aae06c7c..a2896d486 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -158,4 +158,29 @@ void git_buf_unescape(git_buf *buf); /* Write data as base64 encoded in buffer */ int git_buf_put_base64(git_buf *buf, const char *data, size_t len); +/* + * Insert, remove or replace a portion of the buffer. + * + * @param buf The buffer to work with + * + * @param where The location in the buffer where the transformation + * should be applied. + * + * @param nb_to_remove The number of chars to be removed. 0 to not + * remove any character in the buffer. + * + * @param data A pointer to the data which should be inserted. + * + * @param nb_to_insert The number of chars to be inserted. 0 to not + * insert any character from the buffer. + * + * @return 0 or an error code. + */ +int git_buf_splice( + git_buf *buf, + size_t where, + size_t nb_to_remove, + const char *data, + size_t nb_to_insert); + #endif |