[gtksourceview] SearchContext: fix bug multi-byte characters
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview] SearchContext: fix bug multi-byte characters
- Date: Sat, 3 Jan 2015 14:54:15 +0000 (UTC)
commit b7adba6815cda2664fe2724885bdcc263cf95ea3
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sat Jan 3 15:34:58 2015 +0100
SearchContext: fix bug multi-byte characters
The @start_position parameter of g_regex_match_full(), g_regex_replace()
etc is in _bytes_, not characters.
gtk_text_iter_get_visible_text() is used because it is the function used
elsewhere in SearchContext to retrieve the text to search.
gtksourceview/gtksourcesearchcontext.c | 12 ++++++++++--
tests/test-search-context.c | 16 ++++++++++++++++
2 files changed, 26 insertions(+), 2 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index 4c6c245..8195af8 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -592,6 +592,7 @@ get_text_search_flags (GtkSourceSearchContext *search)
return flags;
}
+/* @start_pos is in bytes. */
static void
regex_search_get_real_start (GtkSourceSearchContext *search,
const GtkTextIter *start,
@@ -599,16 +600,23 @@ regex_search_get_real_start (GtkSourceSearchContext *search,
gint *start_pos)
{
gint max_lookbehind = g_regex_get_max_lookbehind (search->priv->regex);
+ gint i;
+ gchar *text;
*real_start = *start;
- for (*start_pos = 0; *start_pos < max_lookbehind; (*start_pos)++)
+ for (i = 0; i < max_lookbehind; i++)
{
if (!gtk_text_iter_backward_char (real_start))
{
break;
}
}
+
+ text = gtk_text_iter_get_visible_text (real_start, start);
+ *start_pos = strlen (text);
+
+ g_free (text);
}
static GRegexMatchFlags
@@ -1858,7 +1866,7 @@ regex_search_scan_segment (GtkSourceSearchContext *search,
DEBUG ({
g_print ("\n*** regex search - scan segment ***\n");
- g_print ("start position in the subject: %d\n", start_pos);
+ g_print ("start position in the subject (in bytes): %d\n", start_pos);
});
match_options = regex_search_get_match_options (&real_start, segment_end);
diff --git a/tests/test-search-context.c b/tests/test-search-context.c
index daa1678..8e3782b 100644
--- a/tests/test-search-context.c
+++ b/tests/test-search-context.c
@@ -1038,6 +1038,22 @@ test_regex_at_word_boundaries (void)
g_assert_cmpstr (content, ==, "&bb");
g_free (content);
+ /* Test replace multi-byte character */
+
+ gtk_text_buffer_set_text (text_buffer, "–aa", -1);
+ gtk_source_search_settings_set_search_text (settings, "aa");
+ flush_queue ();
+
+ gtk_text_buffer_get_iter_at_offset (text_buffer, &match_start, 1);
+ gtk_text_buffer_get_end_iter (text_buffer, &match_end);
+ gtk_source_search_context_replace (context, &match_start, &match_end, "bb", -1, NULL);
+
+ gtk_text_buffer_get_start_iter (text_buffer, &start);
+ gtk_text_buffer_get_end_iter (text_buffer, &end);
+ content = gtk_text_iter_get_visible_text (&start, &end);
+ g_assert_cmpstr (content, ==, "–bb");
+ g_free (content);
+
g_object_unref (source_buffer);
g_object_unref (settings);
g_object_unref (context);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]