Class: StringIO

Inherits:
Object
  • Object
show all
Includes:
Enumerable, IO::Readable, IO::Writable
Defined in:
lib/rubysl/stringio/stringio.rb

Defined Under Namespace

Classes: Data

Constant Summary collapse

DEFAULT_RECORD_SEPARATOR =
"\n"
Undefined =

This is why we need undefined in Ruby

Object.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string = nil, mode = nil) ⇒ StringIO

Returns a new instance of StringIO.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rubysl/stringio/stringio.rb', line 44

def initialize(string=nil, mode=nil)
  if string.nil?
    @__data__ = Data.new ""
    set_encoding(nil)
    mode = IO::RDWR
  else
    string = Rubinius::Type.coerce_to string, String, :to_str
    @__data__ = Data.new string
  end

  if mode
    if mode.is_a?(Integer)
      mode_from_integer(mode)
    else
      mode = StringValue(mode)
      mode_from_string(mode)
    end
  else
    mode_from_string(string.frozen? ? "r" : "r+")
  end

  self
end

Instance Attribute Details

#__data__Object (readonly)

Returns the value of attribute __data__.



42
43
44
# File 'lib/rubysl/stringio/stringio.rb', line 42

def __data__
  @__data__
end

Class Method Details

.open(*args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rubysl/stringio/stringio.rb', line 29

def self.open(*args)
  io = new(*args)
  return io unless block_given?

  begin
    yield io
  ensure
    io.close
    io.__data__.string = nil
    self
  end
end

Instance Method Details

#<<(str) ⇒ Object



175
176
177
178
# File 'lib/rubysl/stringio/stringio.rb', line 175

def <<(str)
  write(str)
  self
end

#binmodeObject



180
181
182
# File 'lib/rubysl/stringio/stringio.rb', line 180

def binmode
  self
end

#closeObject

Raises:

  • (IOError)


218
219
220
221
# File 'lib/rubysl/stringio/stringio.rb', line 218

def close
  raise IOError, "closed stream" if closed?
  @readable = @writable = nil
end

#close_readObject



227
228
229
230
# File 'lib/rubysl/stringio/stringio.rb', line 227

def close_read
  check_readable
  @readable = nil
end

#close_writeObject



236
237
238
239
# File 'lib/rubysl/stringio/stringio.rb', line 236

def close_write
  check_writable
  @writable = nil
end

#closed?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/rubysl/stringio/stringio.rb', line 223

def closed?
  !@readable && !@writable
end

#closed_read?Boolean

Returns:

  • (Boolean)


232
233
234
# File 'lib/rubysl/stringio/stringio.rb', line 232

def closed_read?
  !@readable
end

#closed_write?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/rubysl/stringio/stringio.rb', line 241

def closed_write?
  !@writable
end

#each(sep = $/, limit = Undefined) ⇒ Object Also known as: each_line, lines



161
162
163
164
165
166
167
168
169
170
# File 'lib/rubysl/stringio/stringio.rb', line 161

def each(sep=$/, limit=Undefined)
  return to_enum :each, sep, limit unless block_given?
  check_readable

  while line = getline(true, sep, limit)
    yield line
  end

  self
end

#each_byteObject Also known as: bytes



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rubysl/stringio/stringio.rb', line 109

def each_byte
  return to_enum :each_byte unless block_given?
  check_readable

  d = @__data__
  string = d.string

  while d.pos < string.length
    byte = string.getbyte d.pos
    d.pos += 1
    yield byte
  end

  self
end

#each_charObject Also known as: chars



127
128
129
130
131
132
133
134
# File 'lib/rubysl/stringio/stringio.rb', line 127

def each_char
  return to_enum :each_char unless block_given?
  while s = getc
    yield s
  end

  self
end

#each_codepoint(&block) ⇒ Object Also known as: codepoints



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rubysl/stringio/stringio.rb', line 138

def each_codepoint(&block)
  return to_enum :each_codepoint unless block_given?
  check_readable

  d = @__data__
  string = d.string

  while d.pos < string.bytesize
    char = string.chr_at d.pos

    unless char
      raise ArgumentError, "invalid byte sequence in #{d.encoding}"
    end

    d.pos += char.bytesize
    yield char.ord
  end

  self
end

#encode_with(coder) ⇒ Object



621
622
# File 'lib/rubysl/stringio/stringio.rb', line 621

def encode_with(coder)
end

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


245
246
247
248
# File 'lib/rubysl/stringio/stringio.rb', line 245

def eof?
  d = @__data__
  d.pos >= d.string.bytesize
end

#external_encodingObject



101
102
103
# File 'lib/rubysl/stringio/stringio.rb', line 101

def external_encoding
  @__data__.encoding
end

#fcntlObject

Raises:

  • (NotImplementedError)


251
252
253
# File 'lib/rubysl/stringio/stringio.rb', line 251

def fcntl
  raise NotImplementedError, "StringIO#fcntl is not implemented"
end

#filenoObject



255
256
257
# File 'lib/rubysl/stringio/stringio.rb', line 255

def fileno
  nil
end

#flushObject



259
260
261
# File 'lib/rubysl/stringio/stringio.rb', line 259

def flush
  self
end

#fsyncObject



263
264
265
# File 'lib/rubysl/stringio/stringio.rb', line 263

def fsync
  0
end

#getbyteObject



278
279
280
281
282
283
284
285
286
287
# File 'lib/rubysl/stringio/stringio.rb', line 278

def getbyte
  check_readable
  d = @__data__

  return nil if eof?

  byte = d.string.getbyte(d.pos)
  d.pos += 1
  byte
end

#getcObject



267
268
269
270
271
272
273
274
275
276
# File 'lib/rubysl/stringio/stringio.rb', line 267

def getc
  check_readable
  d = @__data__

  return nil if eof?

  char = d.string.find_character(d.pos)
  d.pos += char.bytesize
  char
end

#gets(sep = $/, limit = Undefined) ⇒ Object



289
290
291
292
293
# File 'lib/rubysl/stringio/stringio.rb', line 289

def gets(sep=$/, limit=Undefined)
  check_readable

  $_ = getline(false, sep, limit)
end

#init_with(coder) ⇒ Object



624
625
626
# File 'lib/rubysl/stringio/stringio.rb', line 624

def init_with(coder)
  @__data__ = Data.new("")
end

#initialize_copy(from) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rubysl/stringio/stringio.rb', line 68

def initialize_copy(from)
  from = Rubinius::Type.coerce_to(from, StringIO, :to_strio)

  taint if from.tainted?

  @append = from.instance_variable_get(:@append)
  @readable = from.instance_variable_get(:@readable)
  @writable = from.instance_variable_get(:@writable)
  @__data__ = from.instance_variable_get(:@__data__)

  self
end

#internal_encodingObject



105
106
107
# File 'lib/rubysl/stringio/stringio.rb', line 105

def internal_encoding
  nil
end

#isattyObject Also known as: tty?



295
296
297
# File 'lib/rubysl/stringio/stringio.rb', line 295

def isatty
  false
end

#linenoObject



300
301
302
# File 'lib/rubysl/stringio/stringio.rb', line 300

def lineno
  @__data__.lineno
end

#lineno=(line) ⇒ Object



304
305
306
# File 'lib/rubysl/stringio/stringio.rb', line 304

def lineno=(line)
  @__data__.lineno = line
end

#pidObject



308
309
310
# File 'lib/rubysl/stringio/stringio.rb', line 308

def pid
  nil
end

#posObject



312
313
314
# File 'lib/rubysl/stringio/stringio.rb', line 312

def pos
  @__data__.pos
end

#pos=(pos) ⇒ Object

Raises:

  • (Errno::EINVAL)


316
317
318
319
# File 'lib/rubysl/stringio/stringio.rb', line 316

def pos=(pos)
  raise Errno::EINVAL if pos < 0
  @__data__.pos = pos
end


321
322
323
324
325
326
# File 'lib/rubysl/stringio/stringio.rb', line 321

def print(*args)
  check_writable
  args << $_ if args.empty?
  write((args << $\).flatten.join)
  nil
end

#printf(*args) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
# File 'lib/rubysl/stringio/stringio.rb', line 328

def printf(*args)
  check_writable

  if args.size > 1
    write(args.shift % args)
  else
    write(args.first)
  end

  nil
end

#putc(obj) ⇒ Object



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
# File 'lib/rubysl/stringio/stringio.rb', line 340

def putc(obj)
  check_writable

  if obj.is_a?(String)
    char = obj[0]
  else
    c = Rubinius::Type.coerce_to obj, Integer, :to_int
    char = (c & 0xff).chr
  end

  d = @__data__
  pos = d.pos
  string = d.string

  if @append || pos == string.bytesize
    string.byte_append char
    d.pos = string.bytesize
  elsif pos > string.bytesize
    m = Rubinius::Mirror.reflect string
    m.splice string.bytesize, 0, "\000" * (pos - string.bytesize)
    string.byte_append char
    d.pos = string.bytesize
  else
    m = Rubinius::Mirror.reflect string
    m.splice pos, char.bytesize, char
    d.pos += char.bytesize
  end

  obj
end

#puts(*args) ⇒ Object



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
# File 'lib/rubysl/stringio/stringio.rb', line 371

def puts(*args)
  if args.empty?
    write(DEFAULT_RECORD_SEPARATOR)
  else
    args.each do |arg|
      if arg.nil?
        line = ""
      elsif Thread.guarding? arg
        line = "[...]"
      else
        begin
          arg = Rubinius::Type.coerce_to(arg, Array, :to_ary)
          Thread.recursion_guard arg do
            arg.each { |a| puts a }
          end
          next
        rescue
          line = arg.to_s
        end
      end

      write(line)
      write(DEFAULT_RECORD_SEPARATOR) unless line[-1] == ?\n
    end
  end

  nil
end

#read(length = nil, buffer = nil) ⇒ Object



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
# File 'lib/rubysl/stringio/stringio.rb', line 400

def read(length=nil, buffer=nil)
  check_readable
  d = @__data__
  pos = d.pos
  string = d.string

  if length
    length = Rubinius::Type.coerce_to length, Integer, :to_int
    raise ArgumentError if length < 0

    buffer = StringValue(buffer) if buffer

    if eof?
      buffer.clear if buffer
      if length == 0
        return "".force_encoding(Encoding::ASCII_8BIT)
      else
        return nil
      end
    end

    str = string.byteslice(pos, length)
    str.force_encoding Encoding::ASCII_8BIT

    str = buffer.replace(str) if buffer
  else
    if eof?
      buffer.clear if buffer
      return "".force_encoding(Encoding::ASCII_8BIT)
    end

    str = string.byteslice(pos..-1)
    buffer.replace str if buffer
  end

  d.pos += str.length
  return str
end

#readbyteObject



444
445
446
# File 'lib/rubysl/stringio/stringio.rb', line 444

def readbyte
  readchar.getbyte(0)
end

#readcharObject

Raises:

  • (IO::EOFError)


439
440
441
442
# File 'lib/rubysl/stringio/stringio.rb', line 439

def readchar
  raise IO::EOFError, "end of file reached" if eof?
  getc
end

#readline(sep = $/, limit = Undefined) ⇒ Object

Raises:

  • (IO::EOFError)


448
449
450
451
452
453
# File 'lib/rubysl/stringio/stringio.rb', line 448

def readline(sep=$/, limit=Undefined)
  check_readable
  raise IO::EOFError, "end of file reached" if eof?

  $_ = getline(true, sep, limit)
end

#readlines(sep = $/, limit = Undefined) ⇒ Object



455
456
457
458
459
460
461
462
463
464
# File 'lib/rubysl/stringio/stringio.rb', line 455

def readlines(sep=$/, limit=Undefined)
  check_readable

  ary = []
  while line = getline(true, sep, limit)
    ary << line
  end

  ary
end

#reopen(string = nil, mode = Undefined) ⇒ Object



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/rubysl/stringio/stringio.rb', line 466

def reopen(string=nil, mode=Undefined)
  if string and not string.kind_of? String and mode.equal? Undefined
    stringio = Rubinius::Type.coerce_to(string, StringIO, :to_strio)

    taint if stringio.tainted?
    initialize_copy stringio
  else
    mode = nil if mode.equal? Undefined
    string = "" unless string

    initialize string, mode
  end

  self
end

#rewindObject



482
483
484
485
# File 'lib/rubysl/stringio/stringio.rb', line 482

def rewind
  d = @__data__
  d.pos = d.lineno = 0
end

#seek(to, whence = IO::SEEK_SET) ⇒ Object

Raises:

  • (IOError)


487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/rubysl/stringio/stringio.rb', line 487

def seek(to, whence = IO::SEEK_SET)
  raise IOError, "closed stream" if self.closed?
  to = Rubinius::Type.coerce_to to, Integer, :to_int

  case whence
  when IO::SEEK_CUR
    to += @__data__.pos
  when IO::SEEK_END
    to += @__data__.string.bytesize
  when IO::SEEK_SET, nil
  else
    raise Errno::EINVAL, "invalid whence"
  end

  raise Errno::EINVAL if to < 0

  @__data__.pos = to

  return 0
end

#set_encoding(external, internal = nil, options = nil) ⇒ Object



94
95
96
97
98
99
# File 'lib/rubysl/stringio/stringio.rb', line 94

def set_encoding(external, internal=nil, options=nil)
  encoding = external || Encoding.default_external
  @__data__.encoding = encoding
  @__data__.string.force_encoding(encoding)
  self
end

#sizeObject Also known as: length



508
509
510
# File 'lib/rubysl/stringio/stringio.rb', line 508

def size
  @__data__.string.bytesize
end

#stringObject



513
514
515
# File 'lib/rubysl/stringio/stringio.rb', line 513

def string
  @__data__.string
end

#string=(string) ⇒ Object



517
518
519
520
521
522
# File 'lib/rubysl/stringio/stringio.rb', line 517

def string=(string)
  d = @__data__
  d.string = StringValue(string)
  d.pos = 0
  d.lineno = 0
end

#syncObject



524
525
526
# File 'lib/rubysl/stringio/stringio.rb', line 524

def sync
  true
end

#sync=(val) ⇒ Object



528
529
530
# File 'lib/rubysl/stringio/stringio.rb', line 528

def sync=(val)
  val
end

#sysread(length = nil, buffer = "") ⇒ Object Also known as: readpartial, read_nonblock



532
533
534
535
536
537
538
539
540
541
# File 'lib/rubysl/stringio/stringio.rb', line 532

def sysread(length=nil, buffer="")
  str = read(length, buffer)

  if str.nil?
    buffer.clear
    raise IO::EOFError, "end of file reached"
  end

  str
end

#tellObject



546
547
548
# File 'lib/rubysl/stringio/stringio.rb', line 546

def tell
  @__data__.pos
end

#to_yaml_propertiesObject



628
629
630
# File 'lib/rubysl/stringio/stringio.rb', line 628

def to_yaml_properties
  []
end

#truncate(length) ⇒ Object

Raises:

  • (Errno::EINVAL)


550
551
552
553
554
555
556
557
558
559
560
561
562
# File 'lib/rubysl/stringio/stringio.rb', line 550

def truncate(length)
  check_writable
  len = Rubinius::Type.coerce_to length, Integer, :to_int
  raise Errno::EINVAL, "negative length" if len < 0
  string = @__data__.string

  if len < string.bytesize
    string[len..string.bytesize] = ""
  else
    string << "\000" * (len - string.bytesize)
  end
  return length
end

#ungetbyte(bytes) ⇒ Object



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
# File 'lib/rubysl/stringio/stringio.rb', line 589

def ungetbyte(bytes)
  check_readable

  return unless bytes

  if bytes.kind_of? Fixnum
    bytes = "" << bytes
  else
    bytes = StringValue(bytes)
    return if bytes.bytesize == 0
  end

  d = @__data__
  pos = d.pos
  string = d.string

  enc = string.encoding

  if d.pos == 0
    d.string = "#{bytes}#{string}"
  else
    size = bytes.bytesize
    a = string.byteslice(0, pos - size) if size < pos
    b = string.byteslice(pos..-1)
    d.string = "#{a}#{bytes}#{b}"
    d.pos = pos > size ? pos - size : 0
  end

  d.string.force_encoding enc
  nil
end

#ungetc(char) ⇒ Object



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/rubysl/stringio/stringio.rb', line 564

def ungetc(char)
  check_readable

  d = @__data__
  pos = d.pos
  string = d.string

  if char.kind_of? Integer
    char = Rubinius::Type.coerce_to char, String, :chr
  else
    char = Rubinius::Type.coerce_to char, String, :to_str
  end

  if pos > string.bytesize
    string[string.bytesize..pos] = "\000" * (pos - string.bytesize)
    d.pos -= 1
    string[d.pos] = char
  elsif pos > 0
    d.pos -= 1
    string[d.pos] = char
  end

  nil
end

#write(str) ⇒ Object Also known as: syswrite, write_nonblock



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
# File 'lib/rubysl/stringio/stringio.rb', line 184

def write(str)
  check_writable

  str = String(str)
  return 0 if str.empty?

  d = @__data__
  pos = d.pos
  string = d.string

  if @append || pos == string.bytesize
    string.byte_append str
    d.pos = string.bytesize
  elsif pos > string.bytesize
    m = Rubinius::Mirror.reflect string
    m.splice string.bytesize, 0, "\000" * (pos - string.bytesize)
    string.byte_append str
    d.pos = string.bytesize
  else
    stop = string.bytesize - pos
    if str.bytesize < stop
      stop = str.bytesize
    end
    m = Rubinius::Mirror.reflect string
    m.splice pos, stop, str
    d.pos += str.bytesize
    string.taint if str.tainted?
  end

  str.bytesize
end

#yaml_initialize(type, val) ⇒ Object



632
633
634
# File 'lib/rubysl/stringio/stringio.rb', line 632

def yaml_initialize(type, val)
  @__data__ = Data.new("")
end