xref: /titanic_50/usr/src/lib/libshell/common/tests/substring.sh (revision 8cdd6a74847b5ae6ed26727528bdf6b139cf7552)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2009 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	let Errors+=1
25}
26alias err_exit='err_exit $LINENO'
27
28Command=${0##*/}
29integer Errors=0 j=4
30base=/home/dgk/foo//bar
31string1=$base/abcabcabc
32if	[[ ${string1:0} != "$string1" ]]
33then	err_exit "string1:0"
34fi
35if	[[ ${string1: -1} != "c" ]]
36then	err_exit "string1: -1"
37fi
38if	[[ ${string1:0:1000} != "$string1" ]]
39then	err_exit "string1:0"
40fi
41if	[[ ${string1:1} != "${string1#?}" ]]
42then	err_exit "string1:1"
43fi
44if	[[ ${string1:1:4} != home ]]
45then	err_exit "string1:1:4"
46fi
47if	[[ ${string1: -5:4} != bcab ]]
48then	err_exit "string1: -5:4"
49fi
50if	[[ ${string1:1:j} != home ]]
51then	err_exit "string1:1:j"
52fi
53if	[[ ${string1:(j?1:0):j} != home ]]
54then	err_exit "string1:(j?1:0):j"
55fi
56if	[[ ${string1%*zzz*} != "$string1" ]]
57then	err_exit "string1%*zzz*"
58fi
59if	[[ ${string1%%*zzz*} != "$string1" ]]
60then	err_exit "string1%%*zzz*"
61fi
62if	[[ ${string1#*zzz*} != "$string1" ]]
63then	err_exit "string1#*zzz*"
64fi
65if	[[ ${string1##*zzz*} != "$string1" ]]
66then	err_exit "string1##*zzz*"
67fi
68if	[[ ${string1%+(abc)} != "$base/abcabc" ]]
69then	err_exit "string1%+(abc)"
70fi
71if	[[ ${string1%%+(abc)} != "$base/" ]]
72then	err_exit "string1%%+(abc)"
73fi
74if	[[ ${string1%/*} != "$base" ]]
75then	err_exit "string1%/*"
76fi
77if	[[ "${string1%/*}" != "$base" ]]
78then	err_exit '"string1%/*"'
79fi
80if	[[ ${string1%"/*"} != "$string1" ]]
81then	err_exit 'string1%"/*"'
82fi
83if	[[ ${string1%%/*} != "" ]]
84then	err_exit "string1%%/*"
85fi
86if	[[ ${string1#*/bar} != /abcabcabc ]]
87then	err_exit "string1#*bar"
88fi
89if	[[ ${string1##*/bar} != /abcabcabc ]]
90then	err_exit "string1#*bar"
91fi
92if	[[ "${string1#@(*/bar|*/foo)}" != //bar/abcabcabc ]]
93then	err_exit "string1#@(*/bar|*/foo)"
94fi
95if	[[ ${string1##@(*/bar|*/foo)} != /abcabcabc ]]
96then	err_exit "string1##@(*/bar|*/foo)"
97fi
98if	[[ ${string1##*/@(bar|foo)} != /abcabcabc ]]
99then	err_exit "string1##*/@(bar|foo)"
100fi
101foo=abc
102if	[[ ${foo#a[b*} != abc ]]
103then	err_exit "abc#a[b*} != abc"
104fi
105if	[[ ${foo//[0-9]/bar} != abc ]]
106then	err_exit '${foo//[0-9]/bar} not expanding correctly'
107fi
108foo='(abc)'
109if	[[ ${foo#'('} != 'abc)' ]]
110then	err_exit "(abc)#( != abc)"
111fi
112if	[[ ${foo%')'} != '(abc' ]]
113then	err_exit "(abc)%) != (abc"
114fi
115foo=a123b456c
116if	[[ ${foo/[0-9]?/""} != a3b456c ]]
117then	err_exit '${foo/[0-9]?/""} not expanding correctly'
118fi
119if	[[ ${foo//[0-9]/""} != abc ]]
120then	err_exit '${foo//[0-9]/""} not expanding correctly'
121fi
122if	[[ ${foo/#a/b} != b123b456c ]]
123then	err_exit '${foo/#a/b} not expanding correctly'
124fi
125if	[[ ${foo/#?/b} != b123b456c ]]
126then	err_exit '${foo/#?/b} not expanding correctly'
127fi
128if	[[ ${foo/%c/b} != a123b456b ]]
129then	err_exit '${foo/%c/b} not expanding correctly'
130fi
131if	[[ ${foo/%?/b} != a123b456b ]]
132then	err_exit '${foo/%?/b} not expanding correctly'
133fi
134while read -r pattern string expected
135do	if	(( expected ))
136	then	if	[[ $string != $pattern ]]
137		then	err_exit "$pattern does not match $string"
138		fi
139		if	[[ ${string##$pattern} != "" ]]
140		then	err_exit "\${$string##$pattern} not null"
141		fi
142		if	[ "${string##$pattern}" != '' ]
143		then	err_exit "\"\${$string##$pattern}\" not null"
144		fi
145		if	[[ ${string/$pattern} != "" ]]
146		then	err_exit "\${$string/$pattern} not null"
147		fi
148	else	if	[[ $string == $pattern ]]
149		then	err_exit "$pattern matches $string"
150		fi
151	fi
152done <<- \EOF
153	+(a)*+(a)	aabca	1
154	!(*.o)		foo.o	0
155	!(*.o)		foo.c	1
156EOF
157xx=a/b/c/d/e
158yy=${xx#*/}
159if	[[ $yy != b/c/d/e ]]
160then	err_exit '${xx#*/} != a/b/c/d/e when xx=a/b/c/d/e'
161fi
162if	[[ ${xx//\//\\} != 'a\b\c\d\e' ]]
163then	err_exit '${xx//\//\\} not working'
164fi
165x=[123]def
166if	[[ "${x//\[(*)\]/\{\1\}}" != {123}def ]]
167then	err_exit 'closing brace escape not working'
168fi
169xx=%28text%29
170if	[[ ${xx//%28/abc\)} != 'abc)text%29' ]]
171then	 err_exit '${xx//%28/abc\)} not working'
172fi
173xx='a:b'
174str='(){}[]*?|&^%$#@l'
175for ((i=0 ; i < ${#str}; i++))
176do      [[ $(eval print -r -- \"\${xx//:/\\${str:i:1}}\") == "a${str:i:1}b" ]] || err_exit "substitution of \\${str:i:1}} failed"
177        [[ $(eval print -rn -- \"\${xx//:/\'${str:i:1}\'}\") == "a${str:i:1}b" ]] || err_exit "substitution of '${str:i:1}' failed"
178        [[ $(eval print -r -- \"\${xx//:/\"${str:i:1}\"}\") == "a${str:i:1}b" ]] || err_exit "substitution of \"${str:i:1}\" failed"
179done
180[[ ${xx//:/\\n} == 'a\nb' ]]  || err_exit "substituion of \\\\n failed"
181[[ ${xx//:/'\n'} == 'a\nb' ]] || err_exit "substituion of '\\n' failed"
182[[ ${xx//:/"\n"} ==  'a\nb' ]] || err_exit "substituion of \"\\n\" failed"
183[[ ${xx//:/$'\n'} ==  $'a\nb' ]] || err_exit "substituion of \$'\\n' failed"
184unset foo
185foo=one/two/three
186if	[[ ${foo//'/'/_} != one_two_three ]]
187then	err_exit 'single quoting / in replacements failed'
188fi
189if	[[ ${foo//"/"/_} != one_two_three ]]
190then	err_exit 'double quoting / in replacements failed'
191fi
192if	[[ ${foo//\//_} != one_two_three ]]
193then	err_exit 'escaping / in replacements failed'
194fi
195function myexport
196{
197	nameref var=$1
198	if	(( $# > 1 ))
199	then	export	$1=$2
200	fi
201	if	(( $# > 2 ))
202	then	print $(myexport "$1" "$3" )
203		return
204	fi
205	typeset val
206	val=$(export | grep "^$1=")
207	print ${val#"$1="}
208
209}
210export dgk=base
211if	[[ $(myexport dgk fun) != fun ]]
212then	err_exit 'export inside function not working'
213fi
214val=$(export | grep "^dgk=")
215if	[[ ${val#dgk=} != base ]]
216then	err_exit 'export not restored after function call'
217fi
218if	[[ $(myexport dgk fun fun2) != fun2 ]]
219then	err_exit 'export inside function not working with recursive function'
220fi
221val=$(export | grep "^dgk=")
222if	[[ ${val#dgk=} != base ]]
223then	err_exit 'export not restored after recursive function call'
224fi
225if	[[ $(dgk=try3 myexport dgk) != try3 ]]
226then	err_exit 'name=value not added to export list with function call'
227fi
228val=$(export | grep "^dgk=")
229if	[[ ${val#dgk=} != base ]]
230then	err_exit 'export not restored name=value function call'
231fi
232unset zzz
233if	[[ $(myexport zzz fun) != fun ]]
234then	err_exit 'export inside function not working for zzz'
235fi
236if	[[ $(export | grep "zzz=") ]]
237then	err_exit 'zzz exported after function call'
238fi
239set -- foo/bar bam/yes last/file/done
240if	[[ ${@/*\/@(*)/${.sh.match[1]}} != 'bar yes done' ]]
241then	err_exit '.sh.match not working with $@'
242fi
243if	[[ ${@/*\/@(*)/\1} != 'bar yes done' ]]
244then	err_exit '\1 not working with $@'
245fi
246var=(foo/bar bam/yes last/file/done)
247if	[[ ${var[@]/*\/@(*)/${.sh.match[1]}} != 'bar yes done' ]]
248then	err_exit '.sh.match not working with ${var[@]}'
249fi
250if	[[ ${var[@]/*\/@(*)/\1} != 'bar yes done' ]]
251then	err_exit '\1 not working with ${var[@]}'
252fi
253var='abc_d2ef.462abc %%'
254if	[[ ${var/+(\w)/Q} != 'Q.462abc %%' ]]
255then	err_exit '${var/+(\w)/Q} not workding'
256fi
257if	[[ ${var//+(\w)/Q} != 'Q.Q %%' ]]
258then	err_exit '${var//+(\w)/Q} not workding'
259fi
260if	[[ ${var//+(\S)/Q} != 'Q Q' ]]
261then	err_exit '${var//+(\S)/Q} not workding'
262fi
263foo='foo+bar+'
264[[ $(print -r -- ${foo//+/'|'}) != 'foo|bar|' ]] && err_exit "\${foobar//+/'|'}"
265[[ $(print -r -- ${foo//+/"|"}) != 'foo|bar|' ]] && err_exit '${foobar//+/"|"}'
266[[ $(print -r -- "${foo//+/'|'}") != 'foo|bar|' ]] && err_exit '"${foobar//+/'"'|'"'}"'
267[[ $(print -r -- "${foo//+/"|"}") != 'foo|bar|' ]] && err_exit '"${foobar//+/"|"}"'
268unset x
269x=abcedfg
270: ${x%@(d)f@(g)}
271[[ ${.sh.match[0]} == dfg ]] || err_exit '.sh.match[0] not dfg'
272[[ ${.sh.match[1]} == d ]] || err_exit '.sh.match[1] not d'
273[[ ${.sh.match[2]} == g ]] || err_exit '.sh.match[2] not g'
274x=abcedddfg
275: ${x%%+(d)f@(g)}
276[[ ${.sh.match[1]} == ddd ]] || err_exit '.sh.match[1] not ddd'
277unset a b
278a='\[abc @(*) def\]'
279b='[abc 123 def]'
280[[ ${b//$a/\1} == 123 ]] || err_exit "\${var/pattern} not working with \[ in pattern"
281unset foo
282foo='(win32.i386) '
283[[ ${foo/'('/'(x11-'} == '(x11-win32.i386) ' ]] || err_exit "\${var/pattern} not working with ' in pattern"
284$SHELL -c $'v=\'$(hello)\'; [[ ${v//\'$(\'/-I\'$(\'} == -I"$v" ]]' 2> /dev/null || err_exit "\${var/pattern} not working with \$( as pattern"
285unset X
286$SHELL -c '[[ ! ${X[@]:0:300} ]]' 2> /dev/null || err_exit '${X[@]:0:300} with X undefined fails'
287$SHELL -c '[[ ${@:0:300} == "$0" ]]' 2> /dev/null || err_exit '${@:0:300} with no arguments fails'
288i=20030704
289[[ ${i#{6}(?)} == 04 ]] ||  err_exit '${i#{6}(?)} not working'
290[[ ${i#{6,6}(?)} == 04 ]] ||  err_exit '${i#{6,6}(?)} not working'
291LC_ALL=posix
292i="   ."
293[[ $(printf "<%s>\n" ${i#' '}) == '<.>' ]] || err_exit 'printf "<%s>\n" ${i#' '} failed'
294unset x
295x=foo
296[[ "${x%o}(1)" == "fo(1)" ]] ||  err_exit 'print ${}() treated as pattern'
297unset i pattern string
298string=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz
299integer i
300for((i=0; i < ${#string}; i++))
301do	pattern+='@(?)'
302done
303[[ $(string=$string $SHELL -c  ": \${string/$pattern/}; print \${.sh.match[26]}") == Z ]] || err_exit -u2 'sh.match[26] not Z'
304: ${string/$pattern/}
305(( ${#.sh.match[@]} == 53 )) || err_exit '.sh.match has wrong number of elements'
306[[ ${.sh.match[@]:2:4} == 'B C D E'  ]] || err_exit '${.sh.match[@]:2:4} incorrect'
307
308D=$';' E=$'\\\\' Q=$'"' S=$'\'' M='nested pattern substitution failed'
309
310x='-(-)-'
311[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
312x='-(-)-)-'
313[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
314x='-(-()-)-'
315[[ ${x/*%(())*/\1} == '()' ]] || err_exit $M
316x='-(-\)-)-'
317[[ ${x/*%(())*/\1} == '(-\)' ]] || err_exit $M
318x='-(-\\)-)-'
319[[ ${x/*%(())*/\1} == '(-\\)' ]] || err_exit $M
320x='-(-(-)-'
321[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
322x='-(-(-)-)-'
323[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
324x='-(-[-]-)-'
325[[ ${x/*%(()[])*/\1} == '(-[-]-)' ]] || err_exit $M
326x='-[-(-)-]-'
327[[ ${x/*%(()[])*/\1} == '(-)' ]] || err_exit $M
328x='-(-[-)-]-'
329[[ ${x/*%(()[])*/\1} == '-(-[-)-]-' ]] || err_exit $M
330x='-(-[-]-)-'
331[[ ${x/*%([]())*/\1} == '[-]' ]] || err_exit $M
332x='-[-(-)-]-'
333[[ ${x/*%([]())*/\1} == '[-(-)-]' ]] || err_exit $M
334x='-(-[-)-]-'
335[[ ${x/*%([]())*/\1} == '-(-[-)-]-' ]] || err_exit $M
336
337x='-((-))-'
338[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
339x='-((-))-'
340[[ ${x/~(-g)*%(())*/\1} == '((-))-' ]] || err_exit $M
341x='-((-))-'
342[[ ${x/~(-g:*)*%(())*/\1} == '(-)' ]] || err_exit $M
343x='-((-))-'
344[[ ${x/~(+g)*%(())*/\1} == '(-)' ]] || err_exit $M
345x='-((-))-'
346[[ ${x/~(+g:*)*%(())*/\1} == '(-)' ]] || err_exit $M
347x='-((-))-'
348[[ ${x/*(?)*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
349x='-((-))-'
350[[ ${x/~(-g)*(?)*%(())*(?)*/:\1:\2:\3:} == '::((-))::-' ]] || err_exit $M
351x='-((-))-'
352[[ ${x/~(-g:*(?))*%(())*(?)*/:\1:\2:\3:} == '::(-):)-:' ]] || err_exit $M
353x='-((-))-'
354[[ ${x/~(+g)*(?)*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
355x='-((-))-'
356[[ ${x/~(+g:*(?))*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
357x='call(a+b,x/(c/d),(0));'
358[[ ${x/+([[:alnum:]])*([[:space:]])(*%(()))*/:\1:\2:\3:} == ':call::(a+b,x/(c/d),(0)):' ]] || err_exit $M
359
360x='-(-;-)-'
361[[ ${x/*%(()D${D})*/\1} == '-(-;-)-' ]] || err_exit $M
362x='-(-);-'
363[[ ${x/*%(()D${D})*/\1} == '(-)' ]] || err_exit $M
364x='-(-)\;-'
365[[ ${x/*%(()D${D})*/\1} == '(-)' ]] || err_exit $M
366x='-(-\;-)-'
367[[ ${x/*%(()D${D}E${E})*/\1} == '(-\;-)' ]] || err_exit $M
368x='-(-)\;-'
369[[ ${x/*%(()D${D}E${E})*/\1} == '(-)' ]] || err_exit $M
370x='-(-(-)\;-)-'
371[[ ${x/*%(()D${D}E${E})*/\1} == '(-)' ]] || err_exit $M
372
373x='-(-")"-)-'
374[[ ${x/*%(()Q${Q})*/\1} == '(-")"-)' ]] || err_exit $M
375x='-(-\")"-)-'
376[[ ${x/*%(()Q${Q})*/\1} == '(-\")"-)' ]] || err_exit $M
377x='-(-\")\"-)-'
378[[ ${x/*%(()Q${Q})*/\1} == '(-\")\"-)' ]] || err_exit $M
379x=$'-(-\\\'")\\\'-)-'
380[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\\'")\\\'-)' ]] || err_exit $M
381x=$'-(-\\\'")"-)-'
382[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
383x=$'-(-\\\'")"\'-)-'
384[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
385x=$'-(-\\"\')\'\\"-)-'
386[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
387x=$'-(-\')\\\'\'-)-'
388[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
389x=$'-(-\'")\'-)-'
390[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\'")\'-)' ]] || err_exit $M
391x=$'-(-\\\'")"-)-'
392[[ ${x/*%(()L${S}Q${Q})*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
393x=$'-(-\\\'")"\'-)-'
394[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
395x=$'-(-\\"\')\'\\"-)-'
396[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
397x=$'-(-\')\\\'\'-)-'
398[[ ${x/*%(()L${S}Q${Q})*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
399x='-(-")"-)-'
400[[ ${x/*%(()Q${Q})*/\1} == '(-")"-)' ]] || err_exit $M
401x='-(-\")"-)-'
402[[ ${x/*%(()Q${Q})*/\1} == '(-\")"-)' ]] || err_exit $M
403x='-(-\")\"-)-'
404[[ ${x/*%(()Q${Q})*/\1} == '(-\")\"-)' ]] || err_exit $M
405
406x='-(-\)-)-'
407[[ ${x/*%(()E${E})*/\1} == '(-\)-)' ]] || err_exit $M
408x='-(-\\)-)-'
409[[ ${x/*%(()E${E})*/\1} == '(-\\)' ]] || err_exit $M
410x='-(-\")"-)-'
411[[ ${x/*%(()E${E}Q${Q})*/\1} == '(-\")' ]] || err_exit $M
412x='-(-\")\"-)-'
413[[ ${x/*%(()E${E}Q${Q})*/\1} == '(-\")' ]] || err_exit $M
414x=$'-(-\'")"-)-'
415[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'-(-\'")"-)-' ]] || err_exit $M
416x=$'-(-\\\'")"-)-'
417[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
418x=$'-(-\\"\')\'\\"-)-'
419[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
420x=$'-(-\\\'")"-)-'
421[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
422x=$'-(-\\"\')\'\\"-)-'
423[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
424x=$'-(-\\"\')\\\'\\"-)-'
425[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\"\')\\\'\\"-)' ]] || err_exit $M
426x=$'-(-\\"\')\\\'\\"\'-)-'
427[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'-(-\\"\')\\\'\\"\'-)-' ]] || err_exit $M
428
429x='-(-;-)-'
430[[ ${x/*%(()D\;)*/\1} == '-(-;-)-' ]] || err_exit $M
431x='-(-);-'
432[[ ${x/*%(()D\;)*/\1} == '(-)' ]] || err_exit $M
433x='-(-)\;-'
434[[ ${x/*%(()D\;)*/\1} == '(-)' ]] || err_exit $M
435x='-(-\;-)-'
436[[ ${x/*%(()D\;E\\)*/\1} == '(-\;-)' ]] || err_exit $M
437x='-(-);-'
438[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
439x='-(-)\;-'
440[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
441x='-(-(-)\;-)-'
442[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
443
444x='-(-")"-)-'
445[[ ${x/*%(()Q\")*/\1} == '(-")"-)' ]] || err_exit $M
446x='-(-\")"-)-'
447[[ ${x/*%(()Q\")*/\1} == '(-\")"-)' ]] || err_exit $M
448x='-(-\")\"-)-'
449[[ ${x/*%(()Q\")*/\1} == '(-\")\"-)' ]] || err_exit $M
450x=$'-(-\\\'")\\\'-)-'
451[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\\'")\\\'-)' ]] || err_exit $M
452x=$'-(-\\\'")"-)-'
453[[ ${x/*%(()Q\'Q\")*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
454x=$'-(-\\\'")"\'-)-'
455[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
456x=$'-(-\\"\')\'\\"-)-'
457[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
458x=$'-(-\')\\\'\'-)-'
459[[ ${x/*%(()Q\'Q\")*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
460x=$'-(-\'")\'-)-'
461[[ ${x/*%(()L\'Q\")*/\1} == $'(-\'")\'-)' ]] || err_exit $M
462x=$'-(-\\\'")"-)-'
463[[ ${x/*%(()L\'Q\")*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
464x=$'-(-\\\'")"\'-)-'
465[[ ${x/*%(()L\'Q\")*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
466x=$'-(-\\"\')\'\\"-)-'
467[[ ${x/*%(()L\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
468x=$'-(-\')\\\'\'-)-'
469[[ ${x/*%(()L\'Q\")*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
470x='-(-")"-)-'
471[[ ${x/*%(()Q\")*/\1} == '(-")"-)' ]] || err_exit $M
472x='-(-\")"-)-'
473[[ ${x/*%(()Q\")*/\1} == '(-\")"-)' ]] || err_exit $M
474x='-(-\")\"-)-'
475[[ ${x/*%(()Q\")*/\1} == '(-\")\"-)' ]] || err_exit $M
476
477x='-(-\)-)-'
478[[ ${x/*%(()E\\)*/\1} == '(-\)-)' ]] || err_exit $M
479x='-(-\\)-)-'
480[[ ${x/*%(()E\\)*/\1} == '(-\\)' ]] || err_exit $M
481x='-(-\")"-)-'
482[[ ${x/*%(()E\\Q\")*/\1} == '(-\")' ]] || err_exit $M
483x='-(-\")\"-)-'
484[[ ${x/*%(()E\\Q\")*/\1} == '(-\")' ]] || err_exit $M
485x=$'-(-\'")"-)-'
486[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'-(-\'")"-)-' ]] || err_exit $M
487x=$'-(-\\\'")"-)-'
488[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
489x=$'-(-\\"\')\'\\"-)-'
490[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
491x=$'-(-\\\'")"-)-'
492[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
493x=$'-(-\\"\')\'\\"-)-'
494[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
495x=$'-(-\\"\')\\\'\\"-)-'
496[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\"\')\\\'\\"-)' ]] || err_exit $M
497x=$'-(-\\"\')\\\'\\"\'-)-'
498[[ ${x/*%(()E\\L\'Q\")*/\1} == $'-(-\\"\')\\\'\\"\'-)-' ]] || err_exit $M
499
500pattern=00
501var=100
502[[ $( print $(( ${var%%00} )) ) == 1 ]] || err_exit "arithmetic with embeddded patterns fails"
503[[ $( print $(( ${var%%$pattern} )) ) == 1 ]] || err_exit "arithmetic with embeddded pattern variables fails"
504if	[[ ax == @(a)* ]] && [[ ${.sh.match[1]:0:${#.sh.match[1]}}  != a ]]
505then	err_exit '${.sh.match[1]:1:${#.sh.match[1]}} not expanding correctly'
506fi
507
508string='foo(d:\nt\box\something)bar'
509expected='d:\nt\box\something'
510[[ ${string/*\(+([!\)])\)*/\1} == "$expected" ]] || err_exit "substring expansion failed '${string/*\(+([!\)])\)*/\1}' returned -- '$expected' expected"
511if	[[ $($SHELL -c $'export LC_ALL=en_US.UTF-8; print -r "\342\202\254\342\202\254\342\202\254\342\202\254w\342\202\254\342\202\254\342\202\254\342\202\254" | wc -m' 2>/dev/null) == 10 ]]
512then	LC_ALL=en_US.UTF-8 $SHELL -c b1=$'"\342\202\254\342\202\254\342\202\254\342\202\254w\342\202\254\342\202\254\342\202\254\342\202\254"; [[ ${b1:4:1} == w ]]' || err_exit 'multibyte ${var:offset:len} not working correctly'
513fi
514{ $SHELL -c 'unset x;[[ ${SHELL:$x} == $SHELL ]]';} 2> /dev/null || err_exit '${var:$x} fails when x is not set'
515{ $SHELL -c 'x=;[[ ${SHELL:$x} == $SHELL ]]';} 2> /dev/null || err_exit '${var:$x} fails when x is null'
516
517#	subject		mode	pattern			result	#
518set --							\
519	'a$z'		'E'	'[$]|#'		'a($)z'	\
520	'a#z'		'E'	'[$]|#'		'a(#)z'	\
521	'a$z'		'Elr'	'[$]|#'		'a$z'	\
522	'a#z'		'Elr'	'[$]|#'		'a#z'	\
523	'a$'		'E'	'[$]|#'		'a($)'	\
524	'a#'		'E'	'[$]|#'		'a(#)'	\
525	'a$'		'Elr'	'[$]|#'		'a$'	\
526	'a#'		'Elr'	'[$]|#'		'a#'	\
527	'$z'		'E'	'[$]|#'		'($)z'	\
528	'#z'		'E'	'[$]|#'		'(#)z'	\
529	'$z'		'Elr'	'[$]|#'		'$z'	\
530	'#z'		'Elr'	'[$]|#'		'#z'	\
531	'$'		'E'	'[$]|#'		'($)'	\
532	'#'		'E'	'[$]|#'		'(#)'	\
533	'$'		'Elr'	'[$]|#'		'($)'	\
534	'#'		'Elr'	'[$]|#'		'(#)'	\
535	'a$z'		'E'	'\$|#'		'a$z()'	\
536	'a$z'		'E'	'\\$|#'		'a$z'	\
537	'a$z'		'E'	'\\\$|#'	'a($)z'	\
538	'a#z'		'E'	'\\\$|#'	'a(#)z'	\
539	'a$z'		'Elr'	'\\\$|#'	'a$z'	\
540	'a#z'		'Elr'	'\\\$|#'	'a#z'	\
541	'a$'		'E'	'\\\$|#'	'a($)'	\
542	'a#'		'E'	'\\\$|#'	'a(#)'	\
543	'a$'		'Elr'	'\\\$|#'	'a$'	\
544	'a#'		'Elr'	'\\\$|#'	'a#'	\
545	'$z'		'E'	'\\\$|#'	'($)z'	\
546	'#z'		'E'	'\\\$|#'	'(#)z'	\
547	'$z'		'Elr'	'\\\$|#'	'$z'	\
548	'#z'		'Elr'	'\\\$|#'	'#z'	\
549	'$'		'E'	'\\\$|#'	'($)'	\
550	'#'		'E'	'\\\$|#'	'(#)'	\
551	'$'		'Elr'	'\\\$|#'	'($)'	\
552	'#'		'Elr'	'\\\$|#'	'(#)'	\
553#	do not delete this line			#
554unset i o
555while	(( $# >= 4 ))
556do	i=$1
557	eval o="\${i/~($2)$3/\\(\\0\\)}"
558	if	[[ "$o" != "$4" ]]
559	then	err_exit "i='$1'; \${i/~($2)$3/\\(\\0\\)} failed -- expected '$4', got '$o'"
560	fi
561	eval o="\${i/~($2)($3)/\\(\\1\\)}"
562	if	[[ "$o" != "$4" ]]
563	then	err_exit "i='$1'; \${i/~($2)($3)/\\(\\1\\)} failed -- expected '$4', got '$o'"
564	fi
565	shift 4
566done
567
568exit $((Errors))
569