From 0dd0cd9914475ff8ab3025daa3fb3c7ee5315769 Mon Sep 17 00:00:00 2001 From: Arun Date: Fri, 12 Nov 2021 18:01:04 +0530 Subject: [PATCH 1/6] startswith for io objects --- base/strings/util.jl | 14 ++++++++++++++ test/strings/basic.jl | 3 +++ 2 files changed, 17 insertions(+) diff --git a/base/strings/util.jl b/base/strings/util.jl index cb219a9f7a8eb..f929eeb43fec4 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -57,6 +57,20 @@ function startswith(a::Union{String, SubString{String}}, end end +""" + startswith(io::IO, prefix::Union{AbstractString,Base.Chars}) + +Check if an `IO` object starts with a prefix. +""" +function Base.startswith(io::IO, prefix::Union{AbstractString,Base.Chars}) + pos = position(io) + s = _getminprefix(io, prefix) + seek(io, pos) + return startswith(s, prefix) +end +_getminprefix(io::IO, prefix::Union{AbstractString,AbstractChar}) = String(read(io, length(prefix))) +_getminprefix(io::IO, chars::Union{Tuple{Vararg{<:AbstractChar}},AbstractVector{<:AbstractChar},Set{<:AbstractChar}}) = _getminprefix(io, first(chars)) + function endswith(a::Union{String, SubString{String}}, b::Union{String, SubString{String}}) cub = ncodeunits(b) diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 2bdac1b353e1f..124f456b6871e 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -164,6 +164,9 @@ end @test endswith(y)(y) @test endswith(z, z) @test endswith(z)(z) + #40616 startswith for IO objects + io = IOBuffer("JuliaLang") + @test startswith(io, "Julia") end @testset "SubStrings and Views" begin From 104a5b39e7a786faada2bd926b184eb1e09afbd7 Mon Sep 17 00:00:00 2001 From: Arun sanganal <74652697+ArunSanganal@users.noreply.github.com> Date: Thu, 24 Mar 2022 18:26:32 +0530 Subject: [PATCH 2/6] Update base/strings/util.jl Co-authored-by: Steven G. Johnson --- base/strings/util.jl | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/base/strings/util.jl b/base/strings/util.jl index f929eeb43fec4..b999163de18d4 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -62,14 +62,19 @@ end Check if an `IO` object starts with a prefix. """ -function Base.startswith(io::IO, prefix::Union{AbstractString,Base.Chars}) - pos = position(io) - s = _getminprefix(io, prefix) - seek(io, pos) - return startswith(s, prefix) +function Base.startswith(io::IO, prefix::Base.Chars) + mark(io) + c = read(io, Char) + reset(io) + return c in prefix end -_getminprefix(io::IO, prefix::Union{AbstractString,AbstractChar}) = String(read(io, length(prefix))) -_getminprefix(io::IO, chars::Union{Tuple{Vararg{<:AbstractChar}},AbstractVector{<:AbstractChar},Set{<:AbstractChar}}) = _getminprefix(io, first(chars)) +function Base.startswith(io::IO, prefix::Union{String,SubString{String}}) + mark(io) + s = read(io, ncodeunits(prefix)) + reset(io) + return s == codeunits(prefix) +end +Base.startswith(io::IO, prefix::AbstractString) = startswith(io, String(prefix)) function endswith(a::Union{String, SubString{String}}, b::Union{String, SubString{String}}) From ea555edf915fb58b930fd5638ae40b084e0c3ce7 Mon Sep 17 00:00:00 2001 From: Arun sanganal <74652697+ArunSanganal@users.noreply.github.com> Date: Sun, 18 Dec 2022 18:44:53 +0530 Subject: [PATCH 3/6] Update basic.jl --- test/strings/basic.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 124f456b6871e..47a30ad222a81 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -165,8 +165,11 @@ end @test endswith(z, z) @test endswith(z)(z) #40616 startswith for IO objects - io = IOBuffer("JuliaLang") - @test startswith(io, "Julia") + let s = "JuliaLang", io = IOBuffer(s) + for prefix in ("Julia", "July", s^2, "Ju", 'J', 'x', ('j','J')) + @test startswith(io, prefix) == startswith(s, prefix) + end + end end @testset "SubStrings and Views" begin From 7df36beb010393284744d4638e676e7cfcfaa369 Mon Sep 17 00:00:00 2001 From: Arun sanganal <74652697+ArunSanganal@users.noreply.github.com> Date: Mon, 26 Dec 2022 21:10:04 +0530 Subject: [PATCH 4/6] news --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 6767ae24ec373..bb0beb2e8f7fd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -35,6 +35,7 @@ New library features Standard library changes ------------------------ +* `startswith` now supports seekable `IO` streams ([#43055]) #### Package Manager From 5d4123d9a1c038eb434d63c0858c769e33b58501 Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Mon, 13 Feb 2023 09:14:26 -0500 Subject: [PATCH 5/6] add cross-reference in peek --- base/essentials.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/essentials.jl b/base/essentials.jl index a9794f372a0d5..14ccce0d125fb 100644 --- a/base/essentials.jl +++ b/base/essentials.jl @@ -946,7 +946,7 @@ function popfirst! end peek(stream[, T=UInt8]) Read and return a value of type `T` from a stream without advancing the current position -in the stream. +in the stream. See also [`startswith(stream, char_or_string)`](@ref). # Examples From 22f5b2ef16fbce0580ff43c55be6e427b861a09e Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Mon, 13 Feb 2023 09:15:42 -0500 Subject: [PATCH 6/6] Update base/strings/util.jl --- base/strings/util.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/strings/util.jl b/base/strings/util.jl index 36ca4cf572f55..7a42d7fecfc91 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -70,7 +70,7 @@ end """ startswith(io::IO, prefix::Union{AbstractString,Base.Chars}) -Check if an `IO` object starts with a prefix. +Check if an `IO` object starts with a prefix. See also [`peek`](@ref). """ function Base.startswith(io::IO, prefix::Base.Chars) mark(io)