generated from huff-language/huff-project-template
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path12_String.huff
42 lines (35 loc) · 1.42 KB
/
12_String.huff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* 接口 */
#define function setString(string memory str_) nonpayable returns ()
#define function getString() view returns (string memory)
/* 方法 */
#define macro SET_STRING() = takes (0) returns (0) {
// 如果string长度<32字节,高阶字节存储string内容,最小一位的字节存储string长度的两倍,即len*2
0x24 calldataload // [len]
0x02 mul // [len*2]
0x44 calldataload // [str, len*2]
add // [str+len*2]
0x00 sstore // [] storage: [0x00: str+len*2]
stop
}
#define macro GET_STRING() = takes (0) returns (0) {
0x20 0x00 mstore // [] memory: [0x00: 0x20]
0x00 sload // [str+len*2] memory: [0x00: 0x20]
dup1 // [str+len*2, str+len*2] memory: [0x00: 0x20]
0xFF and 0x01 shr // [len, str+len*2] memory: [0x00: 0x20]
0x20 mstore // [str+len*2] memory: [0x00: 0x20, 0x20: len]
0xFF not and // [str] memory: [0x00: 0x20]
0x40 mstore // [] memory: [0x00: 0x20, 0x20: len, 0x40: str]
0x60 0x00 return
}
#define macro MAIN() = takes (0) returns (0) {
// 通过selector判断要调用哪个函数
0x00 calldataload 0xE0 shr
dup1 __FUNC_SIG(setString) eq set_string jumpi
dup1 __FUNC_SIG(getString) eq get_string jumpi
// 如果没有匹配的函数,就revert
0x00 0x00 revert
set_string:
SET_STRING()
get_string:
GET_STRING()
}