class String

The String class is monkey patched to support sscanf.

The String class is monkey patched to support sscanf.

Constants

RSF_BINARY

A regular expression for binary integers.

RSF_COMPLEX

A regular expression for complex numbers.

RSF_DECIMAL

A regular expression for decimal integers.

RSF_FLOAT_PARSE
RSF_FLOAT_SKIP
RSF_HEX_PARSE
RSF_HEX_SKIP
RSF_INTEGER

A regular expression for flexible base integers.

RSF_OCTAL

A regular expression for octal integers.

RSF_QUOTED

A regular expression for quoted strings.

RSF_RATIONAL

A regular expression for rational numbers.

RSF_RGX

The standard handlers for formats with embedded regular expressions.

RSF_RGX_SKIP
RSF_STRING

A regular expression for a string.

RSF_UNSIGNED

A regular expression for unsigned decimal integers.

Public Class Methods

sscanf_unparsed() click to toggle source

Get any unparsed text.

# File lib/ruby_sscanf.rb, line 14
def self.sscanf_unparsed
  get_parser_engine.unparsed
end

Protected Class Methods

get_parser_engine() click to toggle source

Get the parsing engine. This is cached on a per-thread basis. That is to say, each thread gets its own FormatEngine::Engine instance.

# File lib/ruby_sscanf/engine.rb, line 52
def self.get_parser_engine

  Thread.current[:ruby_sscanf_engine] ||= FormatEngine::Engine.new(
    "%a"  => RSF_FLOAT_PARSE,
    "%*a" => RSF_FLOAT_SKIP,

    "%A"  => RSF_FLOAT_PARSE,
    "%*A" => RSF_FLOAT_SKIP,

    "%b"  => lambda {parse(RSF_BINARY) ? dst << found.to_i(2) : :break},
    "%*b" => lambda {parse(RSF_BINARY) || :break},

    "%c"  => lambda {dst << grab},
    "%*c" => lambda {grab},

    "%d"  => lambda {parse(RSF_DECIMAL) ? dst << found.to_i : :break},
    "%*d" => lambda {parse(RSF_DECIMAL) || :break},

    "%e"  => RSF_FLOAT_PARSE,
    "%*e" => RSF_FLOAT_SKIP,

    "%E"  => RSF_FLOAT_PARSE,
    "%*E" => RSF_FLOAT_SKIP,

    "%f"  => RSF_FLOAT_PARSE,
    "%*f" => RSF_FLOAT_SKIP,

    "%F"  => RSF_FLOAT_PARSE,
    "%*F" => RSF_FLOAT_SKIP,

    "%g"  => RSF_FLOAT_PARSE,
    "%*g" => RSF_FLOAT_SKIP,

    "%G"  => RSF_FLOAT_PARSE,
    "%*G" => RSF_FLOAT_SKIP,

    "%i"  => lambda {parse(RSF_INTEGER) ? dst << found.to_i(0) : :break},
    "%*i" => lambda {parse(RSF_INTEGER) || :break},

    "%j"  => lambda {parse(RSF_COMPLEX) ? dst << Complex(found) : :break},
    "%*j" => lambda {parse(RSF_COMPLEX) || :break},

    "%o"  => lambda {parse(RSF_OCTAL) ? dst << found.to_i(8) : :break},
    "%*o" => lambda {parse(RSF_OCTAL) || :break},

    "%q"  => lambda do
      if parse(RSF_QUOTED)
        dst << found[1..-2].gsub(/\\./) {|seq| seq[-1]}
      else
        :break
      end
    end,

    "%*q" => lambda {parse(RSF_QUOTED) || :break},

    "%r"  => lambda {parse(RSF_RATIONAL) ? dst << found.to_r : :break},
    "%*r" => lambda {parse(RSF_RATIONAL) || :break},

    "%s"  => lambda {parse(RSF_STRING) ? dst << found : :break},
    "%*s" => lambda {parse(RSF_STRING) || :break},

    "%u"  => lambda {parse(RSF_UNSIGNED) ? dst << found.to_i : :break},
    "%*u" => lambda {parse(RSF_UNSIGNED) || :break},

    "%x"  => RSF_HEX_PARSE,
    "%*x" => RSF_HEX_SKIP,

    "%X"  => RSF_HEX_PARSE,
    "%*X" => RSF_HEX_SKIP,

    "%["  => RSF_RGX,
    "%*[" => RSF_RGX_SKIP,

    "%/"  => RSF_RGX,
    "%*/" => RSF_RGX_SKIP)
end

Public Instance Methods

sscanf(format) click to toggle source

Scan the formatted input.

# File lib/ruby_sscanf.rb, line 9
def sscanf(format)
  String.get_parser_engine.do_parse(self, [], format)
end