File tree 3 files changed +34
-0
lines changed
3 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -160,6 +160,12 @@ pub trait Pattern<'a>: Sized {
160
160
None
161
161
}
162
162
}
163
+
164
+ /// Return the pattern as a fixed slice of UTF-8 bytes, if possible.
165
+ #[ inline]
166
+ fn as_bytes ( & self ) -> Option < & [ u8 ] > {
167
+ None
168
+ }
163
169
}
164
170
165
171
// Searcher
@@ -917,6 +923,11 @@ where
917
923
/// Delegates to the `&str` impl.
918
924
impl < ' a , ' b , ' c > Pattern < ' a > for & ' c & ' b str {
919
925
pattern_methods ! ( StrSearcher <' a, ' b>, |& s| s, |s| s) ;
926
+
927
+ #[ inline]
928
+ fn as_bytes ( & self ) -> Option < & [ u8 ] > {
929
+ ( * self ) . as_bytes ( )
930
+ }
920
931
}
921
932
922
933
/////////////////////////////////////////////////////////////////////////////
@@ -1001,6 +1012,11 @@ impl<'a, 'b> Pattern<'a> for &'b str {
1001
1012
None
1002
1013
}
1003
1014
}
1015
+
1016
+ #[ inline]
1017
+ fn as_bytes ( & self ) -> Option < & [ u8 ] > {
1018
+ Some ( str:: as_bytes ( self ) )
1019
+ }
1004
1020
}
1005
1021
1006
1022
/////////////////////////////////////////////////////////////////////////////
Original file line number Diff line number Diff line change @@ -288,10 +288,18 @@ impl Slice {
288
288
289
289
#[ inline]
290
290
pub fn starts_with < ' a , P : Pattern < ' a > > ( & ' a self , pattern : P ) -> bool {
291
+ if let Some ( pattern_bytes) = pattern. as_bytes ( ) {
292
+ return self . inner . starts_with ( pattern_bytes) ;
293
+ }
291
294
self . to_str_prefix ( ) . starts_with ( pattern)
292
295
}
293
296
294
297
pub fn strip_prefix < ' a , P : Pattern < ' a > > ( & ' a self , prefix : P ) -> Option < & ' a Slice > {
298
+ if let Some ( prefix_bytes) = prefix. as_bytes ( ) {
299
+ let suffix = self . inner . strip_prefix ( prefix_bytes) ?;
300
+ return Some ( Slice :: from_u8_slice ( suffix) ) ;
301
+ }
302
+
295
303
let p = self . to_str_prefix ( ) ;
296
304
let prefix_len = match prefix. into_searcher ( p) . next ( ) {
297
305
SearchStep :: Match ( 0 , prefix_len) => prefix_len,
Original file line number Diff line number Diff line change @@ -801,10 +801,20 @@ impl Wtf8 {
801
801
802
802
#[ inline]
803
803
pub fn starts_with < ' a , P : Pattern < ' a > > ( & ' a self , pattern : P ) -> bool {
804
+ if let Some ( pattern_bytes) = pattern. as_bytes ( ) {
805
+ return self . bytes . starts_with ( pattern_bytes) ;
806
+ }
804
807
self . to_str_prefix ( ) . starts_with ( pattern)
805
808
}
806
809
807
810
pub fn strip_prefix < ' a , P : Pattern < ' a > > ( & ' a self , prefix : P ) -> Option < & ' a Wtf8 > {
811
+ if let Some ( prefix_bytes) = prefix. as_bytes ( ) {
812
+ let suffix = self . bytes . strip_prefix ( prefix_bytes) ?;
813
+ // SAFETY: WTF-8 is a superset of UTF-8, so stripping off a UTF-8
814
+ // prefix will yield a suffix that is valid WTF-8.
815
+ return unsafe { Some ( Wtf8 :: from_bytes_unchecked ( suffix) ) } ;
816
+ }
817
+
808
818
let p = self . to_str_prefix ( ) ;
809
819
let prefix_len = match prefix. into_searcher ( p) . next ( ) {
810
820
SearchStep :: Match ( 0 , prefix_len) => prefix_len,
You can’t perform that action at this time.
0 commit comments