Skip to content
Permalink
Browse files

Merge pull request #493 from obolzer/String#upto

Implement String#upto and fix String#succ
  • Loading branch information
alex committed Mar 11, 2013
2 parents 14a4f6f + 06dc93e commit a64623af40d2ae7dd13352d31f1f30717083d221
Showing with 30 additions and 14 deletions.
  1. +28 −0 lib-topaz/string.rb
  2. +0 −1 spec/tags/core/string/succ_tags.txt
  3. +0 −12 spec/tags/core/string/upto_tags.txt
  4. +2 −1 topaz/objects/stringobject.py
@@ -59,4 +59,32 @@ def succ
self.dup.succ!
end
alias next succ

def upto(max, exclusive = false, &block)
raise TypeError.new("can't convert #{max.class} into String") unless max.respond_to?(:to_str)
raise NotImplementedError.new("Object#enum_for") unless block

maximum = max.to_str
return self if self > maximum

current = self.dup
while current <= maximum
break if current.length > maximum.length
break if exclusive and current == maximum
yield current
# special handling to use ASCII map for single letters
# ("9" followed by ":" not handled by String#succ)
if current == "9" && self.length == 1 && maximum.length == 1 then
current = ":"
elsif current == "Z" && self.length == 1 && maximum.length == 1 then
current = "["
elsif current == "z" && self.length == 1 && maximum.length == 1 then
current = "{"
else
current = current.succ
end
end

return self
end
end
@@ -1,3 +1,2 @@
fails:String#succ increases the next best alphanumeric (jumping over non-alphanumerics) if there is a carry
fails:String#succ returns subclass instances when called on a subclass
fails:String#succ! raises a RuntimeError if self is frozen
@@ -1,13 +1 @@
fails:String#upto passes successive values, starting at self and ending at other_string, to the block
fails:String#upto calls the block once even when start eqals stop
fails:String#upto doesn't call block with self even if self is less than stop but stop length is less than self length
fails:String#upto doesn't call block if stop is less than self and stop length is less than self length
fails:String#upto doesn't call the block if self is greater than stop
fails:String#upto stops iterating as soon as the current value's character count gets higher than stop's
fails:String#upto returns self
fails:String#upto tries to convert other to string using to_str
fails:String#upto raises a TypeError if other can't be converted to a string
fails:String#upto does not work with symbols
fails:String#upto returns an enumerator when no block given
fails:String#upto uses the ASCII map for single letters
fails:String#upto stops before the last value if exclusive
@@ -261,6 +261,7 @@ def succ(self, storage):
storage[start] = "A"
else:
storage[start] = chr(ord(ch) + 1)
carry = "\0"

if carry == "\0":
break
@@ -280,7 +281,7 @@ def succ(self, storage):
break
start -= 1

if start < 0:
if start < 0 and carry != "\0":
last_alnum_ch = storage[last_alnum]
storage[last_alnum] = carry
storage.insert(last_alnum + 1, last_alnum_ch)

0 comments on commit a64623a

Please sign in to comment.