aboutsummaryrefslogtreecommitdiff
path: root/contrib/bmake/unit-tests/varmod-subst.mk
blob: 2903d36445f843015dfd257e2350854a78ed9cb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# $NetBSD: varmod-subst.mk,v 1.14 2023/12/18 11:13:51 rillig Exp $
#
# Tests for the :S,from,to, variable modifier.

all: mod-subst
all: mod-subst-delimiter
all: mod-subst-chain
all: mod-subst-dollar

WORDS=		sequences of letters

# The empty pattern never matches anything, except if it is anchored at the
# beginning or the end of the word.
.if ${WORDS:S,,,} != ${WORDS}
.  error
.endif

# The :S modifier flag '1' is applied exactly once.
.if ${WORDS:S,e,*,1} != "s*quences of letters"
.  error
.endif

# The :S modifier flag '1' is applied to the first occurrence, no matter if
# the occurrence is in the first word or not.
.if ${WORDS:S,f,*,1} != "sequences o* letters"
.  error
.endif

# The :S modifier replaces every first match per word.
.if ${WORDS:S,e,*,} != "s*quences of l*tters"
.  error
.endif

# The :S modifier flag 'g' replaces every occurrence.
.if ${WORDS:S,e,*,g} != "s*qu*nc*s of l*tt*rs"
.  error
.endif

# The '^' in the search pattern anchors the pattern at the beginning of each
# word, thereby matching a prefix.
.if ${WORDS:S,^sequ,occurr,} != "occurrences of letters"
.  error
.endif

# The :S modifier with a '^' anchor replaces the whole word if that word is
# exactly the pattern.
.if ${WORDS:S,^of,with,} != "sequences with letters"
.  error
.endif

# The :S modifier does not match if the pattern is longer than the word.
.if ${WORDS:S,^office,does not match,} != ${WORDS}
.  warning
.endif

# The '$' in the search pattern anchors the pattern at the end of each word,
# thereby matching a suffix.
.if ${WORDS:S,f$,r,} != "sequences or letters"
.  error
.endif

# The :S modifier with a '$' anchor replaces at most one occurrence per word.
.if ${WORDS:S,s$,,} != "sequence of letter"
.  error
.endif

# The :S modifier with a '$' anchor replaces the whole word if that word is
# exactly the pattern.
.if ${WORDS:S,of$,,} != "sequences letters"
.  error
.endif

# The :S modifier with a '$' anchor and a pattern that is longer than a word
# cannot match that word.
.if ${WORDS:S,eof$,,} != ${WORDS}
.  warning
.endif

# The :S modifier with the '^' and '$' anchors matches an exact word.
.if ${WORDS:S,^of$,,} != "sequences letters"
.  error
.endif

# The :S modifier with the '^' and '$' anchors does not match a word that
# starts with the pattern but is longer than the pattern.
.if ${WORDS:S,^o$,,} != ${WORDS}
.  error
.endif

# The :S modifier with the '^' and '$' anchors does not match a word that ends
# with the pattern but is longer than the pattern.
.if ${WORDS:S,^f$,,} != ${WORDS}
.  error
.endif

# The :S modifier with the '^' and '$' anchors does not match a word if the
# pattern ends with the word but is longer than the word.
.if ${WORDS:S,^eof$,,} != ${WORDS}
.  error
.endif

# The :S modifier with the '^' and '$' anchors does not match a word if the
# pattern starts with the word but is longer than the word.
.if ${WORDS:S,^office$,,} != ${WORDS}
.  error
.endif

# Except for the '^' and '$' anchors, the pattern does not contain any special
# characters, so the '*' from the pattern would only match a literal '*' in a
# word.
.if ${WORDS:S,*,replacement,} != ${WORDS}
.  error
.endif

# Except for the '^' and '$' anchors, the pattern does not contain any special
# characters, so the '.' from the pattern would only match a literal '.' in a
# word.
.if ${WORDS:S,.,replacement,} != ${WORDS}
.  error
.endif

# The '&' in the replacement is a placeholder for the text matched by the
# pattern.
.if ${:Uvalue:S,^val,&,} != "value"
.  error
.endif
.if ${:Uvalue:S,ue$,&,} != "value"
.  error
.endif
.if ${:Uvalue:S,^val,&-&-&,} != "val-val-value"
.  error
.endif
.if ${:Uvalue:S,ue$,&-&-&,} != "value-ue-ue"
.  error
.endif


# When a word is replaced with nothing, the remaining words are separated by a
# single space, not two.
.if ${1 2 3:L:S,2,,} != "1 3"
.  error
.endif


# In an empty expression, the ':S' modifier matches a single time, but only if
# the search string is empty and anchored at either the beginning or the end
# of the word.
.if ${:U:S,,out-of-nothing,} != ""
.  error
.endif
.if ${:U:S,^,out-of-nothing,} != "out-of-nothing"
.  error
.endif
.if ${:U:S,$,out-of-nothing,} != "out-of-nothing"
.  error
.endif
.if ${:U:S,^$,out-of-nothing,} != "out-of-nothing"
.  error
.endif
.if ${:U:S,,out-of-nothing,g} != ""
.  error
.endif
.if ${:U:S,^,out-of-nothing,g} != "out-of-nothing"
.  error
.endif
.if ${:U:S,$,out-of-nothing,g} != "out-of-nothing"
.  error
.endif
.if ${:U:S,^$,out-of-nothing,g} != "out-of-nothing"
.  error
.endif
.if ${:U:S,,out-of-nothing,W} != ""
.  error
.endif
.if ${:U:S,^,out-of-nothing,W} != "out-of-nothing"
.  error
.endif
.if ${:U:S,$,out-of-nothing,W} != "out-of-nothing"
.  error
.endif
.if ${:U:S,^$,out-of-nothing,W} != "out-of-nothing"
.  error
.endif


mod-subst:
	@echo $@:
	@echo :${:Ua b b c:S,a b,,:Q}:
	@echo :${:Ua b b c:S,a b,,1:Q}:
	@echo :${:Ua b b c:S,a b,,W:Q}:
	@echo :${:Ua b b c:S,b,,g:Q}:
	@echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
	@echo ${:U12345:S,,sep,g:Q}

# The :S and :C modifiers accept an arbitrary character as the delimiter,
# including characters that are otherwise used as escape characters or
# interpreted in a special way.  This can be used to confuse humans.
mod-subst-delimiter:
	@echo $@:
	@echo ${:U1 2 3:S	2	two	:Q} horizontal tabulator
	@echo ${:U1 2 3:S 2 two :Q} space
	@echo ${:U1 2 3:S!2!two!:Q} exclamation mark
	@echo ${:U1 2 3:S"2"two":Q} quotation mark
	# In shell command lines, the hash does not need to be escaped.
	# It needs to be escaped in variable assignment lines though.
	@echo ${:U1 2 3:S#2#two#:Q} number sign
	@echo ${:U1 2 3:S$2$two$:Q} dollar sign
	@echo ${:U1 2 3:S%2%two%:Q} percent sign
	@echo ${:U1 2 3:S&2&two&:Q} ampersand
	@echo ${:U1 2 3:S'2'two':Q} apostrophe
	@echo ${:U1 2 3:S(2(two(:Q} left parenthesis
	@echo ${:U1 2 3:S)2)two):Q} right parenthesis
	@echo ${:U1 2 3:S*2*two*:Q} asterisk
	@echo ${:U1 2 3:S+2+two+:Q} plus sign
	@echo ${:U1 2 3:S,2,two,:Q} comma
	@echo ${:U1 2 3:S-2-two-:Q} hyphen-minus
	@echo ${:U1 2 3:S.2.two.:Q} full stop
	@echo ${:U1 2 3:S/2/two/:Q} solidus
	@echo ${:U1 2 3:S121two1:Q} digit
	@echo ${:U1 2 3:S:2:two::Q} colon
	@echo ${:U1 2 3:S;2;two;:Q} semicolon
	@echo ${:U1 2 3:S<2<two<:Q} less-than sign
	@echo ${:U1 2 3:S=2=two=:Q} equals sign
	@echo ${:U1 2 3:S>2>two>:Q} greater-than sign
	@echo ${:U1 2 3:S?2?two?:Q} question mark
	@echo ${:U1 2 3:S@2@two@:Q} commercial at
	@echo ${:U1 2 3:SA2AtwoA:Q} capital letter
	@echo ${:U1 2 3:S[2[two[:Q} left square bracket
	@echo ${:U1 2 3:S\2\two\:Q} reverse solidus
	@echo ${:U1 2 3:S]2]two]:Q} right square bracket
	@echo ${:U1 2 3:S^2^two^:Q} circumflex accent
	@echo ${:U1 2 3:S_2_two_:Q} low line
	@echo ${:U1 2 3:S`2`two`:Q} grave accent
	@echo ${:U1 2 3:Sa2atwoa:Q} small letter
	@echo ${:U1 2 3:S{2{two{:Q} left curly bracket
	@echo ${:U1 2 3:S|2|two|:Q} vertical line
	@echo ${:U1 2 3:S}2}two}:Q} right curly bracket
	@echo ${:U1 2 3:S~2~two~:Q} tilde

# The :S and :C modifiers can be chained without a separating ':'.
# This is not documented in the manual page.
# It works because ApplyModifier_Subst scans for the known modifiers g1W
# and then just returns to ApplyModifiers.  There, the colon is optionally
# skipped (see the *st.next == ':' at the end of the loop).
#
# Most other modifiers cannot be chained since their parsers skip until
# the next ':' or '}' or ')'.
mod-subst-chain:
	@echo $@:
	@echo ${:Ua b c:S,a,A,S,b,B,}.
	# There is no 'i' modifier for the :S or :C modifiers.
	# The error message is "make: Unknown modifier 'i'", which is
	# kind of correct, although it is mixing the terms for variable
	# modifiers with the matching modifiers.
	@echo ${:Uvalue:S,a,x,i}.

# No matter how many dollar signs there are, they all get merged
# into a single dollar by the :S modifier.
#
# As of 2020-08-09, this is because ParseModifierPart sees a '$' and
# calls Var_Parse to expand the variable.  In all other places, the "$$"
# is handled outside of Var_Parse.  Var_Parse therefore considers "$$"
# one of the "really stupid names", skips the first dollar, and parsing
# continues with the next character.  This repeats for the other dollar
# signs, except the one before the delimiter.  That one is handled by
# the code that optionally interprets the '$' as the end-anchor in the
# first part of the :S modifier.  That code doesn't call Var_Parse but
# simply copies the dollar to the result.
mod-subst-dollar:
	@echo $@:${:U1:S,^,$,:Q}:
	@echo $@:${:U2:S,^,$$,:Q}:
	@echo $@:${:U3:S,^,$$$,:Q}:
	@echo $@:${:U4:S,^,$$$$,:Q}:
	@echo $@:${:U5:S,^,$$$$$,:Q}:
	@echo $@:${:U6:S,^,$$$$$$,:Q}:
	@echo $@:${:U7:S,^,$$$$$$$,:Q}:
	@echo $@:${:U8:S,^,$$$$$$$$,:Q}:
	@echo $@:${:U40:S,^,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,:Q}:
# This generates no dollar at all:
	@echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
# Here is an alternative way to generate dollar signs.
# It's unexpectedly complicated though.
	@echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
# In modifiers, dollars are escaped using the backslash, not using another
# dollar sign.  Therefore, creating a dollar sign is pretty simple:
	@echo $@:${:Ugood3:S,^,\$\$\$,:Q}