aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-01-10 12:32:04 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-01-10 12:45:58 +0100
commitb1c4b7249a33098ca7f2e8f2b786e969493dddcf (patch)
tree1a9db1a93281d5b4a8ccef3a68c9a1fb447cda73
parente6e077c8469f857ac58d804c788e887ce86e2303 (diff)
downloadvis-b1c4b7249a33098ca7f2e8f2b786e969493dddcf.tar.gz
vis-b1c4b7249a33098ca7f2e8f2b786e969493dddcf.tar.xz
Simplify code by using text_bytes_alloc0
-rw-r--r--text-regex.c8
-rw-r--r--ui-curses.c7
-rw-r--r--vis-motions.c8
3 files changed, 4 insertions, 19 deletions
diff --git a/text-regex.c b/text-regex.c
index cff8587..dbaa5fc 100644
--- a/text-regex.c
+++ b/text-regex.c
@@ -32,11 +32,9 @@ void text_regex_free(Regex *r) {
}
int text_search_range_forward(Text *txt, size_t pos, size_t len, Regex *r, size_t nmatch, RegexMatch pmatch[], int eflags) {
- char *buf = malloc(len + 1);
+ char *buf = text_bytes_alloc0(txt, pos, len);
if (!buf)
return REG_NOMATCH;
- len = text_bytes_get(txt, pos, len, buf);
- buf[len] = '\0';
regmatch_t match[nmatch];
int ret = regexec(&r->regex, buf, nmatch, match, eflags);
if (!ret) {
@@ -50,11 +48,9 @@ int text_search_range_forward(Text *txt, size_t pos, size_t len, Regex *r, size_
}
int text_search_range_backward(Text *txt, size_t pos, size_t len, Regex *r, size_t nmatch, RegexMatch pmatch[], int eflags) {
- char *buf = malloc(len + 1);
+ char *buf = text_bytes_alloc0(txt, pos, len);
if (!buf)
return REG_NOMATCH;
- len = text_bytes_get(txt, pos, len, buf);
- buf[len] = '\0';
regmatch_t match[nmatch];
char *cur = buf;
int ret = REG_NOMATCH;
diff --git a/ui-curses.c b/ui-curses.c
index a9ae5f9..0bfd9d8 100644
--- a/ui-curses.c
+++ b/ui-curses.c
@@ -988,12 +988,7 @@ static char *ui_prompt_input(Ui *ui) {
if (!uic->prompt_win)
return NULL;
Text *text = vis_file_text(uic->prompt_win->file);
- char *buf = malloc(text_size(text) + 1);
- if (!buf)
- return NULL;
- size_t len = text_bytes_get(text, 0, text_size(text), buf);
- buf[len] = '\0';
- return buf;
+ return text_bytes_alloc0(text, 0, text_size(text));
}
static void ui_prompt_hide(Ui *ui) {
diff --git a/vis-motions.c b/vis-motions.c
index 754b132..8c2146f 100644
--- a/vis-motions.c
+++ b/vis-motions.c
@@ -10,13 +10,7 @@ static char *get_word_at(Text *txt, size_t pos) {
Filerange word = text_object_word(txt, pos);
if (!text_range_valid(&word))
return NULL;
- size_t len = word.end - word.start;
- char *buf = malloc(len+1);
- if (!buf)
- return NULL;
- len = text_bytes_get(txt, word.start, len, buf);
- buf[len] = '\0';
- return buf;
+ return text_bytes_alloc0(txt, word.start, word.end - word.start);
}
/** motion implementations */