varmod-subst-regex.mk revision 1.3 1 1.3 rillig # $NetBSD: varmod-subst-regex.mk,v 1.3 2020/08/28 17:15:04 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :C,from,to, variable modifier.
4 1.1 rillig
5 1.3 rillig all: mod-regex-compile-error
6 1.2 rillig all: mod-regex-limits
7 1.2 rillig all: mod-regex-errors
8 1.1 rillig
9 1.3 rillig # The variable expression expands to 4 words. Of these words, none matches
10 1.3 rillig # the regular expression "a b" since these words don't contain any
11 1.3 rillig # whitespace.
12 1.3 rillig .if ${:Ua b b c:C,a b,,} != "a b b c"
13 1.3 rillig .error
14 1.3 rillig .endif
15 1.2 rillig
16 1.3 rillig # Using the '1' modifier does not change anything. The '1' modifier just
17 1.3 rillig # means to apply at most 1 replacement in the whole variable expression.
18 1.3 rillig .if ${:Ua b b c:C,a b,,1} != "a b b c"
19 1.3 rillig .error
20 1.3 rillig .endif
21 1.3 rillig
22 1.3 rillig # The 'W' modifier treats the whole variable value as a single big word,
23 1.3 rillig # containing whitespace. This big word matches the regular expression,
24 1.3 rillig # therefore it gets replaced. Whitespace is preserved after replacing.
25 1.3 rillig .if ${:Ua b b c:C,a b,,W} != " b c"
26 1.3 rillig .error
27 1.3 rillig .endif
28 1.3 rillig
29 1.3 rillig # The 'g' modifier does not have any effect here since each of the words
30 1.3 rillig # contains the character 'b' a single time.
31 1.3 rillig .if ${:Ua b b c:C,b,,g} != "a c"
32 1.3 rillig .error
33 1.3 rillig .endif
34 1.3 rillig
35 1.3 rillig # The first :C modifier has the 'W' modifier, which makes the whole
36 1.3 rillig # expression a single word. The 'g' modifier then replaces all occurrences
37 1.3 rillig # of "1 2" with "___". The 'W' modifier only applies to this single :C
38 1.3 rillig # modifier. This is demonstrated by the :C modifier that follows. If the
39 1.3 rillig # 'W' modifier would be preserved, only a single underscore would have been
40 1.3 rillig # replaced with an 'x'.
41 1.3 rillig .if ${:U1 2 3 1 2 3:C,1 2,___,Wg:C,_,x,} != "x__ 3 x__ 3"
42 1.3 rillig .error
43 1.3 rillig .endif
44 1.3 rillig
45 1.3 rillig # The regular expression does not match in the first word.
46 1.3 rillig # It matches once in the second word, and the \0\0 doubles that word.
47 1.3 rillig # In the third word, the regular expression matches as early as possible,
48 1.3 rillig # and since the matches must not overlap, the next possible match would
49 1.3 rillig # start at the 6, but at that point, there is only one character left,
50 1.3 rillig # and that cannot match the regular expression "..". Therefore only the
51 1.3 rillig # "45" is doubled in the result.
52 1.3 rillig .if ${:U1 23 456:C,..,\0\0,} != "1 2323 45456"
53 1.3 rillig .error
54 1.3 rillig .endif
55 1.3 rillig
56 1.3 rillig # The modifier '1' applies the replacement at most once, across the whole
57 1.3 rillig # variable value, no matter whether it is a single big word or many small
58 1.3 rillig # words.
59 1.3 rillig #
60 1.3 rillig # Up to 2020-08-28, the manual page said that the modifiers '1' and 'g'
61 1.3 rillig # were orthogonal, which was wrong.
62 1.3 rillig .if ${:U12345 12345:C,.,\0\0,1} != "112345 12345"
63 1.3 rillig .error
64 1.3 rillig .endif
65 1.3 rillig
66 1.3 rillig # Multiple asterisks form an invalid regular expression. This produces an
67 1.3 rillig # error message and (as of 2020-08-28) stops parsing in the middle of the
68 1.3 rillig # variable expression. The unparsed part of the expression is then copied
69 1.3 rillig # verbatim to the output, which is unexpected and can lead to strange shell
70 1.3 rillig # commands being run.
71 1.3 rillig mod-regex-compile-error:
72 1.3 rillig @echo $@: ${:Uword1 word2:C,****,____,g:C,word,____,:Q}.
73 1.3 rillig
74 1.3 rillig # These tests generate error messages but as of 2020-08-28 just continue
75 1.3 rillig # parsing and execution as if nothing bad had happened.
76 1.2 rillig mod-regex-limits:
77 1.2 rillig @echo $@:11-missing:${:U1 23 456:C,..,\1\1,:Q}
78 1.2 rillig @echo $@:11-ok:${:U1 23 456:C,(.).,\1\1,:Q}
79 1.2 rillig @echo $@:22-missing:${:U1 23 456:C,..,\2\2,:Q}
80 1.2 rillig @echo $@:22-missing:${:U1 23 456:C,(.).,\2\2,:Q}
81 1.2 rillig @echo $@:22-ok:${:U1 23 456:C,(.)(.),\2\2,:Q}
82 1.2 rillig # The :C modifier only handles single-digit capturing groups,
83 1.2 rillig # which is more than enough for daily use.
84 1.2 rillig @echo $@:capture:${:UabcdefghijABCDEFGHIJrest:C,(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.),\9\8\7\6\5\4\3\2\1\0\10\11\12,}
85 1.2 rillig
86 1.2 rillig mod-regex-errors:
87 1.2 rillig @echo $@: ${UNDEF:Uvalue:C,[,,}
88