diff options
author | Russell Belfer <rb@github.com> | 2012-11-28 21:58:48 +0400 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-11-28 21:58:48 +0400 |
commit | 7bf87ab6987cf6b9e166e23d2d9dbdcd2511fb32 (patch) | |
tree | dcc8a92ce69b2a0d9d8cca98d67f0cc71177ce40 /src/buffer.c | |
parent | 693021262ba0eeac2923bbce1b2262717019c807 (diff) |
Consolidate text buffer functions
There are many scattered functions that look into the contents of
buffers to do various text manipulations (such as escaping or
unescaping data, calculating text stats, guessing if content is
binary, etc). This groups all those functions together into a
new file and converts the code to use that.
This has two enhancements to existing functionality. The old
text stats function is significantly rewritten and the BOM
detection code was extended (although largely we can't deal with
anything other than a UTF8 BOM).
Diffstat (limited to 'src/buffer.c')
-rw-r--r-- | src/buffer.c | 114 |
1 files changed, 6 insertions, 108 deletions
diff --git a/src/buffer.c b/src/buffer.c index e55b0a230..0e2c005df 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -31,15 +31,7 @@ void git_buf_init(git_buf *buf, size_t initial_size) git_buf_grow(buf, initial_size); } -int git_buf_grow(git_buf *buf, size_t target_size) -{ - int error = git_buf_try_grow(buf, target_size); - if (error != 0) - buf->ptr = git_buf__oom; - return error; -} - -int git_buf_try_grow(git_buf *buf, size_t target_size) +int git_buf_try_grow(git_buf *buf, size_t target_size, bool mark_oom) { char *new_ptr; size_t new_size; @@ -67,8 +59,12 @@ int git_buf_try_grow(git_buf *buf, size_t target_size) new_size = (new_size + 7) & ~7; new_ptr = git__realloc(new_ptr, new_size); - if (!new_ptr) + + if (!new_ptr) { + if (mark_oom) + buf->ptr = git_buf__oom; return -1; + } buf->asize = new_size; buf->ptr = new_ptr; @@ -141,51 +137,6 @@ 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; - size_t total = 0, esc_len = strlen(esc_with), count; - - if (!string) - return 0; - - for (scan = string; *scan; ) { - /* count run of non-escaped characters */ - count = strcspn(scan, esc_chars); - total += count; - scan += count; - /* count run of escaped characters */ - count = strspn(scan, esc_chars); - total += count * (esc_len + 1); - scan += count; - } - - ENSURE_SIZE(buf, buf->size + total + 1); - - for (scan = string; *scan; ) { - count = strcspn(scan, esc_chars); - - memmove(buf->ptr + buf->size, scan, count); - scan += count; - buf->size += count; - - for (count = strspn(scan, esc_chars); count > 0; --count) { - /* copy escape sequence */ - memmove(buf->ptr + buf->size, esc_with, esc_len); - buf->size += esc_len; - /* copy character to be escaped */ - buf->ptr[buf->size] = *scan; - buf->size++; - scan++; - } - } - - buf->ptr[buf->size] = '\0'; - - return 0; -} - static const char b64str[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -497,59 +448,6 @@ int git_buf_cmp(const git_buf *a, const git_buf *b) (a->size < b->size) ? -1 : (a->size > b->size) ? 1 : 0; } -int git_buf_common_prefix(git_buf *buf, const git_strarray *strings) -{ - size_t i; - const char *str, *pfx; - - git_buf_clear(buf); - - if (!strings || !strings->count) - return 0; - - /* initialize common prefix to first string */ - if (git_buf_sets(buf, strings->strings[0]) < 0) - return -1; - - /* go through the rest of the strings, truncating to shared prefix */ - for (i = 1; i < strings->count; ++i) { - - for (str = strings->strings[i], pfx = buf->ptr; - *str && *str == *pfx; str++, pfx++) - /* scanning */; - - git_buf_truncate(buf, pfx - buf->ptr); - - if (!buf->size) - break; - } - - return 0; -} - -bool git_buf_is_binary(const git_buf *buf) -{ - size_t i; - int printable = 0, nonprintable = 0; - - for (i = 0; i < buf->size; i++) { - unsigned char c = buf->ptr[i]; - if (c > 0x1F && c < 0x7F) - printable++; - else if (c == '\0') - return true; - else if (!git__isspace(c)) - nonprintable++; - } - - return ((printable >> 7) < nonprintable); -} - -void git_buf_unescape(git_buf *buf) -{ - buf->size = git__unescape(buf->ptr); -} - int git_buf_splice( git_buf *buf, size_t where, |