Skip to content

Commit ced6f15

Browse files
committed
add bytes.makertans
1 parent bd21667 commit ced6f15

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

tests/snippets/bytes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,12 @@
232232
assert b"abcd".find(b"b", 3, 4) == -1
233233
assert b"abcd".find(1) == -1
234234
assert b"abcd".find(99) == 2
235+
236+
237+
# make trans
238+
# fmt: off
239+
assert (
240+
bytes.maketrans(memoryview(b"abc"), bytearray(b"zzz"))
241+
== bytes([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 122, 122, 122, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255])
242+
)
243+
# fmt: on

vm/src/obj/objbyteinner.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,30 @@ impl PyByteInner {
590590
}
591591
Ok(-1isize)
592592
}
593+
594+
pub fn maketrans(from: PyObjectRef, to: PyObjectRef, vm :&VirtualMachine) -> PyResult{
595+
let mut res = vec![];
596+
597+
let from = match try_as_bytes_like(&from) {
598+
Some(value) => value,
599+
None => {return Err(vm.new_type_error(format!("a bytes-like object is required, not {}", from)));},
600+
};
601+
602+
let to = match try_as_bytes_like(&to) {
603+
Some(value) => value,
604+
None => {return Err(vm.new_type_error(format!("a bytes-like object is required, not {}", to)));},
605+
};
606+
607+
for i in 0..=255 {
608+
res.push(if let Some(position) = from.iter().position(|&x| x ==i) {
609+
to[position]
610+
} else {
611+
i
612+
});
613+
}
614+
615+
Ok(vm.ctx.new_bytes(res))
616+
}
593617
}
594618

595619
pub fn try_as_byte(obj: &PyObjectRef) -> Option<Vec<u8>> {

vm/src/obj/objbytes.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub fn init(context: &PyContext) {
6262
let bytes_type = &context.bytes_type;
6363
extend_class!(context, bytes_type, {
6464
"fromhex" => context.new_rustfunc(PyBytesRef::fromhex),
65+
"maketrans" => context.new_rustfunc(PyByteInner::maketrans),
66+
6567
});
6668
let bytesiterator_type = &context.bytesiterator_type;
6769
extend_class!(context, bytesiterator_type, {

0 commit comments

Comments
 (0)