diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index 9c89f5538aad..d1679a06a867 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -1136,11 +1136,10 @@ impl TextBuffer { fn find_select_next(&mut self, search: &mut ActiveSearch, offset: usize, wrap: bool) { if search.buffer_generation != self.buffer.generation() { - unsafe { search.regex.set_text(&search.text) }; + unsafe { search.regex.set_text(&search.text, offset) }; search.buffer_generation = self.buffer.generation(); - } - - if search.next_search_offset != offset { + search.next_search_offset = offset; + } else if search.next_search_offset != offset { search.next_search_offset = offset; search.regex.reset(offset); } diff --git a/src/icu.rs b/src/icu.rs index c3855cceb1c5..d8f36a7d084d 100644 --- a/src/icu.rs +++ b/src/icu.rs @@ -628,17 +628,20 @@ impl Regex { /// # Safety /// /// The caller must ensure that the given `Text` outlives the `Regex` instance. - pub unsafe fn set_text(&mut self, text: &Text) { + pub unsafe fn set_text(&mut self, text: &Text, offset: usize) { let f = assume_loaded(); let mut status = icu_ffi::U_ZERO_ERROR; unsafe { (f.uregex_setUText)(self.0, text.0 as *const _ as *mut _, &mut status) }; + // `uregex_setUText` resets the regex to the start of the text. + // Because of this, we must also call `uregex_reset64`. + unsafe { (f.uregex_reset64)(self.0, offset as i64, &mut status) }; } /// Sets the regex to the absolute offset in the underlying text. - pub fn reset(&mut self, index: usize) { + pub fn reset(&mut self, offset: usize) { let f = assume_loaded(); let mut status = icu_ffi::U_ZERO_ERROR; - unsafe { (f.uregex_reset64)(self.0, index as i64, &mut status) }; + unsafe { (f.uregex_reset64)(self.0, offset as i64, &mut status) }; } }