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
Constant Summary
Constants included from Private
Private::PRE_DEFINED_TERM_PATTERNS
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.
-
#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
- #position ⇒ Object
- #position=(pos) ⇒ Object
- #read(term = nil) ⇒ Object
- #read_until(term) ⇒ Object
Methods included from Encoding
Constructor Details
#initialize(arg, encoding = nil) ⇒ Source
Constructor value, overriding all encoding detection
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rexml/source.rb', line 51 def initialize(arg, encoding=nil) @orig = arg @scanner = StringScanner.new(@orig) if encoding self.encoding = encoding else detect_encoding end @line = 0 end |
Instance Attribute Details
#encoding ⇒ Object
Returns the value of attribute encoding.
35 36 37 |
# File 'lib/rexml/source.rb', line 35 def encoding @encoding end |
#line ⇒ Object (readonly)
The line number of the last consumed text
34 35 36 |
# File 'lib/rexml/source.rb', line 34 def line @line end |
Instance Method Details
#buffer ⇒ Object
The current buffer (what we’re going to read next)
63 64 65 |
# File 'lib/rexml/source.rb', line 63 def buffer @scanner.rest end |
#buffer_encoding=(encoding) ⇒ Object
67 68 69 |
# File 'lib/rexml/source.rb', line 67 def buffer_encoding=(encoding) @scanner.string.force_encoding(encoding) end |
#current_line ⇒ Object
Returns 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 @scanner.rest[0..30] res = res[-1] if res.kind_of? Array lines.index( res ) if res end |
#empty? ⇒ Boolean
Returns true if the Source is exhausted.
111 112 113 |
# File 'lib/rexml/source.rb', line 111 def empty? @scanner.eos? end |
#ensure_buffer ⇒ Object
91 92 |
# File 'lib/rexml/source.rb', line 91 def ensure_buffer end |
#match(pattern, cons = false) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/rexml/source.rb', line 94 def match(pattern, cons=false) if cons @scanner.scan(pattern).nil? ? nil : @scanner else @scanner.check(pattern).nil? ? nil : @scanner end end |
#position ⇒ Object
102 103 104 |
# File 'lib/rexml/source.rb', line 102 def position @scanner.pos end |
#position=(pos) ⇒ Object
106 107 108 |
# File 'lib/rexml/source.rb', line 106 def position=(pos) @scanner.pos = pos end |
#read(term = nil) ⇒ Object
78 79 |
# File 'lib/rexml/source.rb', line 78 def read(term = nil) end |
#read_until(term) ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/rexml/source.rb', line 81 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 |