From e0c5b2bd7d4abf971d1fa6039704a44af334e3e2 Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sun, 7 Jun 2026 22:02:56 +0200 Subject: fixup aa7b0bb1445feeefafdcf47fd639b10500b45c03: some minor optimizations A partial match __should__ always be at the end of the subject string (i.e. block), so we don't have to check for it. Also, partial matches will be rare, so we can discriminate against the branch that handles them. --- src/search.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/search.c b/src/search.c index 92be012..90975c2 100644 --- a/src/search.c +++ b/src/search.c @@ -751,7 +751,12 @@ teco_do_search_backwards(GRegex *re, gsize from, gsize to, gint *count, GError * return FALSE; } - if (g_match_info_is_partial_match(info)) { + if (g_match_info_matches(info)) { + g_free(matched->matches[i].ranges); + matched->matches[i].ranges = teco_get_ranges(info, from+from_block, + &matched->matches[i].num_ranges); + i = ++matched_total % matched_num; + } else if (G_UNLIKELY(g_match_info_is_partial_match(info))) { /* * Match may fall on the block boundary, * so retry matching the rest of the document. @@ -769,32 +774,27 @@ teco_do_search_backwards(GRegex *re, gsize from, gsize to, gint *count, GError * rc = g_match_info_fetch_pos(info, 0, &partial_start, &partial_end); //g_assert(rc == TRUE); if (!rc) + /* make sure that test case fails */ abort(); + g_assert(partial_end == to_block-from_block); - if (partial_end == to_block-from_block) { - g_autoptr(GMatchInfo) partial_info = NULL; + g_autoptr(GMatchInfo) partial_info = NULL; - g_regex_match_full(re, buffer+partial_start, to-from-partial_start, 0, - G_REGEX_MATCH_ANCHORED, &partial_info, &tmp_error); - if (tmp_error) { - g_propagate_error(error, tmp_error); - return FALSE; - } - - if (g_match_info_matches(partial_info)) { - g_free(matched->matches[i].ranges); - matched->matches[i].ranges = teco_get_ranges(partial_info, from+partial_start, - &matched->matches[i].num_ranges); - i = ++matched_total % matched_num; - } + g_regex_match_full(re, buffer+partial_start, to-from-partial_start, 0, + G_REGEX_MATCH_ANCHORED, &partial_info, &tmp_error); + if (tmp_error) { + g_propagate_error(error, tmp_error); + return FALSE; + } + + if (g_match_info_matches(partial_info)) { + g_free(matched->matches[i].ranges); + matched->matches[i].ranges = teco_get_ranges(partial_info, from+partial_start, + &matched->matches[i].num_ranges); + i = ++matched_total % matched_num; } /* there might still be other matches within the current block */ - } else if (g_match_info_matches(info)) { - g_free(matched->matches[i].ranges); - matched->matches[i].ranges = teco_get_ranges(info, from+from_block, - &matched->matches[i].num_ranges); - i = ++matched_total % matched_num; } else { break; } -- cgit v1.2.3