Skip to content

Commit 53dea48

Browse files
Merge pull request RustPython#878 from sanxiyn/fix-just
Fix justification
2 parents bea6b48 + 0e9acdc commit 53dea48

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

tests/snippets/strings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@
5757
assert s.lstrip('^*') == 'RustPython*^'
5858
assert s.rstrip('^*') == '^*RustPython'
5959

60+
s = 'RustPython'
61+
assert s.ljust(8) == 'RustPython'
62+
assert s.rjust(8) == 'RustPython'
63+
assert s.ljust(12) == 'RustPython '
64+
assert s.rjust(12) == ' RustPython'
65+
assert s.ljust(12, '_') == 'RustPython__'
66+
assert s.rjust(12, '_') == '__RustPython'
67+
# The fill character must be exactly one character long
68+
assert_raises(TypeError, lambda: s.ljust(12, '__'))
69+
assert_raises(TypeError, lambda: s.rjust(12, '__'))
70+
6071
c = 'hallo'
6172
assert c.capitalize() == 'Hallo'
6273
assert c.center(11, '-') == '---hallo---'

vm/src/obj/objstr.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,11 @@ impl PyString {
740740
) -> PyResult<String> {
741741
let value = &self.value;
742742
let rep_char = Self::get_fill_char(&rep, vm)?;
743-
Ok(format!("{}{}", value, rep_char.repeat(len)))
743+
if len <= value.len() {
744+
Ok(value.to_string())
745+
} else {
746+
Ok(format!("{}{}", value, rep_char.repeat(len - value.len())))
747+
}
744748
}
745749

746750
#[pymethod]
@@ -752,7 +756,11 @@ impl PyString {
752756
) -> PyResult<String> {
753757
let value = &self.value;
754758
let rep_char = Self::get_fill_char(&rep, vm)?;
755-
Ok(format!("{}{}", rep_char.repeat(len), value))
759+
if len <= value.len() {
760+
Ok(value.to_string())
761+
} else {
762+
Ok(format!("{}{}", rep_char.repeat(len - value.len()), value))
763+
}
756764
}
757765

758766
#[pymethod]

0 commit comments

Comments
 (0)