Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement StringPairs for efficient pairs(::AbstractString) #51631

Merged
merged 8 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using .Base:
SizeUnknown, HasLength, HasShape, IsInfinite, EltypeUnknown, HasEltype, OneTo,
@propagate_inbounds, @isdefined, @boundscheck, @inbounds, Generator, IdDict,
AbstractRange, AbstractUnitRange, UnitRange, LinearIndices, TupleOrBottom,
(:), |, +, -, *, !==, !, ==, !=, <=, <, >, >=, missing,
(:), |, +, -, *, !==, !, ==, !=, <=, <, >, >=, =>, missing,
any, _counttuple, eachindex, ntuple, zero, prod, reduce, in, firstindex, lastindex,
tail, fieldtypes, min, max, minimum, zero, oneunit, promote, promote_shape
using Core: @doc
Expand Down Expand Up @@ -1572,4 +1572,39 @@ only(x::NamedTuple) = throw(
ArgumentError("NamedTuple contains $(length(x)) elements, must contain exactly 1 element")
)

"""
IterableStatePairs(x)

This internal type is returned by [`pairs`](@ref), when the key is the same as
the state of `iterate`. This allows the iterator to determine the key => value
pairs by only calling iterate on the values.

"""
struct IterableStatePairs{T}
x::T
end

IteratorSize(::Type{<:IterableStatePairs{T}}) where T = IteratorSize(T)
length(x::IterableStatePairs) = length(x.x)
Base.eltype(::Type{IterableStatePairs{T}}) where T = Pair{<:Any, eltype(T)}

function iterate(x::IterableStatePairs, state=first(keys(x.x)))
it = iterate(x.x, state)
it === nothing && return nothing
(state => first(it), last(it))
end

reverse(x::IterableStatePairs) = IterableStatePairs(Iterators.reverse(x.x))
reverse(x::IterableStatePairs{<:Iterators.Reverse}) = IterableStatePairs(x.x.itr)

function iterate(x::IterableStatePairs{<:Iterators.Reverse}, state=last(keys(x.x.itr)))
it = iterate(x.x, state)
it === nothing && return nothing
(state => first(it), last(it))
end

# According to the docs of iterate(::AbstractString), the iteration state must
# be the same as the keys, so this is a valid optimization (see #51631)
pairs(s::AbstractString) = IterableStatePairs(s)

end
2 changes: 1 addition & 1 deletion base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ is_valid_continuation(c) = c & 0xc0 == 0x80
b = @inbounds codeunit(s, i)
u = UInt32(b) << 24
between(b, 0x80, 0xf7) || return reinterpret(Char, u), i+1
return iterate_continued(s, i, u)
return @noinline iterate_continued(s, i, u)
end

# duck-type s so that external UTF-8 string packages like StringViews can hook in
Expand Down
14 changes: 14 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,20 @@ end
@test collect(Iterators.partition(lstrip("01111", '0'), 2)) == ["11", "11"]
end

@testset "IterableStringPairs" begin
for s in ["", "a", "abcde", "γ", "∋γa"]
for T in (String, SubString, GenericString)
sT = T(s)
p = pairs(sT)
@test collect(p) == [k=>v for (k,v) in zip(keys(sT), sT)]
rv = Iterators.reverse(p)
@test collect(rv) == reverse([k=>v for (k,v) in zip(keys(sT), sT)])
rrv = Iterators.reverse(rv)
@test collect(rrv) == collect(p)
end
end
end

let itr = (i for i in 1:9) # Base.eltype == Any
@test first(Iterators.partition(itr, 3)) isa Vector{Any}
@test collect(zip(repeat([Iterators.Stateful(itr)], 3)...)) == [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
Expand Down