@@ -5,10 +5,13 @@ use crate::cmp;
5
5
use crate :: hash:: { Hash , Hasher } ;
6
6
use crate :: rc:: Rc ;
7
7
use crate :: sync:: Arc ;
8
+ use crate :: needle:: { Hay , Haystack , Needle , Span , Searcher , ReverseSearcher , Consumer , ReverseConsumer } ;
8
9
9
- use crate :: sys:: os_str:: { Buf , Slice } ;
10
+ use crate :: sys:: os_str:: { Buf , Slice , OsStrSearcher } ;
10
11
use crate :: sys_common:: { AsInner , IntoInner , FromInner } ;
11
12
13
+ use core:: slice:: needles:: { TwoWaySearcher , SliceSearcher , NaiveSearcher } ;
14
+
12
15
/// A type that can represent owned, mutable platform-native strings, but is
13
16
/// cheaply inter-convertible with Rust strings.
14
17
///
@@ -1246,3 +1249,166 @@ mod tests {
1246
1249
assert_eq ! ( OsString :: from_wide( & [ 0xDF0D , 0xD83C ] ) , & os_str[ 7 ..11 ] ) ;
1247
1250
}
1248
1251
}
1252
+
1253
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1254
+ unsafe impl Hay for OsStr {
1255
+ type Index = usize ;
1256
+
1257
+ #[ inline]
1258
+ fn empty < ' a > ( ) -> & ' a Self {
1259
+ Self :: new ( "" )
1260
+ }
1261
+
1262
+ #[ inline]
1263
+ fn start_index ( & self ) -> usize {
1264
+ 0
1265
+ }
1266
+
1267
+ #[ inline]
1268
+ fn end_index ( & self ) -> usize {
1269
+ self . len ( )
1270
+ }
1271
+
1272
+ #[ inline]
1273
+ unsafe fn slice_unchecked ( & self , range : ops:: Range < usize > ) -> & Self {
1274
+ & self [ range]
1275
+ }
1276
+
1277
+ #[ inline]
1278
+ unsafe fn next_index ( & self , index : usize ) -> usize {
1279
+ self . inner . next_index ( index)
1280
+ }
1281
+
1282
+ #[ inline]
1283
+ unsafe fn prev_index ( & self , index : usize ) -> usize {
1284
+ self . inner . prev_index ( index)
1285
+ }
1286
+ }
1287
+
1288
+ // use a macro here since the type of `hay.inner.inner` is platform dependent
1289
+ // and we don't want to expose that type.
1290
+ macro_rules! span_as_inner {
1291
+ ( $span: expr) => { {
1292
+ let ( hay, range) = $span. into_parts( ) ;
1293
+ unsafe { Span :: from_parts( & hay. inner. inner, range) }
1294
+ } }
1295
+ }
1296
+
1297
+ fn span_as_inner_bytes ( span : Span < & OsStr > ) -> Span < & [ u8 ] > {
1298
+ let ( hay, range) = span. into_parts ( ) ;
1299
+ unsafe { Span :: from_parts ( hay. inner . as_bytes_for_searcher ( ) , range) }
1300
+ }
1301
+
1302
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1303
+ unsafe impl < ' p > Searcher < OsStr > for TwoWaySearcher < ' p , u8 > {
1304
+ #[ inline]
1305
+ fn search ( & mut self , span : Span < & OsStr > ) -> Option < ops:: Range < usize > > {
1306
+ self . search ( span_as_inner_bytes ( span) )
1307
+ }
1308
+ }
1309
+
1310
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1311
+ unsafe impl < ' p > ReverseSearcher < OsStr > for TwoWaySearcher < ' p , u8 > {
1312
+ #[ inline]
1313
+ fn rsearch ( & mut self , span : Span < & OsStr > ) -> Option < ops:: Range < usize > > {
1314
+ self . rsearch ( span_as_inner_bytes ( span) )
1315
+ }
1316
+ }
1317
+
1318
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1319
+ unsafe impl < ' p > Consumer < OsStr > for NaiveSearcher < ' p , u8 > {
1320
+ #[ inline]
1321
+ fn consume ( & mut self , span : Span < & OsStr > ) -> Option < usize > {
1322
+ self . consume ( span_as_inner_bytes ( span) )
1323
+ }
1324
+
1325
+ #[ inline]
1326
+ fn trim_start ( & mut self , hay : & OsStr ) -> usize {
1327
+ self . trim_start ( hay. inner . as_bytes_for_searcher ( ) )
1328
+ }
1329
+ }
1330
+
1331
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1332
+ unsafe impl < ' p > ReverseConsumer < OsStr > for NaiveSearcher < ' p , u8 > {
1333
+ #[ inline]
1334
+ fn rconsume ( & mut self , span : Span < & OsStr > ) -> Option < usize > {
1335
+ self . rconsume ( span_as_inner_bytes ( span) )
1336
+ }
1337
+
1338
+ #[ inline]
1339
+ fn trim_end ( & mut self , hay : & OsStr ) -> usize {
1340
+ self . trim_end ( hay. inner . as_bytes_for_searcher ( ) )
1341
+ }
1342
+ }
1343
+
1344
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1345
+ unsafe impl < ' p > Searcher < OsStr > for OsStrSearcher < SliceSearcher < ' p , u8 > > {
1346
+ #[ inline]
1347
+ fn search ( & mut self , span : Span < & OsStr > ) -> Option < ops:: Range < usize > > {
1348
+ self . search ( span_as_inner ! ( span) )
1349
+ }
1350
+ }
1351
+
1352
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1353
+ unsafe impl < ' p > ReverseSearcher < OsStr > for OsStrSearcher < SliceSearcher < ' p , u8 > > {
1354
+ #[ inline]
1355
+ fn rsearch ( & mut self , span : Span < & OsStr > ) -> Option < ops:: Range < usize > > {
1356
+ self . rsearch ( span_as_inner ! ( span) )
1357
+ }
1358
+ }
1359
+
1360
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1361
+ unsafe impl < ' p > Consumer < OsStr > for OsStrSearcher < NaiveSearcher < ' p , u8 > > {
1362
+ #[ inline]
1363
+ fn consume ( & mut self , span : Span < & OsStr > ) -> Option < usize > {
1364
+ self . consume ( span_as_inner ! ( span) )
1365
+ }
1366
+
1367
+ #[ inline]
1368
+ fn trim_start ( & mut self , hay : & OsStr ) -> usize {
1369
+ self . trim_start ( & hay. inner . inner )
1370
+ }
1371
+ }
1372
+
1373
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1374
+ unsafe impl < ' p > ReverseConsumer < OsStr > for OsStrSearcher < NaiveSearcher < ' p , u8 > > {
1375
+ #[ inline]
1376
+ fn rconsume ( & mut self , span : Span < & OsStr > ) -> Option < usize > {
1377
+ self . rconsume ( span_as_inner ! ( span) )
1378
+ }
1379
+
1380
+ #[ inline]
1381
+ fn trim_end ( & mut self , hay : & OsStr ) -> usize {
1382
+ self . trim_end ( & hay. inner . inner )
1383
+ }
1384
+ }
1385
+
1386
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1387
+ impl < ' p , H : Haystack < Target = OsStr > > Needle < H > for & ' p OsStr {
1388
+ type Searcher = OsStrSearcher < SliceSearcher < ' p , u8 > > ;
1389
+ type Consumer = OsStrSearcher < NaiveSearcher < ' p , u8 > > ;
1390
+
1391
+ fn into_searcher ( self ) -> Self :: Searcher {
1392
+ self . inner . into_searcher ( )
1393
+ }
1394
+
1395
+ fn into_consumer ( self ) -> Self :: Consumer {
1396
+ self . inner . into_consumer ( )
1397
+ }
1398
+ }
1399
+
1400
+ // FIXME cannot impl `Needle<(_: Haystack<Target = OsStr>)>` due to RFC 1672 being postponed.
1401
+ // (need to wait for chalk)
1402
+ #[ unstable( feature = "needle" , issue = "56345" ) ]
1403
+ impl < ' h , ' p > Needle < & ' h OsStr > for & ' p str {
1404
+ type Searcher = SliceSearcher < ' p , u8 > ;
1405
+ type Consumer = NaiveSearcher < ' p , u8 > ;
1406
+
1407
+ fn into_searcher ( self ) -> Self :: Searcher {
1408
+ SliceSearcher :: new ( self . as_bytes ( ) )
1409
+ }
1410
+
1411
+ fn into_consumer ( self ) -> Self :: Consumer {
1412
+ NaiveSearcher :: new ( self . as_bytes ( ) )
1413
+ }
1414
+ }
0 commit comments