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)
9 # Characters considered whitespace, as a list.
10 .whitespace-chars = " " " " "
13 # Characters considered whitespace, as a single string.
14 .whitespace = $(.whitespace-chars:J="") ;
17 # Returns the canonical set of whitespace characters, as a list.
19 rule whitespace-chars ( )
21 return $(.whitespace-chars) ;
25 # Returns the canonical set of whitespace characters, as a single string.
29 return $(.whitespace) ;
33 # Splits the given string into a list of strings composed of each character of
34 # the string in sequence.
37 string # The string to split.
43 local s = [ MATCH (.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.*) : $(string) ] ;
48 # Trim off empty strings.
49 while $(result[1]) && ! $(result[-1])
51 result = $(result[1--2]) ;
58 # Apply a set of standard transformations to string to produce an abbreviation
59 # no more than 5 characters long.
61 rule abbreviate ( string )
63 local r = $(.abbreviated-$(string)) ;
68 # Anything less than 4 characters gets no abbreviation.
69 else if ! [ MATCH (....) : $(string) ]
71 .abbreviated-$(string) = $(string) ;
76 # Separate the initial letter in case it's a vowel.
77 local s1 = [ MATCH ^(.)(.*) : $(string) ] ;
79 # Drop trailing "ing".
80 local s2 = [ MATCH ^(.*)ing$ : $(s1[2]) ] ;
83 # Reduce all doubled characters to one.
85 for local c in [ chars $(s2) ]
95 # Chop all vowels out of the remainder.
96 s2 = [ regex.replace $(s2) [AEIOUaeiou] "" ] ;
98 # Shorten remaining consonants to 4 characters.
99 s2 = [ MATCH ^(.?.?.?.?) : $(s2) ] ;
101 # Glue the initial character back on to the front.
104 .abbreviated-$(string) = $(s2) ;
110 # Concatenates the given strings, inserting the given separator between each
114 strings * # The strings to join.
115 : separator ? # The optional separator.
119 return $(strings:J=$(separator)) ;
123 # Split a string into whitespace separated words.
126 string # The string to split.
127 : whitespace * # Optional, characters to consider as whitespace.
130 whitespace = $(whitespace:J="") ;
131 whitespace ?= $(.whitespace) ;
135 string = [ MATCH "^[$(whitespace)]*([^$(whitespace)]*)(.*)" : $(string) ] ;
136 if $(string[1]) && $(string[1]) != ""
140 string = $(string[2]) ;
146 # Check that the given string is composed entirely of whitespace.
149 string ? # The string to test.
152 if ! $(string) { return true ; }
153 else if $(string) = "" { return true ; }
154 else if [ MATCH "^([$(.whitespace)]+)$" : $(string) ] { return true ; }
161 assert.result a b c : chars abc ;
163 assert.result rntm : abbreviate runtime ;
164 assert.result ovrld : abbreviate overload ;
165 assert.result dbg : abbreviate debugging ;
166 assert.result async : abbreviate asynchronous ;
167 assert.result pop : abbreviate pop ;
168 assert.result aaa : abbreviate aaa ;
169 assert.result qck : abbreviate quack ;
170 assert.result sttc : abbreviate static ;
172 # Check boundary cases.
173 assert.result a : chars a ;
174 assert.result : chars "" ;
175 assert.result a b c d e f g h : chars abcdefgh ;
176 assert.result a b c d e f g h i : chars abcdefghi ;
177 assert.result a b c d e f g h i j : chars abcdefghij ;
178 assert.result a b c d e f g h i j k : chars abcdefghijk ;
180 assert.result a//b/c/d : join a "" b c d : / ;
181 assert.result abcd : join a "" b c d ;
183 assert.result a b c : words "a b c" ;
185 assert.true is-whitespace " " ;
186 assert.false is-whitespace " a b c " ;
187 assert.true is-whitespace "" ;
188 assert.true is-whitespace ;