Skip to content
Permalink
Browse files

Merge pull request #809 from sideshowcoder/adding_chomp

fixing chomp special cases
  • Loading branch information
alex committed Jul 17, 2013
2 parents 642c9eb + 6756914 commit 3ac2451099b52b7b1329d1011f1d440a9d318164
Showing with 22 additions and 34 deletions.
  1. +5 −0 lib-topaz/kernel.rb
  2. +0 −13 spec/tags/core/kernel/chomp_tags.txt
  3. +0 −19 spec/tags/core/string/chomp_tags.txt
  4. +17 −2 topaz/objects/stringobject.py
@@ -28,6 +28,11 @@ def chop
end
private :chop

def chomp
$_.chomp!
end
private :chomp

def Array(arg)
if ary = Topaz.try_convert_type(arg, Array, :to_ary)
ary

This file was deleted.

@@ -1,25 +1,6 @@
fails:String#chomp with separator removes carriage return (except \r) chars multiple times when separator is an empty string
fails:String#chomp with separator removes carriage return chars (\n, \r, \r\n) when separator is \n
fails:String#chomp with separator returns subclass instances when called on a subclass
fails:String#chomp! with separator modifies self in place and returns self
fails:String#chomp! with separator returns nil if no modifications were made
fails:String#chomp! with separator raises a RuntimeError on a frozen instance when it is modified
fails:String#chomp! with separator raises a RuntimeError on a frozen instance when it would not be modified
fails:String#chomp when passed no argument removes one trailing newline
fails:String#chomp when passed no argument removes one trailing carriage return
fails:String#chomp when passed no argument removes one trailing carrige return, newline pair
fails:String#chomp when passed '' does not remove a final carriage return
fails:String#chomp when passed '\n' removes one trailing newline
fails:String#chomp when passed '\n' removes one trailing carriage return
fails:String#chomp when passed '\n' removes one trailing carrige return, newline pair
fails:String#chomp! when passed no argument removes one trailing newline
fails:String#chomp! when passed no argument removes one trailing carriage return
fails:String#chomp! when passed no argument removes one trailing carrige return, newline pair
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! when passed '\n' removes one trailing newline
fails:String#chomp! when passed '\n' removes one trailing carriage return
fails:String#chomp! when passed '\n' removes one trailing carrige return, newline pair
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
@@ -214,8 +214,22 @@ def capitalize(self, storage):
def chomp(self, storage, newline=None):
storage = self.unerase(storage)
changed = False
linebreaks = ["\n", "\r"]
if len(storage) == 0:
return changed
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 i < len(storage) - 1:
i += 1
changed = True
if i > 0:
del storage[i:]
else:
del storage[:]
elif newline is not None and len(storage) >= len(newline):
for i in xrange(len(newline) - 1, -1, -1):
if newline[i] != storage[len(storage) - len(newline) + i]:
@@ -227,7 +241,8 @@ def chomp(self, storage, newline=None):
elif newline is None:
ch = storage[-1]
i = len(storage) - 1
while i >= 0 and ch in "\n\r":
while i >= 0 and linebreaks and ch in linebreaks:
linebreaks.remove(ch)
i -= 1
ch = storage[i]
if i < len(storage) - 1:
@@ -1172,7 +1187,7 @@ def method_chomp_i(self, space, w_newline=None):
if w_newline is space.w_nil:
return self
newline = space.str_w(space.convert_type(w_newline, space.w_string, "to_str"))
if newline in "\n\r":
if newline and newline in "\n\r":
newline = None
self.strategy.to_mutable(space, self)
changed = self.strategy.chomp(self.str_storage, newline)

0 comments on commit 3ac2451

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