mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-17 05:33:52 -04: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
|
#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
|
void
|
||||||
search_add_chars(struct terminal *term, const char *src, size_t count)
|
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};
|
ps = (mbstate_t){0};
|
||||||
wchar_t wcs[wchars + 1];
|
wchar_t wcs[wchars + 1];
|
||||||
mbsnrtowcs(wcs, &_src, count, wchars, &ps);
|
mbsnrtowcs(wcs, &_src, count, wchars, &ps);
|
||||||
|
add_wchars(term, wcs, wchars);
|
||||||
/* 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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue