-
-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- v1.8.3.rc1
- v1.8.2
- v1.8.1
- v1.8.0
- v1.8.0.beta1
- v1.8.0.alpha1
- v1.7.4
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- v1.7.0.rc1
- v1.6.1
- v1.6.0
- v1.6.0.rc1
- v1.6.0.alpha1
- v1.5.1
- v1.5.0
- v1.5.0.rc1
- v1.4.1
- v1.4.0
- v1.4.0.alpha1
- v1.3.2
- v1.3.1
- v1.3.0
- v1.3.0.rc1
- v1.3.0.alpha1
- v1.2.0
- v1.2.0.beta1
- v1.1.1
- v1.1.1.rc1
- v1.1.0
- v1.1.0.rc1
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v1.0.0.beta1
- v0.11.4
- v0.11.3
- v0.11.2
- v0.11.1
- v0.11.1.pre
- v0.11.0
- v0.11.0.rc1
- v0.10.6
- v0.10.6.beta
- v0.10.5
- v0.10.4
- v0.10.3
- v0.10.2
- v0.10.1
- v0.10.0
- v0.10.0.rc2
- v0.10.0.rc1
- v0.10.0.beta5
- v0.10.0.beta4
- v0.10.0.beta3
- v0.10.0.beta2
- v0.10.0.beta1
- v0.9.4
- v0.9.3
- v0.9.2
- v0.9.1
- v0.9.0
- v0.9.0.rc1
- v0.9.0.beta2
- v0.9.0.beta1
- v0.8.1
- v0.8.1.rc1
- v0.8.0
- v0.8.0.rc3
- v0.8.0.rc2
- v0.8.0.rc1
- v0.8.0.beta1
- v0.7.2
- v0.7.1
- v0.7.0
- v0.7.0.rc1
- v0.7.0.beta3
- v0.7.0.beta2
- v0.7.0.beta1
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.5
- v0.5.4
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.4
- v0.0.1-alpha.1
- npm-0.0.1-alpha.8
- npm-0.0.1-alpha.7
- npm-0.0.1-alpha.6
- npm-0.0.1-alpha.5
- npm-0.0.1-alpha.4
- npm-0.0.1-alpha.3
- lerna-0.0.1-alpha.2
Showing
4 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class IO | ||
SEEK_SET = 0 | ||
SEEK_CUR = 1 | ||
SEEK_END = 2 | ||
|
||
module Writable | ||
def <<(string) | ||
write(string) | ||
|
||
self | ||
end | ||
|
||
def print(*args) | ||
write args.map { |arg| String(arg) }.join($,) | ||
end | ||
|
||
def puts(*args) | ||
write args.map { |arg| String(arg) }.join($/) | ||
end | ||
end | ||
|
||
module Readable | ||
def readbyte | ||
getbyte | ||
end | ||
|
||
def readchar | ||
getc | ||
end | ||
|
||
def readline(sep = $/) | ||
raise NotImplementedError | ||
end | ||
|
||
def readpartial(integer, outbuf = nil) | ||
raise NotImplementedError | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
class StringIO < IO | ||
include IO::Readable | ||
include IO::Writable | ||
|
||
def self.open(string = "", mode = nil, &block) | ||
io = new(string, mode) | ||
res = block.call(io) | ||
io.close | ||
|
||
res | ||
end | ||
|
||
attr_accessor :string | ||
|
||
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 | ||
|
||
def eof? | ||
check_readable | ||
|
||
@position == @string.length | ||
end | ||
|
||
alias eof eof? | ||
|
||
def seek(pos, whence = IO::SEEK_SET) | ||
case whence | ||
when IO::SEEK_SET | ||
@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 | ||
|
||
def tell | ||
@position | ||
end | ||
|
||
alias pos tell | ||
|
||
alias pos= seek | ||
|
||
def rewind | ||
seek 0 | ||
end | ||
|
||
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 | ||
|
||
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 | ||
|
||
def write(string) | ||
check_writable | ||
|
||
string = String(string) | ||
|
||
if @string.length == @position | ||
@string += string | ||
else | ||
before = @string[0 .. @position - 1] | ||
after = @string[@position + string.length .. -1] | ||
|
||
@string = before + string + after | ||
end | ||
end | ||
|
||
def read(length = nil, outbuf = nil) | ||
check_readable | ||
|
||
return if eof? | ||
|
||
string = if length | ||
@string[@position, length] | ||
else | ||
@string[@position .. -1] | ||
end | ||
|
||
if outbuf | ||
outbuf.write(string) | ||
else | ||
string | ||
end | ||
end | ||
|
||
def close | ||
@closed = :both | ||
end | ||
|
||
def close_read | ||
if @closed == :write | ||
@closed = :both | ||
else | ||
@closed = :read | ||
end | ||
end | ||
|
||
def close_write | ||
if @closed == :read | ||
@closed = :both | ||
else | ||
@closed = :write | ||
end | ||
end | ||
|
||
def closed? | ||
@closed == :both | ||
end | ||
|
||
def closed_read? | ||
@closed == :read || @closed == :both | ||
end | ||
|
||
def closed_write? | ||
@closed == :write || @closed == :both | ||
end | ||
|
||
def check_writable | ||
if closed_write? | ||
raise IOError, "not opened for writing" | ||
end | ||
end | ||
|
||
def check_readable | ||
if closed_read? | ||
raise IOError, "not opened for reading" | ||
end | ||
end | ||
end |