class MaRuKu::In::Markdown::SpanLevelParser::CharSourceStrscan
a wrapper around StringScanner
Public Class Methods
new(s, parent=nil)
click to toggle source
# File lib/maruku/input/charsource.rb, line 139 def initialize(s, parent=nil) @scanner = StringScanner.new(s) @size = s.size end
Public Instance Methods
consume_whitespace()
click to toggle source
# File lib/maruku/input/charsource.rb, line 193 def consume_whitespace @scanner.skip(/\s+/) end
cur_char()
click to toggle source
Return current char as a String
(or nil).
# File lib/maruku/input/charsource.rb, line 145 def cur_char @scanner.peek(1)[0] end
cur_chars(n)
click to toggle source
Return the next n chars as a String
.
# File lib/maruku/input/charsource.rb, line 150 def cur_chars(n) @scanner.peek(n) end
cur_chars_are(string)
click to toggle source
Returns true if string matches what we’re pointing to
# File lib/maruku/input/charsource.rb, line 180 def cur_chars_are(string) @scanner.peek(string.size) == string end
current_remaining_buffer()
click to toggle source
Return the rest of the string
# File lib/maruku/input/charsource.rb, line 175 def current_remaining_buffer @scanner.rest end
describe()
click to toggle source
# File lib/maruku/input/charsource.rb, line 197 def describe len = 75 num_before = [len/2, @scanner.pos].min num_after = [len/2, @scanner.rest_size].min num_before_max = @scanner.pos num_after_max = @scanner.rest_size num_before = [num_before_max, len - num_after].min num_after = [num_after_max, len - num_before].min index_start = [@scanner.pos - num_before, 0].max index_end = [@scanner.pos + num_after, @size].min size = index_end - index_start str = @scanner.string[index_start, size] str.gsub!("\n", 'N') str.gsub!("\t", 'T') if index_end == @size str += "EOF" end pre_s = @scanner.pos - index_start pre_s = [pre_s, 0].max pre_s2 = [len-pre_s, 0].max pre = " " * pre_s "-" * len + "\n" + str + "\n" + "-" * pre_s + "|" + "-" * pre_s2 + "\n" + pre + "+--- Byte #{@scanner.pos}\n" + "Shown bytes [#{index_start} to #{size}] of #{@size}:\n" + @scanner.string.gsub(/^/, ">") end
ignore_char()
click to toggle source
Advance the pointer
# File lib/maruku/input/charsource.rb, line 165 def ignore_char @scanner.getch end
ignore_chars(n)
click to toggle source
Advance the pointer by n
# File lib/maruku/input/charsource.rb, line 170 def ignore_chars(n) n.times { @scanner.getch } end
next_char()
click to toggle source
Return the char after current char as a String
(or nil).
# File lib/maruku/input/charsource.rb, line 155 def next_char @scanner.peek(2)[1] end
next_matches(r)
click to toggle source
Returns true if Regexp r matches what we’re pointing to
# File lib/maruku/input/charsource.rb, line 185 def next_matches(r) @scanner.check(r) end
read_regexp(r)
click to toggle source
# File lib/maruku/input/charsource.rb, line 189 def read_regexp(r) r.match(@scanner.scan(r)) end
shift_char()
click to toggle source
Return a character as a String
, advancing the pointer.
# File lib/maruku/input/charsource.rb, line 160 def shift_char @scanner.getch[0] end