Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_sscanf.rb

Overview

The String class is monkey patched to support sscanf.

Constant Summary collapse

RSF_DECIMAL =

A regular expression for decimal integers.

/[+-]?\d+/
RSF_UNSIGNED =

A regular expression for unsigned decimal integers.

/[+]?\d+/
RSF_HEX =

A regular expression for hexadecimal integers.

/[+-]?(0[xX])?\h+/
RSF_HEX_PARSE =
lambda {parse(RSF_HEX) ? dst << found.to_i(16) : :break}
RSF_HEX_SKIP =
lambda {parse(RSF_HEX) || :break}
RSF_OCTAL =

A regular expression for octal integers.

/[+-]?(0[oO])?[0-7]+/
RSF_BINARY =

A regular expression for binary integers.

/[+-]?(0[bB])?[01]+/
RSF_INTEGER =

A regular expression for flexible base integers.

/[+-]?((0[xX]\h+)|(0[bB][01]+)|(0[oO]?[0-7]*)|([1-9]\d*))/
RSF_FLOAT =

A regular expression for floating point and scientific notation numbers.

/[+-]?\d+(\.\d+)?([eE][+-]?\d+)?/
RSF_FLOAT_PARSE =
lambda {parse(RSF_FLOAT) ? dst << found.to_f : :break}
RSF_FLOAT_SKIP =
lambda {parse(RSF_FLOAT) || :break}
RSF_RATIONAL =

A regular expression for rational numbers.

/[+-]?\d+\/\d+(r)?/
RSF_COMPLEX =

A regular expression for complex numbers.

%r{(?<num> \d+(\.\d+)?([eE][+-]?\d+)?){0}
 [+-]?\g<num>[+-]\g<num>[ij]
}x
RSF_STRING =

A regular expression for a string.

/\S+/
RSF_QUOTED =

A regular expression for quoted strings.

/("([^\\"]|\\.)*")|('([^\\']|\\.)*')/
RSF_RGX =

The standard handlers for formats with embedded regular expressions.

lambda {parse(fmt.regex) ? dst << found : :break}
RSF_RGX_SKIP =
lambda {parse(fmt.regex) || :break}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_parser_engineObject

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ruby_sscanf.rb', line 53

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

Instance Method Details

#sscanf(format) ⇒ Object

Scan the formatted input.



131
132
133
# File 'lib/ruby_sscanf.rb', line 131

def sscanf(format)
  String.get_parser_engine.do_parse(self, [], format)
end