varmod-to-separator.mk revision 1.7 1 1.7 rillig # $NetBSD: varmod-to-separator.mk,v 1.7 2020/11/15 20:20:58 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :ts variable modifier, which joins the words of the variable
4 1.2 rillig # using an arbitrary character as word separator.
5 1.1 rillig
6 1.3 rillig WORDS= one two three four five six
7 1.3 rillig
8 1.3 rillig # The words are separated by a single space, just as usual.
9 1.3 rillig .if ${WORDS:ts } != "one two three four five six"
10 1.3 rillig . warning Space as separator does not work.
11 1.3 rillig .endif
12 1.3 rillig
13 1.3 rillig # The separator can be an arbitrary character, for example a comma.
14 1.3 rillig .if ${WORDS:ts,} != "one,two,three,four,five,six"
15 1.3 rillig . warning Comma as separator does not work.
16 1.3 rillig .endif
17 1.3 rillig
18 1.3 rillig # After the :ts modifier, other modifiers can follow.
19 1.3 rillig .if ${WORDS:ts/:tu} != "ONE/TWO/THREE/FOUR/FIVE/SIX"
20 1.3 rillig . warning Chaining modifiers does not work.
21 1.3 rillig .endif
22 1.3 rillig
23 1.3 rillig # To use the ':' as the separator, just write it normally.
24 1.3 rillig # The first colon is the separator, the second ends the modifier.
25 1.3 rillig .if ${WORDS:ts::tu} != "ONE:TWO:THREE:FOUR:FIVE:SIX"
26 1.3 rillig . warning Colon as separator does not work.
27 1.3 rillig .endif
28 1.3 rillig
29 1.3 rillig # When there is just a colon but no other character, the words are
30 1.3 rillig # "separated" by an empty string, that is, they are all squashed
31 1.3 rillig # together.
32 1.3 rillig .if ${WORDS:ts:tu} != "ONETWOTHREEFOURFIVESIX"
33 1.3 rillig . warning Colon as separator does not work.
34 1.3 rillig .endif
35 1.3 rillig
36 1.3 rillig # Applying the :tu modifier first and then the :ts modifier does not change
37 1.3 rillig # anything since neither of these modifiers is related to how the string is
38 1.3 rillig # split into words. Beware of separating the words using a single or double
39 1.3 rillig # quote though, or other special characters like dollar or backslash.
40 1.3 rillig #
41 1.3 rillig # This example also demonstrates that the closing brace is not interpreted
42 1.3 rillig # as a separator, but as the closing delimiter of the whole variable
43 1.3 rillig # expression.
44 1.3 rillig .if ${WORDS:tu:ts} != "ONETWOTHREEFOURFIVESIX"
45 1.3 rillig . warning Colon as separator does not work.
46 1.3 rillig .endif
47 1.3 rillig
48 1.3 rillig # The '}' plays the same role as the ':' in the preceding examples.
49 1.3 rillig # Since there is a single character before it, that character is taken as
50 1.3 rillig # the separator.
51 1.3 rillig .if ${WORDS:tu:ts/} != "ONE/TWO/THREE/FOUR/FIVE/SIX"
52 1.3 rillig . warning Colon as separator does not work.
53 1.3 rillig .endif
54 1.3 rillig
55 1.3 rillig # Now it gets interesting and ambiguous: The separator could either be empty
56 1.3 rillig # since it is followed by a colon. Or it could be the colon since that
57 1.3 rillig # colon is followed by the closing brace. It's the latter case.
58 1.3 rillig .if ${WORDS:ts:} != "one:two:three:four:five:six"
59 1.3 rillig . warning Colon followed by closing brace does not work.
60 1.3 rillig .endif
61 1.3 rillig
62 1.3 rillig # As in the ${WORDS:tu:ts} example above, the separator is empty.
63 1.3 rillig .if ${WORDS:ts} != "onetwothreefourfivesix"
64 1.3 rillig . warning Empty separator before closing brace does not work.
65 1.3 rillig .endif
66 1.3 rillig
67 1.3 rillig # The :ts modifier can be followed by other modifiers.
68 1.3 rillig .if ${WORDS:ts:S/two/2/} != "one2threefourfivesix"
69 1.3 rillig . warning Separator followed by :S modifier does not work.
70 1.3 rillig .endif
71 1.3 rillig
72 1.3 rillig # The :ts modifier can follow other modifiers.
73 1.3 rillig .if ${WORDS:S/two/2/:ts} != "one2threefourfivesix"
74 1.3 rillig . warning :S modifier followed by :ts modifier does not work.
75 1.3 rillig .endif
76 1.3 rillig
77 1.3 rillig # The :ts modifier with an actual separator can be followed by other
78 1.3 rillig # modifiers.
79 1.3 rillig .if ${WORDS:ts/:S/two/2/} != "one/2/three/four/five/six"
80 1.3 rillig . warning The :ts modifier followed by an :S modifier does not work.
81 1.3 rillig .endif
82 1.3 rillig
83 1.3 rillig # The separator can be \n, which is a newline.
84 1.3 rillig .if ${WORDS:[1..3]:ts\n} != "one${.newline}two${.newline}three"
85 1.3 rillig . warning The separator \n does not produce a newline.
86 1.3 rillig .endif
87 1.3 rillig
88 1.3 rillig # The separator can be \t, which is a tab.
89 1.3 rillig .if ${WORDS:[1..3]:ts\t} != "one two three"
90 1.3 rillig . warning The separator \t does not produce a tab.
91 1.3 rillig .endif
92 1.3 rillig
93 1.3 rillig # The separator can be given as octal number.
94 1.3 rillig .if ${WORDS:[1..3]:ts\012:tu} != "ONE${.newline}TWO${.newline}THREE"
95 1.3 rillig . warning The separator \012 is not interpreted in octal ASCII.
96 1.3 rillig .endif
97 1.3 rillig
98 1.4 rillig # The octal number can have as many digits as it wants.
99 1.4 rillig .if ${WORDS:[1..2]:ts\000000000000000000000000012:tu} != "ONE${.newline}TWO"
100 1.4 rillig . warning The separator \012 cannot have many leading zeroes.
101 1.4 rillig .endif
102 1.4 rillig
103 1.4 rillig # The value of the separator character must not be outside the value space
104 1.4 rillig # for an unsigned character though.
105 1.6 rillig #
106 1.6 rillig # Since 2020-11-01, these out-of-bounds values are rejected.
107 1.4 rillig .if ${WORDS:[1..3]:ts\400:tu}
108 1.4 rillig . warning The separator \400 is accepted even though it is out of bounds.
109 1.4 rillig .else
110 1.4 rillig . warning The separator \400 is accepted even though it is out of bounds.
111 1.4 rillig .endif
112 1.4 rillig
113 1.3 rillig # The separator can be given as hexadecimal number.
114 1.3 rillig .if ${WORDS:[1..3]:ts\xa:tu} != "ONE${.newline}TWO${.newline}THREE"
115 1.3 rillig . warning The separator \xa is not interpreted in hexadecimal ASCII.
116 1.3 rillig .endif
117 1.3 rillig
118 1.4 rillig # The hexadecimal number must be in the range of an unsigned char.
119 1.6 rillig #
120 1.6 rillig # Since 2020-11-01, these out-of-bounds values are rejected.
121 1.4 rillig .if ${WORDS:[1..3]:ts\x100:tu}
122 1.4 rillig . warning The separator \x100 is accepted even though it is out of bounds.
123 1.4 rillig .else
124 1.4 rillig . warning The separator \x100 is accepted even though it is out of bounds.
125 1.4 rillig .endif
126 1.4 rillig
127 1.5 rillig # Negative numbers are not allowed for the separator character.
128 1.5 rillig .if ${WORDS:[1..3]:ts\-300:tu}
129 1.5 rillig . warning The separator \-300 is accepted even though it is negative.
130 1.5 rillig .else
131 1.5 rillig . warning The separator \-300 is accepted even though it is negative.
132 1.5 rillig .endif
133 1.5 rillig
134 1.5 rillig # The character number is interpreted as octal number by default.
135 1.5 rillig # The digit '8' is not an octal digit though.
136 1.5 rillig .if ${1 2 3:L:ts\8:tu}
137 1.5 rillig . warning The separator \8 is accepted even though it is not octal.
138 1.5 rillig .else
139 1.5 rillig . warning The separator \8 is accepted even though it is not octal.
140 1.5 rillig .endif
141 1.5 rillig
142 1.5 rillig # Trailing characters after the octal character number are rejected.
143 1.5 rillig .if ${1 2 3:L:ts\100L}
144 1.5 rillig . warning The separator \100L is accepted even though it contains an 'L'.
145 1.5 rillig .else
146 1.5 rillig . warning The separator \100L is accepted even though it contains an 'L'.
147 1.5 rillig .endif
148 1.5 rillig
149 1.5 rillig # Trailing characters after the hexadecimal character number are rejected.
150 1.5 rillig .if ${1 2 3:L:ts\x40g}
151 1.5 rillig . warning The separator \x40g is accepted even though it contains a 'g'.
152 1.5 rillig .else
153 1.5 rillig . warning The separator \x40g is accepted even though it contains a 'g'.
154 1.5 rillig .endif
155 1.5 rillig
156 1.5 rillig
157 1.3 rillig # In the :t modifier, the :t must be followed by any of A, l, s, u.
158 1.3 rillig .if ${WORDS:tx} != "anything"
159 1.3 rillig . info This line is not reached because of the malformed condition.
160 1.3 rillig . info If this line were reached, it would be visible in the -dcpv log.
161 1.3 rillig .endif
162 1.3 rillig
163 1.3 rillig # After the backslash, only n, t, an octal number, or x and a hexadecimal
164 1.3 rillig # number are allowed.
165 1.3 rillig .if ${WORDS:t\X} != "anything"
166 1.3 rillig . info This line is not reached.
167 1.3 rillig .endif
168 1.1 rillig
169 1.7 rillig # TODO: This modifier used to accept decimal numbers as well, in the form
170 1.7 rillig # ':ts\120'. When has this been changed to octal, and what happens now
171 1.7 rillig # for ':ts\90' ('Z' in decimal ASCII, undefined in octal)?
172 1.7 rillig
173 1.7 rillig # TODO: :ts\x1F600
174 1.7 rillig
175 1.1 rillig all:
176