4 module Puppet::Parser::Functions
5 newfunction(:is_numeric, :type => :rvalue, :doc => <<-DOC
7 **Deprecated:** Returns true if the given value is numeric.
9 Returns true if the given argument is a Numeric (Integer or Float),
10 or a String containing either a valid integer in decimal base 10 form, or
11 a valid floating point string representation.
13 The function recognizes only decimal (base 10) integers and float but not
14 integers in hex (base 16) or octal (base 8) form.
16 The string representation may start with a '-' (minus). If a decimal '.' is used,
17 it must be followed by at least one digit.
20 Returns `true` or `false`
22 > **Note:* **Deprecated** Will be removed in a future version of stdlib. See
23 [`validate_legacy`](#validate_legacy).
27 function_deprecation([:is_numeric, 'This method is deprecated, please use the stdlib validate_legacy function,
28 with Stdlib::Compat::Numeric. There is further documentation for validate_legacy function in the README.'])
30 if arguments.size != 1
31 raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments given #{arguments.size} for 1")
36 # Regex is taken from the lexer of puppet
37 # puppet/pops/parser/lexer.rb but modified to match also
38 # negative values and disallow invalid octal numbers or
39 # numbers prefixed with multiple 0's (except in hex numbers)
41 # TODO these parameters should be constants but I'm not sure
42 # if there is no risk to declare them inside of the module
43 # Puppet::Parser::Functions
45 # TODO: decide if this should be used
51 # numeric_hex = %r{^-?0[xX][0-9A-Fa-f]+$}
53 # TODO: decide if this should be used
57 # numeric_oct = %r{^-?0[1-7][0-7]*$}
59 # Integer/Float numbers like
63 numeric = %r{^-?(?:(?:[1-9]\d*)|0)(?:\.\d+)?(?:[eE]-?\d+)?$}
65 if value.is_a?(Numeric) || (value.is_a?(String) &&
66 value.match(numeric) # or
67 # value.match(numeric_hex) or
68 # value.match(numeric_oct)