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.h | |
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.h')
-rw-r--r-- | src/buffer.h | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/src/buffer.h b/src/buffer.h index 50c75f64e..75f3b0e4f 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -91,6 +91,18 @@ int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...); int git_buf_join(git_buf *buf, char separator, const char *str_a, const char *str_b); /** + * Copy string into buf prefixing every character that is contained in the + * esc_chars string with the esc_with string. + */ +int git_buf_puts_escaped( + git_buf *buf, const char *string, const char *esc_chars, const char *esc_with); + +GIT_INLINE(int) git_buf_puts_escape_regex(git_buf *buf, const char *string) +{ + return git_buf_puts_escaped(buf, string, "^.[]$()|*+?{}\\", "\\"); +} + +/** * Join two strings as paths, inserting a slash between as needed. * @return 0 on success, -1 on failure */ @@ -121,6 +133,13 @@ GIT_INLINE(ssize_t) git_buf_rfind_next(git_buf *buf, char ch) return idx; } +GIT_INLINE(ssize_t) git_buf_rfind(git_buf *buf, char ch) +{ + ssize_t idx = (ssize_t)buf->size - 1; + while (idx >= 0 && buf->ptr[idx] != ch) idx--; + return idx; +} + /* Remove whitespace from the end of the buffer */ void git_buf_rtrim(git_buf *buf); |