Skip to content
Permalink
Browse files

Merge pull request #763 from kachick/implement-string-setbyte

Implement String#setbyte
  • Loading branch information
alex committed Jun 24, 2013
2 parents 1932525 + 11c33f0 commit f481d57c73507977f834489ea8e75802d84c494f
Showing with 17 additions and 10 deletions.
  1. +0 −10 spec/tags/core/string/setbyte_tags.txt
  2. +17 −0 topaz/objects/stringobject.py
@@ -1,12 +1,2 @@
fails:String#setbyte returns an Integer
fails:String#setbyte modifies the receiver
fails:String#setbyte changes the byte at the given index to the new byte
fails:String#setbyte allows changing bytes in multi-byte characters
fails:String#setbyte can invalidate a String's encoding
fails:String#setbyte regards a negative index as counting from the end of the String
fails:String#setbyte raises an IndexError if the index is greater than the String bytesize
fails:String#setbyte raises an IndexError if the nexgative index is greater magnitude than the String bytesize
fails:String#setbyte sets a byte at an index greater than String size
fails:String#setbyte does not modify the original string when using String.new
fails:String#setbyte raises a RuntimeError if self is frozen
fails:String#setbyte raises a TypeError unless the second argument is an Integer
@@ -292,6 +292,10 @@ def insert(self, storage, idx, other):
storage.insert(idx, char)
idx += 1

def replaceitem(self, storage, idx, char):
storage = self.unerase(storage)
storage[idx] = char

def strip(self, storage):
storage = self.unerase(storage)
if not storage:
@@ -885,6 +889,19 @@ def method_getbyte(self, space, pos):
ch = self.strategy.getitem(self.str_storage, pos)
return space.newint(ord(ch))

@classdef.method("setbyte", pos="int", replacement="int")
@check_frozen()
def method_setbyte(self, space, pos, replacement):
if pos >= self.length() or pos < -self.length():
raise space.error(space.w_IndexError,
"index %d out of string" % pos
)
if pos < 0:
pos += self.length()
self.strategy.to_mutable(space, self)
self.strategy.replaceitem(self.str_storage, pos, chr(replacement))
return space.newint(replacement)

@classdef.method("include?", substr="str")
def method_includep(self, space, substr):
return space.newbool(substr in space.str_w(self))

0 comments on commit f481d57

Please sign in to comment.