From 2b6904f504dc4fc990c595eb04efd131518f4322 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 21 May 2024 14:32:20 +0800 Subject: [PATCH 1/6] Add ABIEncode and Decode for String --- sway-lib-std/src/string.sw | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index a8432ee4869..c7a05569312 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -279,6 +279,33 @@ impl Hash for String { } } +impl AbiEncode for String { + fn abi_encode(self, buffer: Buffer) -> Buffer { + let mut buffer = self.bytes.len().abi_encode(buffer); + + let mut i = 0; + while i < self.bytes.len() { + let item = self.bytes.get(i).unwrap(); + buffer = item.abi_encode(buffer); + i += 1; + } + + buffer + } +} + +impl AbiDecode for String { + fn abi_decode(ref mut buffer: BufferReader) -> Self { + let len = u64::abi_decode(buffer); + let data = buffer.read_bytes(len); + let raw_slice = raw_slice::from_parts(data.ptr(), len); + String { + bytes: Bytes::from(raw_slice), + } + } +} + + // Tests #[test] From e394d8c85a261bdfe78e16ff785c78d282c30e29 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 21 May 2024 14:36:42 +0800 Subject: [PATCH 2/6] Run formatter --- sway-lib-std/src/string.sw | 1 - 1 file changed, 1 deletion(-) diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index c7a05569312..466d268bfd9 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -305,7 +305,6 @@ impl AbiDecode for String { } } - // Tests #[test] From 0035315dec855e3c9aaa291a6d63288c844efd9f Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 21 May 2024 14:46:37 +0800 Subject: [PATCH 3/6] Add encoding and decoding tests --- sway-lib-std/src/string.sw | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index 466d268bfd9..0c29038df78 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -298,9 +298,8 @@ impl AbiDecode for String { fn abi_decode(ref mut buffer: BufferReader) -> Self { let len = u64::abi_decode(buffer); let data = buffer.read_bytes(len); - let raw_slice = raw_slice::from_parts(data.ptr(), len); String { - bytes: Bytes::from(raw_slice), + bytes: Bytes::from(Bytes::from(raw_slice::from_parts::(data.ptr(), len))), } } } @@ -547,3 +546,18 @@ fn string_test_hash() { assert(sha256(string) == sha256(bytes)); } + +#[test] +fn string_test_abi_encoding() { + let string = String::from_ascii_str("fuel"); + + let buffer = Buffer::new(); + let encoded_string = string.abi_encode(buffer); + + let encoded_raw_slice = encoded_string.as_raw_slice(); + let mut buffer_reader = BufferReader::from_parts(encoded_raw_slice.ptr(), encoded_raw_slice.number_of_bytes()); + + let decoded_string = String::abi_decode(buffer_reader); + + assert(string == decoded_string); +} From aae9117be3055f73e74323cb16496b83cae6050f Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 21 May 2024 14:48:35 +0800 Subject: [PATCH 4/6] Add comments --- sway-lib-std/src/string.sw | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index 0c29038df78..c52f9afbaa2 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -281,8 +281,10 @@ impl Hash for String { impl AbiEncode for String { fn abi_encode(self, buffer: Buffer) -> Buffer { + // Encode the length let mut buffer = self.bytes.len().abi_encode(buffer); + // Encode each byte of the string let mut i = 0; while i < self.bytes.len() { let item = self.bytes.get(i).unwrap(); @@ -296,8 +298,10 @@ impl AbiEncode for String { impl AbiDecode for String { fn abi_decode(ref mut buffer: BufferReader) -> Self { + // Get length and string data let len = u64::abi_decode(buffer); let data = buffer.read_bytes(len); + // Create string from the ptr and len as parts of a raw_slice String { bytes: Bytes::from(Bytes::from(raw_slice::from_parts::(data.ptr(), len))), } @@ -556,7 +560,7 @@ fn string_test_abi_encoding() { let encoded_raw_slice = encoded_string.as_raw_slice(); let mut buffer_reader = BufferReader::from_parts(encoded_raw_slice.ptr(), encoded_raw_slice.number_of_bytes()); - + let decoded_string = String::abi_decode(buffer_reader); assert(string == decoded_string); From 8da81a45ec01968411754749f0c100317008df39 Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 21 May 2024 15:16:16 +0800 Subject: [PATCH 5/6] Remove extraneous Bytes::from --- sway-lib-std/src/string.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index c52f9afbaa2..54c117eb13a 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -303,7 +303,7 @@ impl AbiDecode for String { let data = buffer.read_bytes(len); // Create string from the ptr and len as parts of a raw_slice String { - bytes: Bytes::from(Bytes::from(raw_slice::from_parts::(data.ptr(), len))), + bytes: Bytes::from((raw_slice::from_parts::(data.ptr(), len))), } } } From 46f9528df88343026c2d504af6d2e58d29fb19cb Mon Sep 17 00:00:00 2001 From: bitzoic Date: Tue, 21 May 2024 15:18:36 +0800 Subject: [PATCH 6/6] Remove parenthesis --- sway-lib-std/src/string.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-lib-std/src/string.sw b/sway-lib-std/src/string.sw index 54c117eb13a..dfe575a629a 100644 --- a/sway-lib-std/src/string.sw +++ b/sway-lib-std/src/string.sw @@ -303,7 +303,7 @@ impl AbiDecode for String { let data = buffer.read_bytes(len); // Create string from the ptr and len as parts of a raw_slice String { - bytes: Bytes::from((raw_slice::from_parts::(data.ptr(), len))), + bytes: Bytes::from(raw_slice::from_parts::(data.ptr(), len)), } } }