James Turnbull wrote: > Thomas Bellman wrote: >> I suggest that we make split() take a regular expression instead of a >> fixed string to split on. That would make it more powerful.
> Can you post the code to the list please. Here goes: From: Thomas Bellman <[email protected]> Date: Thu, 14 May 2009 14:37:21 +0200 Subject: [PATCH] Make the split() function split on regexps. Change the second parameter to split() into a regular expression instead of a fixed string, making it more flexible. This is a non-backwards compatible change, but since no release has been made containing split(), it should not be a problem. Unit tests for split() are also added. Signed-off-by: Thomas Bellman <[email protected]> --- lib/puppet/parser/functions/split.rb | 28 ++++++++++++++---- spec/unit/parser/functions/split.rb | 51 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 6 deletions(-) create mode 100755 spec/unit/parser/functions/split.rb diff --git a/lib/puppet/parser/functions/split.rb b/lib/puppet/parser/functions/split.rb index cfb3ab5..53991d5 100644 --- a/lib/puppet/parser/functions/split.rb +++ b/lib/puppet/parser/functions/split.rb @@ -1,13 +1,29 @@ module Puppet::Parser::Functions - newfunction(:split, :type => :rvalue, - :doc => "Split a string variable into an array using the specified split character. + newfunction(:split, :type => :rvalue, + :doc => "\ +Split a string variable into an array using the specified split regexp. Usage:: - $string = 'value1,value2' - $array_var = split($string, ',') + $string = 'v1.v2:v3.v4' + $array_var1 = split($string, ':') + $array_var2 = split($string, '[.]') + $array_var3 = split($string, '[.:]') -$array_var holds the result ['value1', 'value2']") do |args| - return args[0].split(args[1]) +$array_var1 now holds the result ['v1.v2', 'v3.v4'], +while $array_var2 holds ['v1', 'v2:v3', 'v4'], and +$array_var3 holds ['v1', 'v2', 'v3', 'v4']. + +Note that in the second example, we split on a string that contains +a regexp meta-character (.), and that needs protection. A simple +way to do that for a single character is to enclose it in square +brackets.") do |args| + + if args.length != 2 + raise Puppet::ParseError, ("split(): wrong number of arguments" + + " (#{args.length}; must be 2)") + end + + return args[0].split(Regexp.compile(args[1])) end end diff --git a/spec/unit/parser/functions/split.rb b/spec/unit/parser/functions/split.rb new file mode 100755 index 0000000..8aa031d --- /dev/null +++ b/spec/unit/parser/functions/split.rb @@ -0,0 +1,51 @@ +#! /usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe "the split function" do + + before :each do + @scope = Puppet::Parser::Scope.new() + end + + it "should exist" do + Puppet::Parser::Functions.function("split").should == "function_split" + end + + it "should raise a ParseError if there is less than 2 arguments" do + lambda { @scope.function_split(["foo"]) }.should( + raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there is more than 2 arguments" do + lambda { @scope.function_split(["foo", "bar", "gazonk"]) }.should( + raise_error(Puppet::ParseError)) + end + + it "should raise a RegexpError if the regexp is malformed" do + lambda { @scope.function_split(["foo", "("]) }.should( + raise_error(RegexpError)) + end + + + it "should handle simple string without metacharacters" do + result = @scope.function_split([ "130;236;254;10", ";"]) + result.should(eql(["130", "236", "254", "10"])) + end + + it "should handle simple regexps" do + result = @scope.function_split([ "130.236;254.;10", "[.;]+"]) + result.should(eql(["130", "236", "254", "10"])) + end + + it "should handle groups" do + result = @scope.function_split([ "130.236;254.;10", "([.;]+)"]) + result.should(eql(["130", ".", "236", ";", "254", ".;", "10"])) + end + + it "should handle simple string without metacharacters" do + result = @scope.function_split([ "130.236.254.10", ";"]) + result.should(eql(["130.236.254.10"])) + end + +end -- 1.6.0.6 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en -~----------~----~----~----~------~----~------~--~---
