diff options
author | Russell Belfer <rb@github.com> | 2012-07-10 15:10:14 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-07-10 23:19:47 -0700 |
commit | 039fc4067989a14a09784ed16ce7126ac75461cb (patch) | |
tree | 798a1a34cd1de1dabdd462a5c1a017815d65b309 /src/buffer.c | |
parent | 6b9a49cd5fd6addc5b9ce3281ea33a50934f5a94 (diff) | |
download | libgit2-039fc4067989a14a09784ed16ce7126ac75461cb.tar.gz |
Add a couple of useful git_buf utilities
* `git_buf_rfind` (with tests and tests for `git_buf_rfind_next`)
* `git_buf_puts_escaped` and `git_buf_puts_escaped_regex` (with tests)
to copy strings into a buffer while injecting an escape sequence
(e.g. '\') in front of particular characters.
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c index 04aaec3df..d16b17160 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -141,6 +141,40 @@ int git_buf_puts(git_buf *buf, const char *string) return git_buf_put(buf, string, strlen(string)); } +int git_buf_puts_escaped( + git_buf *buf, const char *string, const char *esc_chars, const char *esc_with) +{ + const char *scan = string; + size_t total = 0, esc_with_len = strlen(esc_with); + + while (*scan) { + size_t count = strcspn(scan, esc_chars); + total += count + 1 + esc_with_len; + scan += count + 1; + } + + ENSURE_SIZE(buf, buf->size + total + 1); + + for (scan = string; *scan; ) { + size_t count = strcspn(scan, esc_chars); + + memmove(buf->ptr + buf->size, scan, count); + scan += count; + buf->size += count; + + if (*scan) { + memmove(buf->ptr + buf->size, esc_with, esc_with_len); + buf->size += esc_with_len; + + memmove(buf->ptr + buf->size, scan, 1); + scan += 1; + buf->size += 1; + } + } + + return 0; +} + int git_buf_vprintf(git_buf *buf, const char *format, va_list ap) { int len; |