Class: StringIO

Inherits:
IO show all
Defined in:
opal/stdlib/stringio.rb

Instance Attribute Summary collapse

Attributes inherited from IO

#lineno

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IO

binread, #initialize_before_node_io, read, write

Constructor Details

#initialize(string = "", mode = 'rw') ⇒ StringIO

Returns a new instance of StringIO.



12
13
14
15
16
17
# File 'opal/stdlib/stringio.rb', line 12

def initialize(string = "", mode = 'rw')
  @string   = string
  @position = 0

  super(nil, mode)
end

Instance Attribute Details

#stringObject

Returns the value of attribute string.



10
11
12
# File 'opal/stdlib/stringio.rb', line 10

def string
  @string
end

Class Method Details

.open(string = "", mode = nil, &block) ⇒ Object



2
3
4
5
6
7
8
# File 'opal/stdlib/stringio.rb', line 2

def self.open(string = "", mode = nil, &block)
  io  = new(string, mode)
  res = block.call(io)
  io.close

  res
end

Instance Method Details

#eof?Boolean Also known as: eof

Returns:



19
20
21
22
23
# File 'opal/stdlib/stringio.rb', line 19

def eof?
  check_readable

  @position == @string.length
end

#read(length = nil, outbuf = nil) ⇒ Object Also known as: readpartial



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'opal/stdlib/stringio.rb', line 87

def read(length = nil, outbuf = nil)
  check_readable

  return if eof?

  string = if length
    str = @string[@position, length]
    @position += length
    @position = @string.length if @position > @string.length
    str
  else
    str = @string[@position .. -1]
    @position = @string.length
    str
  end

  if outbuf
    outbuf.write(string)
  else
    string
  end
end

#rewindObject



63
64
65
# File 'opal/stdlib/stringio.rb', line 63

def rewind
  seek 0
end

#seek(pos, whence = IO::SEEK_SET) ⇒ Object Also known as: pos=



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
# File 'opal/stdlib/stringio.rb', line 27

def seek(pos, whence = IO::SEEK_SET)
  # Let's reset the read buffer, because it will be most likely wrong
  @read_buffer = ''

  case whence
  when IO::SEEK_SET
    raise Errno::EINVAL unless pos >= 0

    @position = pos

  when IO::SEEK_CUR
    if @position + pos > @string.length
      @position = @string.length
    else
      @position += pos
    end

  when IO::SEEK_END
    if pos > @string.length
      @position = 0
    else
      @position -= pos
    end
  end

  0
end

#sysread(length) ⇒ Object



110
111
112
113
114
# File 'opal/stdlib/stringio.rb', line 110

def sysread(length)
  check_readable

  read(length)
end

#tellObject Also known as: pos



55
56
57
# File 'opal/stdlib/stringio.rb', line 55

def tell
  @position
end

#write(string) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'opal/stdlib/stringio.rb', line 67

def write(string)
  check_writable

  # Let's reset the read buffer, because it will be most likely wrong
  @read_buffer = ''

  string = String(string)

  if @string.length == @position
    @string   += string
    @position += string.length
  else
    before = @string[0 .. @position - 1]
    after  = @string[@position + string.length .. -1]

    @string   = before + string + after
    @position += string.length
  end
end