diff --git a/new/regex.jam b/new/regex.jam index de272651b..1470b1cc8 100644 --- a/new/regex.jam +++ b/new/regex.jam @@ -79,6 +79,32 @@ rule escape ( string : symbols : escape-symbol ) return $(result) ; } +# Replaces occurances of a string in a given string. Returns the +# new string. +# +rule replace ( + string # The string to modify. + match # The characters to replace. + replacement # The string to replace with. + ) +{ + local result = "" ; + local parts = 1 ; + while $(parts) + { + parts = [ MATCH ^(.*)($(match))(.*) : $(string) ] ; + if $(parts) + { + parts += "" ; + result = "$(replacement)$(parts[3])$(result)" ; + string = $(parts[1]) ; + } + } + string ?= "" ; + result = "$(string)$(result)" ; + return $(result) ; +} + rule __test__ ( ) { import assert ; @@ -107,4 +133,9 @@ rule __test__ ( ) assert.result "" : escape "" : "\\\"" : "\\" ; + + assert.result "string string " : replace "string string " " " " " ; + assert.result " string string" : replace " string string" " " " " ; + assert.result "string  string" : replace "string string" " " " " ; + assert.result "-" : replace "&" "&" "-" ; } diff --git a/v2/util/regex.jam b/v2/util/regex.jam index de272651b..1470b1cc8 100644 --- a/v2/util/regex.jam +++ b/v2/util/regex.jam @@ -79,6 +79,32 @@ rule escape ( string : symbols : escape-symbol ) return $(result) ; } +# Replaces occurances of a string in a given string. Returns the +# new string. +# +rule replace ( + string # The string to modify. + match # The characters to replace. + replacement # The string to replace with. + ) +{ + local result = "" ; + local parts = 1 ; + while $(parts) + { + parts = [ MATCH ^(.*)($(match))(.*) : $(string) ] ; + if $(parts) + { + parts += "" ; + result = "$(replacement)$(parts[3])$(result)" ; + string = $(parts[1]) ; + } + } + string ?= "" ; + result = "$(string)$(result)" ; + return $(result) ; +} + rule __test__ ( ) { import assert ; @@ -107,4 +133,9 @@ rule __test__ ( ) assert.result "" : escape "" : "\\\"" : "\\" ; + + assert.result "string string " : replace "string string " " " " " ; + assert.result " string string" : replace " string string" " " " " ; + assert.result "string  string" : replace "string string" " " " " ; + assert.result "-" : replace "&" "&" "-" ; }