4 = String handling class
8 # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
10 # Permission is hereby granted, free of charge, to any person obtaining
11 # a copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
18 # The above copyright notice and this permission notice shall be
19 # included in all copies or substantial portions of the Software.
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 # Note: Originally licensed under LGPL v2+. Using MIT license for Rails
30 # with permission of Minero Aoki.
33 class StringInput
#:nodoc:
70 "#<#{self.class}:#{@closed ? 'closed' : 'open'},src=#{@src[0,30].inspect}>"
90 def seek( offset
, whence
= IO
::SEEK_SET )
98 @pos = @src.size
- offset
100 raise ArgumentError
, "unknown seek flag: #{whence}"
103 @pos = [@pos, @src.size
+ 1].min
128 if idx
= @src.index(?\n, @pos)
130 line
= @src[ @pos ... idx
]
132 @pos += 1 if @pos == @src.size
134 line
= @src[ @pos .. -1 ]
146 @pos += 1 if @pos == @src.size
150 def read( len
= nil )
152 return read_all
unless len
153 str
= @src[@pos, len
]
155 @pos += 1 if @pos == @src.size
164 rest
= @src[@pos ... @src.size
]
170 @closed and raise IOError
, 'closed stream'
176 class StringOutput
#:nodoc:
197 def initialize( str
= '' )
224 "#<#{self.class}:#{@dest ? 'open' : 'closed'},#{object_id}>"
229 raise ArgumentError
, 'wrong # of argument (0 for >1)' if args
.empty
?
231 raise ArgumentError
, 'nil not allowed' if s
.nil?
240 @dest << (s
= str
.to_s
)
241 @dest << "\n" unless s
[-1] == ?\n
243 @dest << "\n" if args
.empty
?
255 @dest << sprintf(*args
)
277 @closed and raise IOError
, 'closed stream'