Skip to content

Commit caca0af

Browse files
author
Gilad Chase
committed
refactor(byte_array): extract ByteArray::append logic
The new aux function will be called from the upcoming `ByteSpan::to_byte_array()`. Note: no logic changes, only extract.
1 parent 8e174c0 commit caca0af

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

corelib/src/byte_array.cairo

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::cmp::min;
5353
#[allow(unused_imports)]
5454
use crate::integer::{U32TryIntoNonZero, u128_safe_divmod};
5555
#[feature("bounded-int-utils")]
56-
use crate::internal::bounded_int::{BoundedInt, downcast};
56+
use crate::internal::bounded_int::{BoundedInt, downcast, upcast};
5757
#[allow(unused_imports)]
5858
use crate::serde::Serde;
5959
use crate::traits::{Into, TryInto};
@@ -156,38 +156,7 @@ pub impl ByteArrayImpl of ByteArrayTrait {
156156
/// assert!(ba == "12");
157157
/// ```
158158
fn append(ref self: ByteArray, other: @ByteArray) {
159-
let mut other_data = other.data.span();
160-
161-
if self.pending_word_len == 0 {
162-
self.data.append_span(other_data);
163-
self.pending_word = *other.pending_word;
164-
self.pending_word_len = *other.pending_word_len;
165-
return;
166-
}
167-
168-
let shift_value = self.shift_value();
169-
// self.pending_word_len is in [1, 30]. This is the split index for all the full words of
170-
// `other`, as for each word, this is the number of bytes left for the next word.
171-
match split_info(self.pending_word_len) {
172-
SplitInfo::Eq16(v) => {
173-
while let Some(word) = other_data.pop_front() {
174-
self.append_shifted(v.split_u256((*word).into()), shift_value);
175-
}
176-
},
177-
SplitInfo::Lt16(v) => {
178-
while let Some(word) = other_data.pop_front() {
179-
self.append_shifted(v.split_u256((*word).into()), shift_value);
180-
}
181-
},
182-
SplitInfo::Gt16(v) => {
183-
while let Some(word) = other_data.pop_front() {
184-
self.append_shifted(v.split_u256((*word).into()), shift_value);
185-
}
186-
},
187-
SplitInfo::BadUserData => crate::panic_with_felt252('bad append len'),
188-
}
189-
// Add the pending word of `other`.
190-
self.append_word(*other.pending_word, *other.pending_word_len);
159+
self.append_span_info(other.data.span(), *other.pending_word, *other.pending_word_len);
191160
}
192161

193162
/// Concatenates two `ByteArray` and returns the result.
@@ -371,6 +340,46 @@ impl InternalImpl of InternalTrait {
371340
const ON_ERR: bytes31 = 'BA_ILLEGAL_USAGE'_u128.into();
372341
self.data.append(value.try_into().unwrap_or(ON_ERR));
373342
}
343+
344+
/// Append data span and pending word
345+
#[inline]
346+
fn append_span_info(
347+
ref self: ByteArray,
348+
mut data: Span<bytes31>,
349+
pending_word: felt252,
350+
pending_word_len: usize,
351+
) {
352+
if self.pending_word_len == 0 {
353+
self.data.append_span(data);
354+
self.pending_word = pending_word;
355+
self.pending_word_len = pending_word_len;
356+
return;
357+
}
358+
359+
let shift_value = self.shift_value();
360+
// self.pending_word_len is in [1, 30]. This is the split index for all the full words of
361+
// `other`, as for each word, this is the number of bytes left for the next word.
362+
match split_info(self.pending_word_len) {
363+
SplitInfo::Eq16(v) => {
364+
while let Some(word) = data.pop_front() {
365+
self.append_shifted(v.split_u256((*word).into()), shift_value);
366+
}
367+
},
368+
SplitInfo::Lt16(v) => {
369+
while let Some(word) = data.pop_front() {
370+
self.append_shifted(v.split_u256((*word).into()), shift_value);
371+
}
372+
},
373+
SplitInfo::Gt16(v) => {
374+
while let Some(word) = data.pop_front() {
375+
self.append_shifted(v.split_u256((*word).into()), shift_value);
376+
}
377+
},
378+
SplitInfo::BadUserData => crate::panic_with_felt252('bad append len'),
379+
}
380+
// Add the pending word of `other`.
381+
self.append_word(pending_word, upcast(pending_word_len));
382+
}
374383
}
375384

376385
/// The value for adding up to 31 bytes to the byte array.

0 commit comments

Comments
 (0)