@@ -53,7 +53,7 @@ use crate::cmp::min;
5353#[allow(unused_imports)]
5454use 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)]
5858use crate :: serde :: Serde ;
5959use crate :: traits :: {Into , TryInto };
@@ -93,8 +93,6 @@ pub(crate) impl ByteArrayStringLiteral of crate::string::StringLiteral<ByteArray
9393/// Functions associated with the `ByteArray` type.
9494#[generate_trait]
9595pub impl ByteArrayImpl of ByteArrayTrait {
96- // TODO(yuval): add a `new` function for initialization.
97-
9896 /// Appends a single word of `len` bytes to the end of the `ByteArray`.
9997 ///
10098 /// This function assumes that:
@@ -156,38 +154,7 @@ pub impl ByteArrayImpl of ByteArrayTrait {
156154 /// assert!(ba == "12");
157155 /// ```
158156 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);
157+ self . append_from_parts (other . data. span (), * other . pending_word, * other . pending_word_len);
191158 }
192159
193160 /// Concatenates two `ByteArray` and returns the result.
@@ -371,6 +338,46 @@ impl InternalImpl of InternalTrait {
371338 const ON_ERR : bytes31 = ' BA_ILLEGAL_USAGE' _u128 . into ();
372339 self . data. append (value . try_into (). unwrap_or (ON_ERR ));
373340 }
341+
342+ /// Append `data`` span and `pending_word` fields to the byte array.
343+ #[inline]
344+ fn append_from_parts (
345+ ref self : ByteArray ,
346+ mut data : Span <bytes31 >,
347+ pending_word : felt252 ,
348+ pending_word_len : usize ,
349+ ) {
350+ if self . pending_word_len == 0 {
351+ self . data. append_span (data );
352+ self . pending_word = pending_word ;
353+ self . pending_word_len = pending_word_len ;
354+ return ;
355+ }
356+
357+ let shift_value = self . shift_value ();
358+ // self.pending_word_len is in [1, 30]. This is the split index for all the full words of
359+ // `other`, as for each word, this is the number of bytes left for the next word.
360+ match split_info (self . pending_word_len) {
361+ SplitInfo :: Eq16 (v ) => {
362+ while let Some (word ) = data . pop_front () {
363+ self . append_shifted (v . split_u256 ((* word ). into ()), shift_value );
364+ }
365+ },
366+ SplitInfo :: Lt16 (v ) => {
367+ while let Some (word ) = data . pop_front () {
368+ self . append_shifted (v . split_u256 ((* word ). into ()), shift_value );
369+ }
370+ },
371+ SplitInfo :: Gt16 (v ) => {
372+ while let Some (word ) = data . pop_front () {
373+ self . append_shifted (v . split_u256 ((* word ). into ()), shift_value );
374+ }
375+ },
376+ SplitInfo :: BadUserData => crate :: panic_with_felt252 (' bad append len' ),
377+ }
378+ // Add the pending word of `other`.
379+ self . append_word (pending_word , upcast (pending_word_len ));
380+ }
374381}
375382
376383/// The value for adding up to 31 bytes to the byte array.
0 commit comments