From 417c5a80ed2fef05ce159d643f3e34a5386ee06c Mon Sep 17 00:00:00 2001 From: Brandon R <54774639+b-j-roberts@users.noreply.github.com> Date: Mon, 12 Feb 2024 04:57:17 -0600 Subject: [PATCH] bug: array slice naming error and generic option (#275) The current implementation of array slices had a variable naming issue, where `len` and `end` seems to represent `end` and `len` respectively. Confusion leaked into the function description as well. I also created a generic `array_slice`. ## Pull Request type Please check the type of change your PR introduces: - [x] Bugfix - [ ] Feature - [x] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build-related changes - [ ] Documentation content changes - [ ] Other (please describe): ## Does this introduce a breaking change? - [ ] Yes - [x] No --- src/bytes/src/utils.cairo | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/bytes/src/utils.cairo b/src/bytes/src/utils.cairo index 48c9d676..125ada82 100644 --- a/src/bytes/src/utils.cairo +++ b/src/bytes/src/utils.cairo @@ -103,11 +103,10 @@ fn u8_array_to_u256(arr: Span) -> u256 { u256 { low, high } } -fn u64_array_slice(src: @Array, mut begin: usize, end: usize) -> Array { +fn u64_array_slice(src: @Array, mut begin: usize, len: usize) -> Array { let mut slice = array![]; - let len = begin + end; - - while begin < len && begin < src.len() { + let end = begin + len; + while begin < end && begin < src.len() { slice.append(*src[begin]); begin += 1; }; @@ -117,13 +116,25 @@ fn u64_array_slice(src: @Array, mut begin: usize, end: usize) -> Array /// Returns the slice of an array. /// * `arr` - The array to slice. /// * `begin` - The index to start the slice at. -/// * `end` - The index to end the slice at (not included). +/// * `len` - The length of the slice. /// # Returns /// * `Array` - The slice of the array. -fn u128_array_slice(src: @Array, mut begin: usize, end: usize) -> Array { +fn u128_array_slice(src: @Array, mut begin: usize, len: usize) -> Array { + let mut slice = array![]; + let end = begin + len; + while begin < end && begin < src.len() { + slice.append(*src[begin]); + begin += 1; + }; + slice +} + +fn array_slice, impl TCopy: Copy>( + src: @Array, mut begin: usize, len: usize +) -> Array { let mut slice = array![]; - let len = begin + end; - while begin < len && begin < src.len() { + let end = begin + len; + while begin < end && begin < src.len() { slice.append(*src[begin]); begin += 1; };