diff options
author | Russell Belfer <rb@github.com> | 2013-07-01 10:20:38 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-07-01 10:20:38 -0700 |
commit | 278ce7468d3870bb18d69bd8177bae406d6cede4 (patch) | |
tree | 9e00f07d6d12ed41a35973f882703d1084842cbd | |
parent | c61300eda94eedb53f072fedbf1bb52be21ca221 (diff) | |
download | libgit2-278ce7468d3870bb18d69bd8177bae406d6cede4.tar.gz |
Add helpful buffer shorten function
-rw-r--r-- | src/buffer.c | 9 | ||||
-rw-r--r-- | src/buffer.h | 1 | ||||
-rw-r--r-- | src/config_file.c | 2 | ||||
-rw-r--r-- | src/indexer.c | 2 |
4 files changed, 12 insertions, 2 deletions
diff --git a/src/buffer.c b/src/buffer.c index 6e3ffe560..b5b2fd678 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -259,6 +259,15 @@ void git_buf_truncate(git_buf *buf, size_t len) } } +void git_buf_shorten(git_buf *buf, size_t amount) +{ + if (amount > buf->size) + amount = buf->size; + + buf->size = buf->size - amount; + buf->ptr[buf->size] = '\0'; +} + void git_buf_rtruncate_at_char(git_buf *buf, char separator) { ssize_t idx = git_buf_rfind_next(buf, separator); diff --git a/src/buffer.h b/src/buffer.h index 5402f3827..f3e1d506f 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -91,6 +91,7 @@ int git_buf_vprintf(git_buf *buf, const char *format, va_list ap); void git_buf_clear(git_buf *buf); void git_buf_consume(git_buf *buf, const char *end); void git_buf_truncate(git_buf *buf, size_t len); +void git_buf_shorten(git_buf *buf, size_t amount); void git_buf_rtruncate_at_char(git_buf *path, char separator); int git_buf_join_n(git_buf *buf, char separator, int nbuf, ...); diff --git a/src/config_file.c b/src/config_file.c index dec952115..2b0732a13 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -1395,7 +1395,7 @@ static int parse_multiline_variable(diskfile_backend *cfg, git_buf *value, int i * standard, this character **has** to be last one in the buf, with * no whitespace after it */ assert(is_multiline_var(value->ptr)); - git_buf_truncate(value, git_buf_len(value) - 1); + git_buf_shorten(value, 1); proc_line = fixup_line(line, in_quotes); if (proc_line == NULL) { diff --git a/src/indexer.c b/src/indexer.c index 1b5339f23..1b638cd8a 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -602,7 +602,7 @@ int git_indexer_stream_finalize(git_indexer_stream *idx, git_transfer_progress * git_vector_sort(&idx->objects); git_buf_sets(&filename, idx->pack->pack_name); - git_buf_truncate(&filename, filename.size - strlen("pack")); + git_buf_shorten(&filename, strlen("pack")); git_buf_puts(&filename, "idx"); if (git_buf_oom(&filename)) return -1; |