Skip to content

Commit d887fb6

Browse files
thisind(s, i): return ncodeunits(s) + 1 if i > ncodeunits(s)
1 parent 05f8e42 commit d887fb6

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

base/strings/basic.jl

+13-7
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ end
237237
## Generic indexing functions ##
238238

239239
"""
240-
thisind(str::AbstractString, i::Integer)
240+
thisind(s::AbstractString, i::Integer)
241241
242-
Get the largest valid string index at or before `i`.
243-
Returns `0` if there is no valid string index at or before `i`.
244-
Returns `endof(str)` if `i≥endof(str)`.
242+
If `i` is the index into a character in `s` then `thisind` returns the index of the
243+
start of that character. If `i < start(s)` then it returns `start(s) - 1`.
244+
If `i > ncodeunits(s)` then it returns `ncodeunits(s) + 1`.
245245
246246
# Examples
247247
```jldoctest
@@ -257,15 +257,21 @@ julia> thisind("αβγdef", 3)
257257
julia> thisind("αβγdef", 4)
258258
3
259259
260-
julia> thisind("αβγdef", 20)
260+
julia> thisind("αβγdef", 9)
261261
9
262+
263+
julia> thisind("αβγdef", 10)
264+
10
265+
266+
julia> thisind("αβγdef", 20)
267+
10
262268
"""
263269
function thisind(s::AbstractString, i::Integer)
264270
j = Int(i)
265271
isvalid(s, j) && return j
266272
j < start(s) && return 0
267-
e = endof(s)
268-
j >= endof(s) && return e
273+
n = ncodeunits(s)
274+
j > n && return n + 1
269275
prevind(s, j)
270276
end
271277

base/strings/string.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ end
123123
function thisind(s::String, i::Integer)
124124
j = Int(i)
125125
j < 1 && return 0
126-
e = endof(s)
127-
j >= e && return e
126+
n = ncodeunits(s)
127+
j > n && return n + 1
128128
@inbounds while j > 0 && is_valid_continuation(codeunit(s,j))
129129
j -= 1
130130
end

base/strings/types.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,16 @@ end
8888

8989
function thisind(s::SubString{String}, i::Integer)
9090
j = Int(i)
91-
j < 1 && return 0
92-
e = endof(s)
93-
j >= e && return e
91+
j < start(s) && return 0
92+
n = ncodeunits(s)
93+
j > n && return n + 1
9494
offset = s.offset
9595
str = s.string
9696
j += offset
9797
@inbounds while j > offset && is_valid_continuation(codeunit(str, j))
9898
j -= 1
9999
end
100-
j-offset
100+
j - offset
101101
end
102102

103103
nextind(s::SubString, i::Integer) = nextind(s.string, i+s.offset)-s.offset

test/strings/basic.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -580,13 +580,14 @@ end
580580
@test thisind(s, 6) == 6
581581
@test thisind(s, 15) == 15
582582
@test thisind(s, 16) == 15
583-
@test thisind(s, 30) == 15
583+
@test thisind(s, 17) == 17
584+
@test thisind(s, 30) == 17
584585
end
585586
end
586587

587588
let strs = Any["", s"", SubString("123", 2, 1), SubString(s"123", 2, 1)]
588589
for s in strs, i in -2:2
589-
@test thisind(s, i) == 0
590+
@test thisind(s, i) == (i > 0)
590591
end
591592
end
592593
end

0 commit comments

Comments
 (0)