-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathio.cr
1267 lines (1133 loc) · 31 KB
/
io.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "c/stdio"
require "c/errno"
{% unless flag?(:win32) %}
require "c/unistd"
{% end %}
# The `IO` class is the basis for all input and output in Crystal.
#
# This class is inherited by types like `File`, `Socket` and `IO::Memory` and
# provides many useful methods for reading from and writing to an IO, like `print`, `puts`,
# `gets` and `printf`.
#
# The only requirement for a type including the `IO` module is to define
# these two methods:
#
# * `read(slice : Bytes)`: read at most *slice.size* bytes from IO into *slice* and return the number of bytes read
# * `write(slice : Bytes)`: write the whole *slice* into the IO
#
# For example, this is a simple `IO` on top of a `Bytes`:
#
# ```
# class SimpleSliceIO < IO
# def initialize(@slice : Bytes)
# end
#
# def read(slice : Bytes)
# slice.size.times { |i| slice[i] = @slice[i] }
# @slice += slice.size
# slice.size
# end
#
# def write(slice : Bytes) : Nil
# slice.size.times { |i| @slice[i] = slice[i] }
# @slice += slice.size
# end
# end
#
# slice = Slice.new(9) { |i| ('a'.ord + i).to_u8 }
# String.new(slice) # => "abcdefghi"
#
# io = SimpleSliceIO.new(slice)
# io.gets(3) # => "abc"
# io.print "xyz"
# String.new(slice) # => "abcxyzghi"
# ```
#
# ### Encoding
#
# An `IO` can be set an encoding with the `#set_encoding` method. When this is
# set, all string operations (`gets`, `gets_to_end`, `read_char`, `<<`, `print`, `puts`
# `printf`) will write in the given encoding, and read from the given encoding.
# Byte operations (`read`, `write`, `read_byte`, `write_byte`, `getb_to_end`) never do
# encoding/decoding operations.
#
# If an encoding is not set, the default one is UTF-8.
#
# Mixing string and byte operations might not give correct results and should be
# avoided, as string operations might need to read extra bytes in order to get characters
# in the given encoding.
abstract class IO
# Default size used for generic stream buffers.
DEFAULT_BUFFER_SIZE = 32768
# Argument to a `seek` operation.
enum Seek
# Seeks to an absolute location
Set = 0
# Seeks to a location relative to the current location
# in the stream
Current = 1
# Seeks to a location relative to the end of the stream
# (you probably want a negative value for the amount)
End = 2
end
@encoding : EncodingOptions?
@encoder : Encoder?
@decoder : Decoder?
# Reads at most *slice.size* bytes from this `IO` into *slice*.
# Returns the number of bytes read, which is 0 if and only if there is no
# more data to read (so checking for 0 is the way to detect end of file).
#
# ```
# io = IO::Memory.new "hello"
# slice = Bytes.new(4)
# io.read(slice) # => 4
# slice # => Bytes[104, 101, 108, 108]
# io.read(slice) # => 1
# slice # => Bytes[111, 101, 108, 108]
# io.read(slice) # => 0
# ```
abstract def read(slice : Bytes)
# Writes the contents of *slice* into this `IO`.
#
# ```
# io = IO::Memory.new
# slice = Bytes.new(4) { |i| ('a'.ord + i).to_u8 }
# io.write(slice)
# io.to_s # => "abcd"
# ```
abstract def write(slice : Bytes) : Nil
# Closes this `IO`.
#
# `IO` defines this is a no-op method, but including types may override.
def close
end
# Returns `true` if this `IO` is closed.
#
# `IO` defines returns `false`, but including types may override.
def closed? : Bool
false
end
protected def check_open
raise IO::Error.new "Closed stream" if closed?
end
# Flushes buffered data, if any.
#
# `IO` defines this is a no-op method, but including types may override.
def flush
end
# Creates a pair of pipe endpoints (connected to each other)
# and returns them as a two-element `Tuple`.
#
# ```
# reader, writer = IO.pipe
# writer.puts "hello"
# writer.puts "world"
# reader.gets # => "hello"
# reader.gets # => "world"
# ```
def self.pipe(read_blocking = false, write_blocking = false) : {IO::FileDescriptor, IO::FileDescriptor}
Crystal::System::FileDescriptor.pipe(read_blocking, write_blocking)
end
# Creates a pair of pipe endpoints (connected to each other) and passes them
# to the given block. Both endpoints are closed after the block.
#
# ```
# IO.pipe do |reader, writer|
# writer.puts "hello"
# writer.puts "world"
# reader.gets # => "hello"
# reader.gets # => "world"
# end
# ```
def self.pipe(read_blocking = false, write_blocking = false, &)
r, w = IO.pipe(read_blocking, write_blocking)
begin
yield r, w
ensure
w.flush
r.close
w.close
end
end
# Writes the given object into this `IO`.
# This ends up calling `to_s(io)` on the object.
#
# ```
# io = IO::Memory.new
# io << 1
# io << '-'
# io << "Crystal"
# io.to_s # => "1-Crystal"
# ```
def <<(obj) : self
obj.to_s self
self
end
# Same as `<<`.
#
# ```
# io = IO::Memory.new
# io.print 1
# io.print '-'
# io.print "Crystal"
# io.to_s # => "1-Crystal"
# ```
def print(obj : _) : Nil
self << obj
nil
end
# Writes the given objects into this `IO` by invoking `to_s(io)`
# on each of the objects.
#
# ```
# io = IO::Memory.new
# io.print 1, '-', "Crystal"
# io.to_s # => "1-Crystal"
# ```
def print(*objects : _) : Nil
objects.each do |obj|
print obj
end
nil
end
# Writes *string* to this `IO`, followed by a newline character
# unless the string already ends with one.
#
# ```
# io = IO::Memory.new
# io.puts "hello\n"
# io.puts "world"
# io.to_s # => "hello\nworld\n"
# ```
def puts(string : String) : Nil
self << string
puts unless string.ends_with?('\n')
nil
end
# Writes *obj* to this `IO`, followed by a newline character.
#
# ```
# io = IO::Memory.new
# io.puts 1
# io.puts "Crystal"
# io.to_s # => "1\nCrystal\n"
# ```
def puts(obj : _) : Nil
self << obj
puts
end
# Writes a newline character.
#
# ```
# io = IO::Memory.new
# io.puts
# io.to_s # => "\n"
# ```
def puts : Nil
print '\n'
nil
end
# Writes *objects* to this `IO`, each followed by a newline character unless
# the object is a `String` and already ends with a newline.
#
# ```
# io = IO::Memory.new
# io.puts 1, '-', "Crystal"
# io.to_s # => "1\n-\nCrystal\n"
# ```
def puts(*objects : _) : Nil
objects.each do |obj|
puts obj
end
nil
end
# Writes a formatted string to this IO.
# For details on the format string, see top-level `::printf`.
def printf(format_string, *args) : Nil
printf format_string, args
end
# :ditto:
def printf(format_string, args : Array | Tuple) : Nil
String::Formatter(typeof(args)).new(format_string, args, self).format
end
# Reads a single byte from this `IO`. Returns `nil` if there is no more
# data to read.
#
# ```
# io = IO::Memory.new "a"
# io.read_byte # => 97
# io.read_byte # => nil
# ```
def read_byte : UInt8?
byte = uninitialized UInt8
if read(Slice.new(pointerof(byte), 1)) == 1
byte
else
nil
end
end
# Reads a single `Char` from this `IO`. Returns `nil` if there is no
# more data to read.
#
# ```
# io = IO::Memory.new "あ"
# io.read_char # => 'あ'
# io.read_char # => nil
# ```
def read_char : Char?
peek = self.peek unless decoder
info = read_char_with_bytesize(peek)
info ? info[0] : nil
end
# :nodoc:
# See also: `Char::Reader#decode_char_at`.
private def read_char_with_bytesize(peek = nil)
first = peek_or_read_utf8(peek, 0)
return nil unless first
first = first.to_u32
if first < 0x80
return first.unsafe_chr, 1
end
if first < 0xc2
raise InvalidByteSequenceError.new("Unexpected byte 0x#{first.to_s(16)} in UTF-8 byte sequence")
end
second = peek_or_read_utf8_masked(peek, 1)
if first < 0xe0
return ((first << 6) &+ (second &- 0x3080)).unsafe_chr, 2
end
third = peek_or_read_utf8_masked(peek, 2)
if first < 0xf0
if first == 0xe0 && second < 0xa0
raise InvalidByteSequenceError.new("Overlong UTF-8 encoding")
end
if first == 0xed && second >= 0xa0
raise InvalidByteSequenceError.new("Invalid UTF-8 codepoint")
end
return ((first << 12) &+ (second << 6) &+ (third &- 0xE2080)).unsafe_chr, 3
end
if first < 0xf5
if first == 0xf0 && second < 0x90
raise InvalidByteSequenceError.new("Overlong UTF-8 encoding")
end
if first == 0xf4 && second >= 0x90
raise InvalidByteSequenceError.new("Invalid UTF-8 codepoint")
end
fourth = peek_or_read_utf8_masked(peek, 3)
return ((first << 18) &+ (second << 12) &+ (third << 6) &+ (fourth &- 0x3C82080)).unsafe_chr, 4
end
raise InvalidByteSequenceError.new("Unexpected byte 0x#{first.to_s(16)} in UTF-8 byte sequence")
end
private def peek_or_read_utf8(peek, index)
if peek && (byte = peek[index]?)
skip(1)
byte
else
read_utf8_byte
end
end
private def peek_or_read_utf8_masked(peek, index)
byte = peek_or_read_utf8(peek, index) || raise InvalidByteSequenceError.new("Incomplete UTF-8 byte sequence")
if (byte & 0xc0) != 0x80
raise InvalidByteSequenceError.new("Unexpected continuation byte 0x#{byte.to_s(16)} in UTF-8 byte sequence")
end
byte.to_u32
end
# Reads a single decoded UTF-8 byte from this `IO`.
# Returns `nil` if there is no more data to read.
#
# If no encoding is set, this is the same as `#read_byte`.
#
# ```
# bytes = "你".encode("GB2312") # => Bytes[196, 227]
#
# io = IO::Memory.new(bytes)
# io.set_encoding("GB2312")
# io.read_utf8_byte # => 228
# io.read_utf8_byte # => 189
# io.read_utf8_byte # => 160
# io.read_utf8_byte # => nil
#
# "你".bytes # => [228, 189, 160]
# ```
def read_utf8_byte : UInt8?
if decoder = decoder()
decoder.read_byte(self)
else
read_byte
end
end
# Reads UTF-8 decoded bytes into the given *slice*.
# Returns the number of UTF-8 bytes read.
#
# If no encoding is set, this is the same as `#read(slice)`.
#
# ```
# bytes = "你".encode("GB2312") # => Bytes[196, 227]
#
# io = IO::Memory.new(bytes)
# io.set_encoding("GB2312")
#
# buffer = uninitialized UInt8[1024]
# bytes_read = io.read_utf8(buffer.to_slice) # => 3
# buffer.to_slice[0, bytes_read] # => Bytes[228, 189, 160]
#
# "你".bytes # => [228, 189, 160]
# ```
def read_utf8(slice : Bytes)
if decoder = decoder()
decoder.read_utf8(self, slice)
else
read(slice)
end
end
# Reads an UTF-8 encoded string of exactly *bytesize* bytes.
# Raises `EOFError` if there are not enough bytes to build
# the string.
#
# ```
# io = IO::Memory.new("hello world")
# io.read_string(5) # => "hello"
# io.read_string(1) # => " "
# io.read_string(6) # raises IO::EOFError
# ```
def read_string(bytesize : Int) : String
return "" if bytesize == 0
String.new(bytesize) do |ptr|
if decoder = decoder()
read = decoder.read_utf8(self, Slice.new(ptr, bytesize))
if read != bytesize
raise IO::EOFError.new
end
else
read_fully(Slice.new(ptr, bytesize))
end
{bytesize, 0}
end
end
# Peeks into this IO, if possible.
#
# It returns:
# - `nil` if this IO isn't peekable at this moment or at all
# - an empty slice if it is, but EOF was reached
# - a non-empty slice if some data can be peeked
#
# The returned bytes are only valid data until a next call
# to any method that reads from this IO is invoked.
#
# By default this method returns `nil`, but IO implementations
# that provide buffering or wrap other IOs should override
# this method.
def peek : Bytes?
nil
end
# Writes the contents of *slice*, interpreted as a sequence of UTF-8 or ASCII
# characters, into this `IO`. The contents are transcoded into this `IO`'s
# current encoding.
#
# ```
# bytes = "你".to_slice # => Bytes[228, 189, 160]
#
# io = IO::Memory.new
# io.set_encoding("GB2312")
# io.write_string(bytes)
# io.to_slice # => Bytes[196, 227]
#
# "你".encode("GB2312") # => Bytes[196, 227]
# ```
def write_string(slice : Bytes) : Nil
if encoder = encoder()
encoder.write(self, slice)
else
write(slice)
end
nil
end
# :ditto:
@[Deprecated("Use `#write_string` instead.")]
def write_utf8(slice : Bytes) : Nil
write_string(slice)
end
private def encoder
if encoding = @encoding
@encoder ||= Encoder.new(encoding)
else
nil
end
end
private def decoder
if encoding = @encoding
@decoder ||= Decoder.new(encoding)
else
nil
end
end
# Tries to read exactly `slice.size` bytes from this `IO` into *slice*.
# Raises `EOFError` if there aren't `slice.size` bytes of data.
#
# ```
# io = IO::Memory.new "123451234"
# slice = Bytes.new(5)
# io.read_fully(slice) # => 5
# slice # => Bytes[49, 50, 51, 52, 53]
# io.read_fully(slice) # raises IO::EOFError
# ```
def read_fully(slice : Bytes) : Int32
read_fully?(slice) || raise(EOFError.new)
end
# Tries to read exactly `slice.size` bytes from this `IO` into *slice*.
# Returns `nil` if there aren't `slice.size` bytes of data, otherwise
# returns the number of bytes read.
#
# ```
# io = IO::Memory.new "123451234"
# slice = Bytes.new(5)
# io.read_fully?(slice) # => 5
# slice # => Bytes[49, 50, 51, 52, 53]
# io.read_fully?(slice) # => nil
# ```
def read_fully?(slice : Bytes) : Int32?
count = slice.size
while slice.size > 0
read_bytes = read slice
return nil if read_bytes == 0
slice += read_bytes
end
count
end
# Reads the rest of this `IO` data as a `String`.
#
# ```
# io = IO::Memory.new "hello world"
# io.gets_to_end # => "hello world"
# io.gets_to_end # => ""
# ```
def gets_to_end : String
String.build do |str|
if decoder = decoder()
while true
decoder.read(self)
break if decoder.out_slice.empty?
decoder.write(str)
end
else
IO.copy(self, str)
end
end
end
# Reads the rest of this `IO` data as a writable `Bytes`.
#
# ```
# io = IO::Memory.new Bytes[0, 1, 3, 6, 10, 15]
# io.getb_to_end # => Bytes[0, 1, 3, 6, 10, 15]
# io.getb_to_end # => Bytes[]
# ```
def getb_to_end : Bytes
io = IO::Memory.new
IO.copy(self, io)
io.to_slice
end
# Reads a line from this `IO`. A line is terminated by the `\n` character.
# Returns `nil` if called at the end of this `IO`.
#
# By default the newline is removed from the returned string,
# unless *chomp* is `false`.
#
# ```
# io = IO::Memory.new "hello\nworld\nfoo\n"
# io.gets # => "hello"
# io.gets(chomp: false) # => "world\n"
# io.gets # => "foo"
# io.gets # => nil
# ```
def gets(chomp = true) : String?
gets '\n', chomp: chomp
end
# Reads a line of at most *limit* bytes from this `IO`.
# A line is terminated by the `\n` character.
# Returns `nil` if called at the end of this `IO`.
#
# ```
# io = IO::Memory.new "hello\nworld"
# io.gets(3) # => "hel"
# io.gets(3) # => "lo\n"
# io.gets(3) # => "wor"
# io.gets(3) # => "ld"
# io.gets(3) # => nil
# ```
def gets(limit : Int, chomp = false) : String?
gets '\n', limit: limit, chomp: chomp
end
# Reads until *delimiter* is found, or the end of the `IO` is reached.
# Returns `nil` if called at the end of this `IO`.
#
# ```
# io = IO::Memory.new "hello\nworld"
# io.gets('o') # => "hello"
# io.gets('r') # => "\nwor"
# io.gets('z') # => "ld"
# io.gets('w') # => nil
# ```
def gets(delimiter : Char, chomp = false) : String?
gets delimiter, Int32::MAX, chomp: chomp
end
# Reads until *delimiter* is found, *limit* bytes are read, or the end of the `IO` is reached.
# Returns `nil` if called at the end of this `IO`.
#
# ```
# io = IO::Memory.new "hello\nworld"
# io.gets('o', 3) # => "hel"
# io.gets('r', 10) # => "lo\nwor"
# io.gets('z', 10) # => "ld"
# io.gets('w', 10) # => nil
# ```
def gets(delimiter : Char, limit : Int, chomp = false) : String?
raise ArgumentError.new "Negative limit" if limit < 0
ascii = delimiter.ascii?
decoder = decoder()
# If the char's representation is a single byte and we have an encoding,
# search the delimiter in the buffer
if ascii && decoder
return decoder.gets(self, delimiter.ord.to_u8, limit: limit, chomp: chomp)
end
# If there's no encoding, the delimiter is ASCII and we can peek,
# use a faster algorithm
if ascii && !decoder && (peek = self.peek)
if peek.empty?
nil
else
gets_peek(delimiter, limit, chomp, peek)
end
else
gets_slow(delimiter, limit, chomp)
end
end
private def gets_peek(delimiter, limit, chomp, peek)
limit = Int32::MAX if limit < 0
delimiter_byte = delimiter.ord.to_u8
# We first check, if the delimiter is already in the peek buffer.
# In that case it's much faster to create a String from a slice
# of the buffer instead of appending to a IO::Memory,
# which happens in the other case.
index = peek.index(delimiter_byte)
if index
# If we find it past the limit, limit the result
if index >= limit
index = limit
else
index += 1
end
advance = index
if chomp && index > 0 && peek[index - 1] === delimiter_byte
index -= 1
if delimiter == '\n' && index > 0 && peek[index - 1] === '\r'
index -= 1
end
end
string = String.new(peek[0, index])
skip(advance)
return string
end
# We didn't find the delimiter, so we append to a String::Builder
# until we find it or we reach the limit, appending what we have
# in the peek buffer and peeking again.
String.build do |buffer|
while peek
available = Math.min(peek.size, limit)
buffer.write peek[0, available]
skip(available)
peek += available
limit -= available
if limit == 0
break
end
if peek.size == 0
peek = self.peek
end
unless peek
# If for some reason this IO became unpeekable,
# default to the slow method. One example where this can
# happen is `IO::Delimited`.
gets_slow(delimiter, limit, chomp, buffer)
break
end
if peek.empty?
if buffer.bytesize == 0
return nil
else
break
end
end
index = peek.index(delimiter_byte)
if index
if index >= limit
index = limit
else
index += 1
end
buffer.write peek[0, index]
skip(index)
break
end
end
buffer.chomp!(delimiter_byte) if chomp
end
end
private def gets_slow(delimiter : Char, limit, chomp)
buffer = String::Builder.new
bytes_read = gets_slow(delimiter, limit, chomp, buffer)
buffer.to_s if bytes_read
end
private def gets_slow(delimiter : Char, limit, chomp, buffer : String::Builder) : Bool
bytes_read = false
chomp_rn = delimiter == '\n' && chomp
while true
info = read_char_with_bytesize
unless info
break
end
char, char_bytesize = info
bytes_read = true
# Consider the case of \r\n when the delimiter is \n and chomp = true
if chomp_rn && char == '\r'
info2 = read_char_with_bytesize
unless info2
buffer << char
break
end
char2, char_bytesize2 = info2
if char2 == '\n'
break
end
buffer << '\r'
break if char_bytesize >= limit
limit -= char_bytesize
buffer << char2
break if char_bytesize2 >= limit
limit -= char_bytesize2
next
elsif char == delimiter
buffer << char unless chomp
break
else
buffer << char
end
break if char_bytesize >= limit
limit -= char_bytesize
end
bytes_read
end
# Reads until *delimiter* is found or the end of the `IO` is reached.
# Returns `nil` if called at the end of this `IO`.
#
# ```
# io = IO::Memory.new "hello\nworld"
# io.gets("wo") # => "hello\nwo"
# io.gets("wo") # => "rld"
# io.gets("wo") # => nil
# ```
def gets(delimiter : String, chomp = false) : String?
# Empty string: read all
if delimiter.empty?
return gets_to_end
end
# One byte: use gets(Char)
if delimiter.bytesize == 1
return gets(delimiter.to_unsafe[0].unsafe_chr, chomp: chomp)
end
# One char: use gets(Char)
if delimiter.size == 1
return gets(delimiter[0], chomp: chomp)
end
# The 'hard' case: we read until we match the last byte,
# and then compare backwards
last_byte = delimiter.byte_at(delimiter.bytesize - 1)
total_bytes = 0
buffer = String::Builder.new
while true
unless byte = read_utf8_byte
return buffer.empty? ? nil : buffer.to_s
end
buffer.write_byte(byte)
total_bytes += 1
if (byte == last_byte) &&
(buffer.bytesize >= delimiter.bytesize) &&
(buffer.buffer + total_bytes - delimiter.bytesize).memcmp(delimiter.to_unsafe, delimiter.bytesize) == 0
buffer.back(delimiter.bytesize) if chomp
break
end
end
buffer.to_s
end
# Same as `gets`, but raises `EOFError` if called at the end of this `IO`.
def read_line(*args, **options) : String
gets(*args, **options) || raise EOFError.new
end
# Reads and discards exactly *bytes_count* bytes.
# Raises `IO::EOFError` if there aren't at least *bytes_count* bytes.
#
# ```
# io = IO::Memory.new "hello world"
# io.skip(6)
# io.gets # => "world"
# io.skip(1) # raises IO::EOFError
# ```
def skip(bytes_count : Int) : Nil
buffer = uninitialized UInt8[DEFAULT_BUFFER_SIZE]
while bytes_count > 0
read_count = read(buffer.to_slice[0, Math.min(bytes_count, buffer.size)])
raise IO::EOFError.new if read_count == 0
bytes_count -= read_count
end
end
# Reads and discards bytes from `self` until there
# are no more bytes.
def skip_to_end : Nil
buffer = uninitialized UInt8[DEFAULT_BUFFER_SIZE]
while read(buffer.to_slice) > 0
end
end
# Writes a single byte into this `IO`.
#
# ```
# io = IO::Memory.new
# io.write_byte 97_u8
# io.to_s # => "a"
# ```
def write_byte(byte : UInt8) : Nil
x = byte
write Slice.new(pointerof(x), 1)
end
# Writes the given object to this `IO` using the specified *format*.
#
# This ends up invoking `object.to_io(self, format)`, so any object defining a
# `to_io(io : IO, format : IO::ByteFormat = IO::ByteFormat::SystemEndian)`
# method can be written in this way.
#
# See `Int#to_io` and `Float#to_io`.
#
# ```
# io = IO::Memory.new
# io.write_bytes(0x01020304, IO::ByteFormat::LittleEndian)
# io.rewind
# io.gets(4) # => "\u{4}\u{3}\u{2}\u{1}"
# ```
def write_bytes(object, format : IO::ByteFormat = IO::ByteFormat::SystemEndian) : Nil
object.to_io(self, format)
end
# Reads an instance of the given *type* from this `IO` using the specified *format*.
#
# This ends up invoking `type.from_io(self, format)`, so any type defining a
# `from_io(io : IO, format : IO::ByteFormat = IO::ByteFormat::SystemEndian)`
# method can be read in this way.
#
# See `Int.from_io` and `Float.from_io`.
#
# ```
# io = IO::Memory.new
# io.puts "\u{4}\u{3}\u{2}\u{1}"
# io.rewind
# io.read_bytes(Int32, IO::ByteFormat::LittleEndian) # => 0x01020304
# ```
def read_bytes(type, format : IO::ByteFormat = IO::ByteFormat::SystemEndian)
type.from_io(self, format)
end
# Returns `true` if this `IO` is associated with a terminal device (tty), `false` otherwise.
#
# IO returns `false`, but including types may override.
#
# ```
# STDIN.tty? # => true
# IO::Memory.new.tty? # => false
# ```
def tty? : Bool
false
end
# Invokes the given block with each *line* in this `IO`, where a line
# is defined by the arguments passed to this method, which can be the same
# ones as in the `gets` methods.
#
# ```
# io = IO::Memory.new("hello\nworld")
# io.each_line do |line|
# puts line
# end
# # output:
# # hello
# # world
# ```
def each_line(*args, **options, &block : String ->) : Nil
while line = gets(*args, **options)
yield line
end
end
# Returns an `Iterator` for the *lines* in this `IO`, where a line
# is defined by the arguments passed to this method, which can be the same
# ones as in the `gets` methods.
#
# ```
# io = IO::Memory.new("hello\nworld")
# iter = io.each_line
# iter.next # => "hello"
# iter.next # => "world"
# ```
def each_line(*args, **options)
LineIterator.new(self, args, options)
end
# Invokes the given block with each `Char` in this `IO`.
#
# ```
# io = IO::Memory.new("あめ")
# io.each_char do |char|
# puts char
# end
# ```
#
# Output:
#
# ```text
# あ
# め
# ```
def each_char(&) : Nil
while char = read_char
yield char
end
end