@@ -17,10 +17,15 @@ contract BytesTest is Test {
17
17
function testIndexOf (bytes memory buffer , bytes1 s , uint256 pos ) public pure {
18
18
uint256 result = Bytes.indexOf (buffer, s, pos);
19
19
20
- // The search value should not me present between `pos` (included) and `result` excluded.
21
- // Do not search after the end of the buffer
22
- for (uint256 i = pos; i < Math.min (result, buffer.length ); ++ i) assertNotEq (buffer[i], s);
23
- if (result != type (uint256 ).max) assertEq (buffer[result], s);
20
+ if (result == type (uint256 ).max) {
21
+ // Case 1: search value could not be found
22
+ for (uint256 i = pos; i < buffer.length ; ++ i) assertNotEq (buffer[i], s);
23
+ } else {
24
+ // Case 2: search value was found
25
+ assertEq (buffer[result], s);
26
+ // search value is not present anywhere before the found location
27
+ for (uint256 i = pos; i < result; ++ i) assertNotEq (buffer[i], s);
28
+ }
24
29
}
25
30
26
31
function testLastIndexOf (bytes memory buffer , bytes1 s ) public pure {
@@ -30,13 +35,16 @@ contract BytesTest is Test {
30
35
function testLastIndexOf (bytes memory buffer , bytes1 s , uint256 pos ) public pure {
31
36
uint256 result = Bytes.lastIndexOf (buffer, s, pos);
32
37
33
- // Case found: the search value should not be present between `result` (excluded) and `pos` (included)
34
- // Case not found: the search value should not be present anywhere before `pos` (included)
35
- unchecked {
36
- // using unchecked gives us `result + 1 == 0` in the "not found" case
37
- for (uint256 i = result + 1 ; i < Math.min (pos + 1 , buffer.length ); ++ i) assertNotEq (buffer[i], s);
38
+ uint256 end = pos == type (uint256 ).max ? buffer.length : Math.min (pos + 1 , buffer.length );
39
+ if (result == type (uint256 ).max) {
40
+ // Case 1: search value could not be found
41
+ for (uint256 i = 0 ; i < end; ++ i) assertNotEq (buffer[i], s);
42
+ } else {
43
+ // Case 2: search value was found
44
+ assertEq (buffer[result], s);
45
+ // search value is not present anywhere after the found location
46
+ for (uint256 i = result + 1 ; i < end; ++ i) assertNotEq (buffer[i], s);
38
47
}
39
- if (result != type (uint256 ).max) assertEq (buffer[result], s);
40
48
}
41
49
42
50
// SLICES
0 commit comments