Fix: ends-with ()

This commit is contained in:
Neill Johnston 2023-08-30 11:17:27 -04:00 committed by GitHub
parent 5b36b46230
commit dacab7869f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -116,7 +116,22 @@ impl Str {
match pattern {
StrPattern::Str(pat) => self.0.ends_with(pat.as_str()),
StrPattern::Regex(re) => {
re.find_iter(self).last().map_or(false, |m| m.end() == self.0.len())
let mut start_byte = 0;
while let Some(mat) = re.find_at(self, start_byte) {
if mat.end() == self.0.len() {
return true;
}
// There might still be a match overlapping this one, so
// restart at the next code point
if let Some(c) = &self[mat.start()..].chars().next() {
start_byte = mat.start() + c.len_utf8();
} else {
break;
}
}
false
}
}
}