Skip to content

Commit

Permalink
Add support for all unsigned and signed word sizes for core::byte_arr…
Browse files Browse the repository at this point in the history
…ay (#185)

<!--- Please provide a general summary of your changes in the title
above -->

## Pull Request type

<!-- Please try to limit your pull request to one type; submit multiple
pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no API changes)
- [ ] Build-related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying, or
link to a relevant issue. -->

Issue Number: N/A

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

- Extends ByteArray to support reading/writing various word sizes from
u16 to u512 and i8 to i128.
- Extends ByteArray to support integer types using two's complement,
translating correctly to felt252 encoding.
- Adds ByteArrayReader to simplify deconstructing more complex types
using a combination of different word lengths, as is commonly seen in
network encoded messages (inspired by the immensely useful ByteBuffer of
Apple's swift-nio).
- Adds a Serde encoding for ByteArray, prefixed by the length of the
array in bytes.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!-- If this does introduce a breaking change, please describe the
impact and migration path for existing applications below. -->

## Other information

<!-- Any other information that is important to this PR, such as
screenshots of how the component looks before and after the change. -->
  • Loading branch information
sveamarcus authored Oct 10, 2023
1 parent 69e36b9 commit af5879e
Show file tree
Hide file tree
Showing 24 changed files with 938 additions and 209 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
scarb 0.7.0
scarb 2.3.0-rc0
2 changes: 1 addition & 1 deletion Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ description = "Community maintained Cairo and Starknet libraries"
homepage = "https://github.com/keep-starknet-strange/alexandria/"

[workspace.dependencies]
starknet = ">=2.2.0"
starknet = ">=2.3.0-rc0"

[scripts]
all = "scarb build && scarb test"
8 changes: 2 additions & 6 deletions src/ascii/src/interger.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,8 @@ impl SmallIntergerToAsciiTraitImpl<
let mut ascii: felt252 = 0;
loop {
match inverse_ascii_arr.pop_back() {
Option::Some(val) => {
ascii = ascii * 256 + *val;
},
Option::None(_) => {
break;
},
Option::Some(val) => { ascii = ascii * 256 + *val; },
Option::None(_) => { break; },
};
};

Expand Down
150 changes: 47 additions & 103 deletions src/data_structures/src/array_ext.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,8 @@ impl ArrayImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of ArrayTraitExt<T>
break;
}
match self.pop_front() {
Option::Some(v) => {
n -= 1;
},
Option::None => {
break;
},
Option::Some(v) => { n -= 1; },
Option::None => { break; },
};
};
}
Expand Down Expand Up @@ -156,12 +152,8 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
break;
}
match self.pop_front() {
Option::Some(v) => {
n -= 1;
},
Option::None => {
break;
},
Option::Some(v) => { n -= 1; },
Option::None => { break; },
};
};
}
Expand All @@ -172,12 +164,8 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
break;
}
match self.pop_back() {
Option::Some(v) => {
n -= 1;
},
Option::None => {
break;
},
Option::Some(v) => { n -= 1; },
Option::None => { break; },
};
};
}
Expand All @@ -186,9 +174,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
let mut response = array![];
loop {
match self.pop_back() {
Option::Some(v) => {
response.append(*v);
},
Option::Some(v) => { response.append(*v); },
Option::None => {
break; // Can't `break response;` "Variable was previously moved"
},
Expand All @@ -200,14 +186,10 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
fn contains<impl TPartialEq: PartialEq<T>>(mut self: Span<T>, item: T) -> bool {
loop {
match self.pop_front() {
Option::Some(v) => {
if *v == item {
break true;
}
},
Option::None => {
break false;
},
Option::Some(v) => { if *v == item {
break true;
} },
Option::None => { break false; },
};
}
}
Expand Down Expand Up @@ -244,9 +226,7 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
}
index += 1;
},
Option::None => {
break Option::None;
},
Option::None => { break Option::None; },
};
}
}
Expand All @@ -255,14 +235,10 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
let mut count = 0_usize;
loop {
match self.pop_front() {
Option::Some(v) => {
if *v == item {
count += 1;
}
},
Option::None => {
break count;
},
Option::Some(v) => { if *v == item {
count += 1;
} },
Option::None => { break count; },
};
}
}
Expand All @@ -272,20 +248,14 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
) -> Option<T> {
let mut min = match self.pop_front() {
Option::Some(item) => *item,
Option::None => {
return Option::None;
},
Option::None => { return Option::None; },
};
loop {
match self.pop_front() {
Option::Some(item) => {
if *item < min {
min = *item
}
},
Option::None => {
break Option::Some(min);
},
Option::Some(item) => { if *item < min {
min = *item
} },
Option::None => { break Option::Some(min); },
};
}
}
Expand All @@ -297,21 +267,15 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
let mut index_of_min = 0;
let mut min: T = match self.pop_front() {
Option::Some(item) => *item,
Option::None => {
return Option::None;
},
Option::None => { return Option::None; },
};
loop {
match self.pop_front() {
Option::Some(item) => {
if *item < min {
index_of_min = index + 1;
min = *item;
}
},
Option::None => {
break Option::Some(index_of_min);
},
Option::Some(item) => { if *item < min {
index_of_min = index + 1;
min = *item;
} },
Option::None => { break Option::Some(index_of_min); },
};
index += 1;
}
Expand All @@ -322,20 +286,14 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
) -> Option<T> {
let mut max = match self.pop_front() {
Option::Some(item) => *item,
Option::None => {
return Option::None;
},
Option::None => { return Option::None; },
};
loop {
match self.pop_front() {
Option::Some(item) => {
if *item > max {
max = *item
}
},
Option::None => {
break Option::Some(max);
},
Option::Some(item) => { if *item > max {
max = *item
} },
Option::None => { break Option::Some(max); },
};
}
}
Expand All @@ -347,21 +305,15 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
let mut index_of_max = 0;
let mut max = match self.pop_front() {
Option::Some(item) => *item,
Option::None => {
return Option::None;
},
Option::None => { return Option::None; },
};
loop {
match self.pop_front() {
Option::Some(item) => {
if *item > max {
index_of_max = index + 1;
max = *item
}
},
Option::None => {
break Option::Some(index_of_max);
},
Option::Some(item) => { if *item > max {
index_of_max = index + 1;
max = *item
} },
Option::None => { break Option::Some(index_of_max); },
};
index += 1;
}
Expand All @@ -377,15 +329,11 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {

loop {
match self.pop_front() {
Option::Some(v) => {
if (last_value != v) {
last_value = v;
ret.append(*v);
};
},
Option::None => {
break;
}
Option::Some(v) => { if (last_value != v) {
last_value = v;
ret.append(*v);
}; },
Option::None => { break; }
};
};

Expand All @@ -396,14 +344,10 @@ impl SpanImpl<T, impl TCopy: Copy<T>, impl TDrop: Drop<T>> of SpanTraitExt<T> {
let mut ret = array![];
loop {
match self.pop_front() {
Option::Some(v) => {
if !ret.contains(*v) {
ret.append(*v);
}
},
Option::None => {
break;
}
Option::Some(v) => { if !ret.contains(*v) {
ret.append(*v);
} },
Option::None => { break; }
};
};
ret
Expand Down
Loading

0 comments on commit af5879e

Please sign in to comment.