@@ -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 };
@@ -93,8 +93,6 @@ pub(crate) impl ByteArrayStringLiteral of crate::string::StringLiteral<ByteArray
93
93
/// Functions associated with the `ByteArray` type.
94
94
#[generate_trait]
95
95
pub impl ByteArrayImpl of ByteArrayTrait {
96
- // TODO(yuval): add a `new` function for initialization.
97
-
98
96
/// Appends a single word of `len` bytes to the end of the `ByteArray`.
99
97
///
100
98
/// This function assumes that:
@@ -156,38 +154,7 @@ pub impl ByteArrayImpl of ByteArrayTrait {
156
154
/// assert!(ba == "12");
157
155
/// ```
158
156
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);
191
158
}
192
159
193
160
/// Concatenates two `ByteArray` and returns the result.
@@ -371,6 +338,46 @@ impl InternalImpl of InternalTrait {
371
338
const ON_ERR : bytes31 = ' BA_ILLEGAL_USAGE' _u128 . into ();
372
339
self . data. append (value . try_into (). unwrap_or (ON_ERR ));
373
340
}
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
+ }
374
381
}
375
382
376
383
/// The value for adding up to 31 bytes to the byte array.
0 commit comments