Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/tools/build/v2/util/regex.jam @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 5.1 KB
Line 
1#  (C) Copyright David Abrahams 2001. Permission to copy, use, modify, sell and
2#  distribute this software is granted provided this copyright notice appears in
3#  all copies. This software is provided "as is" without express or implied
4#  warranty, and with no claim as to its suitability for any purpose.
5
6#
7#   Returns a list of the following substrings:
8#   1) from beginning till the first occurence of 'separator' or till the end,
9#   2) between each occurence of 'separator' and the next occurence,
10#   3) from the last occurence of 'separator' till the end.
11#   If no separator is present, the result will contain only one element.
12#
13rule split ( string separator )
14{
15    local result ;
16    local s = $(string) ;
17
18    # Break pieaces off 's' until it has no separators left.
19    local match = 1 ;
20    while $(match)
21    {
22        match = [ MATCH ^(.*)($(separator))(.*) : $(s) ] ;
23        if $(match) {
24            match += "" ; # in case 3rd item was empty - works around MATCH bug
25            result = $(match[3]) $(result) ;
26            s = $(match[1]) ;
27        }
28    }
29    # Combine the remaining part at the beginning, which does not have
30    # separators, with the pieces broken off.
31    # Note that rule's signature does not allow initial s to be empty.
32    return $(s) $(result) ;
33}
34
35# Match string against pattern, and return the elements indicated by
36# indices.
37rule match ( pattern : string : indices * )
38{
39    indices ?= 1 2 3 4 5 6 7 8 9 ;
40    local x = [ MATCH $(pattern) : $(string) ] ;
41    return $(x[$(indices)]) ;
42}
43
44# Matches all elements of 'list' agains the 'pattern'
45# and returns a list of the elements indicated by indices of
46# all successfull matches. If 'indices' is omitted returns
47# a list of first paranthethised groups of all successfull
48# matches.
49#
50rule transform ( list * : pattern : indices * )
51{
52    indices ?= 1 ;
53    local result ;
54    for local e in $(list)
55    {
56        local m = [ MATCH $(pattern) : $(e) ] ;
57        if $(m)
58        {
59            result += $(m[$(indices)]) ;
60        }       
61    }
62    return $(result) ;
63}
64
65# Commented out because of 'indices' parameter of 'transform' rule.
66#
67#NATIVE_RULE regex : transform ;
68
69# Escapes all of the characters in symbols using the escape symbol
70# escape-symbol for the given string, and returns the escaped string
71rule escape ( string : symbols : escape-symbol )
72{
73  local result = "" ;
74  local m = 1 ;
75  while $(m)
76  {
77    m = [ MATCH ^([^$(symbols)]*)([$(symbols)])(.*) : $(string) ] ;
78    if $(m)
79    {
80      m += "" ;  # Supposedly a bug fix; borrowed from regex.split
81      result = "$(result)$(m[1])$(escape-symbol)$(m[2])" ;
82      string = $(m[3]) ;
83    }
84  }
85  string ?= "" ;
86  result = "$(result)$(string)" ;
87  return $(result) ;
88}
89
90# Replaces occurances of a match string in a given string. Returns the
91# new string. The match string can be a regex expression.
92#
93rule replace (
94    string # The string to modify.
95    match # The characters to replace.
96    replacement # The string to replace with.
97    )
98{
99    local result = "" ;
100    local parts = 1 ;
101    while $(parts)
102    {
103        parts = [ MATCH ^(.*)($(match))(.*) : $(string) ] ;
104        if $(parts)
105        {
106            parts += "" ;
107            result = "$(replacement)$(parts[3])$(result)" ;
108            string = $(parts[1]) ;
109        }
110    }
111    string ?= "" ;
112    result = "$(string)$(result)" ;
113    return $(result) ;
114}
115
116# Replaces occurances of a match string in a given list of strings.
117# Returns the list of new strings. The match string can be a regex
118# expression.
119#
120# list        - the list of strings to modify.
121# match       - the search expression.
122# replacement - the string to replace with.
123#
124rule replace-list ( list * : match : replacement )
125{
126    local result ;
127    for local e in $(list)
128    {
129        result += [ replace $(e) $(match) $(replacement) ] ;
130    }
131    return $(result) ;
132}
133
134rule __test__ ( )
135{
136    import assert ;
137
138    assert.result a b c : split "a/b/c" / ;
139    assert.result "" a b c : split "/a/b/c" / ;
140    assert.result "" "" a b c : split "//a/b/c" / ;
141    assert.result "" a "" b c : split "/a//b/c" / ;
142    assert.result "" a "" b c "" : split "/a//b/c/" / ;
143    assert.result "" a "" b c "" "" : split "/a//b/c//" / ;
144   
145    assert.result a c b d
146      : match (.)(.)(.)(.) : abcd : 1 3 2 4 ;
147   
148    assert.result a b c d
149      : match (.)(.)(.)(.) : abcd ;
150   
151    assert.result ababab cddc
152      : match ((ab)*)([cd]+) : abababcddc : 1 3 ;
153
154    assert.result a.h c.h
155      : transform <a.h> \"b.h\" <c.h> :  <(.*)> ;
156
157    assert.result a.h "" b.h c.h
158      : transform <a.h> \"b.h\" <c.h> :  <([^>]*)>|\"([^\"]*)\" : 1 2 ;
159
160    assert.result "^<?xml version=\"1.0\"^>"
161      : escape "<?xml version=\"1.0\">" : "&|()<>^" : "^" ;
162   
163    assert.result "<?xml version=\\\"1.0\\\">"
164      : escape "<?xml version=\"1.0\">" : "\\\"" : "\\" ;
165   
166    assert.result "string&nbsp;string&nbsp;" : replace "string string " " " "&nbsp;" ;
167    assert.result "&nbsp;string&nbsp;string" : replace " string string" " " "&nbsp;" ;
168    assert.result "string&nbsp;&nbsp;string" : replace "string  string" " " "&nbsp;" ;
169    assert.result "-" : replace "&" "&" "-" ;
170
171    assert.result "-" "a-b" : replace-list "&" "a&b" : "&" : "-" ;
172}
Note: See TracBrowser for help on using the repository browser.