@@ -170,24 +170,48 @@ The text is assumed to be encoded in UTF-8.
170
170
readuntil (filename:: AbstractString , args... ) = open (io-> readuntil (io, args... ), filename)
171
171
172
172
"""
173
- readline(stream::IO=STDIN)
174
- readline(filename::AbstractString)
173
+ readline(stream::IO=STDIN; chomp::Bool=true)
174
+ readline(filename::AbstractString; chomp::Bool=true)
175
+
176
+ Read a single line of text from the given I/O stream or file (defaults to `STDIN`).
177
+ When reading from a file, the text is assumed to be encoded in UTF-8. Lines in the
178
+ input end with `'\\ n'` or `"\\ r\\ n"` or the end of an input stream. When `chomp` is
179
+ true (as it is by default), these trailing newline characters are removed from the
180
+ line before it is returned. When `chomp` is false, they are returned as part of the
181
+ line.
182
+ """
183
+ function readline (filename:: AbstractString ; chomp:: Bool = true )
184
+ open (filename) do f
185
+ readline (f, chomp= chomp)
186
+ end
187
+ end
175
188
176
- Read a single line of text, including a trailing newline character (if one is reached before
177
- the end of the input), from the given I/O stream or file (defaults to `STDIN`).
178
- When reading from a file, the text is assumed to be encoded in UTF-8.
179
- """
180
- readline (filename:: AbstractString ) = open (readline, filename)
189
+ function readline (s:: IO = STDIN; chomp:: Bool = true )
190
+ line = readuntil (s, 0x0a )
191
+ i = length (line)
192
+ if ! chomp || i == 0 || line[i] != 0x0a
193
+ return String (line)
194
+ elseif i < 2 || line[i- 1 ] != 0x0d
195
+ return String (resize! (line,i- 1 ))
196
+ else
197
+ return String (resize! (line,i- 2 ))
198
+ end
199
+ end
181
200
182
201
"""
183
- readlines(stream::IO)
184
- readlines(filename::AbstractString)
202
+ readlines(stream::IO=STDIN; chomp::Bool=true )
203
+ readlines(filename::AbstractString; chomp::Bool=true )
185
204
186
- Read all lines of an I/O stream or a file as a vector of strings.
187
- The text is assumed to be encoded in UTF-8.
205
+ Read all lines of an I/O stream or a file as a vector of strings. Behavior is
206
+ equivalent to saving the result of reading `readline` repeatedly with the same
207
+ arguments and saving the resulting lines as a vector of strings.
188
208
"""
189
- readlines (filename:: AbstractString ) = open (readlines, filename)
190
-
209
+ function readlines (filename:: AbstractString ; chomp:: Bool = true )
210
+ open (filename) do f
211
+ readlines (f, chomp= chomp)
212
+ end
213
+ end
214
+ readlines (s:: IO = STDIN; chomp:: Bool = true ) = collect (eachline (s, chomp= chomp))
191
215
192
216
# # byte-order mark, ntoh & hton ##
193
217
@@ -454,9 +478,6 @@ function readuntil(s::IO, t::AbstractString)
454
478
return String (take! (out))
455
479
end
456
480
457
- readline () = readline (STDIN)
458
- readline (s:: IO ) = readuntil (s, ' \n ' )
459
-
460
481
"""
461
482
readchomp(x)
462
483
@@ -520,35 +541,39 @@ readstring(filename::AbstractString) = open(readstring, filename)
520
541
type EachLine
521
542
stream:: IO
522
543
ondone:: Function
523
- EachLine (stream) = EachLine (stream, ()-> nothing )
524
- EachLine (stream, ondone) = new (stream, ondone)
544
+ chomp:: Bool
545
+
546
+ EachLine (stream:: IO = STDIN; ondone:: Function = ()-> nothing , chomp:: Bool = true ) =
547
+ new (stream, ondone, chomp)
525
548
end
526
549
527
550
"""
528
- eachline(stream::IO)
529
- eachline(filename::AbstractString)
551
+ eachline(stream::IO=STDIN; chomp::Bool=true )
552
+ eachline(filename::AbstractString; chomp::Bool=true )
530
553
531
- Create an iterable object that will yield each line from an I/O stream or a file.
532
- The text is assumed to be encoded in UTF-8.
554
+ Create an iterable `EachLine` object that will yield each line from an I/O stream
555
+ or a file. Iteration calls `readline` on the stream argument repeatedly with
556
+ `chomp` passed through, determining whether trailing end-of-line characters are
557
+ removed. When called with a file name, the file is opened once at the beginning of
558
+ iteration and closed at the end. If iteration is interrupted, the file will be
559
+ closed when the `EachLine` object is garbage collected.
533
560
"""
534
- eachline (stream:: IO ) = EachLine (stream)
535
- function eachline (filename:: AbstractString )
561
+ eachline (stream:: IO = STDIN; chomp:: Bool = true ) = EachLine (stream, chomp= chomp)
562
+
563
+ function eachline (filename:: AbstractString ; chomp:: Bool = true )
536
564
s = open (filename)
537
- EachLine (s, ()-> close (s))
565
+ EachLine (s, ondone = ()-> close (s), chomp = chomp )
538
566
end
539
567
540
568
start (itr:: EachLine ) = nothing
541
- function done (itr:: EachLine , nada)
542
- if ! eof (itr. stream)
543
- return false
544
- end
569
+ function done (itr:: EachLine , :: Void )
570
+ eof (itr. stream) || return false
545
571
itr. ondone ()
546
572
true
547
573
end
548
- next (itr:: EachLine , nada) = (readline (itr. stream), nothing )
549
- eltype (:: Type{EachLine} ) = String
574
+ next (itr:: EachLine , :: Void ) = (readline (itr. stream, chomp= itr. chomp), nothing )
550
575
551
- readlines (s = STDIN ) = collect ( eachline (s))
576
+ eltype ( :: Type{EachLine} ) = String
552
577
553
578
iteratorsize (:: Type{EachLine} ) = SizeUnknown ()
554
579
0 commit comments