Remove redundant NULL checks
[pidgin-git.git] / pidgin / gtksourceiter.c
blob050f683c43a803986b6ec69246c3651a2c73e1ab
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 * gtksourceiter.c
4 * Pidgin is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * The following copyright notice applies to this file:
10 * Copyright (C) 2000 - 2005 Paolo Maggi
11 * Copyright (C) 2002, 2003 Jeroen Zwartepoorte
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Library General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Library General Public License for more details.
23 * You should have received a copy of the GNU Library General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA.
29 * Parts of this file are copied from the gedit and glimmer project.
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
36 #include <string.h>
37 #include "gtksourceiter.h"
39 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
41 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
42 * decomposable character it consumes the decomposition length from the given
43 * offset. So it's useful when the offset was calculated for the normalized
44 * version of str, but we need a pointer to str itself. */
45 static const gchar *
46 pointer_from_offset_skipping_decomp (const gchar *str, gint offset)
48 gchar *casefold, *normal;
49 const gchar *p, *q;
51 p = str;
52 while (offset > 0)
54 q = g_utf8_next_char (p);
55 casefold = g_utf8_casefold (p, q - p);
56 normal = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
57 offset -= g_utf8_strlen (normal, -1);
58 g_free (casefold);
59 g_free (normal);
60 p = q;
62 return p;
65 static const gchar *
66 g_utf8_strcasestr (const gchar *haystack, const gchar *needle)
68 gsize needle_len;
69 gsize haystack_len;
70 const gchar *ret = NULL;
71 gchar *p;
72 gchar *casefold;
73 gchar *caseless_haystack;
74 gint i;
76 g_return_val_if_fail (haystack != NULL, NULL);
77 g_return_val_if_fail (needle != NULL, NULL);
79 casefold = g_utf8_casefold (haystack, -1);
80 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
81 g_free (casefold);
83 needle_len = g_utf8_strlen (needle, -1);
84 haystack_len = g_utf8_strlen (caseless_haystack, -1);
86 if (needle_len == 0)
88 ret = (gchar *)haystack;
89 goto finally_1;
92 if (haystack_len < needle_len)
94 ret = NULL;
95 goto finally_1;
98 p = (gchar*)caseless_haystack;
99 needle_len = strlen (needle);
100 i = 0;
102 while (*p)
104 if ((strncmp (p, needle, needle_len) == 0))
106 ret = pointer_from_offset_skipping_decomp (haystack, i);
107 goto finally_1;
110 p = g_utf8_next_char (p);
111 i++;
114 finally_1:
115 g_free (caseless_haystack);
117 return ret;
120 static const gchar *
121 g_utf8_strrcasestr (const gchar *haystack, const gchar *needle)
123 gsize needle_len;
124 gsize haystack_len;
125 const gchar *ret = NULL;
126 gchar *p;
127 gchar *casefold;
128 gchar *caseless_haystack;
129 gint i;
131 g_return_val_if_fail (haystack != NULL, NULL);
132 g_return_val_if_fail (needle != NULL, NULL);
134 casefold = g_utf8_casefold (haystack, -1);
135 caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
136 g_free (casefold);
138 needle_len = g_utf8_strlen (needle, -1);
139 haystack_len = g_utf8_strlen (caseless_haystack, -1);
141 if (needle_len == 0)
143 ret = (gchar *)haystack;
144 goto finally_1;
147 if (haystack_len < needle_len)
149 ret = NULL;
150 goto finally_1;
153 i = haystack_len - needle_len;
154 p = g_utf8_offset_to_pointer (caseless_haystack, i);
155 needle_len = strlen (needle);
157 while (1)
159 if (strncmp (p, needle, needle_len) == 0)
161 ret = pointer_from_offset_skipping_decomp (haystack, i);
162 goto finally_1;
165 if (p > caseless_haystack)
166 p = g_utf8_prev_char (p);
167 else
168 goto finally_1;
170 i--;
173 finally_1:
174 g_free (caseless_haystack);
176 return ret;
179 static gboolean
180 g_utf8_caselessnmatch (const char *s1, const char *s2,
181 gssize n1, gssize n2)
183 gchar *casefold;
184 gchar *normalized_s1;
185 gchar *normalized_s2;
186 gint len_s1;
187 gint len_s2;
188 gboolean ret = FALSE;
190 g_return_val_if_fail (s1 != NULL, FALSE);
191 g_return_val_if_fail (s2 != NULL, FALSE);
192 g_return_val_if_fail (n1 > 0, FALSE);
193 g_return_val_if_fail (n2 > 0, FALSE);
195 casefold = g_utf8_casefold (s1, n1);
196 normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
197 g_free (casefold);
199 casefold = g_utf8_casefold (s2, n2);
200 normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
201 g_free (casefold);
203 len_s1 = strlen (normalized_s1);
204 len_s2 = strlen (normalized_s2);
206 if (len_s1 < len_s2)
207 goto finally_2;
209 ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0);
211 finally_2:
212 g_free (normalized_s1);
213 g_free (normalized_s2);
215 return ret;
218 static void
219 forward_chars_with_skipping (GtkTextIter *iter,
220 gint count,
221 gboolean skip_invisible,
222 gboolean skip_nontext,
223 gboolean skip_decomp)
225 gint i;
227 g_return_if_fail (count >= 0);
229 i = count;
231 while (i > 0)
233 gboolean ignored = FALSE;
235 /* minimal workaround to avoid the infinite loop of bug #168247.
236 * It doesn't fix the problemjust the symptom...
238 if (gtk_text_iter_is_end (iter))
239 return;
241 if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR)
242 ignored = TRUE;
244 #if 0
245 if (!ignored && skip_invisible &&
246 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE)
247 ignored = TRUE;
248 #endif
250 if (!ignored && skip_decomp)
252 /* being UTF8 correct sucks; this accounts for extra
253 offsets coming from canonical decompositions of
254 UTF8 characters (e.g. accented characters) which
255 g_utf8_normalize() performs */
256 gchar *normal;
257 gchar buffer[6];
258 gint buffer_len;
260 buffer_len = g_unichar_to_utf8 (gtk_text_iter_get_char (iter), buffer);
261 normal = g_utf8_normalize (buffer, buffer_len, G_NORMALIZE_NFD);
262 i -= (g_utf8_strlen (normal, -1) - 1);
263 g_free (normal);
266 gtk_text_iter_forward_char (iter);
268 if (!ignored)
269 --i;
273 static gboolean
274 lines_match (const GtkTextIter *start,
275 const gchar **lines,
276 gboolean visible_only,
277 gboolean slice,
278 GtkTextIter *match_start,
279 GtkTextIter *match_end)
281 GtkTextIter next;
282 gchar *line_text;
283 const gchar *found;
284 gint offset;
286 if (*lines == NULL || **lines == '\0')
288 if (match_start)
289 *match_start = *start;
290 if (match_end)
291 *match_end = *start;
292 return TRUE;
295 next = *start;
296 gtk_text_iter_forward_line (&next);
298 /* No more text in buffer, but *lines is nonempty */
299 if (gtk_text_iter_equal (start, &next))
300 return FALSE;
302 if (slice)
304 if (visible_only)
305 line_text = gtk_text_iter_get_visible_slice (start, &next);
306 else
307 line_text = gtk_text_iter_get_slice (start, &next);
309 else
311 if (visible_only)
312 line_text = gtk_text_iter_get_visible_text (start, &next);
313 else
314 line_text = gtk_text_iter_get_text (start, &next);
317 if (match_start) /* if this is the first line we're matching */
319 found = g_utf8_strcasestr (line_text, *lines);
321 else
323 /* If it's not the first line, we have to match from the
324 * start of the line.
326 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
327 strlen (*lines)))
328 found = line_text;
329 else
330 found = NULL;
333 if (found == NULL)
335 g_free (line_text);
336 return FALSE;
339 /* Get offset to start of search string */
340 offset = g_utf8_strlen (line_text, found - line_text);
342 next = *start;
344 /* If match start needs to be returned, set it to the
345 * start of the search string.
347 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
348 if (match_start)
350 *match_start = next;
353 /* Go to end of search string */
354 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
356 g_free (line_text);
358 ++lines;
360 if (match_end)
361 *match_end = next;
363 /* pass NULL for match_start, since we don't need to find the
364 * start again.
366 return lines_match (&next, lines, visible_only, slice, NULL, match_end);
369 static gboolean
370 backward_lines_match (const GtkTextIter *start,
371 const gchar **lines,
372 gboolean visible_only,
373 gboolean slice,
374 GtkTextIter *match_start,
375 GtkTextIter *match_end)
377 GtkTextIter line, next;
378 gchar *line_text;
379 const gchar *found;
380 gint offset;
382 if (*lines == NULL || **lines == '\0')
384 if (match_start)
385 *match_start = *start;
386 if (match_end)
387 *match_end = *start;
388 return TRUE;
391 line = next = *start;
392 if (gtk_text_iter_get_line_offset (&next) == 0)
394 if (!gtk_text_iter_backward_line (&next))
395 return FALSE;
397 else
398 gtk_text_iter_set_line_offset (&next, 0);
400 if (slice)
402 if (visible_only)
403 line_text = gtk_text_iter_get_visible_slice (&next, &line);
404 else
405 line_text = gtk_text_iter_get_slice (&next, &line);
407 else
409 if (visible_only)
410 line_text = gtk_text_iter_get_visible_text (&next, &line);
411 else
412 line_text = gtk_text_iter_get_text (&next, &line);
415 if (match_start) /* if this is the first line we're matching */
417 found = g_utf8_strrcasestr (line_text, *lines);
419 else
421 /* If it's not the first line, we have to match from the
422 * start of the line.
424 if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text),
425 strlen (*lines)))
426 found = line_text;
427 else
428 found = NULL;
431 if (found == NULL)
433 g_free (line_text);
434 return FALSE;
437 /* Get offset to start of search string */
438 offset = g_utf8_strlen (line_text, found - line_text);
440 forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE);
442 /* If match start needs to be returned, set it to the
443 * start of the search string.
445 if (match_start)
447 *match_start = next;
450 /* Go to end of search string */
451 forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE);
453 g_free (line_text);
455 ++lines;
457 if (match_end)
458 *match_end = next;
460 /* try to match the rest of the lines forward, passing NULL
461 * for match_start so lines_match will try to match the entire
462 * line */
463 return lines_match (&next, lines, visible_only,
464 slice, NULL, match_end);
467 /* strsplit () that retains the delimiter as part of the string. */
468 static gchar **
469 strbreakup (const char *string,
470 const char *delimiter,
471 gint max_tokens)
473 GSList *string_list = NULL, *slist;
474 gchar **str_array, *s, *casefold, *new_string;
475 guint i, n = 1;
477 g_return_val_if_fail (string != NULL, NULL);
478 g_return_val_if_fail (delimiter != NULL, NULL);
480 if (max_tokens < 1)
481 max_tokens = G_MAXINT;
483 s = strstr (string, delimiter);
484 if (s)
486 guint delimiter_len = strlen (delimiter);
490 guint len;
492 len = s - string + delimiter_len;
493 new_string = g_new (gchar, len + 1);
494 strncpy (new_string, string, len);
495 new_string[len] = 0;
496 casefold = g_utf8_casefold (new_string, -1);
497 g_free (new_string);
498 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
499 g_free (casefold);
500 string_list = g_slist_prepend (string_list, new_string);
501 n++;
502 string = s + delimiter_len;
503 s = strstr (string, delimiter);
504 } while (--max_tokens && s);
507 if (*string)
509 n++;
510 casefold = g_utf8_casefold (string, -1);
511 new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_NFD);
512 g_free (casefold);
513 string_list = g_slist_prepend (string_list, new_string);
516 str_array = g_new (gchar*, n);
518 i = n - 1;
520 str_array[i--] = NULL;
521 for (slist = string_list; slist; slist = slist->next)
522 str_array[i--] = slist->data;
524 g_slist_free (string_list);
526 return str_array;
529 static GtkTextSearchFlags
530 _source_flags_to_text_flags(GtkSourceSearchFlags flags)
532 GtkTextSearchFlags text_flags = 0;
534 if (flags & GTK_SOURCE_SEARCH_VISIBLE_ONLY)
535 text_flags |= GTK_TEXT_SEARCH_VISIBLE_ONLY;
536 if (flags & GTK_SOURCE_SEARCH_TEXT_ONLY)
537 text_flags |= GTK_TEXT_SEARCH_TEXT_ONLY;
539 return text_flags;
543 * gtk_source_iter_forward_search:
544 * @iter: start of search.
545 * @str: a search string.
546 * @flags: flags affecting how the search is done.
547 * @match_start: return location for start of match, or %%NULL.
548 * @match_end: return location for end of match, or %%NULL.
549 * @limit: bound for the search, or %%NULL for the end of the buffer.
551 * Searches forward for @str. Any match is returned by setting
552 * @match_start to the first character of the match and @match_end to the
553 * first character after the match. The search will not continue past
554 * @limit. Note that a search is a linear or O(n) operation, so you
555 * may wish to use @limit to avoid locking up your UI on large
556 * buffers.
558 * If the #GTK_SOURCE_SEARCH_VISIBLE_ONLY flag is present, the match may
559 * have invisible text interspersed in @str. i.e. @str will be a
560 * possibly-noncontiguous subsequence of the matched range. similarly,
561 * if you specify #GTK_SOURCE_SEARCH_TEXT_ONLY, the match may have
562 * pixbufs or child widgets mixed inside the matched range. If these
563 * flags are not given, the match must be exact; the special 0xFFFC
564 * character in @str will match embedded pixbufs or child widgets.
565 * If you specify the #GTK_SOURCE_SEARCH_CASE_INSENSITIVE flag, the text will
566 * be matched regardless of what case it is in.
568 * Same as gtk_text_iter_forward_search(), but supports case insensitive
569 * searching.
571 * Return value: whether a match was found.
573 gboolean
574 gtk_source_iter_forward_search (const GtkTextIter *iter,
575 const gchar *str,
576 GtkSourceSearchFlags flags,
577 GtkTextIter *match_start,
578 GtkTextIter *match_end,
579 const GtkTextIter *limit)
581 gchar **lines = NULL;
582 GtkTextIter match;
583 gboolean retval = FALSE;
584 GtkTextIter search;
585 gboolean visible_only;
586 gboolean slice;
588 g_return_val_if_fail (iter != NULL, FALSE);
589 g_return_val_if_fail (str != NULL, FALSE);
591 if ((flags & GTK_SOURCE_SEARCH_CASE_INSENSITIVE) == 0)
592 return gtk_text_iter_forward_search (iter, str, _source_flags_to_text_flags(flags),
593 match_start, match_end,
594 limit);
596 if (limit && gtk_text_iter_compare (iter, limit) >= 0)
597 return FALSE;
599 if (*str == '\0')
601 /* If we can move one char, return the empty string there */
602 match = *iter;
604 if (gtk_text_iter_forward_char (&match))
606 if (limit && gtk_text_iter_equal (&match, limit))
607 return FALSE;
609 if (match_start)
610 *match_start = match;
611 if (match_end)
612 *match_end = match;
613 return TRUE;
615 else
617 return FALSE;
621 visible_only = (flags & GTK_SOURCE_SEARCH_VISIBLE_ONLY) != 0;
622 slice = (flags & GTK_SOURCE_SEARCH_TEXT_ONLY) == 0;
624 /* locate all lines */
625 lines = strbreakup (str, "\n", -1);
627 search = *iter;
631 /* This loop has an inefficient worst-case, where
632 * gtk_text_iter_get_text () is called repeatedly on
633 * a single line.
635 GtkTextIter end;
637 if (limit && gtk_text_iter_compare (&search, limit) >= 0)
638 break;
640 if (lines_match (&search, (const gchar**)lines,
641 visible_only, slice, &match, &end))
643 if (limit == NULL ||
644 (limit && gtk_text_iter_compare (&end, limit) <= 0))
646 retval = TRUE;
648 if (match_start)
649 *match_start = match;
650 if (match_end)
651 *match_end = end;
653 break;
655 } while (gtk_text_iter_forward_line (&search));
657 g_strfreev ((gchar**)lines);
659 return retval;
663 * gtk_source_iter_backward_search:
664 * @iter: a #GtkTextIter where the search begins.
665 * @str: search string.
666 * @flags: bitmask of flags affecting the search.
667 * @match_start: return location for start of match, or %%NULL.
668 * @match_end: return location for end of match, or %%NULL.
669 * @limit: location of last possible @match_start, or %%NULL for start of buffer.
671 * Same as gtk_text_iter_backward_search(), but supports case insensitive
672 * searching.
674 * Return value: whether a match was found.
676 gboolean
677 gtk_source_iter_backward_search (const GtkTextIter *iter,
678 const gchar *str,
679 GtkSourceSearchFlags flags,
680 GtkTextIter *match_start,
681 GtkTextIter *match_end,
682 const GtkTextIter *limit)
684 gchar **lines = NULL;
685 GtkTextIter match;
686 gboolean retval = FALSE;
687 GtkTextIter search;
688 gboolean visible_only;
689 gboolean slice;
691 g_return_val_if_fail (iter != NULL, FALSE);
692 g_return_val_if_fail (str != NULL, FALSE);
694 if ((flags & GTK_SOURCE_SEARCH_CASE_INSENSITIVE) == 0)
695 return gtk_text_iter_backward_search (iter, str, _source_flags_to_text_flags(flags),
696 match_start, match_end,
697 limit);
699 if (limit && gtk_text_iter_compare (iter, limit) <= 0)
700 return FALSE;
702 if (*str == '\0')
704 /* If we can move one char, return the empty string there */
705 match = *iter;
707 if (gtk_text_iter_backward_char (&match))
709 if (limit && gtk_text_iter_equal (&match, limit))
710 return FALSE;
712 if (match_start)
713 *match_start = match;
714 if (match_end)
715 *match_end = match;
716 return TRUE;
718 else
720 return FALSE;
724 visible_only = (flags & GTK_SOURCE_SEARCH_VISIBLE_ONLY) != 0;
725 slice = (flags & GTK_SOURCE_SEARCH_TEXT_ONLY) == 0;
727 /* locate all lines */
728 lines = strbreakup (str, "\n", -1);
730 search = *iter;
732 while (TRUE)
734 /* This loop has an inefficient worst-case, where
735 * gtk_text_iter_get_text () is called repeatedly on
736 * a single line.
738 GtkTextIter end;
740 if (limit && gtk_text_iter_compare (&search, limit) <= 0)
741 break;
743 if (backward_lines_match (&search, (const gchar**)lines,
744 visible_only, slice, &match, &end))
746 if (limit == NULL || (limit &&
747 gtk_text_iter_compare (&end, limit) > 0))
749 retval = TRUE;
751 if (match_start)
752 *match_start = match;
753 if (match_end)
754 *match_end = end;
756 break;
759 if (gtk_text_iter_get_line_offset (&search) == 0)
761 if (!gtk_text_iter_backward_line (&search))
762 break;
764 else
766 gtk_text_iter_set_line_offset (&search, 0);
770 g_strfreev ((gchar**)lines);
772 return retval;
776 * gtk_source_iter_find_matching_bracket is implemented in gtksourcebuffer.c