Skip to content

Commit

Permalink
Prefer encoding the char when checking for string prefix/suffix
Browse files Browse the repository at this point in the history
This enables constant folding when matching a literal char.

Fixes #41993.
  • Loading branch information
ranma42 committed Dec 12, 2019
1 parent cc863f0 commit 1f6d023
Showing 1 changed file with 4 additions and 10 deletions.
14 changes: 4 additions & 10 deletions src/libcore/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,21 +450,15 @@ impl<'a> Pattern<'a> for char {

#[inline]
fn is_prefix_of(self, haystack: &'a str) -> bool {
if let Some(ch) = haystack.chars().next() {
self == ch
} else {
false
}
let mut buffer = [0u8; 4];
self.encode_utf8(&mut buffer).is_prefix_of(haystack)
}

#[inline]
fn is_suffix_of(self, haystack: &'a str) -> bool where Self::Searcher: ReverseSearcher<'a>
{
if let Some(ch) = haystack.chars().next_back() {
self == ch
} else {
false
}
let mut buffer = [0u8; 4];
self.encode_utf8(&mut buffer).is_suffix_of(haystack)
}
}

Expand Down

0 comments on commit 1f6d023

Please sign in to comment.