1 | # (C) Copyright David Abrahams, 2002. |
---|
2 | # (C) Copyright Rene Rivera, 2003. |
---|
3 | # |
---|
4 | # See accompanying license for terms and conditions of use. |
---|
5 | # |
---|
6 | |
---|
7 | import regex ; |
---|
8 | |
---|
9 | # Characters considered whitespace, as a list. |
---|
10 | .whitespace-chars = " " " " " |
---|
11 | " ; |
---|
12 | |
---|
13 | # Characters considered whitespace, as a single string. |
---|
14 | .whitespace = $(.whitespace-chars:J="") ; |
---|
15 | |
---|
16 | # Returns the canonical set of whitespace characters, as a list. |
---|
17 | # |
---|
18 | rule whitespace-chars ( ) |
---|
19 | { |
---|
20 | return $(.whitespace-chars) ; |
---|
21 | } |
---|
22 | |
---|
23 | # Returns the canonical set of whitespace characters, as a single string. |
---|
24 | # |
---|
25 | rule whitespace ( ) |
---|
26 | { |
---|
27 | return $(.whitespace) ; |
---|
28 | } |
---|
29 | |
---|
30 | # Splits the given string into a list of strings composed |
---|
31 | # of each character of the string in sequence. |
---|
32 | # |
---|
33 | rule chars ( |
---|
34 | string # The string to split. |
---|
35 | ) |
---|
36 | { |
---|
37 | local result ; |
---|
38 | while $(string) |
---|
39 | { |
---|
40 | local s = [ MATCH (.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.*) : $(string) ] ; |
---|
41 | string = $(s[9]) ; |
---|
42 | result += $(s[1-8]) ; |
---|
43 | } |
---|
44 | |
---|
45 | # trim off empty strings |
---|
46 | while $(result[1]) && ! $(result[-1]) |
---|
47 | { |
---|
48 | result = $(result[1--2]) ; |
---|
49 | } |
---|
50 | |
---|
51 | return $(result) ; |
---|
52 | } |
---|
53 | |
---|
54 | # Concatenates the given strings, inserting the given separator |
---|
55 | # between each string. |
---|
56 | # |
---|
57 | rule join ( |
---|
58 | strings * # The strings to join. |
---|
59 | : separator ? # The optional separator. |
---|
60 | ) |
---|
61 | { |
---|
62 | separator ?= "" ; |
---|
63 | return $(strings:J=$(separator)) ; |
---|
64 | } |
---|
65 | |
---|
66 | # Split a string into whitespace separated words. |
---|
67 | # |
---|
68 | rule words ( |
---|
69 | string # The string to split. |
---|
70 | : whitespace * # Optional, characters to consider as whitespace. |
---|
71 | ) |
---|
72 | { |
---|
73 | whitespace = $(whitespace:J="") ; |
---|
74 | whitespace ?= $(.whitespace) ; |
---|
75 | local w = ; |
---|
76 | while $(string) |
---|
77 | { |
---|
78 | string = [ MATCH "^[$(whitespace)]*([^$(whitespace)]*)(.*)" : $(string) ] ; |
---|
79 | if $(string[1]) && $(string[1]) != "" |
---|
80 | { |
---|
81 | w += $(string[1]) ; |
---|
82 | } |
---|
83 | string = $(string[2]) ; |
---|
84 | } |
---|
85 | return $(w) ; |
---|
86 | } |
---|
87 | |
---|
88 | # Check that the given string is composed entirely of whitespace. |
---|
89 | # |
---|
90 | rule is-whitespace ( |
---|
91 | string ? # The string to test. |
---|
92 | ) |
---|
93 | { |
---|
94 | if ! $(string) { return true ; } |
---|
95 | else if $(string) = "" { return true ; } |
---|
96 | else if [ MATCH "^([$(.whitespace)]+)$" : $(string) ] { return true ; } |
---|
97 | else { return ; } |
---|
98 | } |
---|
99 | |
---|
100 | rule __test__ ( ) |
---|
101 | { |
---|
102 | import assert ; |
---|
103 | assert.result a b c : chars abc ; |
---|
104 | |
---|
105 | # check boundary cases |
---|
106 | assert.result a : chars a ; |
---|
107 | assert.result : chars "" ; |
---|
108 | assert.result a b c d e f g h : chars abcdefgh ; |
---|
109 | assert.result a b c d e f g h i : chars abcdefghi ; |
---|
110 | assert.result a b c d e f g h i j : chars abcdefghij ; |
---|
111 | assert.result a b c d e f g h i j k : chars abcdefghijk ; |
---|
112 | |
---|
113 | assert.result a//b/c/d : join a "" b c d : / ; |
---|
114 | assert.result abcd : join a "" b c d ; |
---|
115 | |
---|
116 | assert.result a b c : words "a b c" ; |
---|
117 | |
---|
118 | assert.true is-whitespace " " ; |
---|
119 | assert.false is-whitespace " a b c " ; |
---|
120 | assert.true is-whitespace "" ; |
---|
121 | assert.true is-whitespace ; |
---|
122 | } |
---|