From cf00550f955abd408bf0f478882c57887522f67e Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Mon, 23 Oct 2023 14:19:11 +0200 Subject: [PATCH] fix eltype for partition iterators of substrings (#51773) Fixes https://github.com/JuliaLang/julia/issues/51771 The convert method that asserts in #51771 is arguably still faulty though. --- base/strings/util.jl | 2 ++ test/iterators.jl | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/base/strings/util.jl b/base/strings/util.jl index fae40cb568842..a3f8e9a4773a6 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -575,6 +575,8 @@ end # Specialization for partition(s,n) to return a SubString eltype(::Type{PartitionIterator{T}}) where {T<:AbstractString} = SubString{T} +# SubStrings do not nest +eltype(::Type{PartitionIterator{T}}) where {T<:SubString} = T function iterate(itr::PartitionIterator{<:AbstractString}, state = firstindex(itr.c)) state > ncodeunits(itr.c) && return nothing diff --git a/test/iterators.jl b/test/iterators.jl index 59588bdac9684..a60ec32bb9ac0 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -1001,3 +1001,7 @@ end end @test v == () end + +@testset "collect partition substring" begin + @test collect(Iterators.partition(lstrip("01111", '0'), 2)) == ["11", "11"] +end