@@ -81,7 +81,7 @@ I.e. the value returned by `codeunit(s, i)` is of the type returned by
81
81
82
82
See also: [`ncodeunits`](@ref), [`checkbounds`](@ref)
83
83
"""
84
- codeunit (s:: AbstractString , i:: Integer ) = typeof (i) === Int ?
84
+ @propagate_inbounds codeunit (s:: AbstractString , i:: Integer ) = typeof (i) === Int ?
85
85
throw (MethodError (codeunit, Tuple{typeof (s),Int})) : codeunit (s, Int (i))
86
86
87
87
"""
@@ -119,7 +119,7 @@ Stacktrace:
119
119
[...]
120
120
```
121
121
"""
122
- isvalid (s:: AbstractString , i:: Integer ) = typeof (i) === Int ?
122
+ @propagate_inbounds isvalid (s:: AbstractString , i:: Integer ) = typeof (i) === Int ?
123
123
throw (MethodError (isvalid, Tuple{typeof (s),Int})) : isvalid (s, Int (i))
124
124
125
125
"""
@@ -134,7 +134,7 @@ a Unicode index error is raised.
134
134
See also: [`getindex`](@ref), [`start`](@ref), [`done`](@ref),
135
135
[`checkbounds`](@ref)
136
136
"""
137
- next (s:: AbstractString , i:: Integer ) = typeof (i) === Int ?
137
+ @propagate_inbounds next (s:: AbstractString , i:: Integer ) = typeof (i) === Int ?
138
138
throw (MethodError (next, Tuple{typeof (s),Int})) : next (s, Int (i))
139
139
140
140
# # basic generic definitions ##
@@ -148,13 +148,21 @@ endof(s::AbstractString) = thisind(s, ncodeunits(s))
148
148
getindex (s:: AbstractString , i:: Integer ) = next (s, i)[1 ]
149
149
getindex (s:: AbstractString , i:: Colon ) = s
150
150
# TODO : handle other ranges with stride ±1 specially?
151
+ # TODO : add more @propagate_inbounds annotations?
151
152
getindex (s:: AbstractString , r:: UnitRange{<:Integer} ) = SubString (s, r)
152
153
getindex (s:: AbstractString , v:: AbstractVector{<:Integer} ) =
153
154
sprint (length (v), io-> (for i in v; write (io, s[i]) end ))
154
155
getindex (s:: AbstractString , v:: AbstractVector{Bool} ) =
155
156
throw (ArgumentError (" logical indexing not supported for strings" ))
156
157
157
- get (s:: AbstractString , i:: Integer , default) = checkbounds (Bool, s, i) ? s[i] : default
158
+ function get (s:: AbstractString , i:: Integer , default)
159
+ # TODO : use ternary once @inbounds is expression-like
160
+ if checkbounds (Bool, s, i)
161
+ @inbounds return s[i]
162
+ else
163
+ return default
164
+ end
165
+ end
158
166
159
167
# # bounds checking ##
160
168
0 commit comments