Skip to content

Commit

Permalink
vlq: requested changes updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Falilah committed Dec 1, 2024
1 parent 056130e commit 71d8c35
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
25 changes: 11 additions & 14 deletions exercises/practice/variable-length-quantity/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const EIGHT_BIT_MASK: u32 = 0x80;
const SEVEN_BIT_MASK: u32 = 0x7f;
const TWO_POW_7: u32 = 128;

pub fn encode(integers: Array<u32>) -> Array<u32> {
let mut result = ArrayTrait::<u32>::new();

Expand All @@ -10,23 +14,20 @@ pub fn encode(integers: Array<u32>) -> Array<u32> {
}
while value > 0 {
// take the lower 7 bits
let mut tmp = value & 0x7f;
let mut tmp = value & SEVEN_BIT_MASK;
// remove them from the original value
value = value / 128;
value = value / TWO_POW_7;

// set continuation bit
if !temp_result.is_empty() {
tmp = tmp | 0x80;
tmp = tmp | EIGHT_BIT_MASK;
}

temp_result.append(tmp);
};

temp_result = reverse_array(temp_result);

for temp in temp_result {
result.append(temp);
};
result.append_span(temp_result.span());
};
result
}
Expand All @@ -37,19 +38,15 @@ pub fn decode(integers: Array<u32>) -> Array<u32> {
let mut tmp = 0;
let mut i = 0;
for integer in integers {
tmp = (tmp * 0x80) | (integer & 0x7f);
tmp = (tmp * EIGHT_BIT_MASK) | (integer & SEVEN_BIT_MASK);

if 0x80 & integer == 0 {
if EIGHT_BIT_MASK & integer == 0 {
// continuation bit not set, number if complete
res.append(tmp);
tmp = 0;
} else {
// check for incomplete bytes
if i + 1 == size {
// the next index would be past the end,
// i.e. there are no more bytes.
panic!("incomplete sequence");
}
assert!(i + 1 != size, "incomplete sequence");
}
i += 1;
};
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/variable-length-quantity/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ version = "0.1.0"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.8.2"
cairo_test = "2.8.2"

0 comments on commit 71d8c35

Please sign in to comment.