1*22619282SSimon J. Gerraty# $NetBSD: varmod-match.mk,v 1.26 2024/07/09 17:07:23 rillig Exp $ 22c3632d1SSimon J. Gerraty# 3d5e0a182SSimon J. Gerraty# Tests for the ':M' modifier, which keeps only those words that match the 42c3632d1SSimon J. Gerraty# given pattern. 5956e45f6SSimon J. Gerraty# 6d5e0a182SSimon J. Gerraty# Table of contents 7d5e0a182SSimon J. Gerraty# 8d5e0a182SSimon J. Gerraty# 1. Pattern characters '*', '?' and '\' 9d5e0a182SSimon J. Gerraty# 2. Character lists and character ranges 10d5e0a182SSimon J. Gerraty# 3. Parsing and escaping 11d5e0a182SSimon J. Gerraty# 4. Interaction with other modifiers 12d5e0a182SSimon J. Gerraty# 5. Performance 13d5e0a182SSimon J. Gerraty# 6. Error handling 14d5e0a182SSimon J. Gerraty# 7. Historical bugs 15d5e0a182SSimon J. Gerraty# 16d5e0a182SSimon J. Gerraty# See ApplyModifier_Match, ParseModifier_Match, ModifyWord_Match and 17d5e0a182SSimon J. Gerraty# Str_Match. 182c3632d1SSimon J. Gerraty 192c3632d1SSimon J. Gerraty 20d5e0a182SSimon J. Gerraty# 1. Pattern characters '*', '?' and '\' 21d5e0a182SSimon J. Gerraty# 22d5e0a182SSimon J. Gerraty# * matches 0 or more characters 23d5e0a182SSimon J. Gerraty# ? matches 1 character 24d5e0a182SSimon J. Gerraty# \x matches the character 'x' 25d5e0a182SSimon J. Gerraty 26d5e0a182SSimon J. Gerraty# The pattern is anchored both at the beginning and at the end of the word. 27d5e0a182SSimon J. Gerraty# Since the pattern 'e' does not contain any pattern matching characters, it 28d5e0a182SSimon J. Gerraty# matches exactly the word 'e', twice. 29d5e0a182SSimon J. Gerraty.if ${a c e aa cc ee e f g:L:Me} != "e e" 30d5e0a182SSimon J. Gerraty. error 31d5e0a182SSimon J. Gerraty.endif 32d5e0a182SSimon J. Gerraty 33d5e0a182SSimon J. Gerraty# The pattern character '?' matches exactly 1 character, the pattern character 34d5e0a182SSimon J. Gerraty# '*' matches 0 or more characters. The whole pattern matches all words that 35d5e0a182SSimon J. Gerraty# start with 's' and have 3 or more characters. 36548bfc56SSimon J. Gerraty.if ${One Two Three Four five six seven so s:L:Ms??*} != "six seven" 37d5e0a182SSimon J. Gerraty. error 38d5e0a182SSimon J. Gerraty.endif 39d5e0a182SSimon J. Gerraty 40548bfc56SSimon J. Gerraty# A pattern without placeholders only matches itself. 41d5e0a182SSimon J. Gerraty.if ${a aa aaa b ba baa bab:L:Ma} != "a" 42d5e0a182SSimon J. Gerraty. error 43d5e0a182SSimon J. Gerraty.endif 44d5e0a182SSimon J. Gerraty 458d5c8e21SSimon J. Gerraty# A pattern that does not start with '*' is anchored at the beginning. 46d5e0a182SSimon J. Gerraty.if ${a aa aaa b ba baa bab:L:Ma*} != "a aa aaa" 47d5e0a182SSimon J. Gerraty. error 48d5e0a182SSimon J. Gerraty.endif 49d5e0a182SSimon J. Gerraty 508d5c8e21SSimon J. Gerraty# A pattern that does not end with '*' is anchored at the end. 51d5e0a182SSimon J. Gerraty.if ${a aa aaa b ba baa bab:L:M*a} != "a aa aaa ba baa" 52d5e0a182SSimon J. Gerraty. error 53d5e0a182SSimon J. Gerraty.endif 54d5e0a182SSimon J. Gerraty 55d5e0a182SSimon J. Gerraty# Test the fast code path for '*' followed by a regular character. 56d5e0a182SSimon J. Gerraty.if ${:U file.c file.*c file.h file\.c :M*.c} != "file.c file\\.c" 57d5e0a182SSimon J. Gerraty. error 58d5e0a182SSimon J. Gerraty.endif 59d5e0a182SSimon J. Gerraty# Ensure that the fast code path correctly handles the backslash. 60d5e0a182SSimon J. Gerraty.if ${:U file.c file.*c file.h file\.c :M*\.c} != "file.c file\\.c" 61d5e0a182SSimon J. Gerraty. error 62d5e0a182SSimon J. Gerraty.endif 63d5e0a182SSimon J. Gerraty# Ensure that the fast code path correctly handles '\*'. 64d5e0a182SSimon J. Gerraty.if ${:U file.c file.*c file.h file\.c :M*\*c} != "file.*c" 65d5e0a182SSimon J. Gerraty. error 66d5e0a182SSimon J. Gerraty.endif 67d5e0a182SSimon J. Gerraty# Ensure that the partial match '.c' doesn't confuse the fast code path. 68d5e0a182SSimon J. Gerraty.if ${:U file.c.cc file.cc.cc file.cc.c :M*.cc} != "file.c.cc file.cc.cc" 69d5e0a182SSimon J. Gerraty. error 70d5e0a182SSimon J. Gerraty.endif 71d5e0a182SSimon J. Gerraty# Ensure that the substring '.cc' doesn't confuse the fast code path for '.c'. 72d5e0a182SSimon J. Gerraty.if ${:U file.c.cc file.cc.cc file.cc.c :M*.c} != "file.cc.c" 73d5e0a182SSimon J. Gerraty. error 74d5e0a182SSimon J. Gerraty.endif 75d5e0a182SSimon J. Gerraty 76d5e0a182SSimon J. Gerraty 77d5e0a182SSimon J. Gerraty# 2. Character lists and character ranges 78d5e0a182SSimon J. Gerraty# 79d5e0a182SSimon J. Gerraty# [...] matches 1 character from the listed characters 80d5e0a182SSimon J. Gerraty# [^...] matches 1 character from the unlisted characters 81d5e0a182SSimon J. Gerraty# [a-z] matches 1 character from the range 'a' to 'z' 82d5e0a182SSimon J. Gerraty# [z-a] matches 1 character from the range 'a' to 'z' 832c3632d1SSimon J. Gerraty 84956e45f6SSimon J. Gerraty# Only keep words that start with an uppercase letter. 85d5e0a182SSimon J. Gerraty.if ${One Two Three Four five six seven:L:M[A-Z]*} != "One Two Three Four" 86956e45f6SSimon J. Gerraty. error 87956e45f6SSimon J. Gerraty.endif 882c3632d1SSimon J. Gerraty 89956e45f6SSimon J. Gerraty# Only keep words that start with a character other than an uppercase letter. 90d5e0a182SSimon J. Gerraty.if ${One Two Three Four five six seven:L:M[^A-Z]*} != "five six seven" 91956e45f6SSimon J. Gerraty. error 92956e45f6SSimon J. Gerraty.endif 93956e45f6SSimon J. Gerraty 941d3f2ddcSSimon J. Gerraty# [] matches never 951d3f2ddcSSimon J. Gerraty.if ${ ab a[]b a[b a b :L:M[]} != "" 961d3f2ddcSSimon J. Gerraty. error 971d3f2ddcSSimon J. Gerraty.endif 981d3f2ddcSSimon J. Gerraty 991d3f2ddcSSimon J. Gerraty# a[]b matches never 1001d3f2ddcSSimon J. Gerraty.if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != "" 1011d3f2ddcSSimon J. Gerraty. error 1021d3f2ddcSSimon J. Gerraty.endif 1031d3f2ddcSSimon J. Gerraty 1041d3f2ddcSSimon J. Gerraty# [^] matches exactly 1 arbitrary character 1051d3f2ddcSSimon J. Gerraty.if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]" 1061d3f2ddcSSimon J. Gerraty. error 1071d3f2ddcSSimon J. Gerraty.endif 1081d3f2ddcSSimon J. Gerraty 1091d3f2ddcSSimon J. Gerraty# a[^]b matches 'a', then exactly 1 arbitrary character, then 'b' 1101d3f2ddcSSimon J. Gerraty.if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b" 1111d3f2ddcSSimon J. Gerraty. error 1121d3f2ddcSSimon J. Gerraty.endif 1131d3f2ddcSSimon J. Gerraty 1141d3f2ddcSSimon J. Gerraty# [Nn0] matches exactly 1 character from the set 'N', 'n', '0' 1151d3f2ddcSSimon J. Gerraty.if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0" 1161d3f2ddcSSimon J. Gerraty. error 1171d3f2ddcSSimon J. Gerraty.endif 1181d3f2ddcSSimon J. Gerraty 1191d3f2ddcSSimon J. Gerraty# [a-c] matches exactly 1 character from the range 'a' to 'c' 1201d3f2ddcSSimon J. Gerraty.if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c" 1211d3f2ddcSSimon J. Gerraty. error 1221d3f2ddcSSimon J. Gerraty.endif 1231d3f2ddcSSimon J. Gerraty 1241d3f2ddcSSimon J. Gerraty# [c-a] matches the same as [a-c] 1251d3f2ddcSSimon J. Gerraty.if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c" 1261d3f2ddcSSimon J. Gerraty. error 1271d3f2ddcSSimon J. Gerraty.endif 1281d3f2ddcSSimon J. Gerraty 1291d3f2ddcSSimon J. Gerraty# [^a-c67] 1301d3f2ddcSSimon J. Gerraty# matches a single character, except for 'a', 'b', 'c', '6' or 1311d3f2ddcSSimon J. Gerraty# '7' 1321d3f2ddcSSimon 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" 1331d3f2ddcSSimon J. Gerraty. error 1341d3f2ddcSSimon J. Gerraty.endif 1351d3f2ddcSSimon J. Gerraty 136d5e0a182SSimon J. Gerraty# [\] matches a single backslash; no escaping takes place in 1371d3f2ddcSSimon J. Gerraty# character ranges 138d5e0a182SSimon J. Gerraty# Without the 'b' in the below words, the backslash would end a word and thus 139954401e6SSimon J. Gerraty# influence how the string is split into words. 140d5e0a182SSimon J. GerratyWORDS= a\b a[\]b ab a\\b 141d5e0a182SSimon J. Gerraty.if ${WORDS:Ma[\]b} != "a\\b" 1421d3f2ddcSSimon J. Gerraty. error 1431d3f2ddcSSimon J. Gerraty.endif 1441d3f2ddcSSimon J. Gerraty 145954401e6SSimon J. Gerraty# [[-]] May look like it would match a single '[', '\' or ']', but 146954401e6SSimon J. Gerraty# the inner ']' has two roles: it is the upper bound of the 147954401e6SSimon J. Gerraty# character range as well as the closing character of the 148954401e6SSimon J. Gerraty# character list. The outer ']' is just a regular character. 149954401e6SSimon J. GerratyWORDS= [ ] [] \] ]] 150954401e6SSimon J. Gerraty.if ${WORDS:M[[-]]} != "[] \\] ]]" 151954401e6SSimon J. Gerraty. error 152954401e6SSimon J. Gerraty.endif 153954401e6SSimon J. Gerraty 154954401e6SSimon J. Gerraty# [b[-]a] 155954401e6SSimon J. Gerraty# Same as for '[[-]]': the character list stops at the first 156954401e6SSimon J. Gerraty# ']', and the 'a]' is treated as a literal string. 157954401e6SSimon J. GerratyWORDS= [a \a ]a []a \]a ]]a [a] \a] ]a] ba] 158954401e6SSimon J. Gerraty.if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]" 159954401e6SSimon J. Gerraty. error 160954401e6SSimon J. Gerraty.endif 161954401e6SSimon J. Gerraty 162954401e6SSimon J. Gerraty# [-] Matches a single '-' since the '-' only becomes part of a 163954401e6SSimon J. Gerraty# character range if it is preceded and followed by another 164954401e6SSimon J. Gerraty# character. 165954401e6SSimon J. GerratyWORDS= - -] 166954401e6SSimon J. Gerraty.if ${WORDS:M[-]} != "-" 167954401e6SSimon J. Gerraty. error 168954401e6SSimon J. Gerraty.endif 169954401e6SSimon J. Gerraty 170d5e0a182SSimon J. Gerraty# Only keep words that don't start with s and at the same time end with 171d5e0a182SSimon J. Gerraty# either of [ex]. 172d5e0a182SSimon J. Gerraty# 173d5e0a182SSimon J. Gerraty# This test case ensures that the negation from the first character list 174d5e0a182SSimon J. Gerraty# '[^s]' does not propagate to the second character list '[ex]'. 175d5e0a182SSimon J. Gerraty.if ${One Two Three Four five six seven:L:M[^s]*[ex]} != "One Three five" 176d5e0a182SSimon J. Gerraty. error 177d5e0a182SSimon J. Gerraty.endif 178d5e0a182SSimon J. Gerraty 179d5e0a182SSimon J. Gerraty 180d5e0a182SSimon J. Gerraty# 3. Parsing and escaping 181d5e0a182SSimon J. Gerraty# 182d5e0a182SSimon J. Gerraty# * matches 0 or more characters 183d5e0a182SSimon J. Gerraty# ? matches 1 character 184d5e0a182SSimon J. Gerraty# \ outside a character list, escapes the following character 185d5e0a182SSimon J. Gerraty# [ starts a character list for matching 1 character 186d5e0a182SSimon J. Gerraty# ] ends a character list for matching 1 character 187d5e0a182SSimon J. Gerraty# - in a character list, forms a character range 188d5e0a182SSimon J. Gerraty# ^ at the beginning of a character list, negates the list 189d5e0a182SSimon J. Gerraty# ( while parsing the pattern, starts a nesting level 190d5e0a182SSimon J. Gerraty# ) while parsing the pattern, ends a nesting level 191d5e0a182SSimon J. Gerraty# { while parsing the pattern, starts a nesting level 192d5e0a182SSimon J. Gerraty# } while parsing the pattern, ends a nesting level 193d5e0a182SSimon J. Gerraty# : while parsing the pattern, terminates the pattern 194d5e0a182SSimon J. Gerraty# $ while parsing the pattern, starts a nested expression 195d5e0a182SSimon J. Gerraty# # in a line except a shell command, starts a comment 196d5e0a182SSimon J. Gerraty 197d5e0a182SSimon J. Gerraty# The pattern can come from an expression. For single-letter 198d5e0a182SSimon J. Gerraty# variables, either the short form or the long form can be used, just as 199d5e0a182SSimon J. Gerraty# everywhere else. 200d5e0a182SSimon J. GerratyPRIMES= 2 3 5 7 11 201d5e0a182SSimon J. Gerratyn= 2 202d5e0a182SSimon J. Gerraty.if ${PRIMES:M$n} != "2" 203d5e0a182SSimon J. Gerraty. error 204d5e0a182SSimon J. Gerraty.endif 205d5e0a182SSimon J. Gerraty.if ${PRIMES:M${n}} != "2" 206d5e0a182SSimon J. Gerraty. error 207d5e0a182SSimon J. Gerraty.endif 208d5e0a182SSimon J. Gerraty.if ${PRIMES:M${:U2}} != "2" 209d5e0a182SSimon J. Gerraty. error 210d5e0a182SSimon J. Gerraty.endif 211d5e0a182SSimon J. Gerraty 212d5e0a182SSimon J. Gerraty# : terminates the pattern 213d5e0a182SSimon J. Gerraty.if ${ A * :L:M:} != "" 214d5e0a182SSimon J. Gerraty. error 215d5e0a182SSimon J. Gerraty.endif 216d5e0a182SSimon J. Gerraty 217d5e0a182SSimon J. Gerraty# \: matches a colon 218d5e0a182SSimon J. Gerraty.if ${ ${:U\: \:\:} :L:M\:} != ":" 219d5e0a182SSimon J. Gerraty. error 220d5e0a182SSimon J. Gerraty.endif 221d5e0a182SSimon J. Gerraty 222d5e0a182SSimon J. Gerraty# ${:U\:} matches a colon 223d5e0a182SSimon J. Gerraty.if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":" 224d5e0a182SSimon J. Gerraty. error 225d5e0a182SSimon J. Gerraty.endif 226d5e0a182SSimon J. Gerraty 227d5e0a182SSimon J. Gerraty# To match a dollar sign in a word, double it. 228d5e0a182SSimon J. Gerraty# 229d5e0a182SSimon J. Gerraty# This is different from the :S and :C modifiers, where a '$' has to be 230d5e0a182SSimon J. Gerraty# escaped as '\$'. 231d5e0a182SSimon J. Gerraty.if ${:Ua \$ sign:M*$$*} != "\$" 232d5e0a182SSimon J. Gerraty. error 233d5e0a182SSimon J. Gerraty.endif 234d5e0a182SSimon J. Gerraty 235d5e0a182SSimon J. Gerraty# In the :M modifier, '\$' does not escape a dollar. Instead it is 236d5e0a182SSimon J. Gerraty# interpreted as a backslash followed by whatever expression the 237d5e0a182SSimon J. Gerraty# '$' starts. 238d5e0a182SSimon J. Gerraty# 239d5e0a182SSimon J. Gerraty# This differs from the :S, :C and several other modifiers. 240d5e0a182SSimon J. Gerraty${:U*}= asterisk 241d5e0a182SSimon J. Gerraty.if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk" 242d5e0a182SSimon J. Gerraty. error 243d5e0a182SSimon J. Gerraty.endif 244d5e0a182SSimon J. Gerraty 245d5e0a182SSimon J. Gerraty# TODO: ${VAR:M(((}}}} 246d5e0a182SSimon J. Gerraty# TODO: ${VAR:M{{{)))} 247d5e0a182SSimon J. Gerraty# TODO: ${VAR:M${UNBALANCED}} 248d5e0a182SSimon J. Gerraty# TODO: ${VAR:M${:U(((\}\}\}}} 249d5e0a182SSimon J. Gerraty 250d5e0a182SSimon J. Gerraty 251d5e0a182SSimon J. Gerraty# 4. Interaction with other modifiers 252d5e0a182SSimon J. Gerraty 253d5e0a182SSimon J. Gerraty# The modifier ':tW' prevents splitting at whitespace. Even leading and 254d5e0a182SSimon J. Gerraty# trailing whitespace is preserved. 255d5e0a182SSimon J. Gerraty.if ${ plain string :L:tW:M*} != " plain string " 256d5e0a182SSimon J. Gerraty. error 257d5e0a182SSimon J. Gerraty.endif 258d5e0a182SSimon J. Gerraty 259548bfc56SSimon J. Gerraty# Without the modifier ':tW', the string is split into words. Whitespace 260548bfc56SSimon J. Gerraty# around the words is discarded, and whitespace between the words is 261548bfc56SSimon J. Gerraty# normalized to a single space. 262d5e0a182SSimon J. Gerraty.if ${ plain string :L:M*} != "plain string" 263d5e0a182SSimon J. Gerraty. error 264d5e0a182SSimon J. Gerraty.endif 265d5e0a182SSimon J. Gerraty 266d5e0a182SSimon J. Gerraty 267d5e0a182SSimon J. Gerraty# 5. Performance 268d5e0a182SSimon J. Gerraty 269d5e0a182SSimon J. Gerraty# Before 2020-06-13, this expression called Str_Match 601,080,390 times. 270d5e0a182SSimon J. Gerraty# Since 2020-06-13, this expression calls Str_Match 1 time. 271d5e0a182SSimon J. Gerraty.if ${:U****************:M****************b} 272d5e0a182SSimon J. Gerraty.endif 273d5e0a182SSimon J. Gerraty 274d5e0a182SSimon J. Gerraty# Before 2023-06-22, this expression called Str_Match 2,621,112 times. 275d5e0a182SSimon J. Gerraty# Adding another '*?' to the pattern called Str_Match 20,630,572 times. 276d5e0a182SSimon J. Gerraty# Adding another '*?' to the pattern called Str_Match 136,405,672 times. 277d5e0a182SSimon J. Gerraty# Adding another '*?' to the pattern called Str_Match 773,168,722 times. 278d5e0a182SSimon J. Gerraty# Adding another '*?' to the pattern called Str_Match 3,815,481,072 times. 279d5e0a182SSimon J. Gerraty# Since 2023-06-22, Str_Match no longer backtracks. 280d5e0a182SSimon J. Gerraty.if ${:U..................................................b:M*?*?*?*?*?a} 281d5e0a182SSimon J. Gerraty.endif 282d5e0a182SSimon J. Gerraty 283d5e0a182SSimon J. Gerraty 284d5e0a182SSimon J. Gerraty# 6. Error handling 285d5e0a182SSimon J. Gerraty 286954401e6SSimon J. Gerraty# [ Incomplete empty character list, never matches. 287954401e6SSimon J. GerratyWORDS= a a[ 288*22619282SSimon J. Gerraty# expect+1: while evaluating variable "WORDS" with value "a a[": Unfinished character list in pattern 'a[' of modifier ':M' 289954401e6SSimon J. Gerraty.if ${WORDS:Ma[} != "" 290954401e6SSimon J. Gerraty. error 291954401e6SSimon J. Gerraty.endif 292954401e6SSimon J. Gerraty 293954401e6SSimon J. Gerraty# [^ Incomplete negated empty character list, matches any single 294954401e6SSimon J. Gerraty# character. 295954401e6SSimon J. GerratyWORDS= a a[ aX 296*22619282SSimon J. Gerraty# expect+1: while evaluating variable "WORDS" with value "a a[ aX": Unfinished character list in pattern 'a[^' of modifier ':M' 297954401e6SSimon J. Gerraty.if ${WORDS:Ma[^} != "a[ aX" 298954401e6SSimon J. Gerraty. error 299954401e6SSimon J. Gerraty.endif 300954401e6SSimon J. Gerraty 301954401e6SSimon J. Gerraty# [-x1-3 Incomplete character list, matches those elements that can be 302954401e6SSimon J. Gerraty# parsed without lookahead. 303954401e6SSimon J. GerratyWORDS= - + x xx 0 1 2 3 4 [x1-3 304*22619282SSimon J. Gerraty# 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' 305954401e6SSimon J. Gerraty.if ${WORDS:M[-x1-3} != "- x 1 2 3" 306954401e6SSimon J. Gerraty. error 307954401e6SSimon J. Gerraty.endif 308954401e6SSimon J. Gerraty 309148ee845SSimon J. Gerraty# *[-x1-3 Incomplete character list after a wildcard, matches those 310148ee845SSimon J. Gerraty# words that end with one of the characters from the list. 311148ee845SSimon J. GerratyWORDS= - + x xx 0 1 2 3 4 00 01 10 11 000 001 010 011 100 101 110 111 [x1-3 312*22619282SSimon J. Gerraty# 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' 313148ee845SSimon J. Gerraty.if ${WORDS:M*[-x1-3} != "- x xx 1 2 3 01 11 001 011 101 111 [x1-3" 314148ee845SSimon J. Gerraty. warning ${WORDS:M*[-x1-3} 315148ee845SSimon J. Gerraty.endif 316148ee845SSimon J. Gerraty 317954401e6SSimon J. Gerraty# [^-x1-3 318954401e6SSimon J. Gerraty# Incomplete negated character list, matches any character 319954401e6SSimon J. Gerraty# except those elements that can be parsed without lookahead. 320954401e6SSimon J. GerratyWORDS= - + x xx 0 1 2 3 4 [x1-3 321*22619282SSimon J. Gerraty# 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' 322954401e6SSimon J. Gerraty.if ${WORDS:M[^-x1-3} != "+ 0 4" 323954401e6SSimon J. Gerraty. error 324954401e6SSimon J. Gerraty.endif 325954401e6SSimon J. Gerraty 326954401e6SSimon J. Gerraty# [\ Incomplete character list containing a single '\'. 3271d3f2ddcSSimon J. Gerraty# 328954401e6SSimon J. Gerraty# A word can only end with a backslash if the preceding 329954401e6SSimon J. Gerraty# character is a backslash as well; in all other cases the final 330954401e6SSimon J. Gerraty# backslash would escape the following space, making the space 331954401e6SSimon J. Gerraty# part of the word. Only the very last word of a string can be 332954401e6SSimon J. Gerraty# '\', as there is no following space that could be escaped. 333954401e6SSimon J. GerratyWORDS= \\ \a ${:Ux\\} 334d5e0a182SSimon J. GerratyPATTERN= ${:U?[\\} 335*22619282SSimon J. Gerraty# expect+1: while evaluating variable "WORDS" with value "\\ \a x\": Unfinished character list in pattern '?[\' of modifier ':M' 336d5e0a182SSimon J. Gerraty.if ${WORDS:M${PATTERN}} != "\\\\ x\\" 337954401e6SSimon J. Gerraty. error 338954401e6SSimon J. Gerraty.endif 339954401e6SSimon J. Gerraty 340954401e6SSimon J. Gerraty# [x- Incomplete character list containing an incomplete character 341954401e6SSimon J. Gerraty# range, matches only the 'x'. 342954401e6SSimon J. GerratyWORDS= [x- x x- y 343*22619282SSimon J. Gerraty# expect+1: while evaluating variable "WORDS" with value "[x- x x- y": Unfinished character range in pattern '[x-' of modifier ':M' 344954401e6SSimon J. Gerraty.if ${WORDS:M[x-} != "x" 345954401e6SSimon J. Gerraty. error 346954401e6SSimon J. Gerraty.endif 347954401e6SSimon J. Gerraty 348954401e6SSimon J. Gerraty# [^x- Incomplete negated character list containing an incomplete 349954401e6SSimon J. Gerraty# character range; matches each word that does not have an 'x' 350954401e6SSimon J. Gerraty# at the position of the character list. 3511d3f2ddcSSimon J. Gerraty# 352954401e6SSimon J. Gerraty# XXX: Even matches strings that are longer than a single 353954401e6SSimon J. Gerraty# character. 354954401e6SSimon J. GerratyWORDS= [x- x x- y yyyyy 355*22619282SSimon J. Gerraty# expect+1: while evaluating variable "WORDS" with value "[x- x x- y yyyyy": Unfinished character range in pattern '[^x-' of modifier ':M' 356954401e6SSimon J. Gerraty.if ${WORDS:M[^x-} != "[x- y yyyyy" 357954401e6SSimon J. Gerraty. error 358954401e6SSimon J. Gerraty.endif 3591d3f2ddcSSimon J. Gerraty 360d5e0a182SSimon J. Gerraty# [:] matches never since the ':' starts the next modifier 361*22619282SSimon J. Gerraty# expect+3: while evaluating variable " : :: " with value " : :: ": Unfinished character list in pattern '[' of modifier ':M' 362*22619282SSimon J. Gerraty# expect+2: while evaluating variable " : :: " with value "": Unknown modifier "]" 363d5e0a182SSimon J. Gerraty# expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":") 364d5e0a182SSimon J. Gerraty.if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":" 3651d3f2ddcSSimon J. Gerraty. error 366d5e0a182SSimon J. Gerraty.else 3671d3f2ddcSSimon J. Gerraty. error 3681d3f2ddcSSimon J. Gerraty.endif 369954401e6SSimon J. Gerraty 370954401e6SSimon J. Gerraty 371d5e0a182SSimon J. Gerraty# 7. Historical bugs 3724fde40d9SSimon J. Gerraty 3734fde40d9SSimon J. Gerraty# Before var.c 1.1031 from 2022-08-24, the following expressions caused an 3744fde40d9SSimon J. Gerraty# out-of-bounds read beyond the indirect ':M' modifiers. 375548bfc56SSimon J. Gerraty# 376548bfc56SSimon J. Gerraty# The argument to the inner ':U' is unescaped to 'M\'. 3778d5c8e21SSimon J. Gerraty# This 'M\' becomes an indirect modifier ':M' with the pattern '\'. 378548bfc56SSimon J. Gerraty# The pattern '\' never matches. 379548bfc56SSimon J. Gerraty.if ${:U:${:UM\\}} 380548bfc56SSimon J. Gerraty. error 381548bfc56SSimon J. Gerraty.endif 382548bfc56SSimon J. Gerraty# The argument to the inner ':U' is unescaped to 'M\:\'. 383548bfc56SSimon J. Gerraty# This 'M\:\' becomes an indirect modifier ':M' with the pattern ':\'. 384548bfc56SSimon J. Gerraty# The pattern ':\' never matches. 385548bfc56SSimon J. Gerraty.if ${:U:${:UM\\\:\\}} 386548bfc56SSimon J. Gerraty. error 387548bfc56SSimon J. Gerraty.endif 388