1 # Copyright 2001, 2002 Dave Abrahams
2 # Copyright 2003 Douglas Gregor
3 # Copyright 2003 Rene Rivera
4 # Copyright 2002, 2003, 2004, 2005 Vladimir Prus
5 # Distributed under the Boost Software License, Version 1.0.
6 # (See accompanying file LICENSE_1_0.txt or copy at
7 # http://www.boost.org/LICENSE_1_0.txt)
10 # Returns a list of the following substrings:
11 # 1) from beginning till the first occurrence of 'separator' or till the end,
12 # 2) between each occurrence of 'separator' and the next occurrence,
13 # 3) from the last occurrence of 'separator' till the end.
14 # If no separator is present, the result will contain only one element.
17 rule split ( string separator )
22 # Break pieaces off 's' until it has no separators left.
26 match = [ MATCH ^(.*)($(separator))(.*) : $(s) ] ;
29 match += "" ; # in case 3rd item was empty - works around MATCH bug
30 result = $(match[3]) $(result) ;
34 # Combine the remaining part at the beginning, which does not have
35 # separators, with the pieces broken off. Note that the rule's signature
36 # does not allow the initial s to be empty.
37 return $(s) $(result) ;
40 if [ HAS_NATIVE_RULE regex : split : 1 ]
42 NATIVE_RULE regex : split ;
45 # Returns the concatenated results of Applying regex.split to every element of
46 # the list using the separator pattern.
48 rule split-list ( list * : separator )
53 result += [ split $(s) $(separator) ] ;
59 # Match string against pattern, and return the elements indicated by indices.
61 rule match ( pattern : string : indices * )
63 indices ?= 1 2 3 4 5 6 7 8 9 ;
64 local x = [ MATCH $(pattern) : $(string) ] ;
65 return $(x[$(indices)]) ;
69 # Matches all elements of 'list' against the 'pattern' and returns a list of
70 # elements indicated by indices of all successful matches. If 'indices' is
71 # omitted returns a list of first parenthesised groups of all successful
74 rule transform ( list * : pattern : indices * )
78 for local e in $(list)
80 local m = [ MATCH $(pattern) : $(e) ] ;
83 result += $(m[$(indices)]) ;
89 NATIVE_RULE regex : transform ;
92 # Escapes all of the characters in symbols using the escape symbol escape-symbol
93 # for the given string, and returns the escaped string.
95 rule escape ( string : symbols : escape-symbol )
101 m = [ MATCH "^([^$(symbols)]*)([$(symbols)])(.*)" : $(string) ] ;
104 m += "" ; # Supposedly a bug fix; borrowed from regex.split
105 result = "$(result)$(m[1])$(escape-symbol)$(m[2])" ;
110 result = "$(result)$(string)" ;
115 # Replaces occurrences of a match string in a given string and returns the new
116 # string. The match string can be a regex expression.
119 string # The string to modify.
120 match # The characters to replace.
121 replacement # The string to replace with.
128 parts = [ MATCH ^(.*)($(match))(.*) : $(string) ] ;
132 result = "$(replacement)$(parts[3])$(result)" ;
133 string = $(parts[1]) ;
137 result = "$(string)$(result)" ;
141 if [ HAS_NATIVE_RULE regex : replace : 1 ]
143 NATIVE_RULE regex : replace ;
147 # Replaces occurrences of a match string in a given list of strings and returns
148 # a list of new strings. The match string can be a regex expression.
150 # list - the list of strings to modify.
151 # match - the search expression.
152 # replacement - the string to replace with.
154 rule replace-list ( list * : match : replacement )
157 for local e in $(list)
159 result += [ replace $(e) $(match) $(replacement) ] ;
169 assert.result a b c : split "a/b/c" / ;
170 assert.result "" a b c : split "/a/b/c" / ;
171 assert.result "" "" a b c : split "//a/b/c" / ;
172 assert.result "" a "" b c : split "/a//b/c" / ;
173 assert.result "" a "" b c "" : split "/a//b/c/" / ;
174 assert.result "" a "" b c "" "" : split "/a//b/c//" / ;
175 assert.result "" a b c "" : split "abc" "" ;
176 assert.result "" "" : split "" "" ;
178 assert.result a c b d
179 : match (.)(.)(.)(.) : abcd : 1 3 2 4 ;
181 assert.result a b c d
182 : match (.)(.)(.)(.) : abcd ;
184 assert.result ababab cddc
185 : match "((ab)*)([cd]+)" : abababcddc : 1 3 ;
187 assert.result a.h c.h
188 : transform <a.h> \"b.h\" <c.h> : <(.*)> ;
190 assert.result a.h b.h c.h
191 : transform <a.h> \"b.h\" <c.h> : "<([^>]*)>|\"([^\"]*)\"" : 1 2 ;
193 assert.result "^<?xml version=\"1.0\"^>"
194 : escape "<?xml version=\"1.0\">" : "&|()<>^" : "^" ;
196 assert.result "<?xml version=\\\"1.0\\\">"
197 : escape "<?xml version=\"1.0\">" : "\\\"" : "\\" ;
199 assert.result "string string " : replace "string string " " " " " ;
200 assert.result " string string" : replace " string string" " " " " ;
201 assert.result "string string" : replace "string string" " " " " ;
202 assert.result "-" : replace "&" "&" "-" ;
203 assert.result "x" : replace "" "" "x" ;
204 assert.result "xax" : replace "a" "" "x" ;
205 assert.result "xaxbx" : replace "ab" "" "x" ;
207 assert.result "-" "a-b" : replace-list "&" "a&b" : "&" : "-" ;