@@ -1537,12 +1537,18 @@ safe_mul{T<:Integer}(n1::T, n2::T) = ((n2 > 0) ? ((n1 > div(typemax(T),n2)) ||
1537
1537
# safe_div{T<:Integer}(n1::T, n2::T) = ((n1 == typemin(T)) && (n2 == T(-1))) ? Nullable{T}() : Nullable{T}(div(n1, n2))
1538
1538
# safe_abs{T<:Integer}(n::T) = (n == typemin(T)) ? Nullable{T}() : abs(n)
1539
1539
1540
- function maybeint_internal {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Int , a:: Int )
1540
+ function tryparse_internal {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Int , a:: Int , raise :: Bool = false )
1541
1541
_n = Nullable {T} ()
1542
1542
sgn, base, i = parseint_preamble (T<: Signed ,s,base)
1543
- (i == 0 ) && return _n
1543
+ if i == 0
1544
+ raise && throw (ArgumentError (" premature end of integer: $(repr (s)) " ))
1545
+ return _n
1546
+ end
1544
1547
c, i = parseint_next (s,i)
1545
- (i == 0 ) && return _n
1548
+ if i == 0
1549
+ raise && throw (ArgumentError (" premature end of integer: $(repr (s)) " ))
1550
+ return _n
1551
+ end
1546
1552
1547
1553
base = convert (T,base)
1548
1554
m:: T = div (typemax (T)- base+ 1 ,base)
@@ -1551,7 +1557,10 @@ function maybeint_internal{T<:Integer}(::Type{T}, s::AbstractString, base::Int,
1551
1557
d:: T = ' 0' <= c <= ' 9' ? c- ' 0' :
1552
1558
' A' <= c <= ' Z' ? c- ' A' + 10 :
1553
1559
' a' <= c <= ' z' ? c- ' a' + a : base
1554
- d < base || return _n
1560
+ if d >= base
1561
+ raise && throw (ArgumentError (" invalid base $base digit $(repr (c)) in $(repr (s)) " ))
1562
+ return _n
1563
+ end
1555
1564
n *= base
1556
1565
n += d
1557
1566
if done (s,i)
@@ -1566,124 +1575,78 @@ function maybeint_internal{T<:Integer}(::Type{T}, s::AbstractString, base::Int,
1566
1575
d:: T = ' 0' <= c <= ' 9' ? c- ' 0' :
1567
1576
' A' <= c <= ' Z' ? c- ' A' + 10 :
1568
1577
' a' <= c <= ' z' ? c- ' a' + a : base
1569
- d < base || return _n
1578
+ if d >= base
1579
+ raise && throw (ArgumentError (" invalid base $base digit $(repr (c)) in $(repr (s)) " ))
1580
+ return _n
1581
+ end
1570
1582
(T <: Signed ) && (d *= sgn)
1571
1583
1572
1584
safe_n = safe_mul (n, base)
1573
- isnull (safe_n) || (safe_n = safe_add (get (safe_n), d))
1574
- isnull (safe_n) && return Nullable {T} ()
1575
- n = get (safe_n)
1585
+ safe_n. isnull || (safe_n = safe_add (safe_n. value, d))
1586
+ if safe_n. isnull
1587
+ raise && throw (OverflowError ())
1588
+ return _n
1589
+ end
1590
+ n = safe_n. value
1576
1591
done (s,i) && return Nullable {T} (n)
1577
1592
c, i = next (s,i)
1578
1593
end
1579
1594
while ! done (s,i)
1580
1595
c, i = next (s,i)
1581
- isspace (c) || return _n
1596
+ if ! isspace (c)
1597
+ raise && throw (ArgumentError (" extra characters after whitespace in $(repr (s)) " ))
1598
+ return _n
1599
+ end
1582
1600
end
1583
1601
return Nullable {T} (n)
1584
1602
end
1585
- maybeint_internal {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Int ) =
1586
- maybeint_internal (T, s, base, base <= 36 ? 10 : 36 )
1587
- maybeint {T<:Integer} (:: Type{T} , s:: AbstractString ) = maybeint_internal (T,s,0 )
1603
+ tryparse_internal {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Int , raise:: Bool = false ) =
1604
+ tryparse_internal (T, s, base, base <= 36 ? 10 : 36 , raise)
1605
+ tryparse {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Int ) =
1606
+ 2 <= base <= 62 ? tryparse_internal (T,s,Int (base)) : throw (ArgumentError (" invalid base: base must be 2 ≤ base ≤ 62, got $base " ))
1607
+ tryparse {T<:Integer} (:: Type{T} , s:: AbstractString ) = tryparse_internal (T,s,0 )
1588
1608
1589
- function parseint_nocheck {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Int , a:: Int )
1590
- sgn, base, i = parseint_preamble (T<: Signed ,s,base)
1591
- (i == 0 ) && throw (ArgumentError (" premature end of integer: $(repr (s)) " ))
1592
-
1593
- c, i = parseint_next (s,i)
1594
- (i == 0 ) && throw (ArgumentError (" premature end of integer: $(repr (s)) " ))
1595
-
1596
- base = convert (T,base)
1597
- # # FIXME : remove 128-bit specific code once 128-bit div doesn't rely on BigInt
1598
- m:: T = T=== UInt128 || T=== Int128 ? typemax (T) : div (typemax (T)- base+ 1 ,base)
1599
- n:: T = 0
1600
- while n <= m
1601
- d:: T = ' 0' <= c <= ' 9' ? c- ' 0' :
1602
- ' A' <= c <= ' Z' ? c- ' A' + 10 :
1603
- ' a' <= c <= ' z' ? c- ' a' + a : base
1604
- d < base || throw (ArgumentError (" invalid base $base digit $(repr (c)) in $(repr (s)) " ))
1605
- n *= base
1606
- n += d
1607
- if done (s,i)
1608
- n *= sgn
1609
- return n
1610
- end
1611
- c, i = next (s,i)
1612
- isspace (c) && break
1613
- end
1614
- (T <: Signed ) && (n *= sgn)
1615
- while ! isspace (c)
1616
- d:: T = ' 0' <= c <= ' 9' ? c- ' 0' :
1617
- ' A' <= c <= ' Z' ? c- ' A' + 10 :
1618
- ' a' <= c <= ' z' ? c- ' a' + a : base
1619
- d < base || throw (ArgumentError (" invalid base $base digit $(repr (c)) in $(repr (s)) " ))
1620
- (T <: Signed ) && (d *= sgn)
1621
- n = checked_mul (n,base)
1622
- n = checked_add (n,d)
1623
- done (s,i) && return n
1624
- c, i = next (s,i)
1625
- end
1626
- while ! done (s,i)
1627
- c, i = next (s,i)
1628
- isspace (c) || throw (ArgumentError (" extra characters after whitespace in $(repr (s)) " ))
1629
- end
1630
- return n
1609
+ function parseint {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Integer )
1610
+ (2 <= base <= 62 ) || throw (ArgumentError (" invalid base: base must be 2 ≤ base ≤ 62, got $base " ))
1611
+ (tryparse_internal (T, s, base, true )). value
1631
1612
end
1632
- parseint_nocheck {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Int ) =
1633
- parseint_nocheck (T, s, base, base <= 36 ? 10 : 36 )
1634
-
1635
- parseint {T<:Integer} (:: Type{T} , s:: AbstractString , base:: Integer ) =
1636
- 2 <= base <= 62 ? parseint_nocheck (T,s,Int (base)) : throw (ArgumentError (" invalid base: base must be 2 ≤ base ≤ 62, got $base " ))
1637
- parseint {T<:Integer} (:: Type{T} , s:: AbstractString ) = parseint_nocheck (T,s,0 )
1613
+ parseint {T<:Integer} (:: Type{T} , s:: AbstractString ) = (tryparse_internal (T, s, 0 , true )). value
1638
1614
parseint (s:: AbstractString , base:: Integer ) = parseint (Int,s,base)
1639
- parseint (s:: AbstractString ) = parseint_nocheck (Int,s, 0 )
1615
+ parseint (s:: AbstractString ) = parseint (Int,s)
1640
1616
1641
1617
# # stringifying integers more efficiently ##
1642
1618
1643
1619
string (x:: Union(Int8,Int16,Int32,Int64,Int128) ) = dec (x)
1644
1620
1645
1621
# # string to float functions ##
1646
1622
1647
- float64_isvalid (s:: AbstractString , out:: Array{Float64,1} ) =
1648
- ccall (:jl_strtod , Int32, (Ptr{UInt8},Ptr{Float64}), s, out) == 0
1649
- float32_isvalid (s:: AbstractString , out:: Array{Float32,1} ) =
1650
- ccall (:jl_strtof , Int32, (Ptr{UInt8},Ptr{Float32}), s, out) == 0
1651
-
1652
- float64_isvalid (s:: SubString , out:: Array{Float64,1} ) =
1653
- ccall (:jl_substrtod , Int32, (Ptr{UInt8},Csize_t,Cint,Ptr{Float64}), s. string, s. offset, s. endof, out) == 0
1654
- float32_isvalid (s:: SubString , out:: Array{Float32,1} ) =
1655
- ccall (:jl_substrtof , Int32, (Ptr{UInt8},Csize_t,Cint,Ptr{Float32}), s. string, s. offset, s. endof, out) == 0
1656
-
1657
- begin
1658
- local tmp:: Array{Float64,1} = Array (Float64,1 )
1659
- local tmpf:: Array{Float32,1} = Array (Float32,1 )
1660
- global parsefloat
1661
- function parsefloat (:: Type{Float64} , s:: AbstractString )
1662
- if ! float64_isvalid (s, tmp)
1663
- throw (ArgumentError (" parsefloat(Float64,::AbstractString): invalid number format $(repr (s)) " ))
1664
- end
1665
- return tmp[1 ]
1666
- end
1623
+ tryparse (:: Type{Float64} , s:: AbstractString ) = ccall (:jl_try_strtod , Nullable{Float64}, (Ptr{UInt8},), s)
1624
+ tryparse (:: Type{Float64} , s:: SubString ) = ccall (:jl_try_substrtod , Nullable{Float64}, (Ptr{UInt8},Csize_t,Cint), s. string, s. offset, s. endof)
1667
1625
1668
- function parsefloat (:: Type{Float32} , s:: AbstractString )
1669
- if ! float32_isvalid (s, tmpf)
1670
- throw (ArgumentError (" parsefloat(Float32,::AbstractString): invalid number format $(repr (s)) " ))
1671
- end
1672
- return tmpf[1 ]
1673
- end
1674
- end
1626
+ tryparse (:: Type{Float32} , s:: AbstractString ) = ccall (:jl_try_strtof , Nullable{Float32}, (Ptr{UInt8},), s)
1627
+ tryparse (:: Type{Float32} , s:: SubString ) = ccall (:jl_try_substrtof , Nullable{Float32}, (Ptr{UInt8},Csize_t,Cint), s. string, s. offset, s. endof)
1675
1628
1676
- float (x:: AbstractString ) = parsefloat (x)
1677
- parsefloat (x:: AbstractString ) = parsefloat (Float64,x)
1629
+ function parse {T<:Union(Float32,Float64)} (:: Type{T} , s:: AbstractString )
1630
+ nf = tryparse (T, s)
1631
+ nf. isnull ? throw (ArgumentError (" invalid number format $(repr (s)) for $T " )) : nf. value
1632
+ end
1678
1633
1679
- maybefloat64 (s:: AbstractString ) = ccall (:jl_maybe_strtod , Nullable{Float64}, (Ptr{UInt8},), s)
1680
- maybefloat64 (s:: SubString ) = ccall (:jl_maybe_substrtod , Nullable{Float64}, (Ptr{UInt8},Csize_t,Cint), s. string, s. offset, s. endof)
1634
+ parsefloat {T<:Union(Float32,Float64)} (:: Type{T} , s:: AbstractString ) = parse (T,s)
1681
1635
1682
- maybefloat32 (s :: AbstractString ) = ccall ( :jl_maybe_strtof , Nullable{Float32}, (Ptr{UInt8},), s )
1683
- maybefloat32 (s :: SubString ) = ccall ( :jl_maybe_substrtof , Nullable{Float32}, (Ptr{UInt8},Csize_t,Cint), s . string, s . offset, s . endof )
1636
+ float (x :: AbstractString ) = parse (Float64,x )
1637
+ parsefloat (x :: AbstractString ) = parse (Float64,x )
1684
1638
1685
1639
float {S<:AbstractString} (a:: AbstractArray{S} ) = map! (float, similar (a,typeof (float (0 ))), a)
1686
1640
1641
+ function float_isvalid {T<:Union(Float32,Float64)} (s:: AbstractString , out:: Array{T,1} )
1642
+ tf = tryparse (T, s)
1643
+ tf. isnull || (out[1 ] = tf. value)
1644
+ ! (tf. isnull)
1645
+ end
1646
+
1647
+ float32_isvalid (s:: AbstractString , out:: Array{Float32,1} ) = float_isvalid (s, out)
1648
+ float64_isvalid (s:: AbstractString , out:: Array{Float64,1} ) = float_isvalid (s, out)
1649
+
1687
1650
# find the index of the first occurrence of a value in a byte array
1688
1651
1689
1652
typealias ByteArray Union (Array{UInt8,1 },Array{Int8,1 })
0 commit comments