Sha256: a3f01600ec6e4ed8febc8b2eec4908eca91268b0fd636c698588a23acbb5d770

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

class StringIO < IO
  VERSION = "0"

  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 = 0

    super(nil, mode)
  end

  def eof?
    check_readable

    @position == @string.length
  end

  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

  def tell
    @position
  end

  def rewind
    seek 0
  end

  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

  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

  def sysread(length)
    check_readable

    read(length)
  end

  alias eof eof?
  alias pos tell
  alias pos= seek
  alias readpartial read
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
opal-1.8.3.rc1 stdlib/stringio.rb
opal-1.8.2 stdlib/stringio.rb
opal-1.8.1 stdlib/stringio.rb
opal-1.8.0 stdlib/stringio.rb
opal-1.8.0.beta1 stdlib/stringio.rb