Skip to content

Commit

Permalink
Do the conversion in getindex of UnitRange only when returning
Browse files Browse the repository at this point in the history
  • Loading branch information
haampie committed Mar 26, 2018
1 parent e62190c commit 01dd920
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,23 @@ end

## indexing

_in_unit_range(v::UnitRange, val, i::Integer) = i > 0 && val <= v.stop && val >= v.start

function getindex(v::UnitRange{T}, i::Integer) where T
@_inline_meta
ret = convert(T, first(v) + i - 1)
@boundscheck ((i > 0) & (ret <= v.stop) & (ret >= v.start)) || throw_boundserror(v, i)
ret
val = convert(T, v.start + i - 1)
@boundscheck _in_unit_range(v, val, i) || throw_boundserror(v, i)
val
end

const OverflowSafe = Union{Bool,Int8,Int16,Int32,Int64,Int128,
UInt8,UInt16,UInt32,UInt64,UInt128}

function getindex(v::UnitRange{T}, i::Integer) where {T<:OverflowSafe}
@_inline_meta
val = v.start + i - 1
@boundscheck _in_unit_range(v, val, i) || throw_boundserror(v, i)
val % T
end

function getindex(v::OneTo{T}, i::Integer) where T
Expand Down

0 comments on commit 01dd920

Please sign in to comment.