Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.

Commit caef94d

Browse files
authored
Merge pull request #5 from qingshi163/master
fix test_string_boundaries
2 parents b497b22 + 9728dd8 commit caef94d

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/engine.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,14 @@ trait MatchContextDrive {
292292
let this = !self.at_end() && word_checker(self.peek_char());
293293
this != that
294294
}
295+
fn at_non_boundary<F: FnMut(u32) -> bool>(&self, mut word_checker: F) -> bool {
296+
if self.at_beginning() && self.at_end() {
297+
return false;
298+
}
299+
let that = !self.at_beginning() && word_checker(self.back_peek_char());
300+
let this = !self.at_end() && word_checker(self.peek_char());
301+
this == that
302+
}
295303
fn back_peek_char(&self) -> u32 {
296304
self.state().string.back_peek(self.ctx().string_offset)
297305
}
@@ -738,14 +746,14 @@ fn at(drive: &StackDrive, atcode: SreAtCode) -> bool {
738746
SreAtCode::BEGINNING | SreAtCode::BEGINNING_STRING => drive.at_beginning(),
739747
SreAtCode::BEGINNING_LINE => drive.at_beginning() || is_linebreak(drive.back_peek_char()),
740748
SreAtCode::BOUNDARY => drive.at_boundary(is_word),
741-
SreAtCode::NON_BOUNDARY => !drive.at_boundary(is_word),
749+
SreAtCode::NON_BOUNDARY => drive.at_non_boundary(is_word),
742750
SreAtCode::END => (drive.remaining_chars() == 1 && drive.at_linebreak()) || drive.at_end(),
743751
SreAtCode::END_LINE => drive.at_linebreak() || drive.at_end(),
744752
SreAtCode::END_STRING => drive.at_end(),
745753
SreAtCode::LOC_BOUNDARY => drive.at_boundary(is_loc_word),
746-
SreAtCode::LOC_NON_BOUNDARY => !drive.at_boundary(is_loc_word),
754+
SreAtCode::LOC_NON_BOUNDARY => drive.at_non_boundary(is_loc_word),
747755
SreAtCode::UNI_BOUNDARY => drive.at_boundary(is_uni_word),
748-
SreAtCode::UNI_NON_BOUNDARY => !drive.at_boundary(is_uni_word),
756+
SreAtCode::UNI_NON_BOUNDARY => drive.at_non_boundary(is_uni_word),
749757
}
750758
}
751759

tests/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,14 @@ fn test_assert() {
3737
state = state.search();
3838
assert!(state.has_matched == Some(true));
3939
}
40+
41+
#[test]
42+
fn test_string_boundaries() {
43+
// pattern big_b = re.compile(r'\B')
44+
// START GENERATED by generate_tests.py
45+
#[rustfmt::skip] let big_b = Pattern { code: &[15, 4, 0, 0, 0, 6, 11, 1], flags: SreFlag::from_bits_truncate(32) };
46+
// END GENERATED
47+
let mut state = big_b.state("", 0..usize::MAX);
48+
state = state.search();
49+
assert!(state.has_matched == None)
50+
}

0 commit comments

Comments
 (0)