static gboolean backward_lines_match (const GtkTextIter *start, const gchar **lines, gboolean visible_only, gboolean slice, GtkTextIter *match_start, GtkTextIter *match_end) { GtkTextIter line, next; gchar *line_text; const gchar *found; gint offset; if (*lines == NULL || **lines == '\0') { if (match_start) *match_start = *start; if (match_end) *match_end = *start; return TRUE; } line = next = *start; if (gtk_text_iter_get_line_offset (&next) == 0) { if (!gtk_text_iter_backward_line (&next)) return FALSE; } else gtk_text_iter_set_line_offset (&next, 0); if (slice) { if (visible_only) line_text = gtk_text_iter_get_visible_slice (&next, &line); else line_text = gtk_text_iter_get_slice (&next, &line); } else { if (visible_only) line_text = gtk_text_iter_get_visible_text (&next, &line); else line_text = gtk_text_iter_get_text (&next, &line); } if (match_start) /* if this is the first line we're matching */ { found = g_utf8_strrcasestr (line_text, *lines); } else { /* If it's not the first line, we have to match from the * start of the line. */ if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text), strlen (*lines))) found = line_text; else found = NULL; } if (found == NULL) { g_free (line_text); return FALSE; } /* Get offset to start of search string */ offset = g_utf8_strlen (line_text, found - line_text); forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE); /* If match start needs to be returned, set it to the * start of the search string. */ if (match_start) { *match_start = next; } /* Go to end of search string */ forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE); g_free (line_text); ++lines; if (match_end) *match_end = next; /* try to match the rest of the lines forward, passing NULL * for match_start so lines_match will try to match the entire * line */ return lines_match (&next, lines, visible_only, slice, NULL, match_end); }
static VALUE rg_get_visible_slice(VALUE self, VALUE rhs) { return CSTR2RVAL(gtk_text_iter_get_visible_slice(_SELF(self), _SELF(rhs))); }
static gboolean lines_match (const GtkTextIter *start, const gchar **lines, gboolean visible_only, gboolean slice, GtkTextIter *match_start, GtkTextIter *match_end) { GtkTextIter next; gchar *line_text; const gchar *found; gint offset; if (*lines == NULL || **lines == '\0') { if (match_start) *match_start = *start; if (match_end) *match_end = *start; return TRUE; } next = *start; gtk_text_iter_forward_line (&next); /* No more text in buffer, but *lines is nonempty */ if (gtk_text_iter_equal (start, &next)) return FALSE; if (slice) { if (visible_only) line_text = gtk_text_iter_get_visible_slice (start, &next); else line_text = gtk_text_iter_get_slice (start, &next); } else { if (visible_only) line_text = gtk_text_iter_get_visible_text (start, &next); else line_text = gtk_text_iter_get_text (start, &next); } if (match_start) /* if this is the first line we're matching */ { found = g_utf8_strcasestr (line_text, *lines); } else { /* If it's not the first line, we have to match from the * start of the line. */ if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text), strlen (*lines))) found = line_text; else found = NULL; } if (found == NULL) { g_free (line_text); return FALSE; } /* Get offset to start of search string */ offset = g_utf8_strlen (line_text, found - line_text); next = *start; /* If match start needs to be returned, set it to the * start of the search string. */ forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE); if (match_start) { *match_start = next; } /* Go to end of search string */ forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE); g_free (line_text); ++lines; if (match_end) *match_end = next; /* pass NULL for match_start, since we don't need to find the * start again. */ return lines_match (&next, lines, visible_only, slice, NULL, match_end); }
/** @brief scan backwards from @a start for text which matches @a lines This performs line-by-line case-insensitive comparisons @param start pointer to textiter in buffer where the search is to commence @param lines array of \n-trailing line-strings, together comprising the search string @param visible_only TRUE to ignore hidden chars in the text @param slice TRUE to include "unknown" (0xFFFC) chars in the text @param match_start textiter to store the start of a matching string, or NULL when re-entering to parse lines after 1st @param match_end textiter store the start of a matching string, or NULL @return TRUE if a match was found */ static gboolean _e2_textiter_backward_lines_match ( const GtkTextIter *start, const gchar **lines, gboolean visible_only, gboolean slice, GtkTextIter *match_start, GtkTextIter *match_end) { GtkTextIter line, next; gchar *line_text; const gchar *found; gint offset; if (*lines == NULL || **lines == '\0') { //nothing to match, so everything matches if (match_start) *match_start = *start; if (match_end) *match_end = *start; return TRUE; } line = next = *start; if (gtk_text_iter_get_line_offset (&next) == 0) //CHECKME why break search into lines ? { if (!gtk_text_iter_backward_line (&next)) return FALSE; } else gtk_text_iter_set_line_offset (&next, 0); if (slice) { if (visible_only) line_text = gtk_text_iter_get_visible_slice (&next, &line); else line_text = gtk_text_iter_get_slice (&next, &line); } else { if (visible_only) line_text = gtk_text_iter_get_visible_text (&next, &line); else line_text = gtk_text_iter_get_text (&next, &line); } if (match_start != NULL) //if this is the first line we're matching { found = e2_utf8_strrcasestr (line_text, *lines); } else { //not the first line, we have to match from the start of the line if (e2_utf8_caseless_match (line_text, *lines, -1, -1)) found = line_text; else found = NULL; } if (found == NULL) { g_free (line_text); return FALSE; } //get offset to start of search string offset = g_utf8_strlen (line_text, found - line_text); _e2_textiter_forward_chars_with_skipping (&next, offset, visible_only, !slice, FALSE); //if match start needs to be returned, set it to the start of the search string if (match_start != NULL) { *match_start = next; } //go to end of search string _e2_textiter_forward_chars_with_skipping (&next, g_utf8_strlen (*lines, -1), visible_only, !slice, TRUE); g_free (line_text); ++lines; if (match_end != NULL) *match_end = next; /* try to match the rest of the lines forward, passing NULL for match_start so lines_match will try to match the entire line */ return _e2_textiter_forward_lines_match (&next, lines, visible_only, slice, NULL, match_end); }