mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-02-04 04:06:06 -05:00
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.
This commit is contained in:
parent
2ab569169e
commit
22266e384b
1 changed files with 28 additions and 23 deletions
51
search.c
51
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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue