Class: StringInput

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tmail-pure/stringio.rb

Overview

stringio.rb

Copyright © 1999-2004 Minero Aoki

This program is free software. You can distribute/modify this program under the terms of the GNU Lesser General Public License version 2.1.

$amstdId: stringio.rb,v 1.12 2004/02/20 00:31:14 aamine Exp $

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#reject, #sort_by

Constructor Details

#initialize(str) ⇒ StringInput

Returns a new instance of StringInput.



34
35
36
37
38
39
# File 'lib/tmail-pure/stringio.rb', line 34

def initialize(str)
  @src = str
  @pos = 0
  @closed = false
  @lineno = 0
end

Instance Attribute Details

#linenoObject (readonly)

Returns the value of attribute lineno.



41
42
43
# File 'lib/tmail-pure/stringio.rb', line 41

def lineno
  @lineno
end

Class Method Details

.new(str) ⇒ Object Also known as: open



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tmail-pure/stringio.rb', line 18

def new(str)
  if block_given?
    begin
      f = super
      yield f
    ensure
      f.close if f
    end
  else
    super
  end
end

Instance Method Details

#closeObject



51
52
53
54
55
# File 'lib/tmail-pure/stringio.rb', line 51

def close
  stream_check!
  @pos = nil
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/tmail-pure/stringio.rb', line 57

def closed?
  @closed
end

#each(&block) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/tmail-pure/stringio.rb', line 95

def each(&block)
  stream_check!
  begin
    @src.each(&block)
  ensure
    @pos = 0
  end
end

#eof?Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/tmail-pure/stringio.rb', line 90

def eof?
  stream_check!
  @pos > @src.size
end

#getcObject



120
121
122
123
124
125
126
# File 'lib/tmail-pure/stringio.rb', line 120

def getc
  stream_check!
  ch = @src[@pos]
  @pos += 1
  @pos += 1 if @pos == @src.size
  ch
end

#getsObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/tmail-pure/stringio.rb', line 104

def gets
  stream_check!
  if idx = @src.index(?\n, @pos)
    idx += 1  # "\n".size
    line = @src[ @pos ... idx ]
    @pos = idx
    @pos += 1 if @pos == @src.size
  else
    line = @src[ @pos .. -1 ]
    @pos = @src.size + 1
  end
  @lineno += 1

  line
end

#inspectObject



47
48
49
# File 'lib/tmail-pure/stringio.rb', line 47

def inspect
  "#<#{self.class}:#{@closed ? 'closed' : 'open'},src=#{@src[0,30].inspect}>"
end

#posObject Also known as: tell



61
62
63
64
# File 'lib/tmail-pure/stringio.rb', line 61

def pos
  stream_check!
  [@pos, @src.size].min
end

#read(len = nil) ⇒ Object Also known as: sysread



128
129
130
131
132
133
134
135
# File 'lib/tmail-pure/stringio.rb', line 128

def read(len = nil)
  stream_check!
  return read_all() unless len
  str = @src[@pos, len]
  @pos += len
  @pos += 1 if @pos == @src.size
  str
end

#read_allObject



139
140
141
142
143
144
145
# File 'lib/tmail-pure/stringio.rb', line 139

def read_all
  stream_check!
  return nil if eof?
  rest = @src[@pos ... @src.size]
  @pos = @src.size + 1
  rest
end

#rewindObject



85
86
87
88
# File 'lib/tmail-pure/stringio.rb', line 85

def rewind
  stream_check!
  @pos = 0
end

#seek(offset, whence = IO::SEEK_SET) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tmail-pure/stringio.rb', line 68

def seek(offset, whence = IO::SEEK_SET)
  stream_check!
  case whence
  when IO::SEEK_SET
    @pos = offset
  when IO::SEEK_CUR
    @pos += offset
  when IO::SEEK_END
    @pos = @src.size - offset
  else
    raise ArgumentError, "unknown seek flag: #{whence}"
  end
  @pos = 0 if @pos < 0
  @pos = [@pos, @src.size + 1].min
  offset
end

#stream_check!Object



147
148
149
# File 'lib/tmail-pure/stringio.rb', line 147

def stream_check!
  @closed and raise IOError, 'closed stream'
end

#stringObject



43
44
45
# File 'lib/tmail-pure/stringio.rb', line 43

def string
  @src
end