diff options
author | Vicent Martà <vicent@github.com> | 2012-07-12 09:32:44 -0700 |
---|---|---|
committer | Vicent Martà <vicent@github.com> | 2012-07-12 09:32:44 -0700 |
commit | 48bcf81dd2584d91e5922dd1458dab6c4173bdcf (patch) | |
tree | c834ff548520151966e6b04ef2ec19d475ff18e6 /src/buffer.c | |
parent | 111ee3fe2d4c6de6729b94235c709986b4079c4b (diff) | |
parent | 54e29b9380f1cd0fa596d71c317de907da19b13d (diff) | |
download | libgit2-48bcf81dd2584d91e5922dd1458dab6c4173bdcf.tar.gz |
Merge pull request #812 from arrbee/assorted-tweaks
Assorted goodies
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/buffer.c b/src/buffer.c index 04aaec3df..5d54ee1a5 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -141,6 +141,42 @@ 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; + } + } + + buf->ptr[buf->size] = '\0'; + + return 0; +} + int git_buf_vprintf(git_buf *buf, const char *format, va_list ap) { int len; |