aboutsummaryrefslogtreecommitdiff
path: root/search.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2022-01-27 17:34:27 +0000
committerDaniel Eklöf <daniel@ekloef.se>2022-01-29 16:50:42 +0000
commit22266e384bab0c211d9ea7901bf6b5cfe64341a2 (patch)
treed773351061940cb53fc3ecf108d3195743571695 /search.c
parentMerge branch 'xcursor-size' (diff)
search: refactor: add add_wchars(), make search_add_chars() use it
search_add_chars() converts the incoming UTF-8 string into a wide-char string, and then calls add_wchars(). add_wchars() inserts the string into the search buffer, at the current cursor position.
Diffstat (limited to 'search.c')
-rw-r--r--search.c51
1 files changed, 28 insertions, 23 deletions
diff --git a/search.c b/search.c
index 817f18b5..2ef6a44f 100644
--- a/search.c
+++ b/search.c
@@ -398,6 +398,33 @@ search_find_next(struct terminal *term)
#undef ROW_DEC
}
+static void
+add_wchars(struct terminal *term, wchar_t *src, size_t count)
+{
+ /* Strip non-printable characters */
+ for (size_t i = 0, j = 0, orig_count = count; i < orig_count; i++) {
+ if (iswprint(src[i]))
+ src[j++] = src[i];
+ else
+ count--;
+ }
+
+ if (!search_ensure_size(term, term->search.len + count))
+ return;
+
+ xassert(term->search.len + count < term->search.sz);
+
+ memmove(&term->search.buf[term->search.cursor + count],
+ &term->search.buf[term->search.cursor],
+ (term->search.len - term->search.cursor) * sizeof(wchar_t));
+
+ memcpy(&term->search.buf[term->search.cursor], src, count * sizeof(wchar_t));
+
+ term->search.len += count;
+ term->search.cursor += count;
+ term->search.buf[term->search.len] = L'\0';
+}
+
void
search_add_chars(struct terminal *term, const char *src, size_t count)
{
@@ -414,29 +441,7 @@ search_add_chars(struct terminal *term, const char *src, size_t count)
ps = (mbstate_t){0};
wchar_t wcs[wchars + 1];
mbsnrtowcs(wcs, &_src, count, wchars, &ps);
-
- /* Strip non-printable characters */
- for (size_t i = 0, j = 0, orig_wchars = wchars; i < orig_wchars; i++) {
- if (iswprint(wcs[i]))
- wcs[j++] = wcs[i];
- else
- wchars--;
- }
-
- if (!search_ensure_size(term, term->search.len + wchars))
- return;
-
- xassert(term->search.len + wchars < term->search.sz);
-
- memmove(&term->search.buf[term->search.cursor + wchars],
- &term->search.buf[term->search.cursor],
- (term->search.len - term->search.cursor) * sizeof(wchar_t));
-
- memcpy(&term->search.buf[term->search.cursor], wcs, wchars * sizeof(wchar_t));
-
- term->search.len += wchars;
- term->search.cursor += wchars;
- term->search.buf[term->search.len] = L'\0';
+ add_wchars(term, wcs, wchars);
}
static void