diff --git a/corelib/src/array.cairo b/corelib/src/array.cairo index 8380558cfeb..5fb780277f2 100644 --- a/corelib/src/array.cairo +++ b/corelib/src/array.cairo @@ -15,6 +15,12 @@ extern fn array_pop_front(ref arr: Array) -> Option> nopanic; extern fn array_pop_front_consume(arr: Array) -> Option<(Array, Box)> nopanic; pub(crate) extern fn array_snapshot_pop_front(ref arr: @Array) -> Option> nopanic; extern fn array_snapshot_pop_back(ref arr: @Array) -> Option> nopanic; +extern fn array_snapshot_multi_pop_front( + ref arr: @Array +) -> Option<@Box<[T; SIZE]>> implicits(RangeCheck) nopanic; +extern fn array_snapshot_multi_pop_back( + ref arr: @Array +) -> Option<@Box<[T; SIZE]>> implicits(RangeCheck) nopanic; #[panic_with('Index out of bounds', array_at)] extern fn array_get( arr: @Array, index: usize @@ -187,6 +193,14 @@ pub impl SpanImpl of SpanTrait { Option::None => Option::None, } } + /// Pops multiple values from the front of the span. + fn multi_pop_front(ref self: Span) -> Option<@Box<[T; SIZE]>> { + array_snapshot_multi_pop_front(ref self.snapshot) + } + /// Pops multiple values from the back of the span. + fn multi_pop_back(ref self: Span) -> Option<@Box<[T; SIZE]>> { + array_snapshot_multi_pop_back(ref self.snapshot) + } #[inline(always)] fn get(self: Span, index: usize) -> Option> { array_get(self.snapshot, index) diff --git a/corelib/src/test/array_test.cairo b/corelib/src/test/array_test.cairo index 1b801c13f14..c9ef04e3a28 100644 --- a/corelib/src/test/array_test.cairo +++ b/corelib/src/test/array_test.cairo @@ -161,3 +161,17 @@ fn test_span_into_fixed_size_array() { assert!(debox::([].span().try_into()).is_none()); assert!(debox::([].span().try_into()) == Option::Some(@[])); } + +#[test] +fn test_span_multi_pop() { + let mut span = array![10, 11, 12, 13].span(); + assert!(span.multi_pop_front::<5>().is_none()); + assert!(debox(span.multi_pop_front::<4>()) == Option::Some(@[10, 11, 12, 13])); + let mut span = array![10, 11, 12, 13].span(); + assert!(debox(span.multi_pop_front::<3>()) == Option::Some(@[10, 11, 12])); + let mut span = array![10, 11, 12, 13].span(); + assert!(span.multi_pop_back::<5>().is_none()); + assert!(debox(span.multi_pop_back::<4>()) == Option::Some(@[10, 11, 12, 13])); + let mut span = array![10, 11, 12, 13].span(); + assert!(debox(span.multi_pop_back::<3>()) == Option::Some(@[11, 12, 13])); +} diff --git a/crates/cairo-lang-runner/src/casm_run/mod.rs b/crates/cairo-lang-runner/src/casm_run/mod.rs index 9c2e3bef5ff..cce31138d0c 100644 --- a/crates/cairo-lang-runner/src/casm_run/mod.rs +++ b/crates/cairo-lang-runner/src/casm_run/mod.rs @@ -1653,8 +1653,8 @@ pub fn execute_core_hint( insert_value_to_cellref!(vm, dst, segment)?; } CoreHint::TestLessThan { lhs, rhs, dst } => { - let lhs_val = get_val(vm, lhs)?; - let rhs_val = get_val(vm, rhs)?; + let lhs_val = get_maybe(vm, lhs)?; + let rhs_val = get_maybe(vm, rhs)?; insert_value_to_cellref!( vm, dst, @@ -1662,8 +1662,8 @@ pub fn execute_core_hint( )?; } CoreHint::TestLessThanOrEqual { lhs, rhs, dst } => { - let lhs_val = get_val(vm, lhs)?; - let rhs_val = get_val(vm, rhs)?; + let lhs_val = get_maybe(vm, lhs)?; + let rhs_val = get_maybe(vm, rhs)?; insert_value_to_cellref!( vm, dst,