Skip to content
Permalink
Browse files

Merge pull request #819 from merryman/master

Implemented String#slice_i
  • Loading branch information
alex committed Aug 28, 2013
2 parents a43eca9 + 7162323 commit 8e07b4cdc0559d329f92c27a75064f5095a25a69
Showing with 42 additions and 18 deletions.
  1. +0 −18 spec/tags/core/string/slice_tags.txt
  2. +19 −0 tests/objects/test_stringobject.py
  3. +23 −0 topaz/objects/stringobject.py
@@ -29,31 +29,16 @@ fails:String#slice with String taints resulting strings when other is tainted
fails:String#slice with String doesn't set $~
fails:String#slice with String returns nil if there is no match
fails:String#slice with String returns a subclass instance when given a subclass instance
fails:String#slice! with index deletes and return the char at the given position
fails:String#slice! with index returns nil if idx is outside of self
fails:String#slice! with index raises a RuntimeError if self is frozen
fails:String#slice! with index calls to_int on index
fails:String#slice! with index, length deletes and returns the substring at idx and the given length
fails:String#slice! with index, length always taints resulting strings when self is tainted
fails:String#slice! with index, length raises a RuntimeError if self is frozen
fails:String#slice! with index, length calls to_int on idx and length
fails:String#slice! with index, length returns subclass instances
fails:String#slice! Range deletes and return the substring given by the offsets of the range
fails:String#slice! Range returns nil if the given range is out of self
fails:String#slice! Range always taints resulting strings when self is tainted
fails:String#slice! Range returns subclass instances
fails:String#slice! Range calls to_int on range arguments
fails:String#slice! Range works with Range subclasses
fails:String#slice! Range raises a RuntimeError on a frozen instance that is modified
fails:String#slice! Range raises a RuntimeError on a frozen instance that would not be modified
fails:String#slice! with Regexp deletes and returns the first match from self
fails:String#slice! with Regexp returns nil if there was no match
fails:String#slice! with Regexp always taints resulting strings when self or regexp is tainted
fails:String#slice! with Regexp doesn't taint self when regexp is tainted
fails:String#slice! with Regexp returns subclass instances
fails:String#slice! with Regexp sets $~ to MatchData when there is a match and nil when there's none
fails:String#slice! with Regexp raises a RuntimeError on a frozen instance that is modified
fails:String#slice! with Regexp raises a RuntimeError on a frozen instance that would not be modified
fails:String#slice! with Regexp, index deletes and returns the capture for idx from self
fails:String#slice! with Regexp, index always taints resulting strings when self or regexp is tainted
fails:String#slice! with Regexp, index doesn't taint self when regexp is tainted
@@ -62,14 +47,11 @@ fails:String#slice! with Regexp, index returns nil if there is no capture for id
fails:String#slice! with Regexp, index calls to_int on idx
fails:String#slice! with Regexp, index returns subclass instances
fails:String#slice! with Regexp, index sets $~ to MatchData when there is a match and nil when there's none
fails:String#slice! with Regexp, index raises a RuntimeError if self is frozen
fails:String#slice! with String removes and returns the first occurrence of other_str from self
fails:String#slice! with String taints resulting strings when other is tainted
fails:String#slice! with String doesn't set $~
fails:String#slice! with String returns nil if self does not contain other
fails:String#slice! with String doesn't call to_str on its argument
fails:String#slice! with String returns a subclass instance when given a subclass instance
fails:String#slice! with String raises a RuntimeError if self is frozen
fails:String#slice! with index returns the character given by the character index
fails:String#slice! with index, length returns the substring given by the character offsets
fails:String#slice! Range returns the substring given by the character offsets of the range
@@ -546,6 +546,25 @@ def test_succ(self, space):
w_res = space.execute('return "***".succ')
assert space.str_w(w_res) == "**+"

def test_slice(self, space):
w_res = space.execute('return "this is a string".slice(2)')
assert space.str_w(w_res) == "i"
w_res = space.execute('return "hello".slice(2, 3)')
assert space.str_w(w_res) == "llo"

def test_slice_i(self, space):
w_res = space.execute('return "this is a string".slice!(2)')
assert space.str_w(w_res) == "i"
w_res = space.execute('return "this is a string".slice!(2, 5)')
assert space.str_w(w_res) == "is is"
w_res = space.execute("""
string = "this is a string"
string.slice!(2)
string.slice!(3..6)
return string
""")
assert space.str_w(w_res) == "thsa string"


class TestStringMod(object):
def test_s(self, space):
@@ -133,6 +133,9 @@ def getitem(self, storage, idx):
def getslice(self, space, storage, start, end):
return space.newstr_fromchars(self.unerase(storage)[start:end])

def delslice(self, space, storage, start, end):
del self.unerase(storage)[start:end]

def hash(self, storage):
storage = self.unerase(storage)
length = len(storage)
@@ -553,6 +556,26 @@ def method_subscript(self, space, w_idx, w_count=None):
else:
return space.newstr_fromstr(self.strategy.getitem(self.str_storage, start))

@classdef.method("slice!")
@check_frozen()
def method_slice_i(self, space, w_idx, w_count=None):
start, end, as_range, nil = space.subscript_access(self.length(), w_idx, w_count=w_count)
if nil:
return space.w_nil
elif as_range:
assert start >= 0
assert end >= 0
self.strategy.to_mutable(space, self)
w_string = self.strategy.getslice(space, self.str_storage, start, end)
self.strategy.delslice(space, self.str_storage, start, end)
return w_string
else:
assert start >= 0
self.strategy.to_mutable(space, self)
w_string = self.strategy.getslice(space, self.str_storage, start, start + 1)
self.strategy.delslice(space, self.str_storage, start, start + 1)
return w_string

@classdef.method("<=>")
def method_comparator(self, space, w_other):
if isinstance(w_other, W_StringObject):

0 comments on commit 8e07b4c

Please sign in to comment.