]> git.lizzy.rs Git - rust.git/commitdiff
Prefer encoding the char when checking for string prefix/suffix
authorAndrea Canciani <ranma42@gmail.com>
Wed, 11 Dec 2019 20:28:37 +0000 (21:28 +0100)
committerAndrea Canciani <ranma42@gmail.com>
Thu, 12 Dec 2019 02:33:46 +0000 (03:33 +0100)
This enables constant folding when matching a literal char.

Fixes #41993.

src/libcore/str/pattern.rs

index dd3765d42da02ec3a4a22208497c759444373215..71dd77fee739687ab91d738758559133fcc708ec 100644 (file)
@@ -450,21 +450,15 @@ fn is_contained_in(self, haystack: &'a str) -> bool {
 
     #[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)
     }
 }