we_cdk/macros/memory.rs
1/// Comparison data of Binary and String types
2///
3/// # Usage
4/// ```
5/// use we_cdk::*;
6///
7/// #[action]
8/// fn _constructor() {
9/// let binary_equals: Boolean = equals!(binary :: &[0, 1], &[0, 1]);
10/// let string_equals: Boolean = equals!(string :: "test", "value");
11/// }
12/// ```
13#[macro_export]
14macro_rules! equals {
15 (binary :: $left:expr, $right:expr) => {{
16 let (error, result) = wevm::v0::bindings::binary_equals(
17 $left.as_ptr(),
18 $left.len(),
19 $right.as_ptr(),
20 $right.len(),
21 );
22 error!(error);
23 result
24 }};
25 (string :: $left:expr, $right:expr) => {{
26 let (error, result) = wevm::v0::bindings::string_equals(
27 $left.as_ptr(),
28 $left.len(),
29 $right.as_ptr(),
30 $right.len(),
31 );
32 error!(error);
33 result
34 }};
35}
36
37/// Joining data of Binary and String types
38///
39/// # Usage
40/// ```
41/// use we_cdk::*;
42///
43/// #[action]
44/// fn _constructor() {
45/// let bytes: Binary = join!(binary :: &[0, 1], &[2, 3]);
46/// let hello_world: String = join!(string :: "Hello", ", ", "world", "!");
47/// }
48/// ```
49#[macro_export]
50macro_rules! join {
51 // For use within a macro
52 (@inner, $temp:expr, $value:expr) => {
53 let (error, ptr, len) = wevm::v0::bindings::join(
54 $temp.as_ptr(),
55 $temp.len(),
56 $value.as_ptr(),
57 $value.len()
58 );
59 error!(error);
60 $temp = core::slice::from_raw_parts(ptr, len);
61 };
62 (binary :: $( $value:expr ),+ ) => {{
63 let mut temp: &[u8] = &[0u8; 0];
64 $( join!(@inner, temp, $value); )+
65 temp
66 }};
67 (string :: $( $value:expr ),+ ) => {{
68 let mut temp: &[u8] = &[0u8; 0];
69 $( join!(@inner, temp, $value); )+
70 core::str::from_utf8_unchecked(temp)
71 }};
72}
73
74/// Checks if the string/binary is contained in a string/binary
75///
76/// # Usage
77/// ```
78/// use we_cdk::*;
79///
80/// #[action]
81/// fn _constructor() {
82/// let result: Boolean = contains!(&[0, 1, 2, 3, 4, 5], &[2, 3]);
83/// let result: Boolean = contains!("Hello, world!", "world");
84/// }
85/// ```
86#[macro_export]
87macro_rules! contains {
88 ($bytes:expr, $subbytes:expr) => {{
89 let (error, result) = wevm::v0::bindings::contains(
90 $bytes.as_ptr(),
91 $bytes.len(),
92 $subbytes.as_ptr(),
93 $subbytes.len(),
94 );
95 error!(error);
96 result
97 }};
98}
99
100/// Returns a string/binary without the first N characters/bytes
101///
102/// # Usage
103/// ```
104/// use we_cdk::*;
105///
106/// #[action]
107/// fn _constructor() {
108/// let result: Binary = drop!(&[0, 1, 2, 3, 4, 5], 1);
109/// }
110/// ```
111#[macro_export]
112macro_rules! drop {
113 ($bytes:expr, $n:expr) => {{
114 let (error, ptr, len) = wevm::v0::bindings::drop($bytes.as_ptr(), $bytes.len(), $n);
115 error!(error);
116 core::slice::from_raw_parts(ptr, len)
117 }};
118}
119
120/// Returns a string/binary without the last N characters/bytes
121///
122/// # Usage
123/// ```
124/// use we_cdk::*;
125///
126/// #[action]
127/// fn _constructor() {
128/// let result: Binary = drop_right!(&[0, 1, 2, 3, 4, 5], 1);
129/// }
130/// ```
131#[macro_export]
132macro_rules! drop_right {
133 ($bytes:expr, $n:expr) => {{
134 let (error, ptr, len) = wevm::v0::bindings::drop_right($bytes.as_ptr(), $bytes.len(), $n);
135 error!(error);
136 core::slice::from_raw_parts(ptr, len)
137 }};
138}
139
140/// Returns the index of the first occurrence of the substring (if no occurrence is found, returns -1)
141///
142/// # Usage
143/// ```
144/// use we_cdk::*;
145///
146/// #[action]
147/// fn _constructor() {
148/// let result: Integer = index_of!("Hello, world!", "world");
149/// }
150/// ```
151#[macro_export]
152macro_rules! index_of {
153 ($string:expr, $substring:expr) => {{
154 let (error, result) = wevm::v0::bindings::index_of(
155 $string.as_ptr(),
156 $string.len(),
157 $substring.as_ptr(),
158 $substring.len(),
159 );
160 error!(error);
161 result
162 }};
163}
164
165/// Returns the index of the last occurrence of the substring (if no occurrence is found, returns -1)
166///
167/// # Usage
168/// ```
169/// use we_cdk::*;
170///
171/// #[action]
172/// fn _constructor() {
173/// let result: Integer = last_index_of!("Hello, world!", "world");
174/// }
175/// ```
176#[macro_export]
177macro_rules! last_index_of {
178 ($string:expr, $substring:expr) => {{
179 let (error, result) = wevm::v0::bindings::last_index_of(
180 $string.as_ptr(),
181 $string.len(),
182 $substring.as_ptr(),
183 $substring.len(),
184 );
185 error!(error);
186 result
187 }};
188}
189
190/// Returns the first N characters/bytes of the string/binary
191///
192/// # Usage
193/// ```
194/// use we_cdk::*;
195///
196/// #[action]
197/// fn _constructor() {
198/// let result: Binary = take!(&[0, 1, 2, 3, 4, 5], 1);
199/// }
200/// ```
201#[macro_export]
202macro_rules! take {
203 ($bytes:expr, $n:expr) => {{
204 let (error, ptr, len) = wevm::v0::bindings::take($bytes.as_ptr(), $bytes.len(), $n);
205 error!(error);
206 core::slice::from_raw_parts(ptr, len)
207 }};
208}
209
210/// Returns a string/binary without the last N characters/bytes
211///
212/// # Usage
213/// ```
214/// use we_cdk::*;
215///
216/// #[action]
217/// fn _constructor() {
218/// let result: Binary = take_right!(&[0, 1, 2, 3, 4, 5], 1);
219/// }
220/// ```
221#[macro_export]
222macro_rules! take_right {
223 ($bytes:expr, $n:expr) => {{
224 let (error, ptr, len) = wevm::v0::bindings::take_right($bytes.as_ptr(), $bytes.len(), $n);
225 error!(error);
226 core::slice::from_raw_parts(ptr, len)
227 }};
228}