@@ -1341,16 +1341,36 @@ public static int IndexOf<T>(T[] array, T value, int startIndex, int count)
1341
1341
ThrowHelper . ThrowArgumentNullException ( ExceptionArgument . array ) ;
1342
1342
}
1343
1343
1344
- if ( startIndex < 0 || startIndex > array . Length )
1344
+ if ( ( uint ) startIndex > ( uint ) array . Length )
1345
1345
{
1346
1346
ThrowHelper . ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index ( ) ;
1347
1347
}
1348
1348
1349
- if ( count < 0 || count > array . Length - startIndex )
1349
+ if ( ( uint ) count > ( uint ) ( array . Length - startIndex ) )
1350
1350
{
1351
1351
ThrowHelper . ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count ( ) ;
1352
1352
}
1353
1353
1354
+ if ( typeof ( T ) == typeof ( byte ) )
1355
+ {
1356
+ int result = SpanHelpers . IndexOf (
1357
+ ref Unsafe . Add ( ref array . GetRawSzArrayData ( ) , startIndex ) ,
1358
+ Unsafe . As < T , byte > ( ref value ) ,
1359
+ count ) ;
1360
+
1361
+ return ( result >= 0 ? startIndex : 0 ) + result ;
1362
+ }
1363
+
1364
+ if ( typeof ( T ) == typeof ( char ) )
1365
+ {
1366
+ int result = SpanHelpers . IndexOf (
1367
+ ref Unsafe . Add ( ref Unsafe . As < byte , char > ( ref array . GetRawSzArrayData ( ) ) , startIndex ) ,
1368
+ Unsafe . As < T , char > ( ref value ) ,
1369
+ count ) ;
1370
+
1371
+ return ( result >= 0 ? startIndex : 0 ) + result ;
1372
+ }
1373
+
1354
1374
return EqualityComparer < T > . Default . IndexOf ( array , value , startIndex , count ) ;
1355
1375
}
1356
1376
@@ -1499,7 +1519,7 @@ public static int LastIndexOf<T>(T[] array, T value, int startIndex, int count)
1499
1519
}
1500
1520
1501
1521
// Make sure we're not out of range
1502
- if ( startIndex < 0 || startIndex >= array . Length )
1522
+ if ( ( uint ) startIndex >= ( uint ) array . Length )
1503
1523
{
1504
1524
ThrowHelper . ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index ( ) ;
1505
1525
}
@@ -1510,6 +1530,28 @@ public static int LastIndexOf<T>(T[] array, T value, int startIndex, int count)
1510
1530
ThrowHelper . ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count ( ) ;
1511
1531
}
1512
1532
1533
+ if ( typeof ( T ) == typeof ( byte ) )
1534
+ {
1535
+ int endIndex = startIndex - count + 1 ;
1536
+ int result = SpanHelpers . LastIndexOf (
1537
+ ref Unsafe . Add ( ref array . GetRawSzArrayData ( ) , endIndex ) ,
1538
+ Unsafe . As < T , byte > ( ref value ) ,
1539
+ count ) ;
1540
+
1541
+ return ( result >= 0 ? endIndex : 0 ) + result ;
1542
+ }
1543
+
1544
+ if ( typeof ( T ) == typeof ( char ) )
1545
+ {
1546
+ int endIndex = startIndex - count + 1 ;
1547
+ int result = SpanHelpers . LastIndexOf (
1548
+ ref Unsafe . Add ( ref Unsafe . As < byte , char > ( ref array . GetRawSzArrayData ( ) ) , endIndex ) ,
1549
+ Unsafe . As < T , char > ( ref value ) ,
1550
+ count ) ;
1551
+
1552
+ return ( result >= 0 ? endIndex : 0 ) + result ;
1553
+ }
1554
+
1513
1555
return EqualityComparer < T > . Default . LastIndexOf ( array , value , startIndex , count ) ;
1514
1556
}
1515
1557
0 commit comments