Skip to content

Commit 2091058

Browse files
authored
Fix :noshift construction of an empty SubString (#51923)
1 parent 9deee46 commit 2091058

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

base/strings/substring.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct SubString{T<:AbstractString} <: AbstractString
3737
return new(s, i-1, nextind(s,j)-i)
3838
end
3939
function SubString{T}(s::T, i::Int, j::Int, ::Val{:noshift}) where T<:AbstractString
40-
@boundscheck begin
40+
@boundscheck if !(i == j == 0)
4141
si, sj = i + 1, prevind(s, j + i + 1)
4242
@inbounds isvalid(s, si) || string_index_err(s, si)
4343
@inbounds isvalid(s, sj) || string_index_err(s, sj)

test/strings/basic.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ end
203203
@test (@views (x[3], x[1:2], x[[1,4]])) isa Tuple{Char, SubString, String}
204204
@test (@views (x[3], x[1:2], x[[1,4]])) == ('c', "ab", "ad")
205205
end
206+
207+
@testset ":noshift constructor" begin
208+
@test SubString("", 0, 0, Val(:noshift)) == ""
209+
@test SubString("abcd", 0, 1, Val(:noshift)) == "a"
210+
@test SubString("abcd", 0, 4, Val(:noshift)) == "abcd"
211+
end
206212
end
207213

208214

0 commit comments

Comments
 (0)