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