@@ -87,14 +87,13 @@ See also: [`ncodeunits`](@ref), [`checkbounds`](@ref)
8787"""
8888 isvalid(s::AbstractString, i::Integer) -> Bool
8989
90- Predicate indicating whether the given index is the start of the encoding of
91- a character in `s` or not. If `isvalid(s, i)` is true then `s[i]` will return
92- the character whose encoding starts at that index, if it's false, then `s[i]`
93- will raise an invalid index error. Behavior of `next(s, i)` is similar except
94- that the character is returned along with the index of the following character.
95- In order for `isvalid(s, i)` to be an O(1) function, the encoding of `s` must
96- be [self-synchronizing](https://en.wikipedia.org/wiki/Self-synchronizing_code);
97- this is a basic assumption of Julia's generic string support.
90+ Predicate indicating whether the given index is the start of the encoding of a
91+ character in `s` or not. If `isvalid(s, i)` is true then `s[i]` will return the
92+ character whose encoding starts at that index, if it's false, then `s[i]` will
93+ raise an invalid index error or a bounds error depending on if `i` is in bounds.
94+ In order for `isvalid(s, i)` to be an O(1) function, the encoding of `s` must be
95+ [self-synchronizing](https://en.wikipedia.org/wiki/Self-synchronizing_code) this
96+ is a basic assumption of Julia's generic string support.
9897
9998See also: [`getindex`](@ref), [`next`](@ref), [`thisind`](@ref),
10099[`nextind`](@ref), [`prevind`](@ref), [`length`](@ref)
@@ -128,8 +127,8 @@ Stacktrace:
128127Return a tuple of the character in `s` at index `i` with the index of the start
129128of the following character in `s`. This is the key method that allows strings to
130129be iterated, yielding a sequences of characters. If `i` is out of bounds in `s`
131- then a bounds error is raised; if `i` is not a valid character index in `s` then
132- a Unicode index error is raised .
130+ then a bounds error is raised. The `next` function, as part of the iteration
131+ protocoal may assume that `i` is the start of a character in `s` .
133132
134133See also: [`getindex`](@ref), [`start`](@ref), [`done`](@ref),
135134[`checkbounds`](@ref)
@@ -145,7 +144,11 @@ eltype(::Type{<:AbstractString}) = Char
145144sizeof (s:: AbstractString ) = ncodeunits (s) * sizeof (codeunit (s))
146145endof (s:: AbstractString ) = thisind (s, ncodeunits (s))
147146
148- getindex (s:: AbstractString , i:: Integer ) = next (s, i)[1 ]
147+ function getindex (s:: AbstractString , i:: Integer )
148+ @boundscheck checkbounds (s, i)
149+ @inbounds return isvalid (s, i) ? next (s, i)[1 ] : string_index_err (s, i)
150+ end
151+
149152getindex (s:: AbstractString , i:: Colon ) = s
150153# TODO : handle other ranges with stride ±1 specially?
151154# TODO : add more @propagate_inbounds annotations?
0 commit comments