Skip to content

Commit 14f1a42

Browse files
committed
Move SplitLinesArgs and ExpandTabsArg to pystr
1 parent b7a399e commit 14f1a42

File tree

5 files changed

+37
-49
lines changed

5 files changed

+37
-49
lines changed

vm/src/obj/objbytearray.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ use std::convert::TryFrom;
44
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};
55

66
use super::objbyteinner::{
7-
ByteInnerExpandtabsOptions, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
8-
ByteInnerSplitOptions, ByteInnerSplitlinesOptions, ByteInnerTranslateOptions, ByteOr,
9-
PyByteInner,
7+
ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerSplitOptions,
8+
ByteInnerTranslateOptions, ByteOr, PyByteInner,
109
};
1110
use super::objint::PyIntRef;
1211
use super::objiter;
1312
use super::objslice::PySliceRef;
1413
use super::objstr::{PyString, PyStringRef};
1514
use super::objtype::PyClassRef;
16-
use super::pystr::PyCommonString;
15+
use super::pystr::{self, PyCommonString};
1716
use crate::cformat::CFormatString;
1817
use crate::function::{OptionalArg, OptionalOption};
1918
use crate::obj::objstr::do_cformat_string;
@@ -440,12 +439,12 @@ impl PyByteArray {
440439
}
441440

442441
#[pymethod(name = "expandtabs")]
443-
fn expandtabs(&self, options: ByteInnerExpandtabsOptions) -> PyByteArray {
442+
fn expandtabs(&self, options: pystr::ExpandTabsArgs) -> PyByteArray {
444443
self.borrow_value().expandtabs(options).into()
445444
}
446445

447446
#[pymethod(name = "splitlines")]
448-
fn splitlines(&self, options: ByteInnerSplitlinesOptions, vm: &VirtualMachine) -> PyResult {
447+
fn splitlines(&self, options: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyResult {
449448
let as_bytes = self
450449
.borrow_value()
451450
.splitlines(options)

vm/src/obj/objbyteinner.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,6 @@ impl ByteInnerTranslateOptions {
257257

258258
pub type ByteInnerSplitOptions = pystr::SplitArgs<PyByteInner, [u8], u8>;
259259

260-
#[derive(FromArgs)]
261-
pub struct ByteInnerExpandtabsOptions {
262-
#[pyarg(positional_or_keyword, optional = true)]
263-
tabsize: OptionalArg<PyIntRef>,
264-
}
265-
266-
impl ByteInnerExpandtabsOptions {
267-
pub fn get_value(self) -> usize {
268-
match self.tabsize.into_option() {
269-
Some(int) => int.as_bigint().to_usize().unwrap_or(0),
270-
None => 8,
271-
}
272-
}
273-
}
274-
275260
#[derive(FromArgs)]
276261
pub struct ByteInnerSplitlinesOptions {
277262
#[pyarg(positional_or_keyword, optional = true)]
@@ -1014,8 +999,8 @@ impl PyByteInner {
1014999
Ok((front, has_mid, back))
10151000
}
10161001

1017-
pub fn expandtabs(&self, options: ByteInnerExpandtabsOptions) -> Vec<u8> {
1018-
let tabsize = options.get_value();
1002+
pub fn expandtabs(&self, options: pystr::ExpandTabsArgs) -> Vec<u8> {
1003+
let tabsize = options.tabsize();
10191004
let mut counter: usize = 0;
10201005
let mut res = vec![];
10211006

@@ -1046,9 +1031,7 @@ impl PyByteInner {
10461031
res
10471032
}
10481033

1049-
pub fn splitlines(&self, options: ByteInnerSplitlinesOptions) -> Vec<&[u8]> {
1050-
let keepends = options.get_value();
1051-
1034+
pub fn splitlines(&self, options: pystr::SplitLinesArgs) -> Vec<&[u8]> {
10521035
let mut res = vec![];
10531036

10541037
if self.elements.is_empty() {
@@ -1057,7 +1040,7 @@ impl PyByteInner {
10571040

10581041
let mut prev_index = 0;
10591042
let mut index = 0;
1060-
let keep = if keepends { 1 } else { 0 };
1043+
let keep = if options.keepends { 1 } else { 0 };
10611044
let slice = &self.elements;
10621045

10631046
while index < slice.len() {

vm/src/obj/objbytes.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use std::ops::Deref;
44
use std::str::FromStr;
55

66
use super::objbyteinner::{
7-
ByteInnerExpandtabsOptions, ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions,
8-
ByteInnerSplitOptions, ByteInnerSplitlinesOptions, ByteInnerTranslateOptions, PyByteInner,
7+
ByteInnerFindOptions, ByteInnerNewOptions, ByteInnerPaddingOptions, ByteInnerSplitOptions,
8+
ByteInnerTranslateOptions, PyByteInner,
99
};
1010
use super::objint::PyIntRef;
1111
use super::objiter;
1212
use super::objslice::PySliceRef;
1313
use super::objstr::{PyString, PyStringRef};
1414
use super::objtype::PyClassRef;
15-
use super::pystr::PyCommonString;
15+
use super::pystr::{self, PyCommonString};
1616
use crate::cformat::CFormatString;
1717
use crate::function::{OptionalArg, OptionalOption};
1818
use crate::obj::objstr::do_cformat_string;
@@ -401,12 +401,12 @@ impl PyBytes {
401401
}
402402

403403
#[pymethod(name = "expandtabs")]
404-
fn expandtabs(&self, options: ByteInnerExpandtabsOptions) -> PyBytes {
404+
fn expandtabs(&self, options: pystr::ExpandTabsArgs) -> PyBytes {
405405
self.inner.expandtabs(options).into()
406406
}
407407

408408
#[pymethod(name = "splitlines")]
409-
fn splitlines(&self, options: ByteInnerSplitlinesOptions, vm: &VirtualMachine) -> PyResult {
409+
fn splitlines(&self, options: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyResult {
410410
let as_bytes = self
411411
.inner
412412
.splitlines(options)

vm/src/obj/objstr.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,6 @@ struct StrArgs {
168168
errors: OptionalArg<PyStringRef>,
169169
}
170170

171-
#[derive(FromArgs)]
172-
struct SplitLineArgs {
173-
#[pyarg(positional_or_keyword, optional = true)]
174-
keepends: OptionalArg<bool>,
175-
}
176-
177-
#[derive(FromArgs)]
178-
struct ExpandtabsArgs {
179-
#[pyarg(positional_or_keyword, optional = true)]
180-
tabsize: OptionalArg<usize>,
181-
}
182-
183171
#[pyimpl(flags(BASETYPE))]
184172
impl PyString {
185173
#[pyslot]
@@ -797,14 +785,13 @@ impl PyString {
797785
}
798786

799787
#[pymethod]
800-
fn splitlines(&self, args: SplitLineArgs, vm: &VirtualMachine) -> PyObjectRef {
801-
let keepends = args.keepends.unwrap_or(false);
788+
fn splitlines(&self, args: pystr::SplitLinesArgs, vm: &VirtualMachine) -> PyObjectRef {
802789
let mut elements = vec![];
803790
let mut curr = "".to_owned();
804791
let mut chars = self.value.chars().peekable();
805792
while let Some(ch) = chars.next() {
806793
if ch == '\n' || ch == '\r' {
807-
if keepends {
794+
if args.keepends {
808795
curr.push(ch);
809796
}
810797
if ch == '\r' && chars.peek() == Some(&'\n') {
@@ -1080,8 +1067,8 @@ impl PyString {
10801067
}
10811068

10821069
#[pymethod]
1083-
fn expandtabs(&self, args: ExpandtabsArgs) -> String {
1084-
let tab_stop = args.tabsize.unwrap_or(8);
1070+
fn expandtabs(&self, args: pystr::ExpandTabsArgs) -> String {
1071+
let tab_stop = args.tabsize();
10851072
let mut expanded_str = String::with_capacity(self.value.len());
10861073
let mut tab_size = tab_stop;
10871074
let mut col_count = 0 as usize;

vm/src/obj/pystr.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::function::{single_or_tuple_any, OptionalOption};
22
use crate::pyobject::{PyObjectRef, PyResult, TryFromObject, TypeProtocol};
33
use crate::vm::VirtualMachine;
4+
use num_traits::cast::ToPrimitive;
45

56
#[derive(FromArgs)]
67
pub struct SplitArgs<T, S, E>
@@ -35,6 +36,24 @@ where
3536
}
3637
}
3738

39+
#[derive(FromArgs)]
40+
pub struct SplitLinesArgs {
41+
#[pyarg(positional_or_keyword, default = "false")]
42+
pub keepends: bool,
43+
}
44+
45+
#[derive(FromArgs)]
46+
pub struct ExpandTabsArgs {
47+
#[pyarg(positional_or_keyword, default = "8")]
48+
tabsize: isize,
49+
}
50+
51+
impl ExpandTabsArgs {
52+
pub fn tabsize(&self) -> usize {
53+
self.tabsize.to_usize().unwrap_or(0)
54+
}
55+
}
56+
3857
// help get optional string indices
3958
pub fn adjust_indices(
4059
start: OptionalOption<isize>,

0 commit comments

Comments
 (0)