aboutsummaryrefslogtreecommitdiff
path: root/search.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2021-08-21 12:50:42 +0000
committerDaniel Eklöf <daniel@ekloef.se>2022-02-05 16:00:54 +0000
commite0227266cabc015c282774ac898e3f52dce943df (patch)
tree84a5a7c58ec52eff77c9ee4d9b7a6fe9bb0f1718 /search.c
parentMerge branch 'config-no-null-strings-in-printf' (diff)
fcft: adapt to API changes in fcft-3.x
Fcft no longer uses wchar_t, but plain uint32_t to represent codepoints. Since we do a fair amount of string operations in foot, it still makes sense to use something that actually _is_ a string (or character), rather than an array of uint32_t. For this reason, we switch out all wchar_t usage in foot to char32_t. We also verify, at compile-time, that char32_t used UTF-32 (which is what fcft expects). Unfortunately, there are no string functions for char32_t. To avoid having to re-implement all wcs*() functions, we add a small wrapper layer of c32*() functions. These wrapper functions take char32_t arguments, but then simply call the corresponding wcs*() function. For this to work, wcs*() must _also_ be UTF-32 compatible. We can check for the presence of the __STDC_ISO_10646__ macro. If set, wchar_t is at least 4 bytes and its internal representation is UTF-32. FreeBSD does *not* define this macro, because its internal wchar_t representation depends on the current locale. It _does_ use UTF-32 _if_ the current locale is UTF-8. Since foot enforces UTF-8, we simply need to check if __FreeBSD__ is defined. Other fcft API changes: * fcft_glyph_rasterize() -> fcft_codepoint_rasterize() * font.space_advance has been removed * ‘tags’ have been removed from fcft_grapheme_rasterize() * ‘fcft_log_init()’ removed * ‘fcft_init()’ and ‘fcft_fini()’ must be explicitly called
Diffstat (limited to 'search.c')
-rw-r--r--search.c82
1 files changed, 38 insertions, 44 deletions
diff --git a/search.c b/search.c
index 8c5051af..8015f265 100644
--- a/search.c
+++ b/search.c
@@ -1,8 +1,6 @@
#include "search.h"
#include <string.h>
-#include <wchar.h>
-#include <wctype.h>
#include <wayland-client.h>
#include <xkbcommon/xkbcommon-compose.h>
@@ -10,6 +8,7 @@
#define LOG_MODULE "search"
#define LOG_ENABLE_DBG 0
#include "log.h"
+#include "char32.h"
#include "config.h"
#include "extract.h"
#include "grid.h"
@@ -64,7 +63,7 @@ search_ensure_size(struct terminal *term, size_t wanted_size)
{
while (wanted_size >= term->search.sz) {
size_t new_sz = term->search.sz == 0 ? 64 : term->search.sz * 2;
- wchar_t *new_buf = realloc(term->search.buf, new_sz * sizeof(term->search.buf[0]));
+ char32_t *new_buf = realloc(term->search.buf, new_sz * sizeof(term->search.buf[0]));
if (new_buf == NULL) {
LOG_ERRNO("failed to resize search buffer");
@@ -145,7 +144,7 @@ search_begin(struct terminal *term)
term->search.len = 0;
term->search.sz = 64;
term->search.buf = xmalloc(term->search.sz * sizeof(term->search.buf[0]));
- term->search.buf[0] = L'\0';
+ term->search.buf[0] = U'\0';
term_xcursor_update(term);
render_refresh_search(term);
@@ -256,7 +255,7 @@ matches_cell(const struct terminal *term, const struct cell *cell, size_t search
{
assert(search_ofs < term->search.len);
- wchar_t base = cell->wc;
+ char32_t base = cell->wc;
const struct composed *composed = NULL;
if (base >= CELL_COMB_CHARS_LO && base <= CELL_COMB_CHARS_HI)
@@ -265,10 +264,10 @@ matches_cell(const struct terminal *term, const struct cell *cell, size_t search
base = composed->chars[0];
}
- if (composed == NULL && base == 0 && term->search.buf[search_ofs] == L' ')
+ if (composed == NULL && base == 0 && term->search.buf[search_ofs] == U' ')
return 1;
- if (wcsncasecmp(&base, &term->search.buf[search_ofs], 1) != 0)
+ if (c32ncasecmp(&base, &term->search.buf[search_ofs], 1) != 0)
return -1;
if (composed != NULL) {
@@ -405,11 +404,11 @@ search_find_next(struct terminal *term)
}
static void
-add_wchars(struct terminal *term, wchar_t *src, size_t count)
+add_wchars(struct terminal *term, char32_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]))
+ if (isc32print(src[i]))
src[j++] = src[i];
else
count--;
@@ -422,32 +421,27 @@ add_wchars(struct terminal *term, wchar_t *src, size_t count)
memmove(&term->search.buf[term->search.cursor + count],
&term->search.buf[term->search.cursor],
- (term->search.len - term->search.cursor) * sizeof(wchar_t));
+ (term->search.len - term->search.cursor) * sizeof(char32_t));
- memcpy(&term->search.buf[term->search.cursor], src, count * sizeof(wchar_t));
+ memcpy(&term->search.buf[term->search.cursor], src, count * sizeof(char32_t));
term->search.len += count;
term->search.cursor += count;
- term->search.buf[term->search.len] = L'\0';
+ term->search.buf[term->search.len] = U'\0';
}
void
search_add_chars(struct terminal *term, const char *src, size_t count)
{
- const char *_src = src;
- mbstate_t ps = {0};
- size_t wchars = mbsnrtowcs(NULL, &_src, count, 0, &ps);
-
- if (wchars == -1) {
- LOG_ERRNO("failed to convert %.*s to wchars", (int)count, src);
+ size_t chars = mbsntoc32(NULL, src, count, 0);
+ if (chars == (size_t)-1) {
+ LOG_ERRNO("failed to convert %.*s to Unicode", (int)count, src);
return;
}
- _src = src;
- ps = (mbstate_t){0};
- wchar_t wcs[wchars + 1];
- mbsnrtowcs(wcs, &_src, count, wchars, &ps);
- add_wchars(term, wcs, wchars);
+ char32_t c32s[chars + 1];
+ mbsntoc32(c32s, src, count, chars);
+ add_wchars(term, c32s, chars);
}
static void
@@ -502,7 +496,7 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only)
break;
} while (pos.col != new_end.col || pos.row != new_end.row);
- wchar_t *new_text;
+ char32_t *new_text;
size_t new_len;
if (!extract_finish_wide(ctx, &new_text, &new_len))
@@ -512,7 +506,7 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only)
return;
for (size_t i = 0; i < new_len; i++) {
- if (new_text[i] == L'\n') {
+ if (new_text[i] == U'\n') {
/* extract() adds newlines, which we never match against */
continue;
}
@@ -520,7 +514,7 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only)
term->search.buf[term->search.len++] = new_text[i];
}
- term->search.buf[term->search.len] = L'\0';
+ term->search.buf[term->search.len] = U'\0';
free(new_text);
if (move_cursor)
@@ -544,22 +538,22 @@ distance_next_word(const struct terminal *term)
/* First eat non-whitespace. This is the word we're skipping past */
while (cursor < term->search.len) {
- if (iswspace(term->search.buf[cursor++]))
+ if (isc32space(term->search.buf[cursor++]))
break;
}
- xassert(cursor == term->search.len || iswspace(term->search.buf[cursor - 1]));
+ xassert(cursor == term->search.len || isc32space(term->search.buf[cursor - 1]));
/* Now skip past whitespace, so that we end up at the beginning of
* the next word */
while (cursor < term->search.len) {
- if (!iswspace(term->search.buf[cursor++]))
+ if (!isc32space(term->search.buf[cursor++]))
break;
}
- xassert(cursor == term->search.len || !iswspace(term->search.buf[cursor - 1]));
+ xassert(cursor == term->search.len || !isc32space(term->search.buf[cursor - 1]));
- if (cursor < term->search.len && !iswspace(term->search.buf[cursor]))
+ if (cursor < term->search.len && !isc32space(term->search.buf[cursor]))
cursor--;
return cursor - term->search.cursor;
@@ -572,20 +566,20 @@ distance_prev_word(const struct terminal *term)
/* First, eat whitespace prefix */
while (cursor > 0) {
- if (!iswspace(term->search.buf[--cursor]))
+ if (!isc32space(term->search.buf[--cursor]))
break;
}
- xassert(cursor == 0 || !iswspace(term->search.buf[cursor]));
+ xassert(cursor == 0 || !isc32space(term->search.buf[cursor]));
/* Now eat non-whitespace. This is the word we're skipping past */
while (cursor > 0) {
- if (iswspace(term->search.buf[--cursor]))
+ if (isc32space(term->search.buf[--cursor]))
break;
}
- xassert(cursor == 0 || iswspace(term->search.buf[cursor]));
- if (cursor > 0 && iswspace(term->search.buf[cursor]))
+ xassert(cursor == 0 || isc32space(term->search.buf[cursor]));
+ if (cursor > 0 && isc32space(term->search.buf[cursor]))
cursor++;
return term->search.cursor - cursor;
@@ -603,7 +597,7 @@ from_clipboard_done(void *user)
{
struct terminal *term = user;
- LOG_DBG("search: buffer: %ls", term->search.buf);
+ LOG_DBG("search: buffer: %ls", (const wchar_t *)term->search.buf);
search_find_next(term);
render_refresh_search(term);
}
@@ -741,9 +735,9 @@ execute_binding(struct seat *seat, struct terminal *term,
memmove(
&term->search.buf[term->search.cursor - 1],
&term->search.buf[term->search.cursor],
- (term->search.len - term->search.cursor) * sizeof(wchar_t));
+ (term->search.len - term->search.cursor) * sizeof(char32_t));
term->search.cursor--;
- term->search.buf[--term->search.len] = L'\0';
+ term->search.buf[--term->search.len] = U'\0';
*update_search_result = *redraw = true;
}
return true;
@@ -756,7 +750,7 @@ execute_binding(struct seat *seat, struct terminal *term,
if (diff > 0) {
memmove(&term->search.buf[new_cursor],
&term->search.buf[old_cursor],
- (term->search.len - old_cursor) * sizeof(wchar_t));
+ (term->search.len - old_cursor) * sizeof(char32_t));
term->search.len -= diff;
term->search.cursor = new_cursor;
@@ -770,8 +764,8 @@ execute_binding(struct seat *seat, struct terminal *term,
memmove(
&term->search.buf[term->search.cursor],
&term->search.buf[term->search.cursor + 1],
- (term->search.len - term->search.cursor - 1) * sizeof(wchar_t));
- term->search.buf[--term->search.len] = L'\0';
+ (term->search.len - term->search.cursor - 1) * sizeof(char32_t));
+ term->search.buf[--term->search.len] = U'\0';
*update_search_result = *redraw = true;
}
return true;
@@ -783,7 +777,7 @@ execute_binding(struct seat *seat, struct terminal *term,
if (diff > 0) {
memmove(&term->search.buf[cursor],
&term->search.buf[cursor + diff],
- (term->search.len - (cursor + diff)) * sizeof(wchar_t));
+ (term->search.len - (cursor + diff)) * sizeof(char32_t));
term->search.len -= diff;
*update_search_result = *redraw = true;
@@ -905,7 +899,7 @@ search_input(struct seat *seat, struct terminal *term, uint32_t key,
search_add_chars(term, (const char *)buf, count);
update_search:
- LOG_DBG("search: buffer: %ls", term->search.buf);
+ LOG_DBG("search: buffer: %ls", (const wchar_t *)term->search.buf);
if (update_search_result)
search_find_next(term);
if (redraw)