# Documentation for \`thisind\` doesn't match behavior

**URL:** <https://discourse.julialang.org/t/documentation-for-thisind-doesnt-match-behavior/8239>\
**Category:** Internals & Design\
**Created:** [January 9, 2018, 2:38am UTC](https://discourse.julialang.org/t/documentation-for-thisind-doesnt-match-behavior/8239 "2018-01-09T02:38:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 9, 2018, 2:38am UTC](https://discourse.julialang.org/t/documentation-for-thisind-doesnt-match-behavior/8239/1 "2018-01-09T02:38:57Z")

</div>

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
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.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 9, 2018, 2:59am UTC](https://discourse.julialang.org/t/documentation-for-thisind-doesnt-match-behavior/8239/2 "2018-01-09T02:59:46Z")

</div>

Note, the documentation for `prevind` is also incorrect, and one of the examples doesn’t work.

```julia
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).

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 9, 2018, 9:45pm UTC](https://discourse.julialang.org/t/documentation-for-thisind-doesnt-match-behavior/8239/3 "2018-01-09T21:45:01Z")

</div>

[https://github.com/JuliaLang/julia/issues/25478](https://github.com/JuliaLang/julia/issues/25478)

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [January 12, 2018, 2:16am UTC](https://discourse.julialang.org/t/documentation-for-thisind-doesnt-match-behavior/8239/4 "2018-01-12T02:16:27Z")

</div>

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.
