Skip to content
Permalink
Browse files

Merge pull request #641 from topazproject/tim/String#strip

Add String#strip
  • Loading branch information
alex committed Apr 20, 2013
2 parents 9da329e + e6a10fe commit dece7d4af4d28b47ed7229b0b44697e9b05b284f
Showing with 44 additions and 9 deletions.
  1. +7 −1 lib-topaz/string.rb
  2. +0 −8 spec/tags/core/string/strip_tags.txt
  3. +37 −0 topaz/objects/stringobject.py
@@ -35,7 +35,7 @@ def casecmp(other)
unless other.respond_to?(:to_str)
raise TypeError.new("can't convert #{other.class} into String")
end

other = other.to_str
diff = self.length - other.length
short = diff < 0
@@ -156,4 +156,10 @@ def upto(max, exclusive = false, &block)

return self
end

def strip
duplicate = self.dup
duplicate.strip!
duplicate
end
end

This file was deleted.

@@ -292,6 +292,33 @@ def insert(self, storage, idx, other):
storage.insert(idx, char)
idx += 1

def strip(self, storage):
storage = self.unerase(storage)
if not storage:
return False

shift = 0
while shift < len(storage) and storage[shift].isspace():
shift += 1
if shift == len(storage):
del storage[:]
return True

pop = len(storage)
while pop > 0 and storage[pop - 1].isspace() or storage[pop - 1] == '\0':
pop -= 1

if pop < len(storage) or shift > 0:
end = pop
new_len = end - shift
assert end >= 0
assert new_len >= 0
storage[0:new_len] = storage[shift:end]
del storage[new_len:]
return True
else:
return False


class W_StringObject(W_Object):
classdef = ClassDef("String", W_Object.classdef, filepath=__file__)
@@ -1026,3 +1053,13 @@ def method_insert(self, space, index, other):
self.strategy.to_mutable(space, self)
self.strategy.insert(self.str_storage, index, other)
return self

@classdef.method("strip!")
@check_frozen()
def method_strip_i(self, space):
self.strategy.to_mutable(space, self)
changed = self.strategy.strip(self.str_storage)
if changed:
return self
else:
return space.w_nil

0 comments on commit dece7d4

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