1# $NetBSD: varmod-match.mk,v 1.20 2023/12/17 23:19:02 rillig Exp $
2#
3# Tests for the ':M' modifier, which keeps only those words that match the
4# given pattern.
5#
6# Table of contents
7#
8# 1. Pattern characters '*', '?' and '\'
9# 2. Character lists and character ranges
10# 3. Parsing and escaping
11# 4. Interaction with other modifiers
12# 5. Performance
13# 6. Error handling
14# 7. Historical bugs
15#
16# See ApplyModifier_Match, ParseModifier_Match, ModifyWord_Match and
17# Str_Match.
18
19
20# 1. Pattern characters '*', '?' and '\'
21#
22#	*	matches 0 or more characters
23#	?	matches 1 character
24#	\x	matches the character 'x'
25
26# The pattern is anchored both at the beginning and at the end of the word.
27# Since the pattern 'e' does not contain any pattern matching characters, it
28# matches exactly the word 'e', twice.
29.if ${a c e aa cc ee e f g:L:Me} != "e e"
30.  error
31.endif
32
33# The pattern character '?' matches exactly 1 character, the pattern character
34# '*' matches 0 or more characters.  The whole pattern matches all words that
35# start with 's' and have 3 or more characters.
36.if ${One Two Three Four five six seven:L:Ms??*} != "six seven"
37.  error
38.endif
39
40# Ensure that a pattern without placeholders only matches itself.
41.if ${a aa aaa b ba baa bab:L:Ma} != "a"
42.  error
43.endif
44
45# Ensure that a pattern that ends with '*' is properly anchored at the
46# beginning.
47.if ${a aa aaa b ba baa bab:L:Ma*} != "a aa aaa"
48.  error
49.endif
50
51# Ensure that a pattern that starts with '*' is properly anchored at the end.
52.if ${a aa aaa b ba baa bab:L:M*a} != "a aa aaa ba baa"
53.  error
54.endif
55
56# Test the fast code path for '*' followed by a regular character.
57.if ${:U file.c file.*c file.h file\.c :M*.c} != "file.c file\\.c"
58.  error
59.endif
60# Ensure that the fast code path correctly handles the backslash.
61.if ${:U file.c file.*c file.h file\.c :M*\.c} != "file.c file\\.c"
62.  error
63.endif
64# Ensure that the fast code path correctly handles '\*'.
65.if ${:U file.c file.*c file.h file\.c :M*\*c} != "file.*c"
66.  error
67.endif
68# Ensure that the partial match '.c' doesn't confuse the fast code path.
69.if ${:U file.c.cc file.cc.cc file.cc.c :M*.cc} != "file.c.cc file.cc.cc"
70.  error
71.endif
72# Ensure that the substring '.cc' doesn't confuse the fast code path for '.c'.
73.if ${:U file.c.cc file.cc.cc file.cc.c :M*.c} != "file.cc.c"
74.  error
75.endif
76
77
78# 2. Character lists and character ranges
79#
80#	[...]	matches 1 character from the listed characters
81#	[^...]	matches 1 character from the unlisted characters
82#	[a-z]	matches 1 character from the range 'a' to 'z'
83#	[z-a]	matches 1 character from the range 'a' to 'z'
84
85# Only keep words that start with an uppercase letter.
86.if ${One Two Three Four five six seven:L:M[A-Z]*} != "One Two Three Four"
87.  error
88.endif
89
90# Only keep words that start with a character other than an uppercase letter.
91.if ${One Two Three Four five six seven:L:M[^A-Z]*} != "five six seven"
92.  error
93.endif
94
95#	[]	matches never
96.if ${ ab a[]b a[b a b :L:M[]} != ""
97.  error
98.endif
99
100#	a[]b	matches never
101.if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != ""
102.  error
103.endif
104
105#	[^]	matches exactly 1 arbitrary character
106.if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]"
107.  error
108.endif
109
110#	a[^]b	matches 'a', then exactly 1 arbitrary character, then 'b'
111.if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b"
112.  error
113.endif
114
115#	[Nn0]	matches exactly 1 character from the set 'N', 'n', '0'
116.if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0"
117.  error
118.endif
119
120#	[a-c]	matches exactly 1 character from the range 'a' to 'c'
121.if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c"
122.  error
123.endif
124
125#	[c-a]	matches the same as [a-c]
126.if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c"
127.  error
128.endif
129
130#	[^a-c67]
131#		matches a single character, except for 'a', 'b', 'c', '6' or
132#		'7'
133.if ${ A B C a b c d 5 6 7 8 [a-c] [a] :L:M[^a-c67]} != "A B C d 5 8"
134.  error
135.endif
136
137#	[\]	matches a single backslash; no escaping takes place in
138#		character ranges
139# Without the 'b' in the below words, the backslash would end a word and thus
140# influence how the string is split into words.
141WORDS=		a\b a[\]b ab a\\b
142.if ${WORDS:Ma[\]b} != "a\\b"
143.  error
144.endif
145
146#	[[-]]	May look like it would match a single '[', '\' or ']', but
147#		the inner ']' has two roles: it is the upper bound of the
148#		character range as well as the closing character of the
149#		character list.  The outer ']' is just a regular character.
150WORDS=		[ ] [] \] ]]
151.if ${WORDS:M[[-]]} != "[] \\] ]]"
152.  error
153.endif
154
155#	[b[-]a]
156#		Same as for '[[-]]': the character list stops at the first
157#		']', and the 'a]' is treated as a literal string.
158WORDS=		[a \a ]a []a \]a ]]a [a] \a] ]a] ba]
159.if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]"
160.  error
161.endif
162
163#	[-]	Matches a single '-' since the '-' only becomes part of a
164#		character range if it is preceded and followed by another
165#		character.
166WORDS=		- -]
167.if ${WORDS:M[-]} != "-"
168.  error
169.endif
170
171# Only keep words that don't start with s and at the same time end with
172# either of [ex].
173#
174# This test case ensures that the negation from the first character list
175# '[^s]' does not propagate to the second character list '[ex]'.
176.if ${One Two Three Four five six seven:L:M[^s]*[ex]} != "One Three five"
177.  error
178.endif
179
180
181# 3. Parsing and escaping
182#
183#	*	matches 0 or more characters
184#	?	matches 1 character
185#	\	outside a character list, escapes the following character
186#	[	starts a character list for matching 1 character
187#	]	ends a character list for matching 1 character
188#	-	in a character list, forms a character range
189#	^	at the beginning of a character list, negates the list
190#	(	while parsing the pattern, starts a nesting level
191#	)	while parsing the pattern, ends a nesting level
192#	{	while parsing the pattern, starts a nesting level
193#	}	while parsing the pattern, ends a nesting level
194#	:	while parsing the pattern, terminates the pattern
195#	$	while parsing the pattern, starts a nested expression
196#	#	in a line except a shell command, starts a comment
197
198# The pattern can come from an expression.  For single-letter
199# variables, either the short form or the long form can be used, just as
200# everywhere else.
201PRIMES=	2 3 5 7 11
202n=	2
203.if ${PRIMES:M$n} != "2"
204.  error
205.endif
206.if ${PRIMES:M${n}} != "2"
207.  error
208.endif
209.if ${PRIMES:M${:U2}} != "2"
210.  error
211.endif
212
213#	:	terminates the pattern
214.if ${ A * :L:M:} != ""
215.  error
216.endif
217
218#	\:	matches a colon
219.if ${ ${:U\: \:\:} :L:M\:} != ":"
220.  error
221.endif
222
223#	${:U\:}	matches a colon
224.if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":"
225.  error
226.endif
227
228# To match a dollar sign in a word, double it.
229#
230# This is different from the :S and :C modifiers, where a '$' has to be
231# escaped as '\$'.
232.if ${:Ua \$ sign:M*$$*} != "\$"
233.  error
234.endif
235
236# In the :M modifier, '\$' does not escape a dollar.  Instead it is
237# interpreted as a backslash followed by whatever expression the
238# '$' starts.
239#
240# This differs from the :S, :C and several other modifiers.
241${:U*}=		asterisk
242.if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk"
243.  error
244.endif
245
246# TODO: ${VAR:M(((}}}}
247# TODO: ${VAR:M{{{)))}
248# TODO: ${VAR:M${UNBALANCED}}
249# TODO: ${VAR:M${:U(((\}\}\}}}
250
251
252# 4. Interaction with other modifiers
253
254# The modifier ':tW' prevents splitting at whitespace.  Even leading and
255# trailing whitespace is preserved.
256.if ${   plain   string   :L:tW:M*} != "   plain   string   "
257.  error
258.endif
259
260# Without the modifier ':tW', the string is split into words.  All whitespace
261# around and between the words is normalized to a single space.
262.if ${   plain    string   :L:M*} != "plain string"
263.  error
264.endif
265
266
267# 5. Performance
268
269# Before 2020-06-13, this expression called Str_Match 601,080,390 times.
270# Since 2020-06-13, this expression calls Str_Match 1 time.
271.if ${:U****************:M****************b}
272.endif
273
274# Before 2023-06-22, this expression called Str_Match 2,621,112 times.
275# Adding another '*?' to the pattern called Str_Match 20,630,572 times.
276# Adding another '*?' to the pattern called Str_Match 136,405,672 times.
277# Adding another '*?' to the pattern called Str_Match 773,168,722 times.
278# Adding another '*?' to the pattern called Str_Match 3,815,481,072 times.
279# Since 2023-06-22, Str_Match no longer backtracks.
280.if ${:U..................................................b:M*?*?*?*?*?a}
281.endif
282
283
284# 6. Error handling
285
286#	[	Incomplete empty character list, never matches.
287WORDS=		a a[
288# expect+1: warning: Unfinished character list in pattern 'a[' of modifier ':M'
289.if ${WORDS:Ma[} != ""
290.  error
291.endif
292
293#	[^	Incomplete negated empty character list, matches any single
294#		character.
295WORDS=		a a[ aX
296# expect+1: warning: Unfinished character list in pattern 'a[^' of modifier ':M'
297.if ${WORDS:Ma[^} != "a[ aX"
298.  error
299.endif
300
301#	[-x1-3	Incomplete character list, matches those elements that can be
302#		parsed without lookahead.
303WORDS=		- + x xx 0 1 2 3 4 [x1-3
304# expect+1: warning: Unfinished character list in pattern '[-x1-3' of modifier ':M'
305.if ${WORDS:M[-x1-3} != "- x 1 2 3"
306.  error
307.endif
308
309#	*[-x1-3	Incomplete character list after a wildcard, matches those
310#		words that end with one of the characters from the list.
311WORDS=		- + x xx 0 1 2 3 4 00 01 10 11 000 001 010 011 100 101 110 111 [x1-3
312# expect+1: warning: Unfinished character list in pattern '*[-x1-3' of modifier ':M'
313.if ${WORDS:M*[-x1-3} != "- x xx 1 2 3 01 11 001 011 101 111 [x1-3"
314.  warning ${WORDS:M*[-x1-3}
315.endif
316
317#	[^-x1-3
318#		Incomplete negated character list, matches any character
319#		except those elements that can be parsed without lookahead.
320WORDS=		- + x xx 0 1 2 3 4 [x1-3
321# expect+1: warning: Unfinished character list in pattern '[^-x1-3' of modifier ':M'
322.if ${WORDS:M[^-x1-3} != "+ 0 4"
323.  error
324.endif
325
326#	[\	Incomplete character list containing a single '\'.
327#
328#		A word can only end with a backslash if the preceding
329#		character is a backslash as well; in all other cases the final
330#		backslash would escape the following space, making the space
331#		part of the word.  Only the very last word of a string can be
332#		'\', as there is no following space that could be escaped.
333WORDS=		\\ \a ${:Ux\\}
334PATTERN=	${:U?[\\}
335# expect+1: warning: Unfinished character list in pattern '?[\' of modifier ':M'
336.if ${WORDS:M${PATTERN}} != "\\\\ x\\"
337.  error
338.endif
339
340#	[x-	Incomplete character list containing an incomplete character
341#		range, matches only the 'x'.
342WORDS=		[x- x x- y
343# expect+1: warning: Unfinished character range in pattern '[x-' of modifier ':M'
344.if ${WORDS:M[x-} != "x"
345.  error
346.endif
347
348#	[^x-	Incomplete negated character list containing an incomplete
349#		character range; matches each word that does not have an 'x'
350#		at the position of the character list.
351#
352#		XXX: Even matches strings that are longer than a single
353#		character.
354WORDS=		[x- x x- y yyyyy
355# expect+1: warning: Unfinished character range in pattern '[^x-' of modifier ':M'
356.if ${WORDS:M[^x-} != "[x- y yyyyy"
357.  error
358.endif
359
360#	[:]	matches never since the ':' starts the next modifier
361# expect+3: warning: Unfinished character list in pattern '[' of modifier ':M'
362# expect+2: Unknown modifier "]"
363# expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":")
364.if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":"
365.  error
366.else
367.  error
368.endif
369
370
371# 7. Historical bugs
372
373# Before var.c 1.1031 from 2022-08-24, the following expressions caused an
374# out-of-bounds read beyond the indirect ':M' modifiers.
375.if ${:U:${:UM\\}}		# The ':M' pattern need not be unescaped, the
376.  error			# resulting pattern is '\', it never matches
377.endif				# anything.
378.if ${:U:${:UM\\\:\\}}		# The ':M' pattern must be unescaped, the
379.  error			# resulting pattern is ':\', it never matches
380.endif				# anything.
381