1 | # Copyright 2002 Dave Abrahams |
---|
2 | # Copyright 2002, 2003 Rene Rivera |
---|
3 | # Distributed under the Boost Software License, Version 1.0. |
---|
4 | # (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | import regex ; |
---|
7 | |
---|
8 | # Characters considered whitespace, as a list. |
---|
9 | .whitespace-chars = " " " " " |
---|
10 | " ; |
---|
11 | |
---|
12 | # Characters considered whitespace, as a single string. |
---|
13 | .whitespace = $(.whitespace-chars:J="") ; |
---|
14 | |
---|
15 | # Returns the canonical set of whitespace characters, as a list. |
---|
16 | # |
---|
17 | rule whitespace-chars ( ) |
---|
18 | { |
---|
19 | return $(.whitespace-chars) ; |
---|
20 | } |
---|
21 | |
---|
22 | # Returns the canonical set of whitespace characters, as a single string. |
---|
23 | # |
---|
24 | rule whitespace ( ) |
---|
25 | { |
---|
26 | return $(.whitespace) ; |
---|
27 | } |
---|
28 | |
---|
29 | # Splits the given string into a list of strings composed |
---|
30 | # of each character of the string in sequence. |
---|
31 | # |
---|
32 | rule chars ( |
---|
33 | string # The string to split. |
---|
34 | ) |
---|
35 | { |
---|
36 | local result ; |
---|
37 | while $(string) |
---|
38 | { |
---|
39 | local s = [ MATCH (.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.*) : $(string) ] ; |
---|
40 | string = $(s[9]) ; |
---|
41 | result += $(s[1-8]) ; |
---|
42 | } |
---|
43 | |
---|
44 | # trim off empty strings |
---|
45 | while $(result[1]) && ! $(result[-1]) |
---|
46 | { |
---|
47 | result = $(result[1--2]) ; |
---|
48 | } |
---|
49 | |
---|
50 | return $(result) ; |
---|
51 | } |
---|
52 | |
---|
53 | # Apply a set of standard transformations to string to produce an |
---|
54 | # abbreviation no more than 5 characters long |
---|
55 | # |
---|
56 | rule abbreviate ( string ) |
---|
57 | { |
---|
58 | local r = $(.abbreviated-$(string)) ; |
---|
59 | if $(r) |
---|
60 | { |
---|
61 | return $(r) ; |
---|
62 | } |
---|
63 | # Anything less than 4 characters gets no abbreviation |
---|
64 | else if ! [ MATCH (....) : $(string) ] |
---|
65 | { |
---|
66 | $(.abbreviated-$(string)) = $(string) ; |
---|
67 | return $(string) ; |
---|
68 | } |
---|
69 | else |
---|
70 | { |
---|
71 | # Separate the initial letter in case it's a vowel |
---|
72 | local s1 = [ MATCH ^(.)(.*) : $(string) ] ; |
---|
73 | |
---|
74 | # drop trailing "ing" |
---|
75 | local s2 = [ MATCH ^(.*)ing$ : $(s1[2]) ] ; |
---|
76 | s2 ?= $(s1[2]) ; |
---|
77 | |
---|
78 | # Reduce all doubled characters to one |
---|
79 | local last = "" ; |
---|
80 | for local c in [ chars $(s2) ] |
---|
81 | { |
---|
82 | if $(c) != $(last) |
---|
83 | { |
---|
84 | r += $(c) ; |
---|
85 | last = $(c) ; |
---|
86 | } |
---|
87 | } |
---|
88 | s2 = $(r:J="") ; |
---|
89 | |
---|
90 | # Chop all vowels out of the remainder |
---|
91 | s2 = [ regex.replace $(s2) [AEIOUaeiou] "" ] ; |
---|
92 | |
---|
93 | # Shorten remaining consonants to 4 characters |
---|
94 | s2 = [ MATCH ^(.?.?.?.?) : $(s2) ] ; |
---|
95 | |
---|
96 | # Glue the initial character back on to the front |
---|
97 | s2 = $(s1[1])$(s2) ; |
---|
98 | |
---|
99 | $(.abbreviated-$(string)) = $(s2) ; |
---|
100 | return $(s2) ; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | # Concatenates the given strings, inserting the given separator |
---|
105 | # between each string. |
---|
106 | # |
---|
107 | rule join ( |
---|
108 | strings * # The strings to join. |
---|
109 | : separator ? # The optional separator. |
---|
110 | ) |
---|
111 | { |
---|
112 | separator ?= "" ; |
---|
113 | return $(strings:J=$(separator)) ; |
---|
114 | } |
---|
115 | |
---|
116 | # Split a string into whitespace separated words. |
---|
117 | # |
---|
118 | rule words ( |
---|
119 | string # The string to split. |
---|
120 | : whitespace * # Optional, characters to consider as whitespace. |
---|
121 | ) |
---|
122 | { |
---|
123 | whitespace = $(whitespace:J="") ; |
---|
124 | whitespace ?= $(.whitespace) ; |
---|
125 | local w = ; |
---|
126 | while $(string) |
---|
127 | { |
---|
128 | string = [ MATCH "^[$(whitespace)]*([^$(whitespace)]*)(.*)" : $(string) ] ; |
---|
129 | if $(string[1]) && $(string[1]) != "" |
---|
130 | { |
---|
131 | w += $(string[1]) ; |
---|
132 | } |
---|
133 | string = $(string[2]) ; |
---|
134 | } |
---|
135 | return $(w) ; |
---|
136 | } |
---|
137 | |
---|
138 | # Check that the given string is composed entirely of whitespace. |
---|
139 | # |
---|
140 | rule is-whitespace ( |
---|
141 | string ? # The string to test. |
---|
142 | ) |
---|
143 | { |
---|
144 | if ! $(string) { return true ; } |
---|
145 | else if $(string) = "" { return true ; } |
---|
146 | else if [ MATCH "^([$(.whitespace)]+)$" : $(string) ] { return true ; } |
---|
147 | else { return ; } |
---|
148 | } |
---|
149 | |
---|
150 | rule __test__ ( ) |
---|
151 | { |
---|
152 | import assert ; |
---|
153 | assert.result a b c : chars abc ; |
---|
154 | |
---|
155 | assert.result rntm : abbreviate runtime ; |
---|
156 | assert.result ovrld : abbreviate overload ; |
---|
157 | assert.result dbg : abbreviate debugging ; |
---|
158 | assert.result async : abbreviate asynchronous ; |
---|
159 | assert.result pop : abbreviate pop ; |
---|
160 | assert.result aaa : abbreviate aaa ; |
---|
161 | assert.result qck : abbreviate quack ; |
---|
162 | assert.result sttc : abbreviate static ; |
---|
163 | |
---|
164 | # check boundary cases |
---|
165 | assert.result a : chars a ; |
---|
166 | assert.result : chars "" ; |
---|
167 | assert.result a b c d e f g h : chars abcdefgh ; |
---|
168 | assert.result a b c d e f g h i : chars abcdefghi ; |
---|
169 | assert.result a b c d e f g h i j : chars abcdefghij ; |
---|
170 | assert.result a b c d e f g h i j k : chars abcdefghijk ; |
---|
171 | |
---|
172 | assert.result a//b/c/d : join a "" b c d : / ; |
---|
173 | assert.result abcd : join a "" b c d ; |
---|
174 | |
---|
175 | assert.result a b c : words "a b c" ; |
---|
176 | |
---|
177 | assert.true is-whitespace " " ; |
---|
178 | assert.false is-whitespace " a b c " ; |
---|
179 | assert.true is-whitespace "" ; |
---|
180 | assert.true is-whitespace ; |
---|
181 | } |
---|