Skip to content
Permalink
Browse files

Merge pull request #821 from danshultz/string_empty_chomp_keeps_carri…

…age_return

String.chomp maintains trailing carriage return
  • Loading branch information
alex committed Oct 6, 2013
2 parents 78c952f + e46e5ac commit 1d6fd7162cf83d734cb4bec457a760b105f1cd8e
Showing with 7 additions and 5 deletions.
  1. +0 −2 spec/tags/core/string/chomp_tags.txt
  2. +3 −0 tests/objects/test_stringobject.py
  3. +4 −3 topaz/objects/stringobject.py
@@ -1,6 +1,4 @@
fails:String#chomp when passed '' does not remove a final carriage return
fails:String#chomp! when passed nil returns nil
fails:String#chomp! when passed nil returns nil when self is empty
fails:String#chomp! when passed '' does not remove a final carriage return
fails:String#chomp removes the final carriage return, newline from a non-ASCII String
fails:String#chomp! removes the final carriage return, newline from a non-ASCII String
@@ -433,12 +433,15 @@ def test_chomp(self, space):
assert space.str_w(space.execute('return "hello\\r\\n".chomp')) == "hello"
assert space.str_w(space.execute('return "hello\\n\\r".chomp')) == "hello"
assert space.str_w(space.execute('return "hello\\r".chomp')) == "hello"
assert space.str_w(space.execute('return "hello\\r".chomp("")')) == "hello\r"
assert space.str_w(space.execute('return "hello \\n there".chomp')) == "hello \n there"
assert space.str_w(space.execute('return "hello".chomp("llo")')) == "he"
w_res = space.execute('return "hello".chomp!')
assert w_res is space.w_nil
w_res = space.execute('return "".chomp!')
assert w_res is space.w_nil
w_res = space.execute('return "abc\\r".chomp!("")')
assert w_res is space.w_nil

def test_chop(self, space):
assert space.str_w(space.execute('return "string\\r\\n".chop')) == "string"
@@ -223,9 +223,10 @@ def chomp(self, storage, newline=None):
elif newline is not None and len(newline) == 0:
ch = storage[-1]
i = len(storage) - 1
while i >= 1 and ch in linebreaks:
i -= 1
ch = storage[i]
if ch != "\r":
while i >= 1 and ch in linebreaks:
i -= 1
ch = storage[i]
if i < len(storage) - 1:
i += 1
changed = True

0 comments on commit 1d6fd71

Please sign in to comment.
You can’t perform that action at this time.