rubysl-stringio 2.0.0 → 2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +8 -4
- data/MRI_LICENSE +56 -0
- data/lib/rubysl/stringio.rb +1 -0
- data/lib/rubysl/stringio/version.rb +1 -1
- data/lib/stringio.rb +742 -1
- data/rubysl-stringio.gemspec +1 -0
- data/spec/codepoints_spec.rb +0 -1
- data/spec/each_codepoint_spec.rb +0 -1
- data/spec/external_encoding_spec.rb +0 -1
- data/spec/getc_spec.rb +0 -1
- data/spec/gets_spec.rb +2 -2
- data/spec/internal_encoding_spec.rb +0 -1
- data/spec/putc_spec.rb +0 -1
- data/spec/puts_spec.rb +0 -1
- data/spec/read_spec.rb +0 -1
- data/spec/set_encoding_spec.rb +5 -1
- data/spec/shared/read.rb +8 -8
- data/spec/ungetbyte_spec.rb +0 -1
- metadata +28 -14
- data/lib/rubysl/stringio/stringio.rb +0 -741
data/rubysl-stringio.gemspec
CHANGED
data/spec/codepoints_spec.rb
CHANGED
data/spec/each_codepoint_spec.rb
CHANGED
data/spec/getc_spec.rb
CHANGED
data/spec/gets_spec.rb
CHANGED
@@ -227,8 +227,8 @@ ruby_version_is "1.9" do
|
|
227
227
|
@io.gets('>', obj).should == "this>"
|
228
228
|
end
|
229
229
|
|
230
|
-
it "
|
231
|
-
|
230
|
+
it "returns a String when both separator and limit are nil" do
|
231
|
+
@io.gets(nil, nil).should == "this>is>an>example"
|
232
232
|
end
|
233
233
|
end
|
234
234
|
end
|
data/spec/putc_spec.rb
CHANGED
data/spec/puts_spec.rb
CHANGED
data/spec/read_spec.rb
CHANGED
data/spec/set_encoding_spec.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'stringio'
|
2
|
-
require File.expand_path('../../../spec_helper', __FILE__)
|
3
2
|
|
4
3
|
ruby_version_is "1.9.2" do
|
5
4
|
describe "StringIO#set_encoding" do
|
@@ -30,5 +29,10 @@ ruby_version_is "1.9.2" do
|
|
30
29
|
io.set_encoding nil
|
31
30
|
io.string.encoding.should == Encoding::UTF_8
|
32
31
|
end
|
32
|
+
|
33
|
+
it "returns self" do
|
34
|
+
io = StringIO.new
|
35
|
+
io.set_encoding(Encoding::UTF_8).should equal(io)
|
36
|
+
end
|
33
37
|
end
|
34
38
|
end
|
data/spec/shared/read.rb
CHANGED
@@ -15,7 +15,7 @@ describe :stringio_read, :shared => true do
|
|
15
15
|
|
16
16
|
ruby_version_is "1.9" do
|
17
17
|
it "truncates buffer when limit is nil and no data reamins" do
|
18
|
-
@io.send(@method)
|
18
|
+
@io.send(@method, nil)
|
19
19
|
@io.send(@method, nil, buffer = "abc").should == ""
|
20
20
|
buffer.should == ""
|
21
21
|
end
|
@@ -111,21 +111,21 @@ describe :stringio_read_no_arguments, :shared => true do
|
|
111
111
|
end
|
112
112
|
|
113
113
|
it "reads the whole content starting from the current position" do
|
114
|
-
@io.send(@method).should == "example"
|
114
|
+
@io.send(@method, 10).should == "example"
|
115
115
|
|
116
116
|
@io.pos = 3
|
117
|
-
@io.send(@method).should == "mple"
|
117
|
+
@io.send(@method, 10).should == "mple"
|
118
118
|
end
|
119
119
|
|
120
120
|
it "updates the current position" do
|
121
|
-
@io.send(@method)
|
121
|
+
@io.send(@method, 10)
|
122
122
|
@io.pos.should eql(7)
|
123
123
|
end
|
124
124
|
|
125
125
|
ruby_bug "readmine#156", "1.8.7" do
|
126
126
|
it "returns an empty String when no data remains" do
|
127
|
-
@io.send(@method).should == "example"
|
128
|
-
@io.send(@method).should == ""
|
127
|
+
@io.send(@method, 7).should == "example"
|
128
|
+
@io.send(@method, nil).should == ""
|
129
129
|
end
|
130
130
|
end
|
131
131
|
|
@@ -177,10 +177,10 @@ end
|
|
177
177
|
describe :stringio_read_not_readable, :shared => true do
|
178
178
|
it "raises an IOError" do
|
179
179
|
io = StringIO.new("test", "w")
|
180
|
-
lambda { io.send(@method) }.should raise_error(IOError)
|
180
|
+
lambda { io.send(@method, 2) }.should raise_error(IOError)
|
181
181
|
|
182
182
|
io = StringIO.new("test")
|
183
183
|
io.close_read
|
184
|
-
lambda { io.send(@method) }.should raise_error(IOError)
|
184
|
+
lambda { io.send(@method, 2) }.should raise_error(IOError)
|
185
185
|
end
|
186
186
|
end
|
data/spec/ungetbyte_spec.rb
CHANGED
metadata
CHANGED
@@ -1,57 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysl-stringio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: '2.1'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Shirai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - ~>
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - ~>
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: mspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '1.5'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubysl-prettyprint
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
55
69
|
description: Ruby standard library stringio.
|
56
70
|
email:
|
57
71
|
- brixen@gmail.com
|
@@ -59,14 +73,14 @@ executables: []
|
|
59
73
|
extensions: []
|
60
74
|
extra_rdoc_files: []
|
61
75
|
files:
|
62
|
-
- .gitignore
|
63
|
-
- .travis.yml
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
64
78
|
- Gemfile
|
65
79
|
- LICENSE
|
80
|
+
- MRI_LICENSE
|
66
81
|
- README.md
|
67
82
|
- Rakefile
|
68
83
|
- lib/rubysl/stringio.rb
|
69
|
-
- lib/rubysl/stringio/stringio.rb
|
70
84
|
- lib/rubysl/stringio/version.rb
|
71
85
|
- lib/stringio.rb
|
72
86
|
- rubysl-stringio.gemspec
|
@@ -158,17 +172,17 @@ require_paths:
|
|
158
172
|
- lib
|
159
173
|
required_ruby_version: !ruby/object:Gem::Requirement
|
160
174
|
requirements:
|
161
|
-
- - ~>
|
175
|
+
- - "~>"
|
162
176
|
- !ruby/object:Gem::Version
|
163
177
|
version: '2.0'
|
164
178
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
179
|
requirements:
|
166
|
-
- -
|
180
|
+
- - ">="
|
167
181
|
- !ruby/object:Gem::Version
|
168
182
|
version: '0'
|
169
183
|
requirements: []
|
170
184
|
rubyforge_project:
|
171
|
-
rubygems_version: 2.
|
185
|
+
rubygems_version: 2.5.1
|
172
186
|
signing_key:
|
173
187
|
specification_version: 4
|
174
188
|
summary: Ruby standard library stringio.
|
@@ -1,741 +0,0 @@
|
|
1
|
-
class IO
|
2
|
-
module Writable
|
3
|
-
end
|
4
|
-
module Readable
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
class StringIO
|
9
|
-
|
10
|
-
include Enumerable
|
11
|
-
include IO::Writable
|
12
|
-
include IO::Readable
|
13
|
-
|
14
|
-
DEFAULT_RECORD_SEPARATOR = "\n" unless defined?(::DEFAULT_RECORD_SEPARATOR)
|
15
|
-
|
16
|
-
# This is why we need undefined in Ruby
|
17
|
-
Undefined = Object.new
|
18
|
-
|
19
|
-
class Data
|
20
|
-
attr_accessor :string, :pos, :lineno, :encoding
|
21
|
-
|
22
|
-
def initialize(string)
|
23
|
-
@string = string
|
24
|
-
@encoding = string.encoding
|
25
|
-
@pos = @lineno = 0
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.open(*args)
|
30
|
-
io = new(*args)
|
31
|
-
return io unless block_given?
|
32
|
-
|
33
|
-
begin
|
34
|
-
yield io
|
35
|
-
ensure
|
36
|
-
io.close
|
37
|
-
io.__data__.string = nil
|
38
|
-
self
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
attr_reader :__data__
|
43
|
-
|
44
|
-
def initialize(string=nil, mode=nil)
|
45
|
-
if string.nil?
|
46
|
-
@__data__ = Data.new ""
|
47
|
-
set_encoding(nil)
|
48
|
-
mode = IO::RDWR
|
49
|
-
else
|
50
|
-
string = Rubinius::Type.coerce_to string, String, :to_str
|
51
|
-
@__data__ = Data.new string
|
52
|
-
end
|
53
|
-
|
54
|
-
if mode
|
55
|
-
if mode.is_a?(Integer)
|
56
|
-
mode_from_integer(mode)
|
57
|
-
else
|
58
|
-
mode = StringValue(mode)
|
59
|
-
mode_from_string(mode)
|
60
|
-
end
|
61
|
-
else
|
62
|
-
mode_from_string(string.frozen? ? "r" : "r+")
|
63
|
-
end
|
64
|
-
|
65
|
-
self
|
66
|
-
end
|
67
|
-
|
68
|
-
def initialize_copy(from)
|
69
|
-
from = Rubinius::Type.coerce_to(from, StringIO, :to_strio)
|
70
|
-
|
71
|
-
taint if from.tainted?
|
72
|
-
|
73
|
-
@append = from.instance_variable_get(:@append)
|
74
|
-
@readable = from.instance_variable_get(:@readable)
|
75
|
-
@writable = from.instance_variable_get(:@writable)
|
76
|
-
@__data__ = from.instance_variable_get(:@__data__)
|
77
|
-
|
78
|
-
self
|
79
|
-
end
|
80
|
-
|
81
|
-
def check_readable
|
82
|
-
raise IOError, "not opened for reading" unless @readable
|
83
|
-
end
|
84
|
-
|
85
|
-
private :check_readable
|
86
|
-
|
87
|
-
def check_writable
|
88
|
-
raise IOError, "not opened for writing" unless @writable
|
89
|
-
raise IOError, "unable to modify data" if @__data__.string.frozen?
|
90
|
-
end
|
91
|
-
|
92
|
-
private :check_writable
|
93
|
-
|
94
|
-
def set_encoding(external, internal=nil, options=nil)
|
95
|
-
encoding = external || Encoding.default_external
|
96
|
-
@__data__.encoding = encoding
|
97
|
-
@__data__.string.force_encoding(encoding)
|
98
|
-
end
|
99
|
-
|
100
|
-
def external_encoding
|
101
|
-
@__data__.encoding
|
102
|
-
end
|
103
|
-
|
104
|
-
def internal_encoding
|
105
|
-
nil
|
106
|
-
end
|
107
|
-
|
108
|
-
def each_byte
|
109
|
-
return to_enum :each_byte unless block_given?
|
110
|
-
check_readable
|
111
|
-
|
112
|
-
d = @__data__
|
113
|
-
string = d.string
|
114
|
-
|
115
|
-
while d.pos < string.length
|
116
|
-
byte = string.getbyte d.pos
|
117
|
-
d.pos += 1
|
118
|
-
yield byte
|
119
|
-
end
|
120
|
-
|
121
|
-
self
|
122
|
-
end
|
123
|
-
|
124
|
-
alias_method :bytes, :each_byte
|
125
|
-
|
126
|
-
def each_char
|
127
|
-
return to_enum :each_char unless block_given?
|
128
|
-
while s = getc
|
129
|
-
yield s
|
130
|
-
end
|
131
|
-
|
132
|
-
self
|
133
|
-
end
|
134
|
-
|
135
|
-
alias_method :chars, :each_char
|
136
|
-
|
137
|
-
def each_codepoint(&block)
|
138
|
-
return to_enum :each_codepoint unless block_given?
|
139
|
-
check_readable
|
140
|
-
|
141
|
-
d = @__data__
|
142
|
-
string = d.string
|
143
|
-
|
144
|
-
while d.pos < string.bytesize
|
145
|
-
char = string.chr_at d.pos
|
146
|
-
|
147
|
-
unless char
|
148
|
-
raise ArgumentError, "invalid byte sequence in #{d.encoding}"
|
149
|
-
end
|
150
|
-
|
151
|
-
d.pos += char.bytesize
|
152
|
-
yield char.ord
|
153
|
-
end
|
154
|
-
|
155
|
-
self
|
156
|
-
end
|
157
|
-
|
158
|
-
alias_method :codepoints, :each_codepoint
|
159
|
-
|
160
|
-
def each(sep=$/, limit=Undefined)
|
161
|
-
return to_enum :each, sep, limit unless block_given?
|
162
|
-
check_readable
|
163
|
-
|
164
|
-
while line = getline(true, sep, limit)
|
165
|
-
yield line
|
166
|
-
end
|
167
|
-
|
168
|
-
self
|
169
|
-
end
|
170
|
-
|
171
|
-
alias_method :each_line, :each
|
172
|
-
alias_method :lines, :each
|
173
|
-
|
174
|
-
def <<(str)
|
175
|
-
write(str)
|
176
|
-
self
|
177
|
-
end
|
178
|
-
|
179
|
-
def binmode
|
180
|
-
self
|
181
|
-
end
|
182
|
-
|
183
|
-
def write(str)
|
184
|
-
check_writable
|
185
|
-
|
186
|
-
str = String(str)
|
187
|
-
return 0 if str.empty?
|
188
|
-
|
189
|
-
d = @__data__
|
190
|
-
pos = d.pos
|
191
|
-
string = d.string
|
192
|
-
|
193
|
-
if @append || pos == string.bytesize
|
194
|
-
string.byte_append str
|
195
|
-
d.pos = string.bytesize
|
196
|
-
elsif pos > string.bytesize
|
197
|
-
m = Rubinius::Mirror.reflect string
|
198
|
-
m.splice string.bytesize, 0, "\000" * (pos - string.bytesize)
|
199
|
-
string.byte_append str
|
200
|
-
d.pos = string.bytesize
|
201
|
-
else
|
202
|
-
stop = string.bytesize - pos
|
203
|
-
if str.bytesize < stop
|
204
|
-
stop = str.bytesize
|
205
|
-
end
|
206
|
-
m = Rubinius::Mirror.reflect string
|
207
|
-
m.splice pos, stop, str
|
208
|
-
d.pos += str.bytesize
|
209
|
-
string.taint if str.tainted?
|
210
|
-
end
|
211
|
-
|
212
|
-
str.bytesize
|
213
|
-
end
|
214
|
-
alias_method :syswrite, :write
|
215
|
-
alias_method :write_nonblock, :write
|
216
|
-
|
217
|
-
def close
|
218
|
-
raise IOError, "closed stream" if closed?
|
219
|
-
@readable = @writable = nil
|
220
|
-
end
|
221
|
-
|
222
|
-
def closed?
|
223
|
-
!@readable && !@writable
|
224
|
-
end
|
225
|
-
|
226
|
-
def close_read
|
227
|
-
check_readable
|
228
|
-
@readable = nil
|
229
|
-
end
|
230
|
-
|
231
|
-
def closed_read?
|
232
|
-
!@readable
|
233
|
-
end
|
234
|
-
|
235
|
-
def close_write
|
236
|
-
check_writable
|
237
|
-
@writable = nil
|
238
|
-
end
|
239
|
-
|
240
|
-
def closed_write?
|
241
|
-
!@writable
|
242
|
-
end
|
243
|
-
|
244
|
-
def eof?
|
245
|
-
d = @__data__
|
246
|
-
d.pos >= d.string.bytesize
|
247
|
-
end
|
248
|
-
alias_method :eof, :eof?
|
249
|
-
|
250
|
-
def fcntl
|
251
|
-
raise NotImplementedError, "StringIO#fcntl is not implemented"
|
252
|
-
end
|
253
|
-
|
254
|
-
def fileno
|
255
|
-
nil
|
256
|
-
end
|
257
|
-
|
258
|
-
def flush
|
259
|
-
self
|
260
|
-
end
|
261
|
-
|
262
|
-
def fsync
|
263
|
-
0
|
264
|
-
end
|
265
|
-
|
266
|
-
def getc
|
267
|
-
check_readable
|
268
|
-
d = @__data__
|
269
|
-
|
270
|
-
return nil if eof?
|
271
|
-
|
272
|
-
char = d.string.find_character(d.pos)
|
273
|
-
d.pos += char.bytesize
|
274
|
-
char
|
275
|
-
end
|
276
|
-
|
277
|
-
def getbyte
|
278
|
-
check_readable
|
279
|
-
d = @__data__
|
280
|
-
|
281
|
-
return nil if eof?
|
282
|
-
|
283
|
-
byte = d.string.getbyte(d.pos)
|
284
|
-
d.pos += 1
|
285
|
-
byte
|
286
|
-
end
|
287
|
-
|
288
|
-
def gets(sep=$/, limit=Undefined)
|
289
|
-
check_readable
|
290
|
-
|
291
|
-
$_ = getline(false, sep, limit)
|
292
|
-
end
|
293
|
-
|
294
|
-
def isatty
|
295
|
-
false
|
296
|
-
end
|
297
|
-
alias_method :tty?, :isatty
|
298
|
-
|
299
|
-
def lineno
|
300
|
-
@__data__.lineno
|
301
|
-
end
|
302
|
-
|
303
|
-
def lineno=(line)
|
304
|
-
@__data__.lineno = line
|
305
|
-
end
|
306
|
-
|
307
|
-
def pid
|
308
|
-
nil
|
309
|
-
end
|
310
|
-
|
311
|
-
def pos
|
312
|
-
@__data__.pos
|
313
|
-
end
|
314
|
-
|
315
|
-
def pos=(pos)
|
316
|
-
raise Errno::EINVAL if pos < 0
|
317
|
-
@__data__.pos = pos
|
318
|
-
end
|
319
|
-
|
320
|
-
def print(*args)
|
321
|
-
check_writable
|
322
|
-
args << $_ if args.empty?
|
323
|
-
write((args << $\).flatten.join)
|
324
|
-
nil
|
325
|
-
end
|
326
|
-
|
327
|
-
def printf(*args)
|
328
|
-
check_writable
|
329
|
-
|
330
|
-
if args.size > 1
|
331
|
-
write(args.shift % args)
|
332
|
-
else
|
333
|
-
write(args.first)
|
334
|
-
end
|
335
|
-
|
336
|
-
nil
|
337
|
-
end
|
338
|
-
|
339
|
-
def putc(obj)
|
340
|
-
check_writable
|
341
|
-
|
342
|
-
if obj.is_a?(String)
|
343
|
-
char = obj[0]
|
344
|
-
else
|
345
|
-
c = Rubinius::Type.coerce_to obj, Integer, :to_int
|
346
|
-
char = (c & 0xff).chr
|
347
|
-
end
|
348
|
-
|
349
|
-
d = @__data__
|
350
|
-
pos = d.pos
|
351
|
-
string = d.string
|
352
|
-
|
353
|
-
if @append || pos == string.bytesize
|
354
|
-
string.byte_append char
|
355
|
-
d.pos = string.bytesize
|
356
|
-
elsif pos > string.bytesize
|
357
|
-
m = Rubinius::Mirror.reflect string
|
358
|
-
m.splice string.bytesize, 0, "\000" * (pos - string.bytesize)
|
359
|
-
string.byte_append char
|
360
|
-
d.pos = string.bytesize
|
361
|
-
else
|
362
|
-
m = Rubinius::Mirror.reflect string
|
363
|
-
m.splice pos, char.bytesize, char
|
364
|
-
d.pos += char.bytesize
|
365
|
-
end
|
366
|
-
|
367
|
-
obj
|
368
|
-
end
|
369
|
-
|
370
|
-
def puts(*args)
|
371
|
-
if args.empty?
|
372
|
-
write(DEFAULT_RECORD_SEPARATOR)
|
373
|
-
else
|
374
|
-
args.each do |arg|
|
375
|
-
if arg.nil?
|
376
|
-
line = ""
|
377
|
-
elsif Thread.guarding? arg
|
378
|
-
line = "[...]"
|
379
|
-
else
|
380
|
-
begin
|
381
|
-
arg = Rubinius::Type.coerce_to(arg, Array, :to_ary)
|
382
|
-
Thread.recursion_guard arg do
|
383
|
-
arg.each { |a| puts a }
|
384
|
-
end
|
385
|
-
next
|
386
|
-
rescue
|
387
|
-
line = arg.to_s
|
388
|
-
end
|
389
|
-
end
|
390
|
-
|
391
|
-
write(line)
|
392
|
-
write(DEFAULT_RECORD_SEPARATOR) unless line[-1] == ?\n
|
393
|
-
end
|
394
|
-
end
|
395
|
-
|
396
|
-
nil
|
397
|
-
end
|
398
|
-
|
399
|
-
def read(length=nil, buffer=nil)
|
400
|
-
check_readable
|
401
|
-
d = @__data__
|
402
|
-
pos = d.pos
|
403
|
-
string = d.string
|
404
|
-
|
405
|
-
if length
|
406
|
-
length = Rubinius::Type.coerce_to length, Integer, :to_int
|
407
|
-
raise ArgumentError if length < 0
|
408
|
-
|
409
|
-
buffer = StringValue(buffer) if buffer
|
410
|
-
|
411
|
-
if eof?
|
412
|
-
buffer.clear if buffer
|
413
|
-
if length == 0
|
414
|
-
return "".force_encoding(Encoding::ASCII_8BIT)
|
415
|
-
else
|
416
|
-
return nil
|
417
|
-
end
|
418
|
-
end
|
419
|
-
|
420
|
-
str = string.byteslice(pos, length)
|
421
|
-
str.force_encoding Encoding::ASCII_8BIT
|
422
|
-
|
423
|
-
str = buffer.replace(str) if buffer
|
424
|
-
else
|
425
|
-
if eof?
|
426
|
-
buffer.clear if buffer
|
427
|
-
return "".force_encoding(Encoding::ASCII_8BIT)
|
428
|
-
end
|
429
|
-
|
430
|
-
str = string.byteslice(pos..-1)
|
431
|
-
buffer.replace str if buffer
|
432
|
-
end
|
433
|
-
|
434
|
-
d.pos += str.length
|
435
|
-
return str
|
436
|
-
end
|
437
|
-
|
438
|
-
def readchar
|
439
|
-
raise IO::EOFError, "end of file reached" if eof?
|
440
|
-
getc
|
441
|
-
end
|
442
|
-
|
443
|
-
def readbyte
|
444
|
-
readchar.getbyte(0)
|
445
|
-
end
|
446
|
-
|
447
|
-
def readline(sep=$/, limit=Undefined)
|
448
|
-
check_readable
|
449
|
-
raise IO::EOFError, "end of file reached" if eof?
|
450
|
-
|
451
|
-
$_ = getline(true, sep, limit)
|
452
|
-
end
|
453
|
-
|
454
|
-
def readlines(sep=$/, limit=Undefined)
|
455
|
-
check_readable
|
456
|
-
|
457
|
-
ary = []
|
458
|
-
while line = getline(true, sep, limit)
|
459
|
-
ary << line
|
460
|
-
end
|
461
|
-
|
462
|
-
ary
|
463
|
-
end
|
464
|
-
|
465
|
-
def reopen(string=nil, mode=Undefined)
|
466
|
-
if string and not string.kind_of? String and mode.equal? Undefined
|
467
|
-
stringio = Rubinius::Type.coerce_to(string, StringIO, :to_strio)
|
468
|
-
|
469
|
-
taint if stringio.tainted?
|
470
|
-
initialize_copy stringio
|
471
|
-
else
|
472
|
-
mode = nil if mode.equal? Undefined
|
473
|
-
string = "" unless string
|
474
|
-
|
475
|
-
initialize string, mode
|
476
|
-
end
|
477
|
-
|
478
|
-
self
|
479
|
-
end
|
480
|
-
|
481
|
-
def rewind
|
482
|
-
d = @__data__
|
483
|
-
d.pos = d.lineno = 0
|
484
|
-
end
|
485
|
-
|
486
|
-
def seek(to, whence = IO::SEEK_SET)
|
487
|
-
raise IOError, "closed stream" if self.closed?
|
488
|
-
to = Rubinius::Type.coerce_to to, Integer, :to_int
|
489
|
-
|
490
|
-
case whence
|
491
|
-
when IO::SEEK_CUR
|
492
|
-
to += @__data__.pos
|
493
|
-
when IO::SEEK_END
|
494
|
-
to += @__data__.string.bytesize
|
495
|
-
when IO::SEEK_SET, nil
|
496
|
-
else
|
497
|
-
raise Errno::EINVAL, "invalid whence"
|
498
|
-
end
|
499
|
-
|
500
|
-
raise Errno::EINVAL if to < 0
|
501
|
-
|
502
|
-
@__data__.pos = to
|
503
|
-
|
504
|
-
return 0
|
505
|
-
end
|
506
|
-
|
507
|
-
def size
|
508
|
-
@__data__.string.bytesize
|
509
|
-
end
|
510
|
-
alias_method :length, :size
|
511
|
-
|
512
|
-
def string
|
513
|
-
@__data__.string
|
514
|
-
end
|
515
|
-
|
516
|
-
def string=(string)
|
517
|
-
d = @__data__
|
518
|
-
d.string = StringValue(string)
|
519
|
-
d.pos = 0
|
520
|
-
d.lineno = 0
|
521
|
-
end
|
522
|
-
|
523
|
-
def sync
|
524
|
-
true
|
525
|
-
end
|
526
|
-
|
527
|
-
def sync=(val)
|
528
|
-
val
|
529
|
-
end
|
530
|
-
|
531
|
-
def sysread(length=nil, buffer="")
|
532
|
-
str = read(length, buffer)
|
533
|
-
|
534
|
-
if str.nil?
|
535
|
-
buffer.clear
|
536
|
-
raise IO::EOFError, "end of file reached"
|
537
|
-
end
|
538
|
-
|
539
|
-
str
|
540
|
-
end
|
541
|
-
|
542
|
-
alias_method :readpartial, :sysread
|
543
|
-
alias_method :read_nonblock, :sysread
|
544
|
-
|
545
|
-
def tell
|
546
|
-
@__data__.pos
|
547
|
-
end
|
548
|
-
|
549
|
-
def truncate(length)
|
550
|
-
check_writable
|
551
|
-
len = Rubinius::Type.coerce_to length, Integer, :to_int
|
552
|
-
raise Errno::EINVAL, "negative length" if len < 0
|
553
|
-
string = @__data__.string
|
554
|
-
|
555
|
-
if len < string.bytesize
|
556
|
-
string[len..string.bytesize] = ""
|
557
|
-
else
|
558
|
-
string << "\000" * (len - string.bytesize)
|
559
|
-
end
|
560
|
-
return length
|
561
|
-
end
|
562
|
-
|
563
|
-
def ungetc(char)
|
564
|
-
check_readable
|
565
|
-
|
566
|
-
d = @__data__
|
567
|
-
pos = d.pos
|
568
|
-
string = d.string
|
569
|
-
|
570
|
-
if char.kind_of? Integer
|
571
|
-
char = Rubinius::Type.coerce_to char, String, :chr
|
572
|
-
else
|
573
|
-
char = Rubinius::Type.coerce_to char, String, :to_str
|
574
|
-
end
|
575
|
-
|
576
|
-
if pos > string.bytesize
|
577
|
-
string[string.bytesize..pos] = "\000" * (pos - string.bytesize)
|
578
|
-
d.pos -= 1
|
579
|
-
string[d.pos] = char
|
580
|
-
elsif pos > 0
|
581
|
-
d.pos -= 1
|
582
|
-
string[d.pos] = char
|
583
|
-
end
|
584
|
-
|
585
|
-
nil
|
586
|
-
end
|
587
|
-
|
588
|
-
def ungetbyte(bytes)
|
589
|
-
check_readable
|
590
|
-
|
591
|
-
return unless bytes
|
592
|
-
|
593
|
-
if bytes.kind_of? Fixnum
|
594
|
-
bytes = "" << bytes
|
595
|
-
else
|
596
|
-
bytes = StringValue(bytes)
|
597
|
-
return if bytes.bytesize == 0
|
598
|
-
end
|
599
|
-
|
600
|
-
d = @__data__
|
601
|
-
pos = d.pos
|
602
|
-
string = d.string
|
603
|
-
|
604
|
-
enc = string.encoding
|
605
|
-
|
606
|
-
if d.pos == 0
|
607
|
-
d.string = "#{bytes}#{string}"
|
608
|
-
else
|
609
|
-
size = bytes.bytesize
|
610
|
-
a = string.byteslice(0, pos - size) if size < pos
|
611
|
-
b = string.byteslice(pos..-1)
|
612
|
-
d.string = "#{a}#{bytes}#{b}"
|
613
|
-
d.pos = pos > size ? pos - size : 0
|
614
|
-
end
|
615
|
-
|
616
|
-
d.string.force_encoding enc
|
617
|
-
nil
|
618
|
-
end
|
619
|
-
|
620
|
-
def encode_with(coder)
|
621
|
-
end
|
622
|
-
|
623
|
-
def init_with(coder)
|
624
|
-
@__data__ = Data.new("")
|
625
|
-
end
|
626
|
-
|
627
|
-
def to_yaml_properties
|
628
|
-
[]
|
629
|
-
end
|
630
|
-
|
631
|
-
def yaml_initialize(type, val)
|
632
|
-
@__data__ = Data.new("")
|
633
|
-
end
|
634
|
-
|
635
|
-
protected
|
636
|
-
|
637
|
-
def mode_from_string(mode)
|
638
|
-
@append = truncate = false
|
639
|
-
|
640
|
-
if mode[0] == ?r
|
641
|
-
@readable = true
|
642
|
-
@writable = mode[-1] == ?+ ? true : false
|
643
|
-
end
|
644
|
-
|
645
|
-
if mode[0] == ?w
|
646
|
-
@writable = truncate = true
|
647
|
-
@readable = mode[-1] == ?+ ? true : false
|
648
|
-
end
|
649
|
-
|
650
|
-
if mode[0] == ?a
|
651
|
-
@append = @writable = true
|
652
|
-
@readable = mode[-1] == ?+ ? true : false
|
653
|
-
end
|
654
|
-
|
655
|
-
d = @__data__
|
656
|
-
raise Errno::EACCES, "Permission denied" if @writable && d.string.frozen?
|
657
|
-
d.string.replace("") if truncate
|
658
|
-
end
|
659
|
-
|
660
|
-
def mode_from_integer(mode)
|
661
|
-
@readable = @writable = @append = false
|
662
|
-
d = @__data__
|
663
|
-
|
664
|
-
if mode == 0 or mode & IO::RDWR != 0
|
665
|
-
@readable = true
|
666
|
-
end
|
667
|
-
|
668
|
-
if mode & (IO::WRONLY | IO::RDWR) != 0
|
669
|
-
raise Errno::EACCES, "Permission denied" if d.string.frozen?
|
670
|
-
@writable = true
|
671
|
-
end
|
672
|
-
|
673
|
-
@append = true if (mode & IO::APPEND) != 0
|
674
|
-
d.string.replace("") if (mode & IO::TRUNC) != 0
|
675
|
-
end
|
676
|
-
|
677
|
-
def getline(arg_error, sep, limit)
|
678
|
-
if limit != Undefined
|
679
|
-
limit = Rubinius::Type.coerce_to limit, Fixnum, :to_int
|
680
|
-
sep = Rubinius::Type.coerce_to sep, String, :to_str if sep
|
681
|
-
else
|
682
|
-
limit = nil
|
683
|
-
|
684
|
-
unless sep == $/ or sep.nil?
|
685
|
-
osep = sep
|
686
|
-
sep = Rubinius::Type.check_convert_type sep, String, :to_str
|
687
|
-
limit = Rubinius::Type.coerce_to osep, Fixnum, :to_int unless sep
|
688
|
-
end
|
689
|
-
end
|
690
|
-
|
691
|
-
raise ArgumentError if arg_error and limit == 0
|
692
|
-
|
693
|
-
return nil if eof?
|
694
|
-
|
695
|
-
d = @__data__
|
696
|
-
pos = d.pos
|
697
|
-
string = d.string
|
698
|
-
|
699
|
-
if sep.nil?
|
700
|
-
if limit
|
701
|
-
line = string.byteslice(pos, limit)
|
702
|
-
else
|
703
|
-
line = string.byteslice(pos, string.bytesize - pos)
|
704
|
-
end
|
705
|
-
d.pos += line.bytesize
|
706
|
-
elsif sep.empty?
|
707
|
-
if stop = string.find_string("\n\n", pos)
|
708
|
-
stop += 2
|
709
|
-
line = string.byteslice(pos, stop - pos)
|
710
|
-
while string.getbyte(stop) == 10
|
711
|
-
stop += 1
|
712
|
-
end
|
713
|
-
d.pos = stop
|
714
|
-
else
|
715
|
-
line = string.byteslice(pos, string.bytesize - pos)
|
716
|
-
d.pos = string.bytesize
|
717
|
-
end
|
718
|
-
else
|
719
|
-
if stop = string.find_string(sep, pos)
|
720
|
-
if limit && stop - pos >= limit
|
721
|
-
stop = pos + limit
|
722
|
-
else
|
723
|
-
stop += sep.bytesize
|
724
|
-
end
|
725
|
-
line = string.byteslice(pos, stop - pos)
|
726
|
-
d.pos = stop
|
727
|
-
else
|
728
|
-
if limit
|
729
|
-
line = string.byteslice(pos, limit)
|
730
|
-
else
|
731
|
-
line = string.byteslice(pos, string.bytesize - pos)
|
732
|
-
end
|
733
|
-
d.pos += line.bytesize
|
734
|
-
end
|
735
|
-
end
|
736
|
-
|
737
|
-
d.lineno += 1
|
738
|
-
|
739
|
-
return line
|
740
|
-
end
|
741
|
-
end
|