varmod-to-separator.mk revision 1.3 1 1.3 rillig # $NetBSD: varmod-to-separator.mk,v 1.3 2020/08/31 19:58:21 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.3 rillig # The separator can be given as hexadecimal number.
99 1.3 rillig .if ${WORDS:[1..3]:ts\xa:tu} != "ONE${.newline}TWO${.newline}THREE"
100 1.3 rillig . warning The separator \xa is not interpreted in hexadecimal ASCII.
101 1.3 rillig .endif
102 1.3 rillig
103 1.3 rillig # In the :t modifier, the :t must be followed by any of A, l, s, u.
104 1.3 rillig .if ${WORDS:tx} != "anything"
105 1.3 rillig . info This line is not reached because of the malformed condition.
106 1.3 rillig . info If this line were reached, it would be visible in the -dcpv log.
107 1.3 rillig .endif
108 1.3 rillig .info Parsing continues here.
109 1.3 rillig
110 1.3 rillig # After the backslash, only n, t, an octal number, or x and a hexadecimal
111 1.3 rillig # number are allowed.
112 1.3 rillig .if ${WORDS:t\X} != "anything"
113 1.3 rillig . info This line is not reached.
114 1.3 rillig .endif
115 1.3 rillig .info Parsing continues here.
116 1.1 rillig
117 1.1 rillig all:
118 1.1 rillig @:;
119