Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string#to_i more tags #778

Merged
merged 2 commits into from
Jun 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions spec/tags/core/string/to_i_tags.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
fails:String#to_i interprets leading characters as a number in the given base
fails:String#to_i auto-detects base 8 via leading 0 when base = 0
fails:String#to_i auto-detects base 2 via 0b when base = 0
fails:String#to_i auto-detects base 10 via 0d when base = 0
fails:String#to_i auto-detects base 8 via 0o when base = 0
fails:String#to_i auto-detects base 16 via 0x when base = 0
fails:String#to_i auto-detects base 10 with no base specifier when base = 0
fails:String#to_i doesn't handle foreign base specifiers when base is > 0
35 changes: 29 additions & 6 deletions topaz/objects/stringobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
from rpython.rlib.rsre import rsre_core
from rpython.rlib.rstring import split

from topaz.coerce import Coerce
from topaz.module import ClassDef, check_frozen
from topaz.modules.comparable import Comparable
from topaz.objects.objectobject import W_Object
from topaz.utils.formatting import StringFormatter

RADIX_MAP = {"x": 16, "d": 10, "b": 2, "o": 8, "X": 16, "D": 10, "B": 2, "O": 8}


def create_trans_table(source, replacement, inv=False):
src = expand_trans_str(source, len(source), inv)
Expand Down Expand Up @@ -775,19 +778,39 @@ def to_bigint(self, s, neg, i, radix):
val = val.neg()
return val

@classdef.method("to_i", radix="int")
def method_to_i(self, space, radix=10):
if not 2 <= radix <= 36:
raise space.error(space.w_ArgumentError, "invalid radix %d" % radix)
@classdef.method("to_i")
def method_to_i(self, space, w_radix=None):
if w_radix is None:
is_radix = False
radix = 10
else:
is_radix = True
radix = Coerce.int(space, w_radix)
s = space.str_w(self)
i = 0
while i < len(s):
length = len(s)
while i < length:
if not s[i].isspace():
break
i += 1
neg = i < len(s) and s[i] == "-"
neg = i < length and s[i] == "-"
if neg:
i += 1
if i < length and s[i] == "0":
if i + 1 < length:
try:
r = RADIX_MAP[s[i + 1]]
except KeyError:
if radix == 0:
radix = 8
else:
if not is_radix or radix == r or radix == 0:
radix = r
i += 2
if radix == 0:
radix = 10
if not 2 <= radix <= 36:
raise space.error(space.w_ArgumentError, "invalid radix %d" % radix)
try:
value = self.to_int(s, neg, i, radix)
except OverflowError:
Expand Down