From 4e1759cbc748dbdb672de791e2d58bbd3953849d 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. (cherry picked from commit cf00550f955abd408bf0f478882c57887522f67e) --- 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 bd4da03ce1571..1154690ee846b 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -571,6 +571,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