File tree 4 files changed +48
-0
lines changed
4 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -782,6 +782,7 @@ export
782
782
close,
783
783
countlines,
784
784
eachline,
785
+ eachof,
785
786
eof,
786
787
fd,
787
788
fdio,
Original file line number Diff line number Diff line change @@ -1003,6 +1003,46 @@ eltype(::Type{<:EachLine}) = String
1003
1003
1004
1004
IteratorSize (:: Type{<:EachLine} ) = SizeUnknown ()
1005
1005
1006
+ struct EachOfIO{T, IOT <: IO }
1007
+ stream:: IOT
1008
+ end
1009
+
1010
+ """
1011
+ eachof(io::IO, T)
1012
+
1013
+ Return an iterable object yielding [`read(io, T)`](@ref).
1014
+
1015
+ See also: [`skipchars`](@ref), [`eachline`](@ref), [`readuntil`](@ref)
1016
+
1017
+ !!! compat "Julia 1.6"
1018
+ `eachof` requires Julia 1.6 or later.
1019
+
1020
+ # Examples
1021
+ ```jldoctest
1022
+ julia> open("my_file.txt", "w") do io
1023
+ write(io, "JuliaLang is a GitHub organization.\\ n It has many members.\\ n");
1024
+ end;
1025
+
1026
+ julia> open("my_file.txt") do io
1027
+ for c in eachof(io, Char)
1028
+ c == '\\ n' && break
1029
+ print(c)
1030
+ end
1031
+ end
1032
+ JuliaLang is a GitHub organization.
1033
+
1034
+ julia> rm("my_file.txt");
1035
+ ```
1036
+ """
1037
+ eachof (stream:: IOT , T:: Type ) where IOT<: IO = EachOfIO {T,IOT} (stream)
1038
+
1039
+ iterate (itr:: EachOfIO{T} , state= nothing ) where T =
1040
+ eof (itr. stream) ? nothing : (read (itr. stream, T), nothing )
1041
+
1042
+ eltype (:: Type{EachOfIO{T}} ) where T = T
1043
+
1044
+ IteratorSize (:: Type{<:EachOfIO} ) = SizeUnknown ()
1045
+
1006
1046
# IOStream Marking
1007
1047
# Note that these functions expect that io.mark exists for
1008
1048
# the concrete IO type. This may not be true for IO types
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ Base.read!
19
19
Base.readbytes!
20
20
Base.unsafe_read
21
21
Base.unsafe_write
22
+ Base.eachof
22
23
Base.peek
23
24
Base.position
24
25
Base.seek
Original file line number Diff line number Diff line change @@ -300,6 +300,12 @@ for (name, f) in l
300
300
301
301
cleanup ()
302
302
303
+ verbose && println (" $name eachof..." )
304
+ @test collect (eachof (io (), Char)) == Vector {Char} (text)
305
+ @test collect (eachof (io (), UInt8)) == Vector {UInt8} (text)
306
+
307
+ cleanup ()
308
+
303
309
verbose && println (" $name countlines..." )
304
310
@test countlines (io ()) == countlines (IOBuffer (text))
305
311
You can’t perform that action at this time.
0 commit comments