1# $NetBSD: varmod-match.mk,v 1.22 2024/04/23 22:51:28 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 so s:L:Ms??*} != "six seven" 37. error 38.endif 39 40# 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# A pattern that ends with '*' is 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# A pattern that starts with '*' is 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. Whitespace 261# around the words is discarded, and whitespace between the words is 262# normalized to a single space. 263.if ${ plain string :L:M*} != "plain string" 264. error 265.endif 266 267 268# 5. Performance 269 270# Before 2020-06-13, this expression called Str_Match 601,080,390 times. 271# Since 2020-06-13, this expression calls Str_Match 1 time. 272.if ${:U****************:M****************b} 273.endif 274 275# Before 2023-06-22, this expression called Str_Match 2,621,112 times. 276# Adding another '*?' to the pattern called Str_Match 20,630,572 times. 277# Adding another '*?' to the pattern called Str_Match 136,405,672 times. 278# Adding another '*?' to the pattern called Str_Match 773,168,722 times. 279# Adding another '*?' to the pattern called Str_Match 3,815,481,072 times. 280# Since 2023-06-22, Str_Match no longer backtracks. 281.if ${:U..................................................b:M*?*?*?*?*?a} 282.endif 283 284 285# 6. Error handling 286 287# [ Incomplete empty character list, never matches. 288WORDS= a a[ 289# expect+1: while evaluating variable "WORDS": warning: Unfinished character list in pattern 'a[' of modifier ':M' 290.if ${WORDS:Ma[} != "" 291. error 292.endif 293 294# [^ Incomplete negated empty character list, matches any single 295# character. 296WORDS= a a[ aX 297# expect+1: while evaluating variable "WORDS": warning: Unfinished character list in pattern 'a[^' of modifier ':M' 298.if ${WORDS:Ma[^} != "a[ aX" 299. error 300.endif 301 302# [-x1-3 Incomplete character list, matches those elements that can be 303# parsed without lookahead. 304WORDS= - + x xx 0 1 2 3 4 [x1-3 305# expect+1: while evaluating variable "WORDS": warning: Unfinished character list in pattern '[-x1-3' of modifier ':M' 306.if ${WORDS:M[-x1-3} != "- x 1 2 3" 307. error 308.endif 309 310# *[-x1-3 Incomplete character list after a wildcard, matches those 311# words that end with one of the characters from the list. 312WORDS= - + x xx 0 1 2 3 4 00 01 10 11 000 001 010 011 100 101 110 111 [x1-3 313# expect+1: while evaluating variable "WORDS": warning: Unfinished character list in pattern '*[-x1-3' of modifier ':M' 314.if ${WORDS:M*[-x1-3} != "- x xx 1 2 3 01 11 001 011 101 111 [x1-3" 315. warning ${WORDS:M*[-x1-3} 316.endif 317 318# [^-x1-3 319# Incomplete negated character list, matches any character 320# except those elements that can be parsed without lookahead. 321WORDS= - + x xx 0 1 2 3 4 [x1-3 322# expect+1: while evaluating variable "WORDS": warning: Unfinished character list in pattern '[^-x1-3' of modifier ':M' 323.if ${WORDS:M[^-x1-3} != "+ 0 4" 324. error 325.endif 326 327# [\ Incomplete character list containing a single '\'. 328# 329# A word can only end with a backslash if the preceding 330# character is a backslash as well; in all other cases the final 331# backslash would escape the following space, making the space 332# part of the word. Only the very last word of a string can be 333# '\', as there is no following space that could be escaped. 334WORDS= \\ \a ${:Ux\\} 335PATTERN= ${:U?[\\} 336# expect+1: while evaluating variable "WORDS": warning: Unfinished character list in pattern '?[\' of modifier ':M' 337.if ${WORDS:M${PATTERN}} != "\\\\ x\\" 338. error 339.endif 340 341# [x- Incomplete character list containing an incomplete character 342# range, matches only the 'x'. 343WORDS= [x- x x- y 344# expect+1: while evaluating variable "WORDS": warning: Unfinished character range in pattern '[x-' of modifier ':M' 345.if ${WORDS:M[x-} != "x" 346. error 347.endif 348 349# [^x- Incomplete negated character list containing an incomplete 350# character range; matches each word that does not have an 'x' 351# at the position of the character list. 352# 353# XXX: Even matches strings that are longer than a single 354# character. 355WORDS= [x- x x- y yyyyy 356# expect+1: while evaluating variable "WORDS": warning: Unfinished character range in pattern '[^x-' of modifier ':M' 357.if ${WORDS:M[^x-} != "[x- y yyyyy" 358. error 359.endif 360 361# [:] matches never since the ':' starts the next modifier 362# expect+3: while evaluating variable " : :: ": warning: Unfinished character list in pattern '[' of modifier ':M' 363# expect+2: while evaluating variable " : :: ": Unknown modifier "]" 364# expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":") 365.if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":" 366. error 367.else 368. error 369.endif 370 371 372# 7. Historical bugs 373 374# Before var.c 1.1031 from 2022-08-24, the following expressions caused an 375# out-of-bounds read beyond the indirect ':M' modifiers. 376# 377# The argument to the inner ':U' is unescaped to 'M\'. 378# This 'M\' becomes an # indirect modifier ':M' with the pattern '\'. 379# The pattern '\' never matches. 380.if ${:U:${:UM\\}} 381. error 382.endif 383# The argument to the inner ':U' is unescaped to 'M\:\'. 384# This 'M\:\' becomes an indirect modifier ':M' with the pattern ':\'. 385# The pattern ':\' never matches. 386.if ${:U:${:UM\\\:\\}} 387. error 388.endif 389