Skip to content

Commit eeb6cea

Browse files
committed
fixed len issue and updated test accordingly
1 parent 81a234e commit eeb6cea

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

β€ŽLib/test/string_tests.pyβ€Ž

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,42 @@ def test_replace_overflow(self):
681681
self.checkraises(OverflowError, A2_16, "replace", "A", A2_16)
682682
self.checkraises(OverflowError, A2_16, "replace", "AA", A2_16+A2_16)
683683

684+
def test_removeprefix(self):
685+
self.checkequal('am', 'spam', 'removeprefix', 'sp')
686+
self.checkequal('spamspam', 'spamspamspam', 'removeprefix', 'spam')
687+
self.checkequal('spam', 'spam', 'removeprefix', 'python')
688+
self.checkequal('spam', 'spam', 'removeprefix', 'spider')
689+
self.checkequal('spam', 'spam', 'removeprefix', 'spam and eggs')
690+
691+
self.checkequal('', '', 'removeprefix', '')
692+
self.checkequal('', '', 'removeprefix', 'abcde')
693+
self.checkequal('abcde', 'abcde', 'removeprefix', '')
694+
self.checkequal('', 'abcde', 'removeprefix', 'abcde')
695+
696+
self.checkraises(TypeError, 'hello', 'removeprefix')
697+
self.checkraises(TypeError, 'hello', 'removeprefix', 42)
698+
self.checkraises(TypeError, 'hello', 'removeprefix', 42, 'h')
699+
self.checkraises(TypeError, 'hello', 'removeprefix', 'h', 42)
700+
self.checkraises(TypeError, 'hello', 'removeprefix', ("he", "l"))
701+
702+
def test_removesuffix(self):
703+
self.checkequal('sp', 'spam', 'removesuffix', 'am')
704+
self.checkequal('spamspam', 'spamspamspam', 'removesuffix', 'spam')
705+
self.checkequal('spam', 'spam', 'removesuffix', 'python')
706+
self.checkequal('spam', 'spam', 'removesuffix', 'blam')
707+
self.checkequal('spam', 'spam', 'removesuffix', 'eggs and spam')
708+
709+
self.checkequal('', '', 'removesuffix', '')
710+
self.checkequal('', '', 'removesuffix', 'abcde')
711+
self.checkequal('abcde', 'abcde', 'removesuffix', '')
712+
self.checkequal('', 'abcde', 'removesuffix', 'abcde')
713+
714+
self.checkraises(TypeError, 'hello', 'removesuffix')
715+
self.checkraises(TypeError, 'hello', 'removesuffix', 42)
716+
self.checkraises(TypeError, 'hello', 'removesuffix', 42, 'h')
717+
self.checkraises(TypeError, 'hello', 'removesuffix', 'h', 42)
718+
self.checkraises(TypeError, 'hello', 'removesuffix', ("lo", "l"))
719+
684720
def test_capitalize(self):
685721
self.checkequal(' hello ', ' hello ', 'capitalize')
686722
self.checkequal('Hello ', 'Hello ','capitalize')

β€Žtests/snippets/strings.pyβ€Ž

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ def try_mutate_str():
475475

476476
# remove*fix test
477477
def test_removeprefix():
478-
s='foobarfoo'
478+
s = 'foobarfoo'
479479
s_ref='foobarfoo'
480480
assert s.removeprefix('f') == s_ref[1:]
481481
assert s.removeprefix('fo') == s_ref[2:]
@@ -491,6 +491,20 @@ def test_removeprefix():
491491

492492
assert s==s_ref, 'undefined test fail'
493493

494+
s_uc = '😱foobarfooπŸ––'
495+
s_ref_uc = '😱foobarfooπŸ––'
496+
assert s_uc.removeprefix('😱') == s_ref_uc[1:]
497+
assert s_uc.removeprefix('😱fo') == s_ref_uc[3:]
498+
assert s_uc.removeprefix('😱foo') == s_ref_uc[4:]
499+
500+
assert s_uc.removeprefix('πŸ––') == s_ref_uc
501+
assert s_uc.removeprefix('foo') == s_ref_uc
502+
assert s_uc.removeprefix(' ') == s_ref_uc
503+
assert s_uc.removeprefix('_😱') == s_ref_uc
504+
assert s_uc.removeprefix(' 😱') == s_ref_uc
505+
assert s_uc.removeprefix('-😱') == s_ref_uc
506+
assert s_uc.removeprefix('#😱') == s_ref_uc
507+
494508
def test_removeprefix_types():
495509
s='0123456'
496510
s_ref='0123456'
@@ -521,6 +535,20 @@ def test_removesuffix():
521535

522536
assert s==s_ref, 'undefined test fail'
523537

538+
s_uc = '😱foobarfooπŸ––'
539+
s_ref_uc = '😱foobarfooπŸ––'
540+
assert s_uc.removesuffix('πŸ––') == s_ref_uc[:-1]
541+
assert s_uc.removesuffix('ooπŸ––') == s_ref_uc[:-3]
542+
assert s_uc.removesuffix('fooπŸ––') == s_ref_uc[:-4]
543+
544+
assert s_uc.removesuffix('😱') == s_ref_uc
545+
assert s_uc.removesuffix('foo') == s_ref_uc
546+
assert s_uc.removesuffix(' ') == s_ref_uc
547+
assert s_uc.removesuffix('πŸ––_') == s_ref_uc
548+
assert s_uc.removesuffix('πŸ–– ') == s_ref_uc
549+
assert s_uc.removesuffix('πŸ––-') == s_ref_uc
550+
assert s_uc.removesuffix('πŸ––#') == s_ref_uc
551+
524552
def test_removesuffix_types():
525553
s='0123456'
526554
s_ref='0123456'

β€Žvm/src/obj/objstr.rsβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,15 @@ impl PyString {
535535
#[pymethod]
536536
fn removeprefix(&self, pref: PyStringRef) -> PyResult<String> {
537537
if self.value.as_str().starts_with(&pref.value) {
538-
return Ok(self.value[pref.len()..].to_string());
538+
return Ok(self.value[pref.value.len()..].to_string());
539539
}
540540
Ok(self.value.to_string())
541541
}
542542

543543
#[pymethod]
544544
fn removesuffix(&self, suff: PyStringRef) -> PyResult<String> {
545545
if self.value.as_str().ends_with(&suff.value) {
546-
return Ok(self.value[..self.value.len() - suff.len()].to_string());
546+
return Ok(self.value[..self.value.len() - suff.value.len()].to_string());
547547
}
548548
Ok(self.value.to_string())
549549
}

0 commit comments

Comments
Β (0)