mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #11047 from ethereum/bytesToBytesNNConversion
Bytes to bytesNN conversion
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
contract C {
|
||||
bytes s = "abcdefghabcdefghabcdefghabcdefg";
|
||||
|
||||
function fromMemory(bytes memory m) public returns (bytes16) {
|
||||
assembly { mstore(m, 14) }
|
||||
return bytes16(m);
|
||||
}
|
||||
function fromCalldata(bytes calldata c) external returns (bytes16) {
|
||||
return bytes16(c);
|
||||
}
|
||||
function fromStorage() external returns (bytes32) {
|
||||
return bytes32(s);
|
||||
}
|
||||
function fromSlice(bytes calldata c) external returns (bytes8) {
|
||||
return bytes8(c[0:6]);
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: true
|
||||
// ----
|
||||
// fromMemory(bytes): 0x20, 16, "abcdefghabcdefgh" -> "abcdefghabcdef\0\0"
|
||||
// fromCalldata(bytes): 0x20, 15, "abcdefghabcdefgh" -> "abcdefghabcdefg\0"
|
||||
// fromStorage() -> "abcdefghabcdefghabcdefghabcdefg\0"
|
||||
// fromSlice(bytes): 0x20, 15, "abcdefghabcdefgh" -> "abcdef\0\0"
|
||||
@@ -0,0 +1,28 @@
|
||||
contract C {
|
||||
bytes s = "abcdefghabcdefgh";
|
||||
bytes sLong = "abcdefghabcdefghabcdefghabcdefgh";
|
||||
|
||||
function fromMemory(bytes memory m) public returns (bytes16) {
|
||||
return bytes16(m);
|
||||
}
|
||||
function fromCalldata(bytes calldata c) external returns (bytes16) {
|
||||
return bytes16(c);
|
||||
}
|
||||
function fromStorage() external returns (bytes16) {
|
||||
return bytes16(s);
|
||||
}
|
||||
function fromStorageLong() external returns (bytes32) {
|
||||
return bytes32(sLong);
|
||||
}
|
||||
function fromSlice(bytes calldata c) external returns (bytes8) {
|
||||
return bytes8(c[1:9]);
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// fromMemory(bytes): 0x20, 16, "abcdefghabcdefgh" -> "abcdefghabcdefgh"
|
||||
// fromCalldata(bytes): 0x20, 16, "abcdefghabcdefgh" -> "abcdefghabcdefgh"
|
||||
// fromStorage() -> "abcdefghabcdefgh"
|
||||
// fromStorageLong() -> "abcdefghabcdefghabcdefghabcdefgh"
|
||||
// fromSlice(bytes): 0x20, 16, "abcdefghabcdefgh" -> "bcdefgha"
|
||||
@@ -0,0 +1,23 @@
|
||||
contract C {
|
||||
bytes s = "abcdefghabcdefghabcdefghabcdefgha";
|
||||
|
||||
function fromMemory(bytes memory m) public returns (bytes32) {
|
||||
return bytes32(m);
|
||||
}
|
||||
function fromCalldata(bytes calldata c) external returns (bytes32) {
|
||||
return bytes32(c);
|
||||
}
|
||||
function fromStorage() external returns (bytes32) {
|
||||
return bytes32(s);
|
||||
}
|
||||
function fromSlice(bytes calldata c) external returns (bytes32) {
|
||||
return bytes32(c[0:33]);
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: also
|
||||
// ----
|
||||
// fromMemory(bytes): 0x20, 33, "abcdefghabcdefghabcdefghabcdefgh", "a" -> "abcdefghabcdefghabcdefghabcdefgh"
|
||||
// fromCalldata(bytes): 0x20, 33, "abcdefghabcdefghabcdefghabcdefgh", "a" -> "abcdefghabcdefghabcdefghabcdefgh"
|
||||
// fromStorage() -> "abcdefghabcdefghabcdefghabcdefgh"
|
||||
// fromSlice(bytes): 0x20, 33, "abcdefghabcdefghabcdefghabcdefgh", "a" -> "abcdefghabcdefghabcdefghabcdefgh"
|
||||
@@ -0,0 +1,7 @@
|
||||
contract C {
|
||||
bytes s;
|
||||
function f(bytes calldata c, string memory m) public view returns (bytes3, bytes8, bytes16, bytes32) {
|
||||
return (bytes3(c[0:3]), bytes8(s), bytes16(c), bytes32(bytes(m)));
|
||||
}
|
||||
}
|
||||
// ----
|
||||
@@ -0,0 +1,15 @@
|
||||
contract C {
|
||||
bytes s;
|
||||
function f(bytes calldata c, string memory m) public view returns (bytes3 r1, bytes16 r2, bytes32 r3) {
|
||||
require(c.length >= 3, "");
|
||||
r2 = s;
|
||||
r1 = c[0:3];
|
||||
r3 = bytes32(m);
|
||||
r3 = m;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError 7407: (183-184): Type bytes storage ref is not implicitly convertible to expected type bytes16.
|
||||
// TypeError 7407: (199-205): Type bytes calldata slice is not implicitly convertible to expected type bytes3.
|
||||
// TypeError 9640: (220-230): Explicit type conversion not allowed from "string memory" to "bytes32".
|
||||
// TypeError 7407: (245-246): Type string memory is not implicitly convertible to expected type bytes32.
|
||||
Reference in New Issue
Block a user