varmod-subst.mk revision 1.2 1 1.2 rillig # $NetBSD: varmod-subst.mk,v 1.2 2020/08/16 12:30:45 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :S,from,to, variable modifier.
4 1.1 rillig
5 1.2 rillig all: mod-subst
6 1.2 rillig all: mod-subst-delimiter
7 1.2 rillig all: mod-subst-chain
8 1.2 rillig all: mod-subst-dollar
9 1.1 rillig
10 1.2 rillig WORDS= sequences of letters
11 1.2 rillig .if ${WORDS:S,,,} != ${WORDS}
12 1.2 rillig .warning The empty pattern matches something.
13 1.2 rillig .endif
14 1.2 rillig .if ${WORDS:S,e,*,1} != "s*quences of letters"
15 1.2 rillig .warning The :S modifier flag '1' is not applied exactly once.
16 1.2 rillig .endif
17 1.2 rillig .if ${WORDS:S,e,*,} != "s*quences of l*tters"
18 1.2 rillig .warning The :S modifier does not replace every first match per word.
19 1.2 rillig .endif
20 1.2 rillig .if ${WORDS:S,e,*,g} != "s*qu*nc*s of l*tt*rs"
21 1.2 rillig .warning The :S modifier flag 'g' does not replace every occurrence.
22 1.2 rillig .endif
23 1.2 rillig .if ${WORDS:S,^sequ,occurr,} != "occurrences of letters"
24 1.2 rillig .warning The :S modifier fails for a short match anchored at the start.
25 1.2 rillig .endif
26 1.2 rillig .if ${WORDS:S,^of,with,} != "sequences with letters"
27 1.2 rillig .warning The :S modifier fails for an exact match anchored at the start.
28 1.2 rillig .endif
29 1.2 rillig .if ${WORDS:S,^office,does not match,} != ${WORDS}
30 1.2 rillig .warning The :S modifier matches a too long pattern anchored at the start.
31 1.2 rillig .endif
32 1.2 rillig .if ${WORDS:S,f$,r,} != "sequences or letters"
33 1.2 rillig .warning The :S modifier fails for a short match anchored at the end.
34 1.2 rillig .endif
35 1.2 rillig .if ${WORDS:S,s$,,} != "sequence of letter"
36 1.2 rillig .warning The :S modifier fails to replace one occurrence per word.
37 1.2 rillig .endif
38 1.2 rillig .if ${WORDS:S,of$,,} != "sequences letters"
39 1.2 rillig .warning The :S modifier fails for an exact match anchored at the end.
40 1.2 rillig .endif
41 1.2 rillig .if ${WORDS:S,eof$,,} != ${WORDS}
42 1.2 rillig .warning The :S modifier matches a too long pattern anchored at the end.
43 1.2 rillig .endif
44 1.2 rillig .if ${WORDS:S,^of$,,} != "sequences letters"
45 1.2 rillig .warning The :S modifier does not match a word anchored at both ends.
46 1.2 rillig .endif
47 1.2 rillig .if ${WORDS:S,^o$,,} != ${WORDS}
48 1.2 rillig .warning The :S modifier matches a prefix anchored at both ends.
49 1.2 rillig .endif
50 1.2 rillig .if ${WORDS:S,^f$,,} != ${WORDS}
51 1.2 rillig .warning The :S modifier matches a suffix anchored at both ends.
52 1.2 rillig .endif
53 1.2 rillig .if ${WORDS:S,^eof$,,} != ${WORDS}
54 1.2 rillig .warning The :S modifier matches a too long prefix anchored at both ends.
55 1.2 rillig .endif
56 1.2 rillig .if ${WORDS:S,^office$,,} != ${WORDS}
57 1.2 rillig .warning The :S modifier matches a too long suffix anchored at both ends.
58 1.2 rillig .endif
59 1.2 rillig
60 1.2 rillig mod-subst:
61 1.2 rillig @echo $@:
62 1.2 rillig @echo :${:Ua b b c:S,a b,,:Q}:
63 1.2 rillig @echo :${:Ua b b c:S,a b,,1:Q}:
64 1.2 rillig @echo :${:Ua b b c:S,a b,,W:Q}:
65 1.2 rillig @echo :${:Ua b b c:S,b,,g:Q}:
66 1.2 rillig @echo :${:U1 2 3 1 2 3:S,1 2,___,Wg:S,_,x,:Q}:
67 1.2 rillig @echo ${:U12345:S,,sep,g:Q}
68 1.2 rillig
69 1.2 rillig # The :S and :C modifiers accept an arbitrary character as the delimiter,
70 1.2 rillig # including characters that are otherwise used as escape characters or
71 1.2 rillig # interpreted in a special way. This can be used to confuse humans.
72 1.2 rillig mod-subst-delimiter:
73 1.2 rillig @echo $@:
74 1.2 rillig @echo ${:U1 2 3:S 2 two :Q} horizontal tabulator
75 1.2 rillig @echo ${:U1 2 3:S 2 two :Q} space
76 1.2 rillig @echo ${:U1 2 3:S!2!two!:Q} exclamation mark
77 1.2 rillig @echo ${:U1 2 3:S"2"two":Q} double quotes
78 1.2 rillig # In shell command lines, the hash does not need to be escaped.
79 1.2 rillig # It needs to be escaped in variable assignment lines though.
80 1.2 rillig @echo ${:U1 2 3:S#2#two#:Q} hash
81 1.2 rillig @echo ${:U1 2 3:S$2$two$:Q} dollar
82 1.2 rillig @echo ${:U1 2 3:S%2%two%:Q} percent
83 1.2 rillig @echo ${:U1 2 3:S'2'two':Q} apostrophe
84 1.2 rillig @echo ${:U1 2 3:S(2(two(:Q} opening parenthesis
85 1.2 rillig @echo ${:U1 2 3:S)2)two):Q} closing parenthesis
86 1.2 rillig @echo ${:U1 2 3:S121two1:Q} digit
87 1.2 rillig @echo ${:U1 2 3:S:2:two::Q} colon
88 1.2 rillig @echo ${:U1 2 3:S<2<two<:Q} less than sign
89 1.2 rillig @echo ${:U1 2 3:S=2=two=:Q} equal sign
90 1.2 rillig @echo ${:U1 2 3:S>2>two>:Q} greater than sign
91 1.2 rillig @echo ${:U1 2 3:S?2?two?:Q} question mark
92 1.2 rillig @echo ${:U1 2 3:S@2@two@:Q} at
93 1.2 rillig @echo ${:U1 2 3:Sa2atwoa:Q} letter
94 1.2 rillig @echo ${:U1 2 3:S[2[two[:Q} opening bracket
95 1.2 rillig @echo ${:U1 2 3:S\2\two\:Q} backslash
96 1.2 rillig @echo ${:U1 2 3:S]2]two]:Q} closing bracket
97 1.2 rillig @echo ${:U1 2 3:S^2^two^:Q} caret
98 1.2 rillig @echo ${:U1 2 3:S{2{two{:Q} opening brace
99 1.2 rillig @echo ${:U1 2 3:S|2|two|:Q} vertical line
100 1.2 rillig @echo ${:U1 2 3:S}2}two}:Q} closing brace
101 1.2 rillig @echo ${:U1 2 3:S~2~two~:Q} tilde
102 1.2 rillig
103 1.2 rillig # The :S and :C modifiers can be chained without a separating ':'.
104 1.2 rillig # This is not documented in the manual page.
105 1.2 rillig # It works because ApplyModifier_Subst scans for the known modifiers g1W
106 1.2 rillig # and then just returns to ApplyModifiers. There, the colon is optionally
107 1.2 rillig # skipped (see the *st.next == ':' at the end of the loop).
108 1.2 rillig #
109 1.2 rillig # Most other modifiers cannot be chained since their parsers skip until
110 1.2 rillig # the next ':' or '}' or ')'.
111 1.2 rillig mod-subst-chain:
112 1.2 rillig @echo $@:
113 1.2 rillig @echo ${:Ua b c:S,a,A,S,b,B,}.
114 1.2 rillig # There is no 'i' modifier for the :S or :C modifiers.
115 1.2 rillig # The error message is "make: Unknown modifier 'i'", which is
116 1.2 rillig # kind of correct, although it is mixing the terms for variable
117 1.2 rillig # modifiers with the matching modifiers.
118 1.2 rillig @echo ${:Uvalue:S,a,x,i}.
119 1.2 rillig
120 1.2 rillig # No matter how many dollar characters there are, they all get merged
121 1.2 rillig # into a single dollar by the :S modifier.
122 1.2 rillig #
123 1.2 rillig # As of 2020-08-09, this is because ParseModifierPart sees a '$' and
124 1.2 rillig # calls Var_Parse to expand the variable. In all other places, the "$$"
125 1.2 rillig # is handled outside of Var_Parse. Var_Parse therefore considers "$$"
126 1.2 rillig # one of the "really stupid names", skips the first dollar, and parsing
127 1.2 rillig # continues with the next character. This repeats for the other dollar
128 1.2 rillig # signs, except the one before the delimiter. That one is handled by
129 1.2 rillig # the code that optionally interprets the '$' as the end-anchor in the
130 1.2 rillig # first part of the :S modifier. That code doesn't call Var_Parse but
131 1.2 rillig # simply copies the dollar to the result.
132 1.2 rillig mod-subst-dollar:
133 1.2 rillig @echo $@:${:U1:S,^,$,:Q}:
134 1.2 rillig @echo $@:${:U2:S,^,$$,:Q}:
135 1.2 rillig @echo $@:${:U3:S,^,$$$,:Q}:
136 1.2 rillig @echo $@:${:U4:S,^,$$$$,:Q}:
137 1.2 rillig @echo $@:${:U5:S,^,$$$$$,:Q}:
138 1.2 rillig @echo $@:${:U6:S,^,$$$$$$,:Q}:
139 1.2 rillig @echo $@:${:U7:S,^,$$$$$$$,:Q}:
140 1.2 rillig @echo $@:${:U8:S,^,$$$$$$$$,:Q}:
141 1.2 rillig @echo $@:${:U40:S,^,$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$,:Q}:
142 1.2 rillig # This generates no dollar at all:
143 1.2 rillig @echo $@:${:UU8:S,^,${:U$$$$$$$$},:Q}:
144 1.2 rillig # Here is an alternative way to generate dollar characters.
145 1.2 rillig # It's unexpectedly complicated though.
146 1.2 rillig @echo $@:${:U:range=5:ts\x24:C,[0-9],,g:Q}:
147 1.2 rillig # In modifiers, dollars are escaped using the backslash, not using another
148 1.2 rillig # dollar sign. Therefore, creating a dollar sign is pretty simple:
149 1.2 rillig @echo $@:${:Ugood3:S,^,\$\$\$,:Q}
150