From 00dbba9f5ff21a544b5fccb678c23261870ee07a Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Fri, 6 Aug 2021 16:32:14 -0400 Subject: [PATCH 1/3] Allow AbstractUnitRange for string getindex --- base/strings/string.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/strings/string.jl b/base/strings/string.jl index e3a9cd56df201..e748dad318a1e 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -252,9 +252,9 @@ function getindex_continued(s::String, i::Int, u::UInt32) return reinterpret(Char, u) end -getindex(s::String, r::UnitRange{<:Integer}) = s[Int(first(r)):Int(last(r))] +getindex(s::String, r::AbstractUnitRange{<:Integer}) = s[Int(first(r)):Int(last(r))] -@inline function getindex(s::String, r::UnitRange{Int}) +@inline function getindex(s::String, r::AbstractUnitRange{Int}) isempty(r) && return "" i, j = first(r), last(r) @boundscheck begin From 04d00120dacc55bc25a8513d669be458a469c965 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Fri, 6 Aug 2021 17:43:41 -0400 Subject: [PATCH 2/3] Inlined String getindex should use UnitRange{Int} --- base/strings/string.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/strings/string.jl b/base/strings/string.jl index e748dad318a1e..c818e2e1844fb 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -254,7 +254,7 @@ end getindex(s::String, r::AbstractUnitRange{<:Integer}) = s[Int(first(r)):Int(last(r))] -@inline function getindex(s::String, r::AbstractUnitRange{Int}) +@inline function getindex(s::String, r::UnitRange{Int}) isempty(r) && return "" i, j = first(r), last(r) @boundscheck begin From c62a799c76fd0ef548e1a099668a31b879c12053 Mon Sep 17 00:00:00 2001 From: Mark Kittisopikul Date: Mon, 16 Aug 2021 19:05:33 -0400 Subject: [PATCH 3/3] Generalize getindex for AbstractString, resolve method ambiguity --- base/strings/substring.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/strings/substring.jl b/base/strings/substring.jl index 3e99cc7477446..0da09f36be931 100644 --- a/base/strings/substring.jl +++ b/base/strings/substring.jl @@ -252,4 +252,4 @@ function filter(f, s::Union{String, SubString{String}}) return String(out) end -getindex(s::AbstractString, r::UnitRange{<:Integer}) = SubString(s, r) +getindex(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, r)