Datasets:
Mirror from SNAPKITTYWEST: runtime/stdlib/bytes.py
Browse files- runtime/stdlib/bytes.py +24 -0
runtime/stdlib/bytes.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from runtime.types import Bytes, Integer, InvalidArgument
|
| 2 |
+
|
| 3 |
+
def bytes_from_list(lst) -> Bytes:
|
| 4 |
+
return Bytes(bytes([x.value if isinstance(x, Integer) else int(x) for x in lst]))
|
| 5 |
+
|
| 6 |
+
def len_bytes(b: Bytes) -> Integer:
|
| 7 |
+
return Integer(len(b.data))
|
| 8 |
+
|
| 9 |
+
def get_byte(b: Bytes, idx: Integer) -> Integer:
|
| 10 |
+
if idx.value < 0 or idx.value >= len(b.data):
|
| 11 |
+
raise InvalidArgument("byte index out of range")
|
| 12 |
+
return Integer(b.data[idx.value])
|
| 13 |
+
|
| 14 |
+
def slice_bytes(b: Bytes, start: Integer, end: Integer) -> Bytes:
|
| 15 |
+
return Bytes(b.data[start.value:end.value])
|
| 16 |
+
|
| 17 |
+
def concat_bytes(*parts) -> Bytes:
|
| 18 |
+
result = b''
|
| 19 |
+
for p in parts:
|
| 20 |
+
if isinstance(p, Bytes):
|
| 21 |
+
result += p.data
|
| 22 |
+
else:
|
| 23 |
+
raise InvalidArgument("concat expects Bytes")
|
| 24 |
+
return Bytes(result)
|