Skip to content
This repository was archived by the owner on May 29, 2024. It is now read-only.

Commit 9ed5e55

Browse files
Use the new List in the bytes struct (#106)
1 parent 2489635 commit 9ed5e55

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

stdlib_extensions/builtins/_bytes.mojo

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ._generic_list import list
22
from .._utils import custom_debug_assert
33
from .string import rjust
4+
from ._dynamic_vector_list import List
45

56

67
fn get_mapping_byte_to_value() -> list[String]:
@@ -275,22 +276,22 @@ struct bytes(Stringable, Sized, CollectionElement):
275276
even some_bytes[7] = some_other_byte (the latter must be only one byte long).
276277
"""
277278

278-
var _vector: DynamicVector[UInt8]
279+
var _vector: List[UInt8]
279280

280281
fn __init__(inout self):
281-
self._vector = DynamicVector[UInt8]()
282+
self._vector = List[UInt8]()
282283

283-
fn __init__(inout self, vector: DynamicVector[UInt8]):
284-
self._vector = vector
284+
fn __init__(inout self, owned vector: List[UInt8]):
285+
self._vector = vector ^
285286

286287
fn __init__(inout self, size: Int):
287-
self._vector = DynamicVector[UInt8](capacity=size)
288+
self._vector = List[UInt8](capacity=size)
288289
for i in range(size):
289290
self._vector.push_back(0)
290291

291292
@staticmethod
292293
fn from_values(*values: UInt8) -> bytes:
293-
var vector = DynamicVector[UInt8](capacity=len(values))
294+
var vector = List[UInt8](capacity=len(values))
294295
for value in values:
295296
vector.push_back(value)
296297
return bytes(vector)
@@ -319,7 +320,7 @@ struct bytes(Stringable, Sized, CollectionElement):
319320
return not self.__eq__(other)
320321

321322
fn __add__(self, other: bytes) -> bytes:
322-
var new_vector = DynamicVector[UInt8](capacity=self.__len__() + other.__len__())
323+
var new_vector = List[UInt8](capacity=self.__len__() + other.__len__())
323324
for i in range(self.__len__()):
324325
new_vector.push_back(self[i])
325326
for i in range(other.__len__()):
@@ -371,15 +372,15 @@ struct bytes(Stringable, Sized, CollectionElement):
371372
@staticmethod
372373
fn fromhex(string: String) -> bytes:
373374
# TODO: remove whitespaces on the input string
374-
var vector_of_bytes = DynamicVector[UInt8](capacity=len(string) // 2)
375+
var vector_of_bytes = List[UInt8](capacity=len(string) // 2)
375376
var string_length = len(string)
376377
for i in range(0, string_length, 2):
377378
var first_char = string[i]
378379
var second_char = string[i + 1]
379380
var first_value = _ascii_char_to_int(first_char)
380381
var second_value = _ascii_char_to_int(second_char)
381382
var final_value = (first_value << 4) + second_value
382-
vector_of_bytes.push_back(UInt8(final_value))
383+
vector_of_bytes.append(UInt8(final_value))
383384
return bytes(vector_of_bytes)
384385

385386

@@ -405,7 +406,7 @@ fn to_bytes(n: Int, length: Int = 1, byteorder: String = "big") -> bytes:
405406
else:
406407
custom_debug_assert(False, "byteorder must be either 'little' or 'big'")
407408

408-
var result_vector = DynamicVector[UInt8](capacity=length)
409+
var result_vector = List[UInt8](capacity=length)
409410

410411
for i in order:
411412
result_vector.push_back((n >> i * 8) & 0xFF)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
alias List = DynamicVector

0 commit comments

Comments
 (0)