Skip to content

Commit 1f6d023

Browse files
committed
Prefer encoding the char when checking for string prefix/suffix
This enables constant folding when matching a literal char. Fixes #41993.
1 parent cc863f0 commit 1f6d023

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
@@ -450,21 +450,15 @@ impl<'a> Pattern<'a> for char {
450450

451451
#[inline]
452452
fn is_prefix_of(self, haystack: &'a str) -> bool {
453-
if let Some(ch) = haystack.chars().next() {
454-
self == ch
455-
} else {
456-
false
457-
}
453+
let mut buffer = [0u8; 4];
454+
self.encode_utf8(&mut buffer).is_prefix_of(haystack)
458455
}
459456

460457
#[inline]
461458
fn is_suffix_of(self, haystack: &'a str) -> bool where Self::Searcher: ReverseSearcher<'a>
462459
{
463-
if let Some(ch) = haystack.chars().next_back() {
464-
self == ch
465-
} else {
466-
false
467-
}
460+
let mut buffer = [0u8; 4];
461+
self.encode_utf8(&mut buffer).is_suffix_of(haystack)
468462
}
469463
}
470464

0 commit comments

Comments
 (0)