summaryrefslogtreecommitdiff
path: root/code/environments/production/modules/stdlib/lib/puppet/parser/functions/convert_base.rb
diff options
context:
space:
mode:
Diffstat (limited to 'code/environments/production/modules/stdlib/lib/puppet/parser/functions/convert_base.rb')
-rw-r--r--code/environments/production/modules/stdlib/lib/puppet/parser/functions/convert_base.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/code/environments/production/modules/stdlib/lib/puppet/parser/functions/convert_base.rb b/code/environments/production/modules/stdlib/lib/puppet/parser/functions/convert_base.rb
new file mode 100644
index 0000000..6d17f85
--- /dev/null
+++ b/code/environments/production/modules/stdlib/lib/puppet/parser/functions/convert_base.rb
@@ -0,0 +1,36 @@
+#
+# convert_base.rb
+#
+module Puppet::Parser::Functions
+ newfunction(:convert_base, :type => :rvalue, :arity => 2, :doc => <<-'DOC') do |args|
+ Converts a given integer or base 10 string representing an integer to a specified base, as a string.
+
+ Usage:
+
+ $binary_repr = convert_base(5, 2) # $binary_repr is now set to "101"
+ $hex_repr = convert_base("254", "16") # $hex_repr is now set to "fe"
+
+ DOC
+
+ raise Puppet::ParseError, 'convert_base(): First argument must be either a string or an integer' unless args[0].is_a?(Integer) || args[0].is_a?(String)
+ raise Puppet::ParseError, 'convert_base(): Second argument must be either a string or an integer' unless args[1].is_a?(Integer) || args[1].is_a?(String)
+
+ if args[0].is_a?(String)
+ raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' unless args[0] =~ %r{^[0-9]+$}
+ end
+
+ if args[1].is_a?(String)
+ raise Puppet::ParseError, 'convert_base(): First argument must be an integer or a string corresponding to an integer in base 10' unless args[1] =~ %r{^[0-9]+$}
+ end
+
+ number_to_convert = args[0]
+ new_base = args[1]
+
+ number_to_convert = number_to_convert.to_i
+ new_base = new_base.to_i
+
+ raise Puppet::ParseError, 'convert_base(): base must be at least 2 and must not be greater than 36' unless new_base >= 2 && new_base <= 36
+
+ return number_to_convert.to_s(new_base)
+ end
+end