fix test_string_boundaries

This commit is contained in:
Kangzhi Shi
2021-04-16 09:35:11 +02:00
parent b497b2234d
commit 9728dd8699
2 changed files with 22 additions and 3 deletions

View File

@@ -292,6 +292,14 @@ trait MatchContextDrive {
let this = !self.at_end() && word_checker(self.peek_char());
this != that
}
fn at_non_boundary<F: FnMut(u32) -> bool>(&self, mut word_checker: F) -> bool {
if self.at_beginning() && self.at_end() {
return false;
}
let that = !self.at_beginning() && word_checker(self.back_peek_char());
let this = !self.at_end() && word_checker(self.peek_char());
this == that
}
fn back_peek_char(&self) -> u32 {
self.state().string.back_peek(self.ctx().string_offset)
}
@@ -738,14 +746,14 @@ fn at(drive: &StackDrive, atcode: SreAtCode) -> bool {
SreAtCode::BEGINNING | SreAtCode::BEGINNING_STRING => drive.at_beginning(),
SreAtCode::BEGINNING_LINE => drive.at_beginning() || is_linebreak(drive.back_peek_char()),
SreAtCode::BOUNDARY => drive.at_boundary(is_word),
SreAtCode::NON_BOUNDARY => !drive.at_boundary(is_word),
SreAtCode::NON_BOUNDARY => drive.at_non_boundary(is_word),
SreAtCode::END => (drive.remaining_chars() == 1 && drive.at_linebreak()) || drive.at_end(),
SreAtCode::END_LINE => drive.at_linebreak() || drive.at_end(),
SreAtCode::END_STRING => drive.at_end(),
SreAtCode::LOC_BOUNDARY => drive.at_boundary(is_loc_word),
SreAtCode::LOC_NON_BOUNDARY => !drive.at_boundary(is_loc_word),
SreAtCode::LOC_NON_BOUNDARY => drive.at_non_boundary(is_loc_word),
SreAtCode::UNI_BOUNDARY => drive.at_boundary(is_uni_word),
SreAtCode::UNI_NON_BOUNDARY => !drive.at_boundary(is_uni_word),
SreAtCode::UNI_NON_BOUNDARY => drive.at_non_boundary(is_uni_word),
}
}

View File

@@ -37,3 +37,14 @@ fn test_assert() {
state = state.search();
assert!(state.has_matched == Some(true));
}
#[test]
fn test_string_boundaries() {
// pattern big_b = re.compile(r'\B')
// START GENERATED by generate_tests.py
#[rustfmt::skip] let big_b = Pattern { code: &[15, 4, 0, 0, 0, 6, 11, 1], flags: SreFlag::from_bits_truncate(32) };
// END GENERATED
let mut state = big_b.state("", 0..usize::MAX);
state = state.search();
assert!(state.has_matched == None)
}