Skip to content

Commit deefce4

Browse files
committed
Clean up str/bytes pad
to be prefixed with _ and embed get_fillchar
1 parent 234c015 commit deefce4

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

vm/src/obj/objbyteinner.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ impl PyByteInner {
671671
}
672672

673673
#[inline]
674-
fn pad(
674+
fn _pad(
675675
&self,
676676
options: ByteInnerPaddingOptions,
677677
pad: fn(&[u8], usize, u8, usize) -> Vec<u8>,
@@ -690,23 +690,23 @@ impl PyByteInner {
690690
options: ByteInnerPaddingOptions,
691691
vm: &VirtualMachine,
692692
) -> PyResult<Vec<u8>> {
693-
self.pad(options, PyCommonString::<u8>::py_center, vm)
693+
self._pad(options, PyCommonString::<u8>::py_center, vm)
694694
}
695695

696696
pub fn ljust(
697697
&self,
698698
options: ByteInnerPaddingOptions,
699699
vm: &VirtualMachine,
700700
) -> PyResult<Vec<u8>> {
701-
self.pad(options, PyCommonString::<u8>::py_ljust, vm)
701+
self._pad(options, PyCommonString::<u8>::py_ljust, vm)
702702
}
703703

704704
pub fn rjust(
705705
&self,
706706
options: ByteInnerPaddingOptions,
707707
vm: &VirtualMachine,
708708
) -> PyResult<Vec<u8>> {
709-
self.pad(options, PyCommonString::<u8>::py_rjust, vm)
709+
self._pad(options, PyCommonString::<u8>::py_rjust, vm)
710710
}
711711

712712
pub fn count(&self, options: ByteInnerFindOptions, vm: &VirtualMachine) -> PyResult<usize> {

vm/src/obj/objstr.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -895,26 +895,22 @@ impl PyString {
895895
unsafe { String::from_utf8_unchecked(self.value.py_zfill(width)) }
896896
}
897897

898-
fn get_fill_char(fillchar: OptionalArg<PyStringRef>, vm: &VirtualMachine) -> PyResult<char> {
899-
match fillchar {
900-
OptionalArg::Present(ref s) => s.value.chars().exactly_one().map_err(|_| {
901-
vm.new_type_error(
902-
"The fill character must be exactly one character long".to_owned(),
903-
)
904-
}),
905-
OptionalArg::Missing => Ok(' '),
906-
}
907-
}
908-
909898
#[inline]
910-
fn pad(
899+
fn _pad(
911900
&self,
912901
width: isize,
913902
fillchar: OptionalArg<PyStringRef>,
914903
pad: fn(&str, usize, char, usize) -> String,
915904
vm: &VirtualMachine,
916905
) -> PyResult<String> {
917-
let fillchar = Self::get_fill_char(fillchar, vm)?;
906+
let fillchar = match fillchar {
907+
OptionalArg::Present(ref s) => s.value.chars().exactly_one().map_err(|_| {
908+
vm.new_type_error(
909+
"The fill character must be exactly one character long".to_owned(),
910+
)
911+
}),
912+
OptionalArg::Missing => Ok(' '),
913+
}?;
918914
Ok(if self.len() as isize >= width {
919915
String::from(&self.value)
920916
} else {
@@ -929,7 +925,7 @@ impl PyString {
929925
fillchar: OptionalArg<PyStringRef>,
930926
vm: &VirtualMachine,
931927
) -> PyResult<String> {
932-
self.pad(width, fillchar, PyCommonString::<char>::py_center, vm)
928+
self._pad(width, fillchar, PyCommonString::<char>::py_center, vm)
933929
}
934930

935931
#[pymethod]
@@ -939,7 +935,7 @@ impl PyString {
939935
fillchar: OptionalArg<PyStringRef>,
940936
vm: &VirtualMachine,
941937
) -> PyResult<String> {
942-
self.pad(width, fillchar, PyCommonString::<char>::py_ljust, vm)
938+
self._pad(width, fillchar, PyCommonString::<char>::py_ljust, vm)
943939
}
944940

945941
#[pymethod]
@@ -949,7 +945,7 @@ impl PyString {
949945
fillchar: OptionalArg<PyStringRef>,
950946
vm: &VirtualMachine,
951947
) -> PyResult<String> {
952-
self.pad(width, fillchar, PyCommonString::<char>::py_rjust, vm)
948+
self._pad(width, fillchar, PyCommonString::<char>::py_rjust, vm)
953949
}
954950

955951
#[pymethod]

0 commit comments

Comments
 (0)