Skip to content

Commit

Permalink
Handle multibyte characters in search window context
Browse files Browse the repository at this point in the history
Grabbing a certain number of bytes before and after the search term to
display will mostly work for English text. But not always! We might end up
chopping a multibyte character in half. Detect if this happens and shorten
the string not to include the invalid character.
  • Loading branch information
ptomato committed Sep 20, 2024
1 parent 0d4439f commit 1300365
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/searchwindow.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,9 +592,17 @@ extract_context(GtkTextBuffer *buffer, GtkTextIter *match_start, GtkTextIter *ma
gchar *before = gtk_text_buffer_get_text(buffer, &context_start, match_start, TRUE);
gchar *term = gtk_text_buffer_get_text(buffer, match_start, match_end, TRUE);
gchar *after = gtk_text_buffer_get_text(buffer, match_end, &context_end, TRUE);
g_autofree char *escaped_before = g_markup_escape_text(before, CONTEXT_BEFORE);

/* We may have chopped a multibyte character in half */
while (!g_utf8_validate(before, -1, NULL))
before++;
char* end;
if (!g_utf8_validate(after, CONTEXT_AFTER, (const char **)&end))
*end = '\0';

g_autofree char *escaped_before = g_markup_escape_text(before, -1);
g_autofree char *escaped_term = g_markup_escape_text(term, -1);
g_autofree char *escaped_after = g_markup_escape_text(after, CONTEXT_AFTER);
g_autofree char *escaped_after = g_markup_escape_text(after, -1);
gchar *context = g_strconcat(escaped_before, "<b>", escaped_term, "</b>", escaped_after, NULL);
g_free(before);
g_free(term);
Expand Down

0 comments on commit 1300365

Please sign in to comment.