[gtksourceview/wip/search] Do not allow NULL iters
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/search] Do not allow NULL iters
- Date: Sat, 6 Jul 2013 18:01:57 +0000 (UTC)
commit 5dcc03b108c388857447b3d0a06e6f63a3e28f20
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sat Jul 6 19:49:13 2013 +0200
Do not allow NULL iters
gsv doesn't select the matches, so take NULL iters for the selection
bounds is not a good thing to do.
gtksourceview/gtksourcebuffer.c | 23 ++++----
gtksourceview/gtksourcesearch.c | 109 +++++++-------------------------------
tests/test-search-ui.c | 34 ++++++++++--
3 files changed, 60 insertions(+), 106 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index d44c146..19630a3 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -2807,14 +2807,12 @@ gtk_source_buffer_get_search_occurrence_position (GtkSourceBuffer *buffer,
/**
* gtk_source_buffer_forward_search:
* @buffer: a #GtkSourceBuffer.
- * @iter: start of search, or %NULL.
+ * @iter: start of search.
* @match_start: return location for start of match, or %NULL.
* @match_end: return location for end of match, or %NULL.
*
* Synchronous forward search.
*
- * If @iter is %NULL, the search begins at the end of the buffer selection.
- *
* Returns: whether a match was found.
*
* Since: 3.10
@@ -2826,6 +2824,7 @@ gtk_source_buffer_forward_search (GtkSourceBuffer *buffer,
GtkTextIter *match_end)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
+ g_return_val_if_fail (iter != NULL, FALSE);
return _gtk_source_search_forward (buffer->priv->search,
iter,
@@ -2836,7 +2835,7 @@ gtk_source_buffer_forward_search (GtkSourceBuffer *buffer,
/**
* gtk_source_buffer_forward_search_async:
* @buffer: a #GtkSourceBuffer.
- * @iter: start of search, or %NULL.
+ * @iter: start of search.
* @cancellable: a #GCancellable, or %NULL.
* @callback: a #GAsyncReadyCallback to call when the operation is finished.
* @user_data: the data to pass to the @callback function.
@@ -2857,6 +2856,7 @@ gtk_source_buffer_forward_search_async (GtkSourceBuffer *buffer,
gpointer user_data)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
+ g_return_if_fail (iter != NULL);
_gtk_source_search_forward_async (buffer->priv->search,
iter,
@@ -2884,14 +2884,12 @@ gtk_source_buffer_forward_search_finish (GtkSourceBuffer *buffer,
/**
* gtk_source_buffer_backward_search:
* @buffer: a #GtkSourceBuffer.
- * @iter: start of search, or %NULL.
+ * @iter: start of search.
* @match_start: return location for start of match, or %NULL.
* @match_end: return location for end of match, or %NULL.
*
* Synchronous backward search.
*
- * If @iter is %NULL, the search begins at the start of the buffer selection.
- *
* Returns: whether a match was found.
*/
gboolean
@@ -2901,6 +2899,7 @@ gtk_source_buffer_backward_search (GtkSourceBuffer *buffer,
GtkTextIter *match_end)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
+ g_return_val_if_fail (iter != NULL, FALSE);
return _gtk_source_search_backward (buffer->priv->search,
iter,
@@ -2916,6 +2915,7 @@ gtk_source_buffer_backward_search_async (GtkSourceBuffer *buffer,
gpointer user_data)
{
g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
+ g_return_if_fail (iter != NULL);
_gtk_source_search_backward_async (buffer->priv->search,
iter,
@@ -2943,17 +2943,14 @@ gtk_source_buffer_backward_search_finish (GtkSourceBuffer *buffer,
/**
* gtk_source_buffer_search_replace:
* @buffer: a #GtkSourceBuffer.
- * @match_start: the start of the match to replace, or %NULL.
- * @match_end: the end of the match to replace, or %NULL.
+ * @match_start: the start of the match to replace.
+ * @match_end: the end of the match to replace.
* @replace: the replacement text.
* @replace_length: the length of @replace in bytes, or -1.
*
* Replaces a search match by another text. If @match_start and @match_end
* doesn't correspond to a search match, %FALSE is returned.
*
- * If you pass %NULL to @match_start and @match_end, the selection bounds will
- * be taken.
- *
* Returns: whether the match has been replaced.
* Since: 3.10
*/
@@ -2965,6 +2962,8 @@ gtk_source_buffer_search_replace (GtkSourceBuffer *buffer,
gint replace_length)
{
g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), FALSE);
+ g_return_val_if_fail (match_start != NULL, FALSE);
+ g_return_val_if_fail (match_end != NULL, FALSE);
g_return_val_if_fail (replace != NULL, FALSE);
return _gtk_source_search_replace (buffer->priv->search,
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index 5ad887c..c512083 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -1896,34 +1896,24 @@ _gtk_source_search_forward (GtkSourceSearch *search,
GtkTextIter *match_start,
GtkTextIter *match_end)
{
- GtkTextIter start_at;
gboolean found;
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), FALSE);
+ g_return_val_if_fail (iter != NULL, FALSE);
if (dispose_has_run (search))
{
return FALSE;
}
- if (iter != NULL)
- {
- start_at = *iter;
- }
- else
- {
- gtk_text_buffer_get_selection_bounds (search->priv->buffer,
- NULL,
- &start_at);
- }
-
- found = smart_forward_search (search, &start_at, match_start, match_end);
+ found = smart_forward_search (search, iter, match_start, match_end);
if (!found && search->priv->wrap_around)
{
- gtk_text_buffer_get_start_iter (search->priv->buffer, &start_at);
+ GtkTextIter start_iter;
+ gtk_text_buffer_get_start_iter (search->priv->buffer, &start_iter);
- found = smart_forward_search (search, &start_at, match_start, match_end);
+ found = smart_forward_search (search, &start_iter, match_start, match_end);
}
return found;
@@ -1936,9 +1926,8 @@ _gtk_source_search_forward_async (GtkSourceSearch *search,
GAsyncReadyCallback callback,
gpointer user_data)
{
- GtkTextIter start_at;
-
g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
+ g_return_if_fail (iter != NULL);
if (dispose_has_run (search))
{
@@ -1948,18 +1937,7 @@ _gtk_source_search_forward_async (GtkSourceSearch *search,
clear_task (search);
search->priv->task = g_task_new (search->priv->buffer, cancellable, callback, user_data);
- if (iter != NULL)
- {
- start_at = *iter;
- }
- else
- {
- gtk_text_buffer_get_selection_bounds (search->priv->buffer,
- NULL,
- &start_at);
- }
-
- smart_forward_search_async (search, &start_at, FALSE);
+ smart_forward_search_async (search, iter, FALSE);
}
gboolean
@@ -2013,34 +1991,25 @@ _gtk_source_search_backward (GtkSourceSearch *search,
GtkTextIter *match_start,
GtkTextIter *match_end)
{
- GtkTextIter start_at;
gboolean found;
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), FALSE);
+ g_return_val_if_fail (iter != NULL, FALSE);
if (dispose_has_run (search))
{
return FALSE;
}
- if (iter != NULL)
- {
- start_at = *iter;
- }
- else
- {
- gtk_text_buffer_get_selection_bounds (search->priv->buffer,
- &start_at,
- NULL);
- }
-
- found = smart_backward_search (search, &start_at, match_start, match_end);
+ found = smart_backward_search (search, iter, match_start, match_end);
if (!found && search->priv->wrap_around)
{
- gtk_text_buffer_get_end_iter (search->priv->buffer, &start_at);
+ GtkTextIter end_iter;
- found = smart_backward_search (search, &start_at, match_start, match_end);
+ gtk_text_buffer_get_end_iter (search->priv->buffer, &end_iter);
+
+ found = smart_backward_search (search, &end_iter, match_start, match_end);
}
return found;
@@ -2053,9 +2022,8 @@ _gtk_source_search_backward_async (GtkSourceSearch *search,
GAsyncReadyCallback callback,
gpointer user_data)
{
- GtkTextIter start_at;
-
g_return_if_fail (GTK_SOURCE_IS_SEARCH (search));
+ g_return_if_fail (iter != NULL);
if (dispose_has_run (search))
{
@@ -2065,18 +2033,7 @@ _gtk_source_search_backward_async (GtkSourceSearch *search,
clear_task (search);
search->priv->task = g_task_new (search->priv->buffer, cancellable, callback, user_data);
- if (iter != NULL)
- {
- start_at = *iter;
- }
- else
- {
- gtk_text_buffer_get_selection_bounds (search->priv->buffer,
- &start_at,
- NULL);
- }
-
- smart_backward_search_async (search, &start_at, FALSE);
+ smart_backward_search_async (search, iter, FALSE);
}
gboolean
@@ -2102,10 +2059,10 @@ _gtk_source_search_replace (GtkSourceSearch *search,
{
GtkTextIter start;
GtkTextIter end;
- GtkTextIter check_match_start;
- GtkTextIter check_match_end;
g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), FALSE);
+ g_return_val_if_fail (match_start != NULL, FALSE);
+ g_return_val_if_fail (match_end != NULL, FALSE);
g_return_val_if_fail (replace != NULL, FALSE);
if (dispose_has_run (search))
@@ -2113,36 +2070,10 @@ _gtk_source_search_replace (GtkSourceSearch *search,
return FALSE;
}
- if (match_start != NULL)
- {
- start = *match_start;
- }
- else
- {
- gtk_text_buffer_get_selection_bounds (search->priv->buffer,
- &start,
- NULL);
- }
-
- if (match_end != NULL)
- {
- end = *match_end;
- }
- else
- {
- gtk_text_buffer_get_selection_bounds (search->priv->buffer,
- NULL,
- &end);
- }
-
- basic_forward_search (search,
- &start,
- &check_match_start,
- &check_match_end,
- &end);
+ basic_forward_search (search, match_start, &start, &end, match_end);
- if (!gtk_text_iter_equal (&start, &check_match_start) ||
- !gtk_text_iter_equal (&end, &check_match_end))
+ if (!gtk_text_iter_equal (match_start, &start) ||
+ !gtk_text_iter_equal (match_end, &end))
{
return FALSE;
}
diff --git a/tests/test-search-ui.c b/tests/test-search-ui.c
index f5627be..027ef37 100644
--- a/tests/test-search-ui.c
+++ b/tests/test-search-ui.c
@@ -182,8 +182,14 @@ static void
button_previous_clicked_cb (TestSearchUI *search,
GtkButton *button)
{
+ GtkTextIter start_at;
+
+ gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
+ &start_at,
+ NULL);
+
gtk_source_buffer_backward_search_async (search->priv->source_buffer,
- NULL,
+ &start_at,
NULL,
(GAsyncReadyCallback)backward_search_finished,
search);
@@ -211,8 +217,14 @@ static void
button_next_clicked_cb (TestSearchUI *search,
GtkButton *button)
{
+ GtkTextIter start_at;
+
+ gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
+ NULL,
+ &start_at);
+
gtk_source_buffer_forward_search_async (search->priv->source_buffer,
- NULL,
+ &start_at,
NULL,
(GAsyncReadyCallback)forward_search_finished,
search);
@@ -222,14 +234,26 @@ static void
button_replace_clicked_cb (TestSearchUI *search,
GtkButton *button)
{
+ GtkTextIter match_start;
+ GtkTextIter match_end;
+ GtkTextIter iter;
+
+ gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
+ &match_start,
+ &match_end);
+
gtk_source_buffer_search_replace (search->priv->source_buffer,
- NULL,
- NULL,
+ &match_start,
+ &match_end,
gtk_entry_get_text (search->priv->replace_entry),
-1);
+ gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (search->priv->source_buffer),
+ NULL,
+ &iter);
+
gtk_source_buffer_forward_search_async (search->priv->source_buffer,
- NULL,
+ &iter,
NULL,
(GAsyncReadyCallback)forward_search_finished,
search);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]