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
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#line ⇒ Object
readonly
The line number of the last consumed text.
Instance Method Summary collapse
-
#buffer ⇒ Object
The current buffer (what we’re going to read next).
- #buffer_encoding=(encoding) ⇒ Object
-
#current_line ⇒ Object
The current line in the source.
- #drop_parsed_content ⇒ Object
-
#empty? ⇒ Boolean
True if the Source is exhausted.
- #ensure_buffer ⇒ Object
-
#initialize(arg, encoding = nil) ⇒ Source
constructor
Constructor value, overriding all encoding detection.
- #match(pattern, cons = false) ⇒ Object
- #match?(pattern, cons = false) ⇒ Boolean
- #peek_byte ⇒ Object
- #position ⇒ Object
- #position=(pos) ⇒ Object
- #read(term = nil) ⇒ Object
- #read_until(term) ⇒ Object
- #scan_byte ⇒ Object
- #skip_spaces ⇒ Object
Methods included from Encoding
Constructor Details
#initialize(arg, encoding = nil) ⇒ Source
Constructor value, overriding all encoding detection
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rexml/source.rb', line 88 def initialize(arg, encoding=nil) @orig = arg @scanner = StringScanner.new(@orig) if encoding self.encoding = encoding else detect_encoding end @line = 0 @encoded_terms = {} end |
Instance Attribute Details
#encoding ⇒ Object
Returns the value of attribute encoding.
65 66 67 |
# File 'lib/rexml/source.rb', line 65 def encoding @encoding end |
#line ⇒ Object (readonly)
The line number of the last consumed text
64 65 66 |
# File 'lib/rexml/source.rb', line 64 def line @line end |
Instance Method Details
#buffer ⇒ Object
The current buffer (what we’re going to read next)
101 102 103 |
# File 'lib/rexml/source.rb', line 101 def buffer @scanner.rest end |
#buffer_encoding=(encoding) ⇒ Object
111 112 113 |
# File 'lib/rexml/source.rb', line 111 def buffer_encoding=(encoding) @scanner.string.force_encoding(encoding) end |
#current_line ⇒ Object
Returns the current line in the source.
180 181 182 183 184 185 |
# File 'lib/rexml/source.rb', line 180 def current_line lines = @orig.split res = lines.grep @scanner.rest[0..30] res = res[-1] if res.kind_of? Array lines.index( res ) if res end |
#drop_parsed_content ⇒ Object
105 106 107 108 109 |
# File 'lib/rexml/source.rb', line 105 def drop_parsed_content if @scanner.pos > Private::SCANNER_RESET_SIZE @scanner.string = @scanner.rest end end |
#empty? ⇒ Boolean
Returns true if the Source is exhausted.
175 176 177 |
# File 'lib/rexml/source.rb', line 175 def empty? @scanner.eos? end |
#ensure_buffer ⇒ Object
135 136 |
# File 'lib/rexml/source.rb', line 135 def ensure_buffer end |
#match(pattern, cons = false) ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/rexml/source.rb', line 138 def match(pattern, cons=false) if cons @scanner.scan(pattern).nil? ? nil : @scanner else @scanner.check(pattern).nil? ? nil : @scanner end end |
#match?(pattern, cons = false) ⇒ Boolean
146 147 148 149 150 151 152 |
# File 'lib/rexml/source.rb', line 146 def match?(pattern, cons=false) if cons !@scanner.skip(pattern).nil? else !@scanner.match?(pattern).nil? end end |
#peek_byte ⇒ Object
166 167 168 |
# File 'lib/rexml/source.rb', line 166 def peek_byte @scanner.peek_byte end |
#position ⇒ Object
158 159 160 |
# File 'lib/rexml/source.rb', line 158 def position @scanner.pos end |
#position=(pos) ⇒ Object
162 163 164 |
# File 'lib/rexml/source.rb', line 162 def position=(pos) @scanner.pos = pos end |
#read(term = nil) ⇒ Object
122 123 |
# File 'lib/rexml/source.rb', line 122 def read(term = nil) end |
#read_until(term) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/rexml/source.rb', line 125 def read_until(term) pattern = Private::PRE_DEFINED_TERM_PATTERNS[term] || /#{Regexp.escape(term)}/ data = @scanner.scan_until(pattern) unless data data = @scanner.rest @scanner.pos = @scanner.string.bytesize end data end |
#scan_byte ⇒ Object
170 171 172 |
# File 'lib/rexml/source.rb', line 170 def scan_byte @scanner.scan_byte end |
#skip_spaces ⇒ Object
154 155 156 |
# File 'lib/rexml/source.rb', line 154 def skip_spaces @scanner.skip(Private::SPACES_PATTERN) ? true : false end |