Skip to content

Commit

Permalink
More liberal int(String) (addresses JuliaLang#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlobaldassi committed Apr 13, 2012
1 parent 7678d7e commit 6457c26
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -864,15 +864,31 @@ strip(s::String) = lstrip(rstrip(s))
function parse_int{T<:Integer}(::Type{T}, s::String, base::Integer)
if !(2 <= base <= 36); error("invalid base: ",base); end
i = start(s)
if done(s,i)
error("premature end of integer (in ",show_to_string(s),")")
while true
if done(s,i)
#throw(ArgumentError(strcat("premature end of integer (in ",show_to_string(s),")")))
throw(ArgumentError("parse_int: premature end of integer)"))
#error("premature end of integer (in ",show_to_string(s),")")
end
c,i = next(s,i)
if !iswspace(c)
break
end
end
c,i = next(s,i)
sgn = one(T)
if T <: Signed && c == '-'
sgn = -sgn
if done(s,i)
error("premature end of integer (in ",show_to_string(s),")")
#throw(ArgumentError(strcat("premature end of integer (in ",show_to_string(s),")")))
throw(ArgumentError("parse_int: premature end of integer"))
#error("premature end of integer (in ",show_to_string(s),")")
end
c,i = next(s,i)
elseif c == '+'
if done(s,i)
#throw(ArgumentError(strcat("premature end of integer (in ",show_to_string(s),")")))
throw(ArgumentError("parse_int: premature end of integer"))
#error("premature end of integer (in ",show_to_string(s),")")
end
c,i = next(s,i)
end
Expand All @@ -883,7 +899,27 @@ function parse_int{T<:Integer}(::Type{T}, s::String, base::Integer)
'A' <= c <= 'Z' ? c-'A'+10 :
'a' <= c <= 'z' ? c-'a'+10 : typemax(Int)
if d >= base
error(show_to_string(c)," is not a valid digit (in ",show_to_string(s),")")
if !iswspace(c)
#throw(ArgumentError(strcat(show_to_string(c)," is not a valid digit (in ",show_to_string(s),")")))
throw(ArgumentError("parse_int: invalid digit"))
#error(show_to_string(c)," is not a valid digit (in ",show_to_string(s),")")
end
finished = false
while true
if done(s,i)
finished = true
break
end
c,i = next(s,i)
if !iswspace(c)
#throw(ArgumentError(strcat("extra characters after whitespace (in ",show_to_string(s),")")))
throw(ArgumentError("parse_int: extra characters after whitespace"))
#error("extra characters after whitespace (in ",show_to_string(s),")")
end
end
if finished
break
end
end
# TODO: overflow detection?
n = n*base + d
Expand Down

0 comments on commit 6457c26

Please sign in to comment.