Class: REXML::Source
Overview
A Source can be searched for patterns, and wraps buffers and other objects and provides consumption of text
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Object) buffer
readonly
The current buffer (what we're going to read next).
-
- (Object) encoding
Returns the value of attribute encoding.
-
- (Object) line
readonly
The line number of the last consumed text.
Instance Method Summary (collapse)
- - (Object) consume(pattern)
-
- (Object) current_line
The current line in the source.
-
- (Boolean) empty?
True if the Source is exhausted.
-
- (Source) initialize(arg, encoding = nil)
constructor
Constructor value, overriding all encoding detection.
- - (Object) match(pattern, cons = false)
- - (Object) match_to(char, pattern)
- - (Object) match_to_consume(char, pattern)
- - (Object) position
- - (Object) read
-
- (Object) scan(pattern, cons = false)
Scans the source for a given pattern.
Methods included from Encoding
Constructor Details
- (Source) initialize(arg, encoding = nil)
Constructor value, overriding all encoding detection
42 43 44 45 46 47 48 49 50 |
# File 'lib/rexml/source.rb', line 42 def initialize(arg, encoding=nil) @orig = @buffer = arg if encoding self.encoding = encoding else detect_encoding end @line = 0 end |
Instance Attribute Details
- (Object) buffer (readonly)
The current buffer (what we're going to read next)
33 34 35 |
# File 'lib/rexml/source.rb', line 33 def buffer @buffer end |
- (Object) encoding
Returns the value of attribute encoding
36 37 38 |
# File 'lib/rexml/source.rb', line 36 def encoding @encoding end |
- (Object) line (readonly)
The line number of the last consumed text
35 36 37 |
# File 'lib/rexml/source.rb', line 35 def line @line end |
Instance Method Details
- (Object) consume(pattern)
86 87 88 |
# File 'lib/rexml/source.rb', line 86 def consume( pattern ) @buffer = $' if pattern.match( @buffer ) end |
- (Object) current_line
The current line in the source
116 117 118 119 120 121 |
# File 'lib/rexml/source.rb', line 116 def current_line lines = @orig.split res = lines.grep @buffer[0..30] res = res[-1] if res.kind_of? Array lines.index( res ) if res end |
- (Boolean) empty?
True if the Source is exhausted
107 108 109 |
# File 'lib/rexml/source.rb', line 107 def empty? @buffer == "" end |
- (Object) match(pattern, cons = false)
100 101 102 103 104 |
# File 'lib/rexml/source.rb', line 100 def match(pattern, cons=false) md = pattern.match(@buffer) @buffer = $' if cons and md return md end |
- (Object) match_to(char, pattern)
90 91 92 |
# File 'lib/rexml/source.rb', line 90 def match_to( char, pattern ) return pattern.match(@buffer) end |
- (Object) match_to_consume(char, pattern)
94 95 96 97 98 |
# File 'lib/rexml/source.rb', line 94 def match_to_consume( char, pattern ) md = pattern.match(@buffer) @buffer = $' return md end |
- (Object) position
111 112 113 |
# File 'lib/rexml/source.rb', line 111 def position @orig.index( @buffer ) end |
- (Object) read
83 84 |
# File 'lib/rexml/source.rb', line 83 def read end |
- (Object) scan(pattern, cons = false)
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. /^s*(#pattern, with no groups)(.*)/. The first group will be returned; the second group is used if the consume flag is set. everything after it in the Source. pattern is not found.
76 77 78 79 80 81 |
# File 'lib/rexml/source.rb', line 76 def scan(pattern, cons=false) return nil if @buffer.nil? rv = @buffer.scan(pattern) @buffer = $' if cons and rv.size>0 rv end |