[gtksourceview/gnome-3-16] SearchContext: fix bug backward regex search
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/gnome-3-16] SearchContext: fix bug backward regex search
- Date: Sun, 23 Aug 2015 19:33:44 +0000 (UTC)
commit 14441a8c67584f466a68e34b9544297a3a0b7041
Author: Sébastien Wilmet <swilmet gnome org>
Date: Thu Aug 20 18:40:03 2015 +0200
SearchContext: fix bug backward regex search
Base basic_backward_regex_search() on basic_forward_regex_search(). The
code is simpler, but less efficient.
gtksourceview/gtksourcesearchcontext.c | 67 +++++++------------------------
tests/test-search-context.c | 27 +++++++++++++
2 files changed, 42 insertions(+), 52 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index a6144a8..14eed4d 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -883,19 +883,10 @@ basic_backward_regex_search (GtkSourceSearchContext *search,
GtkTextIter *match_end,
const GtkTextIter *limit)
{
- GtkTextIter start;
- GtkTextIter real_start;
- GtkTextIter end;
- gint start_pos;
- gchar *subject;
- gssize subject_length;
- GRegexMatchFlags match_options;
- GMatchInfo *match_info;
- GtkTextIter iter;
- gint iter_byte_pos;
+ GtkTextIter lower_bound;
+ GtkTextIter m_start;
+ GtkTextIter m_end;
gboolean found = FALSE;
- GtkTextIter tmp_match_start;
- GtkTextIter tmp_match_end;
if (search->priv->regex == NULL ||
search->priv->regex_error != NULL)
@@ -905,58 +896,30 @@ basic_backward_regex_search (GtkSourceSearchContext *search,
if (limit == NULL)
{
- gtk_text_buffer_get_start_iter (search->priv->buffer, &start);
+ gtk_text_buffer_get_start_iter (search->priv->buffer, &lower_bound);
}
else
{
- start = *limit;
+ lower_bound = *limit;
}
- regex_search_get_real_start (search, &start, &real_start, &start_pos);
-
- end = *start_at;
-
- match_options = regex_search_get_match_options (&real_start, &end);
-
- subject = gtk_text_iter_get_visible_text (&real_start, &end);
- subject_length = strlen (subject);
-
- g_regex_match_full (search->priv->regex,
- subject,
- subject_length,
- start_pos,
- match_options,
- &match_info,
- &search->priv->regex_error);
-
- iter = real_start;
- iter_byte_pos = 0;
-
- while (regex_search_fetch_match (match_info,
- subject,
- subject_length,
- &iter,
- &iter_byte_pos,
- &tmp_match_start,
- &tmp_match_end))
+ while (basic_forward_regex_search (search, &lower_bound, &m_start, &m_end, start_at))
{
found = TRUE;
- *match_start = tmp_match_start;
- *match_end = tmp_match_end;
+ if (match_start != NULL)
+ {
+ *match_start = m_start;
+ }
- g_match_info_next (match_info, &search->priv->regex_error);
- }
+ if (match_end != NULL)
+ {
+ *match_end = m_end;
+ }
- if (search->priv->regex_error != NULL)
- {
- g_object_notify (G_OBJECT (search), "regex-error");
- found = FALSE;
+ lower_bound = m_end;
}
- g_free (subject);
- g_match_info_free (match_info);
-
return found;
}
diff --git a/tests/test-search-context.c b/tests/test-search-context.c
index 7f03334..c4df179 100644
--- a/tests/test-search-context.c
+++ b/tests/test-search-context.c
@@ -1080,9 +1080,11 @@ test_regex_look_behind (void)
gtk_source_search_settings_set_search_text (settings, "(?<=1)23");
flush_queue ();
+ /* Occurrences count */
count = gtk_source_search_context_get_occurrences_count (context);
g_assert_cmpint (count, ==, 1);
+ /* Forward search */
gtk_text_buffer_get_start_iter (text_buffer, &iter);
found = gtk_source_search_context_forward (context, &iter, &match_start, &match_end);
g_assert (found);
@@ -1092,6 +1094,17 @@ test_regex_look_behind (void)
offset = gtk_text_iter_get_offset (&match_end);
g_assert_cmpint (offset, ==, 9);
+ /* Backward search */
+ gtk_text_buffer_get_end_iter (text_buffer, &iter);
+ found = gtk_source_search_context_backward (context, &iter, &match_start, &match_end);
+ g_assert (found);
+
+ offset = gtk_text_iter_get_offset (&match_start);
+ g_assert_cmpint (offset, ==, 7);
+ offset = gtk_text_iter_get_offset (&match_end);
+ g_assert_cmpint (offset, ==, 9);
+
+ /* Occurrence position */
pos = gtk_source_search_context_get_occurrence_position (context, &match_start, &match_end);
g_assert_cmpint (pos, ==, 1);
@@ -1120,9 +1133,12 @@ test_regex_look_ahead (void)
gtk_source_search_settings_set_regex_enabled (settings, TRUE);
gtk_source_search_settings_set_search_text (settings, "12(?=3)");
flush_queue ();
+
+ /* Occurrences count */
count = gtk_source_search_context_get_occurrences_count (context);
g_assert_cmpint (count, ==, 1);
+ /* Forward search */
gtk_text_buffer_get_start_iter (text_buffer, &iter);
found = gtk_source_search_context_forward (context, &iter, &match_start, &match_end);
g_assert (found);
@@ -1132,6 +1148,17 @@ test_regex_look_ahead (void)
offset = gtk_text_iter_get_offset (&match_end);
g_assert_cmpint (offset, ==, 8);
+ /* Backward search */
+ gtk_text_buffer_get_end_iter (text_buffer, &iter);
+ found = gtk_source_search_context_backward (context, &iter, &match_start, &match_end);
+ g_assert (found);
+
+ offset = gtk_text_iter_get_offset (&match_start);
+ g_assert_cmpint (offset, ==, 6);
+ offset = gtk_text_iter_get_offset (&match_end);
+ g_assert_cmpint (offset, ==, 8);
+
+ /* Occurrence position */
pos = gtk_source_search_context_get_occurrence_position (context, &match_start, &match_end);
g_assert_cmpint (pos, ==, 1);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]