From e3ff5de6a5fcd9f732715ea0081a6b54c632355f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bla=C5=BE=20Hrastnik?= Date: Wed, 9 Jun 2021 18:24:23 +0900 Subject: [PATCH] Fix searching through several chunks --- helix-core/src/search.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/helix-core/src/search.rs b/helix-core/src/search.rs index 3a0ed88f9..b8157d743 100644 --- a/helix-core/src/search.rs +++ b/helix-core/src/search.rs @@ -109,7 +109,6 @@ impl Searcher { /// Returns the end offset of the longest match. If no match exists, then None is returned. /// NOTE: based on DFA::find_at fn find(&self, text: RopeSlice, dfa: &impl DFA) -> Option { - // TOOD: needs to change to rfind condition if searching reverse // TODO: check this inside main search // if dfa.is_anchored() && start > 0 { // return None; @@ -124,6 +123,8 @@ impl Searcher { None }; + let mut chunk_byte_offset = 0; + for chunk in text.chunks() { for (i, &b) in chunk.as_bytes().iter().enumerate() { state = unsafe { dfa.next_state_unchecked(state, b) }; @@ -131,9 +132,10 @@ impl Searcher { if dfa.is_dead_state(state) { return last_match; } - last_match = Some(i + 1); + last_match = Some(chunk_byte_offset + i + 1); } } + chunk_byte_offset += chunk.len(); } last_match @@ -159,18 +161,19 @@ impl Searcher { }; // This is basically chunks().rev() - let (mut chunks, _, _, _) = text.chunks_at_byte(text.len_bytes()); + let (mut chunks, mut chunk_byte_offset, _, _) = text.chunks_at_byte(text.len_bytes()); while let Some(chunk) = chunks.prev() { - for (i, &b) in chunk.as_bytes().iter().enumerate().rev() { + for (i, &b) in chunk.as_bytes().iter().rev().enumerate() { state = unsafe { dfa.next_state_unchecked(state, b) }; if dfa.is_match_or_dead_state(state) { if dfa.is_dead_state(state) { return last_match; } - last_match = Some(i); + last_match = Some(chunk_byte_offset - i - 1); } } + chunk_byte_offset -= chunk.len(); } last_match }