Class: REXML::Source
- Includes:
- Encoding
- Defined in:
- lib/extensions/rexml/rexml/source.rb,
lib/extensions/rhoxml/rexml/source.rb more...
Overview
A Source can be searched for patterns, and wraps buffers and other objects and provides consumption of text
Direct Known Subclasses
Constant Summary
Constants included from Encoding
Encoding::UNILE, Encoding::UTF_16, Encoding::UTF_8
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
The current buffer (what we’re going to read next).
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#line ⇒ Object
readonly
The line number of the last consumed text.
Instance Method Summary collapse
- #consume(pattern) ⇒ Object
-
#current_line ⇒ Object
The current line in the source.
-
#empty? ⇒ Boolean
True if the Source is exhausted.
-
#initialize(arg, encoding = nil) ⇒ Source
constructor
Constructor value, overriding all encoding detection.
- #match(pattern, cons = false) ⇒ Object
- #match_to(char, pattern) ⇒ Object
- #match_to_consume(char, pattern) ⇒ Object
- #position ⇒ Object
- #read ⇒ Object
-
#scan(pattern, cons = false) ⇒ Object
Scans the source for a given pattern.
Methods included from Encoding
apply, #check_encoding, #decode_ascii, #decode_cp1252, #decode_iconv, #decode_unile, #decode_utf16, #decode_utf8, #encode_ascii, #encode_cp1252, #encode_iconv, #encode_unile, #encode_utf16, #encode_utf8, encoding_method, #from_iso_8859_15, register, #to_iso_8859_15
Constructor Details
permalink #initialize(arg, encoding = nil) ⇒ Source
Constructor value, overriding all encoding detection
41 42 43 44 45 46 47 48 49 |
# File 'lib/extensions/rexml/rexml/source.rb', line 41 def initialize(arg, encoding=nil) @orig = @buffer = arg if encoding self.encoding = encoding else self.encoding = check_encoding( @buffer ) end @line = 0 end |
Instance Attribute Details
permalink #buffer ⇒ Object (readonly)
The current buffer (what we’re going to read next)
32 33 34 |
# File 'lib/extensions/rexml/rexml/source.rb', line 32 def buffer @buffer end |
Instance Method Details
permalink #consume(pattern) ⇒ Object
[View source]
94 95 96 |
# File 'lib/extensions/rexml/rexml/source.rb', line 94 def consume( pattern ) @buffer = $' if pattern.match( @buffer ) end |
permalink #current_line ⇒ Object
Returns the current line in the source.
124 125 126 127 128 129 |
# File 'lib/extensions/rexml/rexml/source.rb', line 124 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 |
permalink #empty? ⇒ Boolean
Returns true if the Source is exhausted.
115 116 117 |
# File 'lib/extensions/rexml/rexml/source.rb', line 115 def empty? @buffer == "" end |
permalink #match(pattern, cons = false) ⇒ Object
[View source]
108 109 110 111 112 |
# File 'lib/extensions/rexml/rexml/source.rb', line 108 def match(pattern, cons=false) md = pattern.match(@buffer) @buffer = $' if cons and md return md end |
permalink #match_to(char, pattern) ⇒ Object
[View source]
98 99 100 |
# File 'lib/extensions/rexml/rexml/source.rb', line 98 def match_to( char, pattern ) return pattern.match(@buffer) end |
permalink #match_to_consume(char, pattern) ⇒ Object
[View source]
102 103 104 105 106 |
# File 'lib/extensions/rexml/rexml/source.rb', line 102 def match_to_consume( char, pattern ) md = pattern.match(@buffer) @buffer = $' return md end |
permalink #position ⇒ Object
[View source]
119 120 121 |
# File 'lib/extensions/rexml/rexml/source.rb', line 119 def position @orig.index( @buffer ) end |
permalink #read ⇒ Object
[View source]
91 92 |
# File 'lib/extensions/rexml/rexml/source.rb', line 91 def read end |
permalink #scan(pattern, cons = false) ⇒ Object
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.
84 85 86 87 88 89 |
# File 'lib/extensions/rexml/rexml/source.rb', line 84 def scan(pattern, cons=false) return nil if @buffer.nil? rv = @buffer.scan(pattern) @buffer = $' if cons and rv.size>0 rv end |