Skip to content

Commit 8bcdbbf

Browse files
committed
use PyInt Ref, add ljust, rjust
1 parent ba67f3c commit 8bcdbbf

File tree

3 files changed

+124
-13
lines changed

3 files changed

+124
-13
lines changed

tests/snippets/bytes.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
with assertRaises(TypeError):
1111
bytes("bla")
1212
with assertRaises(TypeError):
13-
bytes("bla", encoding = b"jilj")
13+
bytes("bla", encoding=b"jilj")
1414

1515
assert (
1616
b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
@@ -143,7 +143,7 @@
143143
except ValueError as e:
144144
str(e) == "non-hexadecimal number found in fromhex() arg at position 1"
145145
with assertRaises(TypeError):
146-
bytes.fromhex(b'hhjjk')
146+
bytes.fromhex(b"hhjjk")
147147
# center
148148
assert [b"koki".center(i, b"|") for i in range(3, 10)] == [
149149
b"koki",
@@ -170,9 +170,70 @@
170170
b"b".center(2, "a")
171171
with assertRaises(TypeError):
172172
b"b".center(2, b"ba")
173-
b"kok".center(5, bytearray(b"x"))
173+
assert b"kok".center(5, bytearray(b"x")) == b"xkokx"
174174
b"kok".center(-5)
175175

176+
177+
# ljust
178+
assert [b"koki".ljust(i, b"|") for i in range(3, 10)] == [
179+
b"koki",
180+
b"koki",
181+
b"koki|",
182+
b"koki||",
183+
b"koki|||",
184+
b"koki||||",
185+
b"koki|||||",
186+
]
187+
assert [b"kok".ljust(i, b"|") for i in range(2, 10)] == [
188+
b"kok",
189+
b"kok",
190+
b"kok|",
191+
b"kok||",
192+
b"kok|||",
193+
b"kok||||",
194+
b"kok|||||",
195+
b"kok||||||",
196+
]
197+
198+
b"kok".ljust(4) == b"kok " # " test no arg"
199+
with assertRaises(TypeError):
200+
b"b".ljust(2, "a")
201+
with assertRaises(TypeError):
202+
b"b".ljust(2, b"ba")
203+
assert b"kok".ljust(5, bytearray(b"x")) == b"kokxx"
204+
assert b"kok".ljust(-5) == b"kok"
205+
206+
# rjust
207+
assert [b"koki".rjust(i, b"|") for i in range(3, 10)] == [
208+
b"koki",
209+
b"koki",
210+
b"|koki",
211+
b"||koki",
212+
b"|||koki",
213+
b"||||koki",
214+
b"|||||koki",
215+
]
216+
assert [b"kok".rjust(i, b"|") for i in range(2, 10)] == [
217+
b"kok",
218+
b"kok",
219+
b"|kok",
220+
b"||kok",
221+
b"|||kok",
222+
b"||||kok",
223+
b"|||||kok",
224+
b"||||||kok",
225+
]
226+
227+
228+
b"kok".rjust(4) == b" kok" # " test no arg"
229+
with assertRaises(TypeError):
230+
b"b".rjust(2, "a")
231+
with assertRaises(TypeError):
232+
b"b".rjust(2, b"ba")
233+
assert b"kok".rjust(5, bytearray(b"x")) == b"xxkok"
234+
assert b"kok".rjust(-5) == b"kok"
235+
236+
176237
# count
177238
assert b"azeazerazeazopia".count(b"aze") == 3
178239
assert b"azeazerazeazopia".count(b"az") == 4

vm/src/obj/objbyteinner.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::obj::objint::PyIntRef;
12
use crate::obj::objslice::PySlice;
23
use crate::pyobject::{PyIterable, PyObjectRef};
34
use num_bigint::BigInt;
@@ -376,7 +377,7 @@ impl PyByteInner {
376377

377378
fn get_center_args(
378379
&self,
379-
width_a: PyObjectRef,
380+
width: PyIntRef,
380381
fillbyte: OptionalArg<PyObjectRef>,
381382
fn_name: String,
382383
vm: &VirtualMachine,
@@ -404,13 +405,8 @@ impl PyByteInner {
404405
b' ' // default is space
405406
};
406407

407-
let width_b = match_class!(width_a,
408-
i @PyInt => i,
409-
obj => {return Err(vm.new_type_error(format!("{} cannot be interpreted as an integer", obj)));}
410-
);
411-
412408
// <0 = no change
413-
let width = if let Some(x) = width_b.as_bigint().to_usize() {
409+
let width = if let Some(x) = width.as_bigint().to_usize() {
414410
if x <= self.len() {
415411
0
416412
} else {
@@ -427,12 +423,12 @@ impl PyByteInner {
427423

428424
pub fn center(
429425
&self,
430-
width_a: PyObjectRef,
426+
width: PyIntRef,
431427
fillbyte: OptionalArg<PyObjectRef>,
432428
vm: &VirtualMachine,
433429
) -> PyResult<Vec<u8>> {
434430
let fn_name = "center".to_string();
435-
let (fillbyte, diff) = self.get_center_args(width_a, fillbyte, fn_name, vm)?;
431+
let (fillbyte, diff) = self.get_center_args(width, fillbyte, fn_name, vm)?;
436432

437433
let mut ln: usize = diff / 2;
438434
let mut rn: usize = ln;
@@ -453,6 +449,39 @@ impl PyByteInner {
453449
Ok(res)
454450
}
455451

452+
pub fn ljust(
453+
&self,
454+
width: PyIntRef,
455+
fillbyte: OptionalArg<PyObjectRef>,
456+
vm: &VirtualMachine,
457+
) -> PyResult<Vec<u8>> {
458+
let fn_name = "ljust".to_string();
459+
let (fillbyte, diff) = self.get_center_args(width, fillbyte, fn_name, vm)?;
460+
461+
// merge all
462+
let mut res = vec![];
463+
res.extend_from_slice(&self.elements[..]);
464+
res.extend_from_slice(&vec![fillbyte; diff][..]);
465+
466+
Ok(res)
467+
}
468+
469+
pub fn rjust(
470+
&self,
471+
width: PyIntRef,
472+
fillbyte: OptionalArg<PyObjectRef>,
473+
vm: &VirtualMachine,
474+
) -> PyResult<Vec<u8>> {
475+
let fn_name = "ljust".to_string();
476+
let (fillbyte, diff) = self.get_center_args(width, fillbyte, fn_name, vm)?;
477+
478+
// merge all
479+
let mut res = vec![fillbyte; diff];
480+
res.extend_from_slice(&self.elements[..]);
481+
482+
Ok(res)
483+
}
484+
456485
pub fn count(
457486
&self,
458487
sub: PyObjectRef,

vm/src/obj/objbytes.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::obj::objint::PyIntRef;
12
use crate::obj::objstr::PyStringRef;
23
use crate::vm::VirtualMachine;
34
use core::cell::Cell;
@@ -223,13 +224,33 @@ impl PyBytesRef {
223224
#[pymethod(name = "center")]
224225
fn center(
225226
self,
226-
width: PyObjectRef,
227+
width: PyIntRef,
227228
fillbyte: OptionalArg<PyObjectRef>,
228229
vm: &VirtualMachine,
229230
) -> PyResult {
230231
Ok(vm.ctx.new_bytes(self.inner.center(width, fillbyte, vm)?))
231232
}
232233

234+
#[pymethod(name = "ljust")]
235+
fn ljust(
236+
self,
237+
width: PyIntRef,
238+
fillbyte: OptionalArg<PyObjectRef>,
239+
vm: &VirtualMachine,
240+
) -> PyResult {
241+
Ok(vm.ctx.new_bytes(self.inner.ljust(width, fillbyte, vm)?))
242+
}
243+
244+
#[pymethod(name = "rjust")]
245+
fn rjust(
246+
self,
247+
width: PyIntRef,
248+
fillbyte: OptionalArg<PyObjectRef>,
249+
vm: &VirtualMachine,
250+
) -> PyResult {
251+
Ok(vm.ctx.new_bytes(self.inner.rjust(width, fillbyte, vm)?))
252+
}
253+
233254
#[pymethod(name = "count")]
234255
fn count(
235256
self,

0 commit comments

Comments
 (0)