Class: StringIO

Inherits:
IO show all
Includes:
IO::Readable, IO::Writable
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, read, write

Constructor Details

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

Returns a new instance of StringIO.



15
16
17
18
19
20
21
22
23
24
# File 'opal/stdlib/stringio.rb', line 15

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

  if mode.include?('r') and not mode.include?('w')
    @closed = :write
  elsif mode.include?('w') and not mode.include?('r')
    @closed = :read
  end
end

Instance Attribute Details

#stringObject

Returns the value of attribute string.



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

def string
  @string
end

Class Method Details

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



5
6
7
8
9
10
11
# File 'opal/stdlib/stringio.rb', line 5

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

  res
end

Instance Method Details

#check_readableObject



209
210
211
212
213
# File 'opal/stdlib/stringio.rb', line 209

def check_readable
  if closed_read?
    raise IOError, "not opened for reading"
  end
end

#check_writableObject



203
204
205
206
207
# File 'opal/stdlib/stringio.rb', line 203

def check_writable
  if closed_write?
    raise IOError, "not opened for writing"
  end
end

#closeObject



171
172
173
# File 'opal/stdlib/stringio.rb', line 171

def close
  @closed = :both
end

#close_readObject



175
176
177
178
179
180
181
# File 'opal/stdlib/stringio.rb', line 175

def close_read
  if @closed == :write
    @closed = :both
  else
    @closed = :read
  end
end

#close_writeObject



183
184
185
186
187
188
189
# File 'opal/stdlib/stringio.rb', line 183

def close_write
  if @closed == :read
    @closed = :both
  else
    @closed = :write
  end
end

#closed?Boolean

Returns:



191
192
193
# File 'opal/stdlib/stringio.rb', line 191

def closed?
  @closed == :both
end

#closed_read?Boolean

Returns:



195
196
197
# File 'opal/stdlib/stringio.rb', line 195

def closed_read?
  @closed == :read || @closed == :both
end

#closed_write?Boolean

Returns:



199
200
201
# File 'opal/stdlib/stringio.rb', line 199

def closed_write?
  @closed == :write || @closed == :both
end

#each(separator = $/) ⇒ Object Also known as: each_line



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

def each(separator = $/)
  return enum_for :each_line unless block_given?
  check_readable
  chomp_lines = false
  if ::Hash === separator
    separator = (chomp_lines = separator[:chomp]) ? /\r?\n/ : $/
  elsif separator
    separator = separator.to_str
  else
    separator = `undefined`
  end
  %x{
    var str = self.string, stringLength = str.length;
    if (self.position < stringLength) str = str.substr(self.position);
    if (separator) {
      var chomped = #{`str`.chomp}, trailing = str.length !== chomped.length, splitted = chomped.split(separator);
      for (var i = 0, len = splitted.length; i < len; i++) {
        var line = chomp_lines ? splitted[i] : (i < len - 1 || trailing ? splitted[i] + separator : splitted[i]);
        #{yield `line`};
      }
    } else if (separator === undefined) {
      #{yield `str`};
    } else {
      var m, re = /(.+(?:\n\n|$))\n*/g;
      while ((m = re.exec(str))) #{yield `m[1]`};
    }
    self.position = stringLength;
  }
  self
end

#each_byte(&block) ⇒ Object



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

def each_byte(&block)
  return enum_for :each_byte unless block

  check_readable

  i = @position
  until eof?
    block.call(@string[i].ord)
    i += 1
  end

  self
end

#each_char(&block) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'opal/stdlib/stringio.rb', line 85

def each_char(&block)
  return enum_for :each_char unless block

  check_readable

  i = @position
  until eof?
    block.call(@string[i])
    i += 1
  end

  self
end

#eof?Boolean Also known as: eof

Returns:



26
27
28
29
30
# File 'opal/stdlib/stringio.rb', line 26

def eof?
  check_readable

  @position == @string.length
end

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'opal/stdlib/stringio.rb', line 149

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

  return if eof?

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

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

#rewindObject



67
68
69
# File 'opal/stdlib/stringio.rb', line 67

def rewind
  seek 0
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'opal/stdlib/stringio.rb', line 34

def seek(pos, whence = IO::SEEK_SET)
  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

#tellObject Also known as: pos



59
60
61
# File 'opal/stdlib/stringio.rb', line 59

def tell
  @position
end

#write(string) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'opal/stdlib/stringio.rb', line 132

def write(string)
  check_writable

  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