@@ -17,10 +17,18 @@ 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 (buffer.length == 0 ) {
21
+ // Case 0: buffer is empty
22
+ assertEq (result, type (uint256 ).max);
23
+ } else if (result == type (uint256 ).max) {
24
+ // Case 1: search value could not be found
25
+ for (uint256 i = pos; i < buffer.length ; ++ i) assertNotEq (buffer[i], s);
26
+ } else {
27
+ // Case 2: search value was found
28
+ assertEq (buffer[result], s);
29
+ // search value is not present anywhere before the found location
30
+ for (uint256 i = pos; i < result; ++ i) assertNotEq (buffer[i], s);
31
+ }
24
32
}
25
33
26
34
function testLastIndexOf (bytes memory buffer , bytes1 s ) public pure {
@@ -30,13 +38,18 @@ contract BytesTest is Test {
30
38
function testLastIndexOf (bytes memory buffer , bytes1 s , uint256 pos ) public pure {
31
39
uint256 result = Bytes.lastIndexOf (buffer, s, pos);
32
40
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);
41
+ if (buffer.length == 0 ) {
42
+ // Case 0: buffer is empty
43
+ assertEq (result, type (uint256 ).max);
44
+ } else if (result == type (uint256 ).max) {
45
+ // Case 1: search value could not be found
46
+ for (uint256 i = 0 ; i <= Math.min (pos, buffer.length - 1 ); ++ i) assertNotEq (buffer[i], s);
47
+ } else {
48
+ // Case 2: search value was found
49
+ assertEq (buffer[result], s);
50
+ // search value is not present anywhere after the found location
51
+ for (uint256 i = result + 1 ; i <= Math.min (pos, buffer.length - 1 ); ++ i) assertNotEq (buffer[i], s);
38
52
}
39
- if (result != type (uint256 ).max) assertEq (buffer[result], s);
40
53
}
41
54
42
55
// SLICES
0 commit comments