1# $NetBSD: varmod-match.mk,v 1.26 2024/07/09 17:07:23 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 does not start with '*' is anchored at the beginning. 46.if ${a aa aaa b ba baa bab:L:Ma*} != "a aa aaa" 47. error 48.endif 49 50# A pattern that does not end with '*' is anchored at the end. 51.if ${a aa aaa b ba baa bab:L:M*a} != "a aa aaa ba baa" 52. error 53.endif 54 55# Test the fast code path for '*' followed by a regular character. 56.if ${:U file.c file.*c file.h file\.c :M*.c} != "file.c file\\.c" 57. error 58.endif 59# Ensure that the fast code path correctly handles the backslash. 60.if ${:U file.c file.*c file.h file\.c :M*\.c} != "file.c file\\.c" 61. error 62.endif 63# Ensure that the fast code path correctly handles '\*'. 64.if ${:U file.c file.*c file.h file\.c :M*\*c} != "file.*c" 65. error 66.endif 67# Ensure that the partial match '.c' doesn't confuse the fast code path. 68.if ${:U file.c.cc file.cc.cc file.cc.c :M*.cc} != "file.c.cc file.cc.cc" 69. error 70.endif 71# Ensure that the substring '.cc' doesn't confuse the fast code path for '.c'. 72.if ${:U file.c.cc file.cc.cc file.cc.c :M*.c} != "file.cc.c" 73. error 74.endif 75 76 77# 2. Character lists and character ranges 78# 79# [...] matches 1 character from the listed characters 80# [^...] matches 1 character from the unlisted characters 81# [a-z] matches 1 character from the range 'a' to 'z' 82# [z-a] matches 1 character from the range 'a' to 'z' 83 84# Only keep words that start with an uppercase letter. 85.if ${One Two Three Four five six seven:L:M[A-Z]*} != "One Two Three Four" 86. error 87.endif 88 89# Only keep words that start with a character other than an uppercase letter. 90.if ${One Two Three Four five six seven:L:M[^A-Z]*} != "five six seven" 91. error 92.endif 93 94# [] matches never 95.if ${ ab a[]b a[b a b :L:M[]} != "" 96. error 97.endif 98 99# a[]b matches never 100.if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != "" 101. error 102.endif 103 104# [^] matches exactly 1 arbitrary character 105.if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]" 106. error 107.endif 108 109# a[^]b matches 'a', then exactly 1 arbitrary character, then 'b' 110.if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b" 111. error 112.endif 113 114# [Nn0] matches exactly 1 character from the set 'N', 'n', '0' 115.if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0" 116. error 117.endif 118 119# [a-c] matches exactly 1 character from the range 'a' to 'c' 120.if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c" 121. error 122.endif 123 124# [c-a] matches the same as [a-c] 125.if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c" 126. error 127.endif 128 129# [^a-c67] 130# matches a single character, except for 'a', 'b', 'c', '6' or 131# '7' 132.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" 133. error 134.endif 135 136# [\] matches a single backslash; no escaping takes place in 137# character ranges 138# Without the 'b' in the below words, the backslash would end a word and thus 139# influence how the string is split into words. 140WORDS= a\b a[\]b ab a\\b 141.if ${WORDS:Ma[\]b} != "a\\b" 142. error 143.endif 144 145# [[-]] May look like it would match a single '[', '\' or ']', but 146# the inner ']' has two roles: it is the upper bound of the 147# character range as well as the closing character of the 148# character list. The outer ']' is just a regular character. 149WORDS= [ ] [] \] ]] 150.if ${WORDS:M[[-]]} != "[] \\] ]]" 151. error 152.endif 153 154# [b[-]a] 155# Same as for '[[-]]': the character list stops at the first 156# ']', and the 'a]' is treated as a literal string. 157WORDS= [a \a ]a []a \]a ]]a [a] \a] ]a] ba] 158.if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]" 159. error 160.endif 161 162# [-] Matches a single '-' since the '-' only becomes part of a 163# character range if it is preceded and followed by another 164# character. 165WORDS= - -] 166.if ${WORDS:M[-]} != "-" 167. error 168.endif 169 170# Only keep words that don't start with s and at the same time end with 171# either of [ex]. 172# 173# This test case ensures that the negation from the first character list 174# '[^s]' does not propagate to the second character list '[ex]'. 175.if ${One Two Three Four five six seven:L:M[^s]*[ex]} != "One Three five" 176. error 177.endif 178 179 180# 3. Parsing and escaping 181# 182# * matches 0 or more characters 183# ? matches 1 character 184# \ outside a character list, escapes the following character 185# [ starts a character list for matching 1 character 186# ] ends a character list for matching 1 character 187# - in a character list, forms a character range 188# ^ at the beginning of a character list, negates the list 189# ( while parsing the pattern, starts a nesting level 190# ) while parsing the pattern, ends a nesting level 191# { while parsing the pattern, starts a nesting level 192# } while parsing the pattern, ends a nesting level 193# : while parsing the pattern, terminates the pattern 194# $ while parsing the pattern, starts a nested expression 195# # in a line except a shell command, starts a comment 196 197# The pattern can come from an expression. For single-letter 198# variables, either the short form or the long form can be used, just as 199# everywhere else. 200PRIMES= 2 3 5 7 11 201n= 2 202.if ${PRIMES:M$n} != "2" 203. error 204.endif 205.if ${PRIMES:M${n}} != "2" 206. error 207.endif 208.if ${PRIMES:M${:U2}} != "2" 209. error 210.endif 211 212# : terminates the pattern 213.if ${ A * :L:M:} != "" 214. error 215.endif 216 217# \: matches a colon 218.if ${ ${:U\: \:\:} :L:M\:} != ":" 219. error 220.endif 221 222# ${:U\:} matches a colon 223.if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":" 224. error 225.endif 226 227# To match a dollar sign in a word, double it. 228# 229# This is different from the :S and :C modifiers, where a '$' has to be 230# escaped as '\$'. 231.if ${:Ua \$ sign:M*$$*} != "\$" 232. error 233.endif 234 235# In the :M modifier, '\$' does not escape a dollar. Instead it is 236# interpreted as a backslash followed by whatever expression the 237# '$' starts. 238# 239# This differs from the :S, :C and several other modifiers. 240${:U*}= asterisk 241.if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk" 242. error 243.endif 244 245# TODO: ${VAR:M(((}}}} 246# TODO: ${VAR:M{{{)))} 247# TODO: ${VAR:M${UNBALANCED}} 248# TODO: ${VAR:M${:U(((\}\}\}}} 249 250 251# 4. Interaction with other modifiers 252 253# The modifier ':tW' prevents splitting at whitespace. Even leading and 254# trailing whitespace is preserved. 255.if ${ plain string :L:tW:M*} != " plain string " 256. error 257.endif 258 259# Without the modifier ':tW', the string is split into words. Whitespace 260# around the words is discarded, and whitespace between the words is 261# 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: while evaluating variable "WORDS" with value "a a[": 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: while evaluating variable "WORDS" with value "a a[ aX": 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: while evaluating variable "WORDS" with value "- + x xx 0 1 2 3 4 [x1-3": 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: while evaluating variable "WORDS" with value "- + x xx 0 1 2 3 4 00 01 10 11 000 001 010 011 100 101 110 111 [x1-3": 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: while evaluating variable "WORDS" with value "- + x xx 0 1 2 3 4 [x1-3": 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: while evaluating variable "WORDS" with value "\\ \a x\": 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: while evaluating variable "WORDS" with value "[x- x x- y": 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: while evaluating variable "WORDS" with value "[x- x x- y yyyyy": 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: while evaluating variable " : :: " with value " : :: ": Unfinished character list in pattern '[' of modifier ':M' 362# expect+2: while evaluating variable " : :: " with value "": 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# 376# The argument to the inner ':U' is unescaped to 'M\'. 377# This 'M\' becomes an indirect modifier ':M' with the pattern '\'. 378# The pattern '\' never matches. 379.if ${:U:${:UM\\}} 380. error 381.endif 382# The argument to the inner ':U' is unescaped to 'M\:\'. 383# This 'M\:\' becomes an indirect modifier ':M' with the pattern ':\'. 384# The pattern ':\' never matches. 385.if ${:U:${:UM\\\:\\}} 386. error 387.endif 388