Skip to content

Adds a convert_base function, which can convert numbers between bases #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ Appends the contents of multiple arrays onto the first array given. For example:
* `concat(['1','2','3'],'4',['5','6','7'])` returns ['1','2','3','4','5','6','7'].
*Type*: rvalue.

#### `convert_base`

Converts a given integer or base 10 string representing an integer to a specified base, as a string. For example:
* `convert_base(5, 2)` results in: '101'
* `convert_base('254', '16')` results in: 'fe'

#### `count`

If called with only an array, it counts the number of elements that are **not** nil/undef. If called with a second argument, counts the number of elements in an array that matches the second argument. *Type*: rvalue.
Expand Down
35 changes: 35 additions & 0 deletions lib/puppet/parser/functions/convert_base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Puppet::Parser::Functions

newfunction(:convert_base, :type => :rvalue, :arity => 2, :doc => <<-'ENDHEREDOC') 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"

ENDHEREDOC

raise Puppet::ParseError, ("convert_base(): First argument must be either a string or an integer") unless (args[0].is_a?(Integer) or 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) or 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] =~ /^[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] =~ /^[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 and new_base <= 36

return number_to_convert.to_s(new_base)
end
end
24 changes: 24 additions & 0 deletions spec/functions/convert_base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'spec_helper'

describe 'convert_base' do
it { is_expected.not_to eq(nil) }
it { is_expected.to run.with_params().and_raise_error(ArgumentError) }
it { is_expected.to run.with_params("asdf").and_raise_error(ArgumentError) }
it { is_expected.to run.with_params("asdf","moo","cow").and_raise_error(ArgumentError) }
it { is_expected.to run.with_params(["1"],"2").and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
it { is_expected.to run.with_params("1",["2"]).and_raise_error(Puppet::ParseError, /argument must be either a string or an integer/) }
it { is_expected.to run.with_params("1",1).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }
it { is_expected.to run.with_params("1",37).and_raise_error(Puppet::ParseError, /base must be at least 2 and must not be greater than 36/) }

it "should raise a ParseError if argument 1 is a string that does not correspond to an integer in base 10" do
is_expected.to run.with_params("ten",6).and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
end

it "should raise a ParseError if argument 2 is a string and does not correspond to an integer in base 10" do
is_expected.to run.with_params(100,"hex").and_raise_error(Puppet::ParseError, /argument must be an integer or a string corresponding to an integer in base 10/)
end

it { is_expected.to run.with_params("11",'16').and_return('b') }
it { is_expected.to run.with_params("35",'36').and_return('z') }
it { is_expected.to run.with_params(5, 2).and_return('101') }
end