Class | REXML::Source |
In: |
lib/rexml/source.rb
|
Parent: | Object |
A Source can be searched for patterns, and wraps buffers and other objects and provides consumption of text
buffer | [R] | The current buffer (what we‘re going to read next) |
encoding | [R] | |
line | [R] | The line number of the last consumed text |
Constructor @param arg must be a String, and should be a valid XML document @param encoding if non-null, sets the encoding of the source to this value, overriding all encoding detection
# File lib/rexml/source.rb, line 40 40: def initialize(arg, encoding=nil) 41: @orig = @buffer = arg 42: if encoding 43: self.encoding = encoding 44: else 45: self.encoding = check_encoding( @buffer ) 46: end 47: @line = 0 48: end
# File lib/rexml/source.rb, line 90 90: def consume( pattern ) 91: @buffer = $' if pattern.match( @buffer ) 92: end
@return the current line in the source
# File lib/rexml/source.rb, line 120 120: def current_line 121: lines = @orig.split 122: res = lines.grep @buffer[0..30] 123: res = res[-1] if res.kind_of? Array 124: lines.index( res ) if res 125: end
Inherited from Encoding Overridden to support optimized en/decoding
# File lib/rexml/source.rb, line 53 53: def encoding=(enc) 54: return unless super 55: @line_break = encode( '>' ) 56: if enc != UTF_8 57: @buffer = decode(@buffer) 58: @to_utf = true 59: else 60: @to_utf = false 61: end 62: end
# File lib/rexml/source.rb, line 104 104: def match(pattern, cons=false) 105: md = pattern.match(@buffer) 106: @buffer = $' if cons and md 107: return md 108: end
# File lib/rexml/source.rb, line 94 94: def match_to( char, pattern ) 95: return pattern.match(@buffer) 96: end
# File lib/rexml/source.rb, line 98 98: def match_to_consume( char, pattern ) 99: md = pattern.match(@buffer) 100: @buffer = $' 101: return md 102: end
Scans the source for a given pattern. Note, that this is not your usual scan() method. For one thing, the pattern argument has some requirements; for another, the source can be consumed. You can easily confuse this method. Originally, the patterns were easier to construct and this method more robust, because this method generated search regexes on the fly; however, this was computationally expensive and slowed down the entire REXML package considerably, since this is by far the most commonly called method. @param pattern must be a Regexp, and must be in the form of /^\s*(#{your pattern, with no groups})(.*)/. The first group will be returned; the second group is used if the consume flag is set. @param consume if true, the pattern returned will be consumed, leaving everything after it in the Source. @return the pattern, if found, or nil if the Source is empty or the pattern is not found.
# File lib/rexml/source.rb, line 80 80: def scan(pattern, cons=false) 81: return nil if @buffer.nil? 82: rv = @buffer.scan(pattern) 83: @buffer = $' if cons and rv.size>0 84: rv 85: end