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



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

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

#binmodeObject



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

def binmode
  self
end

#closeObject

Raises:

  • (IOError)


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

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

#close_readObject



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

def close_read
  check_readable
  @readable = nil
end

#close_writeObject



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

def close_write
  check_writable
  @writable = nil
end

#closed?Boolean

Returns:

  • (Boolean)


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

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

#closed_read?Boolean

Returns:

  • (Boolean)


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

def closed_read?
  !@readable
end

#closed_write?Boolean

Returns:

  • (Boolean)


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

def closed_write?
  !@writable
end

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



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

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



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

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



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

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



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

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



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

def encode_with(coder)
end

#eof?Boolean Also known as: eof

Returns:

  • (Boolean)


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

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

#external_encodingObject



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

def external_encoding
  @__data__.encoding
end

#fcntlObject

Raises:

  • (NotImplementedError)


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

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

#filenoObject



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

def fileno
  nil
end

#flushObject



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

def flush
  self
end

#fsyncObject



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

def fsync
  0
end

#getbyteObject



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

def getbyte
  check_readable
  d = @__data__

  return nil if eof?

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

#getcObject



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

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



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

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

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

#init_with(coder) ⇒ Object



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

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



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

def internal_encoding
  nil
end

#isattyObject Also known as: tty?



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

def isatty
  false
end

#linenoObject



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

def lineno
  @__data__.lineno
end

#lineno=(line) ⇒ Object



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

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

#pidObject



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

def pid
  nil
end

#posObject



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

def pos
  @__data__.pos
end

#pos=(pos) ⇒ Object

Raises:

  • (Errno::EINVAL)


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

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


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

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

#printf(*args) ⇒ Object



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

def printf(*args)
  check_writable

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

  nil
end

#putc(obj) ⇒ Object



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

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



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

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



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

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



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

def readbyte
  readchar.getbyte(0)
end

#readcharObject

Raises:

  • (IO::EOFError)


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

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

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

Raises:

  • (IO::EOFError)


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

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



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

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



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

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



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

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

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

Raises:

  • (IOError)


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

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
# 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)
end

#sizeObject Also known as: length



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

def size
  @__data__.string.bytesize
end

#stringObject



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

def string
  @__data__.string
end

#string=(string) ⇒ Object



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

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

#syncObject



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

def sync
  true
end

#sync=(val) ⇒ Object



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

def sync=(val)
  val
end

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



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

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



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

def tell
  @__data__.pos
end

#to_yaml_propertiesObject



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

def to_yaml_properties
  []
end

#truncate(length) ⇒ Object

Raises:

  • (Errno::EINVAL)


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

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



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

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



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

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



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

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



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

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