@@ -53,7 +53,7 @@ use crate::cmp::min;
53
53
#[allow(unused_imports)]
54
54
use crate :: integer :: {U32TryIntoNonZero , u128_safe_divmod};
55
55
#[feature(" bounded-int-utils" )]
56
- use crate :: internal :: bounded_int :: {BoundedInt , downcast};
56
+ use crate :: internal :: bounded_int :: {BoundedInt , downcast, upcast };
57
57
#[allow(unused_imports)]
58
58
use crate :: serde :: Serde ;
59
59
use crate :: traits :: {Into , TryInto };
@@ -156,38 +156,7 @@ pub impl ByteArrayImpl of ByteArrayTrait {
156
156
/// assert!(ba == "12");
157
157
/// ```
158
158
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);
191
160
}
192
161
193
162
/// Concatenates two `ByteArray` and returns the result.
@@ -371,6 +340,46 @@ impl InternalImpl of InternalTrait {
371
340
const ON_ERR : bytes31 = ' BA_ILLEGAL_USAGE' _u128 . into ();
372
341
self . data. append (value . try_into (). unwrap_or (ON_ERR ));
373
342
}
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
+ }
374
383
}
375
384
376
385
/// The value for adding up to 31 bytes to the byte array.
0 commit comments