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 rust-lang#41993.
  • Loading branch information
ranma42 authored and vivekvpandya committed Dec 18, 2019
1 parent 61f995f commit c59272c
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 @@ -445,21 +445,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 c59272c

Please sign in to comment.