1*954401e6SSimon J. Gerraty# $NetBSD: varmod-match.mk,v 1.11 2022/06/11 09:15:49 rillig Exp $ 22c3632d1SSimon J. Gerraty# 32c3632d1SSimon J. Gerraty# Tests for the :M variable modifier, which filters words that match the 42c3632d1SSimon J. Gerraty# given pattern. 5956e45f6SSimon J. Gerraty# 6956e45f6SSimon J. Gerraty# See ApplyModifier_Match and ModifyWord_Match for the implementation. 72c3632d1SSimon J. Gerraty 8956e45f6SSimon J. Gerraty.MAKEFLAGS: -dc 92c3632d1SSimon J. Gerraty 102c3632d1SSimon J. GerratyNUMBERS= One Two Three Four five six seven 112c3632d1SSimon J. Gerraty 12956e45f6SSimon J. Gerraty# Only keep words that start with an uppercase letter. 13956e45f6SSimon J. Gerraty.if ${NUMBERS:M[A-Z]*} != "One Two Three Four" 14956e45f6SSimon J. Gerraty. error 15956e45f6SSimon J. Gerraty.endif 162c3632d1SSimon J. Gerraty 17956e45f6SSimon J. Gerraty# Only keep words that start with a character other than an uppercase letter. 18956e45f6SSimon J. Gerraty.if ${NUMBERS:M[^A-Z]*} != "five six seven" 19956e45f6SSimon J. Gerraty. error 20956e45f6SSimon J. Gerraty.endif 21956e45f6SSimon J. Gerraty 22956e45f6SSimon J. Gerraty# Only keep words that don't start with s and at the same time end with 23956e45f6SSimon J. Gerraty# either of [ex]. 24956e45f6SSimon J. Gerraty# 25956e45f6SSimon J. Gerraty# This test case ensures that the negation from the first character class 26956e45f6SSimon J. Gerraty# does not propagate to the second character class. 27956e45f6SSimon J. Gerraty.if ${NUMBERS:M[^s]*[ex]} != "One Three five" 28956e45f6SSimon J. Gerraty. error 29956e45f6SSimon J. Gerraty.endif 302c3632d1SSimon J. Gerraty 31*954401e6SSimon J. Gerraty# Before 2020-06-13, this expression called Str_Match 601,080,390 times. 32*954401e6SSimon J. Gerraty# Since 2020-06-13, this expression calls Str_Match 1 time. 33956e45f6SSimon J. Gerraty.if ${:U****************:M****************b} 34956e45f6SSimon J. Gerraty.endif 35956e45f6SSimon J. Gerraty 36*954401e6SSimon J. Gerraty# As of 2022-06-11, this expression calls Str_Match 5,242,223 times. 37*954401e6SSimon J. Gerraty# Adding another '*?' to the pattern calls Str_Match 41,261,143 times. 38*954401e6SSimon J. Gerraty.if ${:U..................................................b:M*?*?*?*?*?a} 39*954401e6SSimon J. Gerraty.endif 40*954401e6SSimon J. Gerraty 41956e45f6SSimon J. Gerraty# To match a dollar sign in a word, double it. 42956e45f6SSimon J. Gerraty# 43956e45f6SSimon J. Gerraty# This is different from the :S and :C variable modifiers, where a '$' 44956e45f6SSimon J. Gerraty# has to be escaped as '\$'. 45956e45f6SSimon J. Gerraty.if ${:Ua \$ sign:M*$$*} != "\$" 46956e45f6SSimon J. Gerraty. error 47956e45f6SSimon J. Gerraty.endif 48956e45f6SSimon J. Gerraty 49956e45f6SSimon J. Gerraty# In the :M modifier, '\$' does not escape a dollar. Instead it is 50956e45f6SSimon J. Gerraty# interpreted as a backslash followed by whatever expression the 51956e45f6SSimon J. Gerraty# '$' starts. 52956e45f6SSimon J. Gerraty# 53956e45f6SSimon J. Gerraty# This differs from the :S, :C and several other variable modifiers. 54956e45f6SSimon J. Gerraty${:U*}= asterisk 55956e45f6SSimon J. Gerraty.if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk" 56956e45f6SSimon J. Gerraty. error 57956e45f6SSimon J. Gerraty.endif 58956e45f6SSimon J. Gerraty 59e2eeea75SSimon J. Gerraty# TODO: ${VAR:M(((}}}} 60e2eeea75SSimon J. Gerraty# TODO: ${VAR:M{{{)))} 61e2eeea75SSimon J. Gerraty# TODO: ${VAR:M${UNBALANCED}} 62e2eeea75SSimon J. Gerraty# TODO: ${VAR:M${:U(((\}\}\}}} 63e2eeea75SSimon J. Gerraty 641d3f2ddcSSimon J. Gerraty.MAKEFLAGS: -d0 651d3f2ddcSSimon J. Gerraty 661d3f2ddcSSimon J. Gerraty# Special characters: 671d3f2ddcSSimon J. Gerraty# * matches 0 or more arbitrary characters 681d3f2ddcSSimon J. Gerraty# ? matches a single arbitrary character 691d3f2ddcSSimon J. Gerraty# \ starts an escape sequence, only outside ranges 701d3f2ddcSSimon J. Gerraty# [ starts a set for matching a single character 711d3f2ddcSSimon J. Gerraty# ] ends a set for matching a single character 721d3f2ddcSSimon J. Gerraty# - in a set, forms a range of characters 731d3f2ddcSSimon J. Gerraty# ^ as the first character in a set, negates the set 741d3f2ddcSSimon J. Gerraty# ( during parsing of the pattern, starts a nesting level 751d3f2ddcSSimon J. Gerraty# ) during parsing of the pattern, ends a nesting level 761d3f2ddcSSimon J. Gerraty# { during parsing of the pattern, starts a nesting level 771d3f2ddcSSimon J. Gerraty# } during parsing of the pattern, ends a nesting level 781d3f2ddcSSimon J. Gerraty# : during parsing of the pattern, finishes the pattern 791d3f2ddcSSimon J. Gerraty# $ during parsing of the pattern, starts a nested expression 801d3f2ddcSSimon J. Gerraty# # in a line except a shell command, starts a comment 811d3f2ddcSSimon J. Gerraty# 821d3f2ddcSSimon J. Gerraty# Pattern parts: 831d3f2ddcSSimon J. Gerraty# * matches 0 or more arbitrary characters 841d3f2ddcSSimon J. Gerraty# ? matches exactly 1 arbitrary character 851d3f2ddcSSimon J. Gerraty# \x matches exactly the character 'x' 861d3f2ddcSSimon J. Gerraty# [...] matches exactly 1 character from the set 871d3f2ddcSSimon J. Gerraty# [^...] matches exactly 1 character outside the set 881d3f2ddcSSimon J. Gerraty# [a-z] matches exactly 1 character from the range 'a' to 'z' 891d3f2ddcSSimon J. Gerraty# 901d3f2ddcSSimon J. Gerraty 911d3f2ddcSSimon J. Gerraty# [] matches never 921d3f2ddcSSimon J. Gerraty.if ${ ab a[]b a[b a b :L:M[]} != "" 931d3f2ddcSSimon J. Gerraty. error 941d3f2ddcSSimon J. Gerraty.endif 951d3f2ddcSSimon J. Gerraty 961d3f2ddcSSimon J. Gerraty# a[]b matches never 971d3f2ddcSSimon J. Gerraty.if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != "" 981d3f2ddcSSimon J. Gerraty. error 991d3f2ddcSSimon J. Gerraty.endif 1001d3f2ddcSSimon J. Gerraty 1011d3f2ddcSSimon J. Gerraty# [^] matches exactly 1 arbitrary character 1021d3f2ddcSSimon J. Gerraty.if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]" 1031d3f2ddcSSimon J. Gerraty. error 1041d3f2ddcSSimon J. Gerraty.endif 1051d3f2ddcSSimon J. Gerraty 1061d3f2ddcSSimon J. Gerraty# a[^]b matches 'a', then exactly 1 arbitrary character, then 'b' 1071d3f2ddcSSimon J. Gerraty.if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b" 1081d3f2ddcSSimon J. Gerraty. error 1091d3f2ddcSSimon J. Gerraty.endif 1101d3f2ddcSSimon J. Gerraty 1111d3f2ddcSSimon J. Gerraty# [Nn0] matches exactly 1 character from the set 'N', 'n', '0' 1121d3f2ddcSSimon J. Gerraty.if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0" 1131d3f2ddcSSimon J. Gerraty. error 1141d3f2ddcSSimon J. Gerraty.endif 1151d3f2ddcSSimon J. Gerraty 1161d3f2ddcSSimon J. Gerraty# [a-c] matches exactly 1 character from the range 'a' to 'c' 1171d3f2ddcSSimon J. Gerraty.if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c" 1181d3f2ddcSSimon J. Gerraty. error 1191d3f2ddcSSimon J. Gerraty.endif 1201d3f2ddcSSimon J. Gerraty 1211d3f2ddcSSimon J. Gerraty# [c-a] matches the same as [a-c] 1221d3f2ddcSSimon J. Gerraty.if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c" 1231d3f2ddcSSimon J. Gerraty. error 1241d3f2ddcSSimon J. Gerraty.endif 1251d3f2ddcSSimon J. Gerraty 1261d3f2ddcSSimon J. Gerraty# [^a-c67] 1271d3f2ddcSSimon J. Gerraty# matches a single character, except for 'a', 'b', 'c', '6' or 1281d3f2ddcSSimon J. Gerraty# '7' 1291d3f2ddcSSimon J. Gerraty.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" 1301d3f2ddcSSimon J. Gerraty. error 1311d3f2ddcSSimon J. Gerraty.endif 1321d3f2ddcSSimon J. Gerraty 133*954401e6SSimon J. Gerraty# [\] matches a single backslash 134*954401e6SSimon J. GerratyWORDS= a\b a[\]b ab 135*954401e6SSimon J. Gerraty.if ${WORDS:Ma[\]b} != "a\\b" 136*954401e6SSimon J. Gerraty. error 137*954401e6SSimon J. Gerraty.endif 138*954401e6SSimon J. Gerraty 1391d3f2ddcSSimon J. Gerraty# : terminates the pattern 1401d3f2ddcSSimon J. Gerraty.if ${ A * :L:M:} != "" 1411d3f2ddcSSimon J. Gerraty. error 1421d3f2ddcSSimon J. Gerraty.endif 1431d3f2ddcSSimon J. Gerraty 1441d3f2ddcSSimon J. Gerraty# \: matches a colon 1451d3f2ddcSSimon J. Gerraty.if ${ ${:U\: \:\:} :L:M\:} != ":" 1461d3f2ddcSSimon J. Gerraty. error 1471d3f2ddcSSimon J. Gerraty.endif 1481d3f2ddcSSimon J. Gerraty 1491d3f2ddcSSimon J. Gerraty# ${:U\:} matches a colon 1501d3f2ddcSSimon J. Gerraty.if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":" 1511d3f2ddcSSimon J. Gerraty. error 1521d3f2ddcSSimon J. Gerraty.endif 1531d3f2ddcSSimon J. Gerraty 1541d3f2ddcSSimon J. Gerraty# [:] matches never since the ':' starts the next modifier 1551d3f2ddcSSimon J. Gerraty# expect+2: Unknown modifier "]" 1561d3f2ddcSSimon J. Gerraty# expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":") 1571d3f2ddcSSimon J. Gerraty.if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":" 1581d3f2ddcSSimon J. Gerraty. error 1591d3f2ddcSSimon J. Gerraty.else 1601d3f2ddcSSimon J. Gerraty. error 1611d3f2ddcSSimon J. Gerraty.endif 1621d3f2ddcSSimon J. Gerraty 1631d3f2ddcSSimon J. Gerraty# [\] matches exactly a backslash; no escaping takes place in 1641d3f2ddcSSimon J. Gerraty# character ranges 165*954401e6SSimon J. Gerraty# Without the 'a' in the below words, the backslash would end a word and thus 166*954401e6SSimon J. Gerraty# influence how the string is split into words. 167*954401e6SSimon J. GerratyWORDS= 1\a 2\\a 168*954401e6SSimon J. Gerraty.if ${WORDS:M?[\]a} != "1\\a" 1691d3f2ddcSSimon J. Gerraty. error 1701d3f2ddcSSimon J. Gerraty.endif 1711d3f2ddcSSimon J. Gerraty 172*954401e6SSimon J. Gerraty# [[-]] May look like it would match a single '[', '\' or ']', but 173*954401e6SSimon J. Gerraty# the inner ']' has two roles: it is the upper bound of the 174*954401e6SSimon J. Gerraty# character range as well as the closing character of the 175*954401e6SSimon J. Gerraty# character list. The outer ']' is just a regular character. 176*954401e6SSimon J. GerratyWORDS= [ ] [] \] ]] 177*954401e6SSimon J. Gerraty.if ${WORDS:M[[-]]} != "[] \\] ]]" 178*954401e6SSimon J. Gerraty. error 179*954401e6SSimon J. Gerraty.endif 180*954401e6SSimon J. Gerraty 181*954401e6SSimon J. Gerraty# [b[-]a] 182*954401e6SSimon J. Gerraty# Same as for '[[-]]': the character list stops at the first 183*954401e6SSimon J. Gerraty# ']', and the 'a]' is treated as a literal string. 184*954401e6SSimon J. GerratyWORDS= [a \a ]a []a \]a ]]a [a] \a] ]a] ba] 185*954401e6SSimon J. Gerraty.if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]" 186*954401e6SSimon J. Gerraty. error 187*954401e6SSimon J. Gerraty.endif 188*954401e6SSimon J. Gerraty 189*954401e6SSimon J. Gerraty# [-] Matches a single '-' since the '-' only becomes part of a 190*954401e6SSimon J. Gerraty# character range if it is preceded and followed by another 191*954401e6SSimon J. Gerraty# character. 192*954401e6SSimon J. GerratyWORDS= - -] 193*954401e6SSimon J. Gerraty.if ${WORDS:M[-]} != "-" 194*954401e6SSimon J. Gerraty. error 195*954401e6SSimon J. Gerraty.endif 196*954401e6SSimon J. Gerraty 197*954401e6SSimon J. Gerraty# [ Incomplete empty character list, never matches. 198*954401e6SSimon J. GerratyWORDS= a a[ 199*954401e6SSimon J. Gerraty.if ${WORDS:Ma[} != "" 200*954401e6SSimon J. Gerraty. error 201*954401e6SSimon J. Gerraty.endif 202*954401e6SSimon J. Gerraty 203*954401e6SSimon J. Gerraty# [^ Incomplete negated empty character list, matches any single 204*954401e6SSimon J. Gerraty# character. 205*954401e6SSimon J. GerratyWORDS= a a[ aX 206*954401e6SSimon J. Gerraty.if ${WORDS:Ma[^} != "a[ aX" 207*954401e6SSimon J. Gerraty. error 208*954401e6SSimon J. Gerraty.endif 209*954401e6SSimon J. Gerraty 210*954401e6SSimon J. Gerraty# [-x1-3 Incomplete character list, matches those elements that can be 211*954401e6SSimon J. Gerraty# parsed without lookahead. 212*954401e6SSimon J. GerratyWORDS= - + x xx 0 1 2 3 4 [x1-3 213*954401e6SSimon J. Gerraty.if ${WORDS:M[-x1-3} != "- x 1 2 3" 214*954401e6SSimon J. Gerraty. error 215*954401e6SSimon J. Gerraty.endif 216*954401e6SSimon J. Gerraty 217*954401e6SSimon J. Gerraty# [^-x1-3 218*954401e6SSimon J. Gerraty# Incomplete negated character list, matches any character 219*954401e6SSimon J. Gerraty# except those elements that can be parsed without lookahead. 220*954401e6SSimon J. GerratyWORDS= - + x xx 0 1 2 3 4 [x1-3 221*954401e6SSimon J. Gerraty.if ${WORDS:M[^-x1-3} != "+ 0 4" 222*954401e6SSimon J. Gerraty. error 223*954401e6SSimon J. Gerraty.endif 224*954401e6SSimon J. Gerraty 225*954401e6SSimon J. Gerraty# [\ Incomplete character list containing a single '\'. 2261d3f2ddcSSimon J. Gerraty# 227*954401e6SSimon J. Gerraty# A word can only end with a backslash if the preceding 228*954401e6SSimon J. Gerraty# character is a backslash as well; in all other cases the final 229*954401e6SSimon J. Gerraty# backslash would escape the following space, making the space 230*954401e6SSimon J. Gerraty# part of the word. Only the very last word of a string can be 231*954401e6SSimon J. Gerraty# '\', as there is no following space that could be escaped. 232*954401e6SSimon J. GerratyWORDS= \\ \a ${:Ux\\} 233*954401e6SSimon J. Gerraty.if ${WORDS:M?[\]} != "\\\\ x\\" 234*954401e6SSimon J. Gerraty. error 235*954401e6SSimon J. Gerraty.endif 236*954401e6SSimon J. Gerraty 237*954401e6SSimon J. Gerraty# [x- Incomplete character list containing an incomplete character 238*954401e6SSimon J. Gerraty# range, matches only the 'x'. 239*954401e6SSimon J. GerratyWORDS= [x- x x- y 240*954401e6SSimon J. Gerraty.if ${WORDS:M[x-} != "x" 241*954401e6SSimon J. Gerraty. error 242*954401e6SSimon J. Gerraty.endif 243*954401e6SSimon J. Gerraty 244*954401e6SSimon J. Gerraty# [^x- Incomplete negated character list containing an incomplete 245*954401e6SSimon J. Gerraty# character range; matches each word that does not have an 'x' 246*954401e6SSimon J. Gerraty# at the position of the character list. 2471d3f2ddcSSimon J. Gerraty# 248*954401e6SSimon J. Gerraty# XXX: Even matches strings that are longer than a single 249*954401e6SSimon J. Gerraty# character. 250*954401e6SSimon J. GerratyWORDS= [x- x x- y yyyyy 251*954401e6SSimon J. Gerraty.if ${WORDS:M[^x-} != "[x- y yyyyy" 252*954401e6SSimon J. Gerraty. error 253*954401e6SSimon J. Gerraty.endif 2541d3f2ddcSSimon J. Gerraty 2551d3f2ddcSSimon J. Gerraty 2561d3f2ddcSSimon J. Gerraty# The modifier ':tW' prevents splitting at whitespace. Even leading and 2571d3f2ddcSSimon J. Gerraty# trailing whitespace is preserved. 2581d3f2ddcSSimon J. Gerraty.if ${ plain string :L:tW:M*} != " plain string " 2591d3f2ddcSSimon J. Gerraty. error 2601d3f2ddcSSimon J. Gerraty.endif 2611d3f2ddcSSimon J. Gerraty 2621d3f2ddcSSimon J. Gerraty# Without the modifier ':tW', the string is split into words. All whitespace 2631d3f2ddcSSimon J. Gerraty# around and between the words is normalized to a single space. 2641d3f2ddcSSimon J. Gerraty.if ${ plain string :L:M*} != "plain string" 2651d3f2ddcSSimon J. Gerraty. error 2661d3f2ddcSSimon J. Gerraty.endif 267*954401e6SSimon J. Gerraty 268*954401e6SSimon J. Gerraty 269*954401e6SSimon J. Gerraty# The pattern can come from a variable expression. For single-letter 270*954401e6SSimon J. Gerraty# variables, either the short form or the long form can be used, just as 271*954401e6SSimon J. Gerraty# everywhere else. 272*954401e6SSimon J. GerratyPRIMES= 2 3 5 7 11 273*954401e6SSimon J. Gerratyn= 2 274*954401e6SSimon J. Gerraty.if ${PRIMES:M$n} != "2" 275*954401e6SSimon J. Gerraty. error 276*954401e6SSimon J. Gerraty.endif 277*954401e6SSimon J. Gerraty.if ${PRIMES:M${n}} != "2" 278*954401e6SSimon J. Gerraty. error 279*954401e6SSimon J. Gerraty.endif 280*954401e6SSimon J. Gerraty.if ${PRIMES:M${:U2}} != "2" 281*954401e6SSimon J. Gerraty. error 282*954401e6SSimon J. Gerraty.endif 283