[Possible Bug?] Weird behavior with small negative indices in findnext()

Take a look at this, y’all, and tell me if this is a bug – I’ll step up later this week to fix it and write some @tests for it if so.

julia> versioninfo()
Julia Version 1.2.0
Commit c6da87ff4b (2019-08-20 00:03 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = "C:\Users\Andre\AppData\Local\atom\app-1.40.1\atom.exe"  -a
  JULIA_NUM_THREADS = 4


julia> 

julia> 

julia> 

julia> findnext("Kitten", "Kitten", 1)
1:6

julia> # As expected. But...

julia> findnext("Kitten", "Kitten", 0)
1:6

julia> # Weird, but not totally out of the box.

julia> findnext("Kitten", "Kitten", -1)
1:6

julia> # Wait. What?

julia> findnext("Kitten", "Kitten", -2)
1:6

julia> findnext("Kitten", "Kitten", -3)
1:6

julia> findnext("Kitten", "Kitten", -4)
1:6

julia> findnext("Kitten", "Kitten", -5)
ERROR: BoundsError: attempt to access 6-element Array{UInt8,1} at index [0]
Stacktrace:
 [1] getindex at .\array.jl:728 [inlined]
 [2] _nthbyte at .\strings\search.jl:148 [inlined]
 [3] _searchindex(::Array{UInt8,1}, ::Array{UInt8,1}, ::Int64) at .\strings\search.jl:185
 [4] _searchindex(::String, ::String, ::Int64) at .\strings\search.jl:153
 [5] _search(::String, ::String, ::Int64) at .\strings\search.jl:220
 [6] findnext(::String, ::String, ::Int64) at .\strings\search.jl:256
 [7] top-level scope at none:0

julia> # WTF.

julia> findnext("supercalifragilisticexpialidocious", "supercalifragilisticexpialidocious", -5)
1:34

julia> findnext("supercalifragilisticexpialidocious", "supercalifragilisticexpialidocious", -10)
1:34

julia> findnext("supercalifragilisticexpialidocious", "supercalifragilisticexpialidocious", -15)
1:34

julia> findnext("supercalifragilisticexpialidocious", "supercalifragilisticexpialidocious", -20)
ERROR: BoundsError: attempt to access 34-element Array{UInt8,1} at index [-16]
Stacktrace:
 [1] getindex at .\array.jl:728 [inlined]
 [2] _nthbyte at .\strings\search.jl:148 [inlined]
 [3] _searchindex(::Array{UInt8,1}, ::Array{UInt8,1}, ::Int64) at .\strings\search.jl:189
 [4] _searchindex(::String, ::String, ::Int64) at .\strings\search.jl:153
 [5] _search(::String, ::String, ::Int64) at .\strings\search.jl:220
 [6] findnext(::String, ::String, ::Int64) at .\strings\search.jl:256
 [7] top-level scope at none:0

julia> # Seems like we can index up to - floor(half(array)). But I thought we weren't using negative indices for strings?     

Is this normal behavior?

Further thinking:

IIRC, Strings are stated in the Julia manual IIRC to always have a first index of 1.

This may or may not be the case for other iterables, but since it is the case for Strings, I do think that either throwing a BoundsError for a non-positive argument, or making any non-positive argument reroute to 1 so that any negative number should work, is wise. This behavior is counterintuitive otherwise, because as it is there’s some arbitrary negative Int that once you pass it you get BoundsErrors.

That does seem buggy. Do you mind filing an issue on GitHub?

Done. :slight_smile:

https://github.com/JuliaLang/julia/issues/33100