@@ -170,24 +170,53 @@ 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()
174
+ readline(stream, chomp::Bool=true)
175
+ readline(filename::AbstractString, chomp::Bool=true)
175
176
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.
177
+ Read a single line of text from the given I/O stream or file (defaults to `STDIN`).
178
+ Lines in the input can end in `'\\ n'` or `"\\ r\\ n"`. When reading from a file, the text is
179
+ assumed to be encoded in UTF-8.
180
+
181
+ If `chomp=false` trailing newline character(s) will be included in the output
182
+ (if reached before the end of the input); otherwise newline characters(s)
183
+ are stripped from result.
179
184
"""
180
- readline (filename:: AbstractString ) = open (readline, filename)
185
+ function readline (filename:: AbstractString , chomp:: Bool = true )
186
+ open (filename) do f
187
+ readline (f, chomp)
188
+ end
189
+ end
190
+ readline () = readline (STDIN, false )
191
+
192
+ function readline (s:: IO , chomp:: Bool = true )
193
+ line = readuntil (s, 0x0a )
194
+ i = length (line)
195
+ if ! chomp || i == 0 || line[i] != 0x0a
196
+ return String (line)
197
+ elseif i < 2 || line[i- 1 ] != 0x0d
198
+ return String (resize! (line,i- 1 ))
199
+ else
200
+ return String (resize! (line,i- 2 ))
201
+ end
202
+ end
181
203
182
204
"""
183
- readlines(stream::IO)
184
- readlines(filename::AbstractString)
205
+ readlines(stream::IO, chomp::Bool=true )
206
+ readlines(filename::AbstractString, chomp::Bool=true )
185
207
186
208
Read all lines of an I/O stream or a file as a vector of strings.
209
+ Lines in the input can end in `'\\ n'` or `"\\ r\\ n"`.
187
210
The text is assumed to be encoded in UTF-8.
188
- """
189
- readlines (filename:: AbstractString ) = open (readlines, filename)
190
211
212
+ If `chomp=false` trailing newline character(s) will be included in the output;
213
+ otherwise newline characters(s) are stripped from result.
214
+ """
215
+ function readlines (filename:: AbstractString , chomp:: Bool = true )
216
+ open (filename) do f
217
+ readlines (f, chomp)
218
+ end
219
+ end
191
220
192
221
# # byte-order mark, ntoh & hton ##
193
222
@@ -454,9 +483,6 @@ function readuntil(s::IO, t::AbstractString)
454
483
return String (take! (out))
455
484
end
456
485
457
- readline () = readline (STDIN)
458
- readline (s:: IO ) = readuntil (s, ' \n ' )
459
-
460
486
"""
461
487
readchomp(x)
462
488
@@ -519,22 +545,28 @@ readstring(filename::AbstractString) = open(readstring, filename)
519
545
520
546
type EachLine
521
547
stream:: IO
548
+ chomp:: Bool
522
549
ondone:: Function
523
- EachLine (stream) = EachLine (stream, ()-> nothing )
524
- EachLine (stream, ondone) = new (stream, ondone)
550
+ EachLine (stream, chomp ) = EachLine (stream, chomp , ()-> nothing )
551
+ EachLine (stream, chomp, ondone) = new (stream, chomp , ondone)
525
552
end
526
553
527
554
"""
528
- eachline(stream::IO)
529
- eachline(filename::AbstractString)
555
+ eachline(stream::IO, chomp::Bool=true )
556
+ eachline(filename::AbstractString, chomp::Bool=true )
530
557
531
558
Create an iterable object that will yield each line from an I/O stream or a file.
559
+ Lines in the input can end in `'\\ n'` or `"\\ r\\ n"`.
532
560
The text is assumed to be encoded in UTF-8.
561
+
562
+ If `chomp=false` trailing newline character(s) will be included in the output;
563
+ otherwise newline characters(s) are stripped from result.
533
564
"""
534
- eachline (stream:: IO ) = EachLine (stream)
535
- function eachline (filename:: AbstractString )
565
+ eachline (stream:: IO , chomp:: Bool = true ) = EachLine (stream, chomp)
566
+
567
+ function eachline (filename:: AbstractString , chomp:: Bool = true )
536
568
s = open (filename)
537
- EachLine (s, ()-> close (s))
569
+ EachLine (s, chomp, ()-> close (s))
538
570
end
539
571
540
572
start (itr:: EachLine ) = nothing
@@ -545,10 +577,11 @@ function done(itr::EachLine, nada)
545
577
itr. ondone ()
546
578
true
547
579
end
548
- next (itr:: EachLine , nada) = (readline (itr. stream), nothing )
580
+
581
+ next (itr:: EachLine , nada) = (readline (itr. stream, itr. chomp), nothing )
549
582
eltype (:: Type{EachLine} ) = String
550
583
551
- readlines (s= STDIN ) = collect (eachline (s))
584
+ readlines (s:: IO , chomp :: Bool = true ) = collect (eachline (s, chomp ))
552
585
553
586
iteratorsize (:: Type{EachLine} ) = SizeUnknown ()
554
587
0 commit comments