@@ -6,13 +6,11 @@ pragma solidity ^0.8.20;
6
6
import {Math} from "./math/Math.sol " ;
7
7
import {SafeCast} from "./math/SafeCast.sol " ;
8
8
import {SignedMath} from "./math/SignedMath.sol " ;
9
- import {Bytes} from "./Bytes.sol " ;
10
9
11
10
/**
12
11
* @dev String operations.
13
12
*/
14
13
library Strings {
15
- using Bytes for bytes ;
16
14
using SafeCast for * ;
17
15
18
16
bytes16 private constant HEX_DIGITS = "0123456789abcdef " ;
@@ -237,8 +235,8 @@ library Strings {
237
235
bytes memory buffer = bytes (input);
238
236
239
237
// Check presence of a negative sign.
240
- bytes1 sign = bytes1 (buffer. unsafeReadBytesOffset (begin));
241
- bool positiveSign = sign == bytes1 ("+ " );
238
+ bytes1 sign = bytes1 (unsafeReadBytesOffset (buffer, begin));
239
+ bool positiveSign = sign == bytes1 ("+ " );
242
240
bool negativeSign = sign == bytes1 ("- " );
243
241
uint256 offset = (positiveSign || negativeSign).toUint ();
244
242
@@ -299,7 +297,7 @@ library Strings {
299
297
bytes memory buffer = bytes (input);
300
298
301
299
// skip 0x prefix if present
302
- bool hasPrefix = bytes2 (buffer. unsafeReadBytesOffset (begin)) == bytes2 ("0x " );
300
+ bool hasPrefix = bytes2 (unsafeReadBytesOffset (buffer, begin)) == bytes2 ("0x " );
303
301
uint256 offset = hasPrefix.toUint () * 2 ;
304
302
305
303
uint256 result = 0 ;
@@ -357,7 +355,7 @@ library Strings {
357
355
uint256 end
358
356
) internal pure returns (bool success , address value ) {
359
357
// check that input is the correct length
360
- bool hasPrefix = bytes2 (bytes (input). unsafeReadBytesOffset ( begin)) == bytes2 ("0x " );
358
+ bool hasPrefix = bytes2 (unsafeReadBytesOffset ( bytes (input), begin)) == bytes2 ("0x " );
361
359
uint256 expectedLength = 40 + hasPrefix.toUint () * 2 ;
362
360
363
361
if (end - begin == expectedLength) {
@@ -386,4 +384,17 @@ library Strings {
386
384
387
385
return value;
388
386
}
387
+
388
+ /**
389
+ * @dev Reads a bytes32 from a bytes array without bounds checking.
390
+ *
391
+ * NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
392
+ * assembly block as such would prevent some optimizations.
393
+ */
394
+ function unsafeReadBytesOffset (bytes memory buffer , uint256 offset ) private pure returns (bytes32 value ) {
395
+ // This is not memory safe in the general case, but all calls to this private function are within bounds.
396
+ assembly ("memory-safe" ) {
397
+ value := mload (add (buffer, add (0x20 , offset)))
398
+ }
399
+ }
389
400
}
0 commit comments