@@ -322,14 +322,14 @@ julia> rm("my_file.txt")
322
322
readuntil (filename:: AbstractString , args... ) = open (io-> readuntil (io, args... ), filename)
323
323
324
324
"""
325
- readline(io::IO=STDIN; chomp ::Bool=true )
326
- readline(filename::AbstractString; chomp ::Bool=true )
325
+ readline(io::IO=STDIN; keep ::Bool=false )
326
+ readline(filename::AbstractString; keep ::Bool=false )
327
327
328
328
Read a single line of text from the given I/O stream or file (defaults to `STDIN`).
329
329
When reading from a file, the text is assumed to be encoded in UTF-8. Lines in the
330
- input end with `'\\ n'` or `"\\ r\\ n"` or the end of an input stream. When `chomp ` is
331
- true (as it is by default), these trailing newline characters are removed from the
332
- line before it is returned. When `chomp ` is false , they are returned as part of the
330
+ input end with `'\\ n'` or `"\\ r\\ n"` or the end of an input stream. When `keep ` is
331
+ false (as it is by default), these trailing newline characters are removed from the
332
+ line before it is returned. When `keep ` is true , they are returned as part of the
333
333
line.
334
334
335
335
# Examples
@@ -342,22 +342,30 @@ julia> open("my_file.txt", "w") do io
342
342
julia> readline("my_file.txt")
343
343
"JuliaLang is a GitHub organization."
344
344
345
- julia> readline("my_file.txt", chomp=false )
345
+ julia> readline("my_file.txt", keep=true )
346
346
"JuliaLang is a GitHub organization.\\ n"
347
347
348
348
julia> rm("my_file.txt")
349
349
```
350
350
"""
351
- function readline (filename:: AbstractString ; chomp:: Bool = true )
351
+ function readline (filename:: AbstractString ; chomp= nothing , keep:: Bool = false )
352
+ if chomp != = nothing
353
+ keep = ! chomp
354
+ depwarn (" The `chomp=$chomp ` argument to `readline` is deprecated in favor of `keep=$keep `." , :readline )
355
+ end
352
356
open (filename) do f
353
- readline (f, chomp = chomp )
357
+ readline (f, keep = keep )
354
358
end
355
359
end
356
360
357
- function readline (s:: IO = STDIN; chomp:: Bool = true )
361
+ function readline (s:: IO = STDIN; chomp= nothing , keep:: Bool = false )
362
+ if chomp != = nothing
363
+ keep = ! chomp
364
+ depwarn (" The `chomp=$chomp ` argument to `readline` is deprecated in favor of `keep=$keep `." , :readline )
365
+ end
358
366
line = readuntil (s, 0x0a )
359
367
i = length (line)
360
- if ! chomp || i == 0 || line[i] != 0x0a
368
+ if keep || i == 0 || line[i] != 0x0a
361
369
return String (line)
362
370
elseif i < 2 || line[i- 1 ] != 0x0d
363
371
return String (resize! (line,i- 1 ))
@@ -367,8 +375,8 @@ function readline(s::IO=STDIN; chomp::Bool=true)
367
375
end
368
376
369
377
"""
370
- readlines(io::IO=STDIN; chomp ::Bool=true )
371
- readlines(filename::AbstractString; chomp ::Bool=true )
378
+ readlines(io::IO=STDIN; keep ::Bool=false )
379
+ readlines(filename::AbstractString; keep ::Bool=false )
372
380
373
381
Read all lines of an I/O stream or a file as a vector of strings. Behavior is
374
382
equivalent to saving the result of reading [`readline`](@ref) repeatedly with the same
@@ -386,20 +394,20 @@ julia> readlines("my_file.txt")
386
394
"JuliaLang is a GitHub organization."
387
395
"It has many members."
388
396
389
- julia> readlines("my_file.txt", chomp=false )
397
+ julia> readlines("my_file.txt", keep=true )
390
398
2-element Array{String,1}:
391
399
"JuliaLang is a GitHub organization.\\ n"
392
400
"It has many members.\\ n"
393
401
394
402
julia> rm("my_file.txt")
395
403
```
396
404
"""
397
- function readlines (filename:: AbstractString ; chomp :: Bool = true )
405
+ function readlines (filename:: AbstractString ; kw ... )
398
406
open (filename) do f
399
- readlines (f, chomp = chomp )
407
+ readlines (f; kw ... )
400
408
end
401
409
end
402
- readlines (s= STDIN; chomp :: Bool = true ) = collect (eachline (s, chomp = chomp ))
410
+ readlines (s= STDIN; kw ... ) = collect (eachline (s; kw ... ))
403
411
404
412
# # byte-order mark, ntoh & hton ##
405
413
@@ -797,20 +805,20 @@ read(s::IO, T::Type) = error("The IO stream does not support reading objects of
797
805
mutable struct EachLine
798
806
stream:: IO
799
807
ondone:: Function
800
- chomp :: Bool
808
+ keep :: Bool
801
809
802
- EachLine (stream:: IO = STDIN; ondone:: Function = ()-> nothing , chomp :: Bool = true ) =
803
- new (stream, ondone, chomp )
810
+ EachLine (stream:: IO = STDIN; ondone:: Function = ()-> nothing , keep :: Bool = false ) =
811
+ new (stream, ondone, keep )
804
812
end
805
813
806
814
"""
807
- eachline(io::IO=STDIN; chomp ::Bool=true )
808
- eachline(filename::AbstractString; chomp ::Bool=true )
815
+ eachline(io::IO=STDIN; keep ::Bool=false )
816
+ eachline(filename::AbstractString; keep ::Bool=false )
809
817
810
818
Create an iterable `EachLine` object that will yield each line from an I/O stream
811
819
or a file. Iteration calls [`readline`](@ref) on the stream argument repeatedly with
812
- `chomp ` passed through, determining whether trailing end-of-line characters are
813
- removed . When called with a file name, the file is opened once at the beginning of
820
+ `keep ` passed through, determining whether trailing end-of-line characters are
821
+ retained . When called with a file name, the file is opened once at the beginning of
814
822
iteration and closed at the end. If iteration is interrupted, the file will be
815
823
closed when the `EachLine` object is garbage collected.
816
824
@@ -828,11 +836,21 @@ JuliaLang is a GitHub organization. It has many members.
828
836
julia> rm("my_file.txt");
829
837
```
830
838
"""
831
- eachline (stream:: IO = STDIN; chomp:: Bool = true ) = EachLine (stream, chomp= chomp):: EachLine
839
+ function eachline (stream:: IO = STDIN; chomp= nothing , keep:: Bool = false )
840
+ if chomp != = nothing
841
+ keep = ! chomp
842
+ depwarn (" The `chomp=$chomp ` argument to `eachline` is deprecated in favor of `keep=$keep `." , :eachline )
843
+ end
844
+ EachLine (stream, keep= keep):: EachLine
845
+ end
832
846
833
- function eachline (filename:: AbstractString ; chomp:: Bool = true )
847
+ function eachline (filename:: AbstractString ; chomp= nothing , keep:: Bool = false )
848
+ if chomp != = nothing
849
+ keep = ! chomp
850
+ depwarn (" The `chomp=$chomp ` argument to `eachline` is deprecated in favor of `keep=$keep `." , :eachline )
851
+ end
834
852
s = open (filename)
835
- EachLine (s, ondone= ()-> close (s), chomp = chomp ):: EachLine
853
+ EachLine (s, ondone= ()-> close (s), keep = keep ):: EachLine
836
854
end
837
855
838
856
start (itr:: EachLine ) = nothing
@@ -841,7 +859,7 @@ function done(itr::EachLine, ::Nothing)
841
859
itr. ondone ()
842
860
true
843
861
end
844
- next (itr:: EachLine , :: Nothing ) = (readline (itr. stream, chomp = itr. chomp ), nothing )
862
+ next (itr:: EachLine , :: Nothing ) = (readline (itr. stream, keep = itr. keep ), nothing )
845
863
846
864
eltype (:: Type{EachLine} ) = String
847
865
0 commit comments