The docstring says “If i is out of bounds in s return i.”, however, if i is < 0 or == sizeof(s)+1, it just returns i, otherwise, it gives a bounds error.
julia> thisind("αβγdef", 11)
ERROR: BoundsError: attempt to access "αβγdef"
at index [11]
Stacktrace:
[1] thisind(::String, ::Int64) at ./strings/string.jl:104
Note: I think that returning i for 0 and sizeof(s)+1 is not a good idea, it doesn’t match the description of thisind, which is to return the valid starting index of the (possibly multibyte or multiword) character, given an index within the character.
To be consistent, it should really only return valid indexes, and if you pass an index that is not part of any character, then it should throw an exception.
Note, the documentation for prevind
is also incorrect, and one of the examples doesn’t work.
julia> prevind("αβγdef", 0)
ERROR: BoundsError: attempt to access "αβγdef"
at index [0]
Stacktrace:
[1] prevind(::String, ::Int64, ::Int64) at ./strings/basic.jl:428
(I think the behavior is the correct one, returning a 0 if you pass 1, and returning endof(str)
if you pass sizeof(str)+1
, but a boundserror for anything else).
I would think that it would make more sense for these functions to have the following rule, that either the input index or the output index should be within the string, i.e. 1 <= pos <= ncodeunits(str)
.
For nextind
, that would mean that you’d allow 0 <= pos <= ncodeunits(str)
for input, and output would range from 1 : ncodeunits(str) + 1
.
For prevind
, input would be from 1 <= pos <= ncodeunits(str) + 1
, and outputs would range from 0 : ncodeunits(str)
(which is it’s current behavior).
For thisind
, since this function seems to be designed to give you the correct starting index given any index within the range of codeunits of a particular character, it doesn’t make sense to allow either 0
or ncodeunits(str) + 1
, neither index into a character.
Therefor, I propose that nextind
be changed to give a bounds error with ncodeunits(str) + 1
as input, and thisind
be changed to give a bounds error with either 0
or ncodeunits(str) + 1
, and that the documentation be updated to match.