class StringScanner

Constants

Id
Version

Attributes

match[R]
pointer[R]
pos[R]
prev_pos[R]

Public Class Methods

must_C_version() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 196
def self.must_C_version
  self
end
new(string, dup=false) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 80
def initialize(string, dup=false)
  if string.instance_of? String
    @original = string
    @string = string
  else
    @original = StringValue(string)
    @string = String.new @original
  end

  reset_state
end

Public Instance Methods

<<(str)
Alias for: concat
[](n) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 24
def [](n)
  raise TypeError, "Bad argument #{n.inspect}" unless n.respond_to? :to_int
  @match[n] if @match
end
beginning_of_line?()
Alias for: bol?
bol?() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 29
def bol?
  @pos == 0 or @string.getbyte(pos-1) == 10
end
Also aliased as: beginning_of_line?
check(pattern) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 35
def check(pattern)
  _scan pattern, false, true, true
end
check_until(pattern) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 39
def check_until(pattern)
  _scan pattern, false, true, false
end
clear() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 43
def clear
  warn "StringScanner#clear is obsolete; use #terminate instead" if $VERBOSE
  terminate
end
concat(str) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 48
def concat(str)
  @string << StringValue(str)
  self
end
Also aliased as: <<
empty?() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 54
def empty?
  warn "StringScanner#empty? is obsolete; use #eos? instead?" if $VERBOSE
  eos?
end
eos?() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 59
def eos?
  @pos >= @string.bytesize
end
exist?(pattern) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 63
def exist?(pattern)
  _scan pattern, false, false, false
end
get_byte() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 67
def get_byte
  scan(/./mn)
end
getbyte() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 71
def getbyte
  warn "StringScanner#getbyte is obsolete; use #get_byte instead" if $VERBOSE
  get_byte
end
getch() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 76
def getch
  scan(/./m)
end
inspect() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 92
def inspect
  if defined? @string
    if eos?
      str = "#<StringScanner fin>"
    else
      if string.bytesize - pos > 5
        rest = "#{string[pos..pos+4]}..."
      else
        rest = string[pos..string.bytesize]
      end

      if pos > 0
        if pos > 5
          prev = "...#{string[pos-5...pos]}"
        else
          prev = string[0...pos]
        end

        str = "#<StringScanner #{pos}/#{string.bytesize} #{prev.inspect} @ #{rest.inspect}>"
      else
        str = "#<StringScanner #{pos}/#{string.bytesize} @ #{rest.inspect}>"
      end
    end

    str.taint if @string.tainted?
    return str
  else
    "#<StringScanner (uninitialized)>"
  end
end
match?(pattern) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 123
def match?(pattern)
  _scan pattern, false, false, true
end
matched() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 127
def matched
  @match.to_s if @match
end
matched?() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 131
def matched?
  !!@match
end
matched_size() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 135
def matched_size
  @match.to_s.bytesize if @match
end
matchedsize() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 139
def matchedsize
  warn "StringScanner#matchedsize is obsolete; use #matched_size instead" if $VERBOSE
  matched_size
end
peek(len) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 238
def peek(len)
  raise ArgumentError if len < 0
  return "" if len.zero?

  begin
    return @string[pos, len]
  rescue TypeError
    raise RangeError, "offset outside of possible range"
  end
end
peep(len) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 249
def peep(len)
  warn "StringScanner#peep is obsolete; use #peek instead" if $VERBOSE
  peek len
end
pointer=(n)
Alias for: pos=
pos=(n) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 9
def pos=(n)
  n = Integer(n)

  n += @string.bytesize if n < 0

  if n < 0 or n > @string.bytesize
    raise RangeError, "index out of range (#{n})"
  end

  @pos = n
end
Also aliased as: pointer=
post_match() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 144
def post_match
  @match.post_match if @match
end
pre_match() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 148
def pre_match
  @string.byteslice(0, match.full.at(0)) if @match
end
reset() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 158
def reset
  reset_state
  self
end
rest() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 163
def rest
  @string.byteslice(@pos, @string.bytesize - @pos)
end
rest?() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 167
def rest?
  return !eos?
end
rest_size() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 171
def rest_size
  @string.bytesize - @pos
end
restsize() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 175
def restsize
  warn "StringScanner#restsize is obsolete; use #rest_size instead" if $VERBOSE
  rest_size
end
scan(pattern) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 180
def scan(pattern)
  _scan pattern, true, true, true
end
scan_full(pattern, advance_pos, getstr) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 188
def scan_full(pattern, advance_pos, getstr)
  _scan pattern, advance_pos, getstr, true
end
scan_until(pattern) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 184
def scan_until(pattern)
  _scan pattern, true, true, false
end
search_full(pattern, advance_pos, getstr) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 192
def search_full(pattern, advance_pos, getstr)
  _scan pattern, advance_pos, getstr, false
end
skip(pattern) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 200
def skip(pattern)
  _scan pattern, true, false, true
end
skip_until(pattern) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 204
def skip_until(pattern)
  _scan pattern, true, false, false
end
string() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 208
def string
  @original
end
string=(string) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 212
def string=(string)
  reset_state

  if string.instance_of? String
    @original = string
    @string = string
  else
    @original = StringValue(string)
    @string = String.new @original
  end
end
terminate() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 224
def terminate
  @match = nil
  @pos = string.bytesize
  self
end
unscan() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 230
def unscan
  raise ScanError if @match.nil?
  @pos = @prev_pos
  @prev_pos = nil
  @match = nil
  self
end

Private Instance Methods

_scan(pattern, advance_pos, getstr, headonly) click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 254
def _scan(pattern, advance_pos, getstr, headonly)
  unless pattern.kind_of? Regexp
    raise TypeError, "bad pattern argument: #{pattern.inspect}"
  end

  @match = nil

  if headonly
    # NOTE - match_start is an Oniguruma feature that Rubinius exposes.
    # We use it here to avoid creating a new Regexp with '^' prepended.
    @match = pattern.match_start @string, @pos
  else
    # NOTE - search_from is an Oniguruma feature that Rubinius exposes.
    # We use it so we can begin the search in the middle of the string
    @match = pattern.search_from @string, @pos
  end

  return nil unless @match

  fin = @match.full.at(1)

  @prev_pos = @pos

  @pos = fin if advance_pos

  width = fin - @prev_pos

  return width unless getstr

  @string.byteslice(@prev_pos, width)
end
reset_state() click to toggle source
# File lib/rubysl/strscan/strscan.rb, line 152
def reset_state
  @prev_pos = @pos = 0
  @match = nil
end