varmod-subst.mk revision 1.11 1 # $NetBSD: varmod-subst.mk,v 1.11 2023/06/01 20:56:35 rillig Exp $
2 #
3 # Tests for the :S,from,to, variable modifier.
4
5 all: mod-subst
6 all: mod-subst-delimiter
7 all: mod-subst-chain
8 all: mod-subst-dollar
9
10 WORDS= sequences of letters
11
12 .if ${WORDS:S,,,} != ${WORDS}
13 . warning The empty pattern matches something.
14 .endif
15
16 .if ${WORDS:S,e,*,1} != "s*quences of letters"
17 . warning The :S modifier flag '1' is not applied exactly once.
18 .endif
19
20 .if ${WORDS:S,f,*,1} != "sequences o* letters"
21 . warning The :S modifier flag '1' is only applied to the first word,\
22 not to the first occurrence.
23 .endif
24
25 .if ${WORDS:S,e,*,} != "s*quences of l*tters"
26 . warning The :S modifier does not replace every first match per word.
27 .endif
28
29 .if ${WORDS:S,e,*,g} != "s*qu*nc*s of l*tt*rs"
30 . warning The :S modifier flag 'g' does not replace every occurrence.
31 .endif
32
33 .if ${WORDS:S,^sequ,occurr,} != "occurrences of letters"
34 . warning The :S modifier fails for a short match anchored at the start.
35 .endif
36
37 .if ${WORDS:S,^of,with,} != "sequences with letters"
38 . warning The :S modifier fails for an exact match anchored at the start.
39 .endif
40
41 .if ${WORDS:S,^office,does not match,} != ${WORDS}
42 . warning The :S modifier matches a too long pattern anchored at the start.
43 .endif
44
45 .if ${WORDS:S,f$,r,} != "sequences or letters"
46 . warning The :S modifier fails for a short match anchored at the end.
47 .endif
48
49 .if ${WORDS:S,s$,,} != "sequence of letter"
50 . warning The :S modifier fails to replace one occurrence per word.
51 .endif
52
53 .if ${WORDS:S,of$,,} != "sequences letters"
54 . warning The :S modifier fails for an exact match anchored at the end.
55 .endif
56
57 .if ${WORDS:S,eof$,,} != ${WORDS}
58 . warning The :S modifier matches a too long pattern anchored at the end.
59 .endif
60
61 .if ${WORDS:S,^of$,,} != "sequences letters"
62 . warning The :S modifier does not match a word anchored at both ends.
63 .endif
64
65 .if ${WORDS:S,^o$,,} != ${WORDS}
66 . warning The :S modifier matches a prefix anchored at both ends.
67 .endif
68
69 .if ${WORDS:S,^f$,,} != ${WORDS}
70 . warning The :S modifier matches a suffix anchored at both ends.
71 .endif
72
73 .if ${WORDS:S,^eof$,,} != ${WORDS}
74 . warning The :S modifier matches a too long prefix anchored at both ends.
75 .endif
76
77 .if ${WORDS:S,^office$,,} != ${WORDS}
78 . warning The :S modifier matches a too long suffix anchored at both ends.
79 .endif
80
81 .if ${WORDS:S,*,replacement,} != ${WORDS}
82 . error The '*' seems to be interpreted as a wildcard of some kind.
83 .endif
84
85 .if ${WORDS:S,.,replacement,} != ${WORDS}
86 . error The '.' seems to be interpreted as a wildcard of some kind.
87 .endif
88
89 .if ${:Uvalue:S,^val,&,} != "value"
90 . error
91 .endif
92 .if ${:Uvalue:S,ue$,&,} != "value"
93 . error
94 .endif
95 .if ${:Uvalue:S,^val,&-&-&,} != "val-val-value"
96 . error
97 .endif
98 .if ${:Uvalue:S,ue$,&-&-&,} != "value-ue-ue"
99 . error
100 .endif
101
102
103 # When a word is replaced with nothing, the remaining words are separated by a
104 # single space, not two.
105 .if ${1 2 3:L:S,2,,} != "1 3"
106 . error
107 .endif
108
109
110 mod-subst:
111 @echo $@:
112 @echo :${:Ua b b c:S,a b,,:Q}:
113 @echo :${:Ua b b c:S,a b,,1:Q}:
114 @echo :${:Ua b b c:S,a b,,W:Q}:
115 @echo :${:Ua b b c:S,b,,g:Q}:
116 @echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
117 @echo ${:U12345:S,,sep,g:Q}
118
119 # The :S and :C modifiers accept an arbitrary character as the delimiter,
120 # including characters that are otherwise used as escape characters or
121 # interpreted in a special way. This can be used to confuse humans.
122 mod-subst-delimiter:
123 @echo $@:
124 @echo ${:U1 2 3:S 2 two :Q} horizontal tabulator
125 @echo ${:U1 2 3:S 2 two :Q} space
126 @echo ${:U1 2 3:S!2!two!:Q} exclamation mark
127 @echo ${:U1 2 3:S"2"two":Q} quotation mark
128 # In shell command lines, the hash does not need to be escaped.
129 # It needs to be escaped in variable assignment lines though.
130 @echo ${:U1 2 3:S#2#two#:Q} number sign
131 @echo ${:U1 2 3:S$2$two$:Q} dollar sign
132 @echo ${:U1 2 3:S%2%two%:Q} percent sign
133 @echo ${:U1 2 3:S&2&two&:Q} ampersand
134 @echo ${:U1 2 3:S'2'two':Q} apostrophe
135 @echo ${:U1 2 3:S(2(two(:Q} left parenthesis
136 @echo ${:U1 2 3:S)2)two):Q} right parenthesis
137 @echo ${:U1 2 3:S*2*two*:Q} asterisk
138 @echo ${:U1 2 3:S+2+two+:Q} plus sign
139 @echo ${:U1 2 3:S,2,two,:Q} comma
140 @echo ${:U1 2 3:S-2-two-:Q} hyphen-minus
141 @echo ${:U1 2 3:S.2.two.:Q} full stop
142 @echo ${:U1 2 3:S/2/two/:Q} solidus
143 @echo ${:U1 2 3:S121two1:Q} digit
144 @echo ${:U1 2 3:S:2:two::Q} colon
145 @echo ${:U1 2 3:S;2;two;:Q} semicolon
146 @echo ${:U1 2 3:S<2<two<:Q} less-than sign
147 @echo ${:U1 2 3:S=2=two=:Q} equals sign
148 @echo ${:U1 2 3:S>2>two>:Q} greater-than sign
149 @echo ${:U1 2 3:S?2?two?:Q} question mark
150 @echo ${:U1 2 3:S@2@two@:Q} commercial at
151 @echo ${:U1 2 3:SA2AtwoA:Q} capital letter
152 @echo ${:U1 2 3:S[2[two[:Q} left square bracket
153 @echo ${:U1 2 3:S\2\two\:Q} reverse solidus
154 @echo ${:U1 2 3:S]2]two]:Q} right square bracket
155 @echo ${:U1 2 3:S^2^two^:Q} circumflex accent
156 @echo ${:U1 2 3:S_2_two_:Q} low line
157 @echo ${:U1 2 3:S`2`two`:Q} grave accent
158 @echo ${:U1 2 3:Sa2atwoa:Q} small letter
159 @echo ${:U1 2 3:S{2{two{:Q} left curly bracket
160 @echo ${:U1 2 3:S|2|two|:Q} vertical line
161 @echo ${:U1 2 3:S}2}two}:Q} right curly bracket
162 @echo ${:U1 2 3:S~2~two~:Q} tilde
163
164 # The :S and :C modifiers can be chained without a separating ':'.
165 # This is not documented in the manual page.
166 # It works because ApplyModifier_Subst scans for the known modifiers g1W
167 # and then just returns to ApplyModifiers. There, the colon is optionally
168 # skipped (see the *st.next == ':' at the end of the loop).
169 #
170 # Most other modifiers cannot be chained since their parsers skip until
171 # the next ':' or '}' or ')'.
172 mod-subst-chain:
173 @echo $@:
174 @echo ${:Ua b c:S,a,A,S,b,B,}.
175 # There is no 'i' modifier for the :S or :C modifiers.
176 # The error message is "make: Unknown modifier 'i'", which is
177 # kind of correct, although it is mixing the terms for variable
178 # modifiers with the matching modifiers.
179 @echo ${:Uvalue:S,a,x,i}.
180
181 # No matter how many dollar signs there are, they all get merged
182 # into a single dollar by the :S modifier.
183 #
184 # As of 2020-08-09, this is because ParseModifierPart sees a '$' and
185 # calls Var_Parse to expand the variable. In all other places, the "$$"
186 # is handled outside of Var_Parse. Var_Parse therefore considers "$$"
187 # one of the "really stupid names", skips the first dollar, and parsing
188 # continues with the next character. This repeats for the other dollar
189 # signs, except the one before the delimiter. That one is handled by
190 # the code that optionally interprets the '$' as the end-anchor in the
191 # first part of the :S modifier. That code doesn't call Var_Parse but
192 # simply copies the dollar to the result.
193 mod-subst-dollar:
194 @echo $@:${:U1:S,^,$,:Q}:
195 @echo $@:${:U2:S,^,$$,:Q}:
196 @echo $@:${:U3:S,^,$$$,:Q}:
197 @echo $@:${:U4:S,^,$$$$,:Q}:
198 @echo $@:${:U5:S,^,$$$$$,:Q}:
199 @echo $@:${:U6:S,^,$$$$$$,:Q}:
200 @echo $@:${:U7:S,^,$$$$$$$,:Q}:
201 @echo $@:${:U8:S,^,$$$$$$$$,:Q}:
202 @echo $@:${:U40:S,^,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,:Q}:
203 # This generates no dollar at all:
204 @echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
205 # Here is an alternative way to generate dollar signs.
206 # It's unexpectedly complicated though.
207 @echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
208 # In modifiers, dollars are escaped using the backslash, not using another
209 # dollar sign. Therefore, creating a dollar sign is pretty simple:
210 @echo $@:${:Ugood3:S,^,\$\$\$,:Q}
211