class LazyString
- Author
-
Natsuki Kawai (natsuki.kawai@gmail.com)
- Copyright
-
Copyright © 2012 Natsuki Kawai
- License
-
2-clause BSDL or Ruby’s
Constants
- SubString
TODO: change name of
SubString
.
Attributes
length[R]
size[R]
Public Class Methods
new(*args)
click to toggle source
# File lib/lazy-string.rb, line 38 def initialize(*args) @size = 0 @chain = [] if args.size == 3 @chain << SubString.new(0, LazySubString.new(*args)) @size = args[2] end end
Public Instance Methods
<<(other)
click to toggle source
TODO: Add ‘add’ method to add a substring of a String.
# File lib/lazy-string.rb, line 49 def <<(other) if other.respond_to?(:to_int) return self << ('' << other) end @chain << SubString.new(@size, other) @size += other.size end
[](*args)
click to toggle source
# File lib/lazy-string.rb, line 58 def [](*args) if args.size == 2 start = args[0] last = start + args[1] length = args[1] e = @chain.each curr = nil begin curr = e.next end while curr.start + curr.value.size < start value = curr.value sub_start = start - curr.start sub_length = value.size - sub_start if sub_length >= length return LazyString.new(value, sub_start, length) else res = LazyString.new(value, sub_start, sub_length) length -= sub_length while length > 0 curr = e.next if length < curr.value.length res << LazyString.new(curr.value, 0, length) length = 0 else res << curr.value length -= curr.value.length end end return res end elsif args.respond_to?(:to_int) warn "#{self.class.name}#[pos] has not implemented yet. Calling #{self.class.name}#to_str." to_str[*args] else # args is one Range object. warn "#{self.class.name}#[range] has not implemented yet. Calling #{self.class.name}#to_str." to_str[*args] end end
to_str()
click to toggle source
# File lib/lazy-string.rb, line 105 def to_str res = '' @chain.each do |substr| res << substr.value end return res end
Also aliased as: to_s
unpack(template)
click to toggle source
# File lib/lazy-string.rb, line 99 def unpack(template) # TODO: Optimize here. # (Stop concat the front strings if template start with "x".) to_str.unpack(template) end