Skip to content

Commit c59272c

Browse files
ranma42vivekvpandya
authored andcommitted
Prefer encoding the char when checking for string prefix/suffix
This enables constant folding when matching a literal char. Fixes rust-lang#41993.
1 parent 61f995f commit c59272c

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/libcore/str/pattern.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -445,21 +445,15 @@ impl<'a> Pattern<'a> for char {
445445

446446
#[inline]
447447
fn is_prefix_of(self, haystack: &'a str) -> bool {
448-
if let Some(ch) = haystack.chars().next() {
449-
self == ch
450-
} else {
451-
false
452-
}
448+
let mut buffer = [0u8; 4];
449+
self.encode_utf8(&mut buffer).is_prefix_of(haystack)
453450
}
454451

455452
#[inline]
456453
fn is_suffix_of(self, haystack: &'a str) -> bool where Self::Searcher: ReverseSearcher<'a>
457454
{
458-
if let Some(ch) = haystack.chars().next_back() {
459-
self == ch
460-
} else {
461-
false
462-
}
455+
let mut buffer = [0u8; 4];
456+
self.encode_utf8(&mut buffer).is_suffix_of(haystack)
463457
}
464458
}
465459

0 commit comments

Comments
 (0)