Skip to content

Commit 84a6e8e

Browse files
committed
merged from TheAnyKey/p39_string_rem_pre_suffix
1 parent a447b88 commit 84a6e8e

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

tests/snippets/strings.py

+71-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from testutils import assert_raises, AssertRaises
1+
from testutils import assert_raises, AssertRaises, skip_if_unsupported
22

33
assert "".__eq__(1) == NotImplemented
44
assert "a" == 'a'
@@ -471,3 +471,73 @@ def try_mutate_str():
471471
assert '{:e}'.format(float('inf')) == 'inf'
472472
assert '{:e}'.format(float('-inf')) == '-inf'
473473
assert '{:E}'.format(float('inf')) == 'INF'
474+
475+
476+
# remove*fix test
477+
def test_removeprefix():
478+
s='foobarfoo'
479+
s_ref='foobarfoo'
480+
assert s.removeprefix('f') == s_ref[1:]
481+
assert s.removeprefix('fo') == s_ref[2:]
482+
assert s.removeprefix('foo') == s_ref[3:]
483+
484+
assert s.removeprefix('') == s_ref
485+
assert s.removeprefix('bar') == s_ref
486+
assert s.removeprefix('lol') == s_ref
487+
assert s.removeprefix('_foo') == s_ref
488+
assert s.removeprefix('-foo') == s_ref
489+
assert s.removeprefix('afoo') == s_ref
490+
assert s.removeprefix('*foo') == s_ref
491+
492+
assert s==s_ref, 'undefined test fail'
493+
494+
def test_removeprefix_types():
495+
s='0123456'
496+
s_ref='0123456'
497+
others=[0,['012']]
498+
found=False
499+
for o in others:
500+
try:
501+
s.removeprefix(o)
502+
except:
503+
found=True
504+
505+
assert found, f'Removeprefix accepts other type: {type(o)}: {o=}'
506+
507+
def test_removesuffix():
508+
s='foobarfoo'
509+
s_ref='foobarfoo'
510+
assert s.removesuffix('o') == s_ref[:-1]
511+
assert s.removesuffix('oo') == s_ref[:-2]
512+
assert s.removesuffix('foo') == s_ref[:-3]
513+
514+
assert s.removesuffix('') == s_ref
515+
assert s.removesuffix('bar') == s_ref
516+
assert s.removesuffix('lol') == s_ref
517+
assert s.removesuffix('foo_') == s_ref
518+
assert s.removesuffix('foo-') == s_ref
519+
assert s.removesuffix('foo*') == s_ref
520+
assert s.removesuffix('fooa') == s_ref
521+
522+
assert s==s_ref, 'undefined test fail'
523+
524+
def test_removesuffix_types():
525+
s='0123456'
526+
s_ref='0123456'
527+
others=[0,6,['6']]
528+
found=False
529+
for o in others:
530+
try:
531+
s.removesuffix(o)
532+
except:
533+
found=True
534+
535+
assert found, f'Removesuffix accepts other type: {type(o)}: {o=}'
536+
537+
538+
skip_if_unsupported(3,9,test_removeprefix)
539+
skip_if_unsupported(3,9,test_removeprefix_types)
540+
skip_if_unsupported(3,9,test_removesuffix)
541+
skip_if_unsupported(3,9,test_removesuffix_types)
542+
543+

vm/src/obj/objstr.rs

+16
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,22 @@ impl PyString {
510510
.to_owned()
511511
}
512512

513+
#[pymethod]
514+
fn removeprefix(&self, pref: PyStringRef) -> PyResult<String> {
515+
if self.value.as_str().starts_with(&pref.value) {
516+
return Ok(self.value[pref.len()..].to_string());
517+
}
518+
Ok(self.value.to_string())
519+
}
520+
521+
#[pymethod]
522+
fn removesuffix(&self, suff: PyStringRef) -> PyResult<String> {
523+
if self.value.as_str().ends_with(&suff.value) {
524+
return Ok(self.value[..self.value.len() - suff.len()].to_string());
525+
}
526+
Ok(self.value.to_string())
527+
}
528+
513529
#[pymethod]
514530
fn endswith(&self, args: pystr::StartsEndsWithArgs, vm: &VirtualMachine) -> PyResult<bool> {
515531
self.value.as_str().py_startsendswith(

0 commit comments

Comments
 (0)