Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/substring.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
let Errors+=1
25
}
26
alias err_exit='err_exit $LINENO'
27
28
Command=${0##*/}
29
integer Errors=0 j=4
30
base=/home/dgk/foo//bar
31
string1=$base/abcabcabc
32
if [[ ${string1:0} != "$string1" ]]
33
then err_exit "string1:0"
34
fi
35
if [[ ${string1: -1} != "c" ]]
36
then err_exit "string1: -1"
37
fi
38
if [[ ${string1:0:1000} != "$string1" ]]
39
then err_exit "string1:0"
40
fi
41
if [[ ${string1:1} != "${string1#?}" ]]
42
then err_exit "string1:1"
43
fi
44
if [[ ${string1:1:4} != home ]]
45
then err_exit "string1:1:4"
46
fi
47
if [[ ${string1: -5:4} != bcab ]]
48
then err_exit "string1: -5:4"
49
fi
50
if [[ ${string1:1:j} != home ]]
51
then err_exit "string1:1:j"
52
fi
53
if [[ ${string1:(j?1:0):j} != home ]]
54
then err_exit "string1:(j?1:0):j"
55
fi
56
if [[ ${string1%*zzz*} != "$string1" ]]
57
then err_exit "string1%*zzz*"
58
fi
59
if [[ ${string1%%*zzz*} != "$string1" ]]
60
then err_exit "string1%%*zzz*"
61
fi
62
if [[ ${string1#*zzz*} != "$string1" ]]
63
then err_exit "string1#*zzz*"
64
fi
65
if [[ ${string1##*zzz*} != "$string1" ]]
66
then err_exit "string1##*zzz*"
67
fi
68
if [[ ${string1%+(abc)} != "$base/abcabc" ]]
69
then err_exit "string1%+(abc)"
70
fi
71
if [[ ${string1%%+(abc)} != "$base/" ]]
72
then err_exit "string1%%+(abc)"
73
fi
74
if [[ ${string1%/*} != "$base" ]]
75
then err_exit "string1%/*"
76
fi
77
if [[ "${string1%/*}" != "$base" ]]
78
then err_exit '"string1%/*"'
79
fi
80
if [[ ${string1%"/*"} != "$string1" ]]
81
then err_exit 'string1%"/*"'
82
fi
83
if [[ ${string1%%/*} != "" ]]
84
then err_exit "string1%%/*"
85
fi
86
if [[ ${string1#*/bar} != /abcabcabc ]]
87
then err_exit "string1#*bar"
88
fi
89
if [[ ${string1##*/bar} != /abcabcabc ]]
90
then err_exit "string1#*bar"
91
fi
92
if [[ "${string1#@(*/bar|*/foo)}" != //bar/abcabcabc ]]
93
then err_exit "string1#@(*/bar|*/foo)"
94
fi
95
if [[ ${string1##@(*/bar|*/foo)} != /abcabcabc ]]
96
then err_exit "string1##@(*/bar|*/foo)"
97
fi
98
if [[ ${string1##*/@(bar|foo)} != /abcabcabc ]]
99
then err_exit "string1##*/@(bar|foo)"
100
fi
101
foo=abc
102
if [[ ${foo#a[b*} != abc ]]
103
then err_exit "abc#a[b*} != abc"
104
fi
105
if [[ ${foo//[0-9]/bar} != abc ]]
106
then err_exit '${foo//[0-9]/bar} not expanding correctly'
107
fi
108
foo='(abc)'
109
if [[ ${foo#'('} != 'abc)' ]]
110
then err_exit "(abc)#( != abc)"
111
fi
112
if [[ ${foo%')'} != '(abc' ]]
113
then err_exit "(abc)%) != (abc"
114
fi
115
foo=a123b456c
116
if [[ ${foo/[0-9]?/""} != a3b456c ]]
117
then err_exit '${foo/[0-9]?/""} not expanding correctly'
118
fi
119
if [[ ${foo//[0-9]/""} != abc ]]
120
then err_exit '${foo//[0-9]/""} not expanding correctly'
121
fi
122
if [[ ${foo/#a/b} != b123b456c ]]
123
then err_exit '${foo/#a/b} not expanding correctly'
124
fi
125
if [[ ${foo/#?/b} != b123b456c ]]
126
then err_exit '${foo/#?/b} not expanding correctly'
127
fi
128
if [[ ${foo/%c/b} != a123b456b ]]
129
then err_exit '${foo/%c/b} not expanding correctly'
130
fi
131
if [[ ${foo/%?/b} != a123b456b ]]
132
then err_exit '${foo/%?/b} not expanding correctly'
133
fi
134
while read -r pattern string expected
135
do 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
152
done <<- \EOF
153
+(a)*+(a) aabca 1
154
!(*.o) foo.o 0
155
!(*.o) foo.c 1
156
EOF
157
xx=a/b/c/d/e
158
yy=${xx#*/}
159
if [[ $yy != b/c/d/e ]]
160
then err_exit '${xx#*/} != a/b/c/d/e when xx=a/b/c/d/e'
161
fi
162
if [[ ${xx//\//\\} != 'a\b\c\d\e' ]]
163
then err_exit '${xx//\//\\} not working'
164
fi
165
x=[123]def
166
if [[ "${x//\[@(*)\]/\{\1\}}" != {123}def ]]
167
then err_exit 'closing brace escape not working'
168
fi
169
xx=%28text%29
170
if [[ ${xx//%28/abc\)} != 'abc)text%29' ]]
171
then err_exit '${xx//%28/abc\)} not working'
172
fi
173
xx='a:b'
174
str='(){}[]*?|&^%$#@l'
175
for ((i=0 ; i < ${#str}; i++))
176
do [[ $(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"
179
done
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"
184
unset foo
185
foo=one/two/three
186
if [[ ${foo//'/'/_} != one_two_three ]]
187
then err_exit 'single quoting / in replacements failed'
188
fi
189
if [[ ${foo//"/"/_} != one_two_three ]]
190
then err_exit 'double quoting / in replacements failed'
191
fi
192
if [[ ${foo//\//_} != one_two_three ]]
193
then err_exit 'escaping / in replacements failed'
194
fi
195
function 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
}
210
export dgk=base
211
if [[ $(myexport dgk fun) != fun ]]
212
then err_exit 'export inside function not working'
213
fi
214
val=$(export | grep "^dgk=")
215
if [[ ${val#dgk=} != base ]]
216
then err_exit 'export not restored after function call'
217
fi
218
if [[ $(myexport dgk fun fun2) != fun2 ]]
219
then err_exit 'export inside function not working with recursive function'
220
fi
221
val=$(export | grep "^dgk=")
222
if [[ ${val#dgk=} != base ]]
223
then err_exit 'export not restored after recursive function call'
224
fi
225
if [[ $(dgk=try3 myexport dgk) != try3 ]]
226
then err_exit 'name=value not added to export list with function call'
227
fi
228
val=$(export | grep "^dgk=")
229
if [[ ${val#dgk=} != base ]]
230
then err_exit 'export not restored name=value function call'
231
fi
232
unset zzz
233
if [[ $(myexport zzz fun) != fun ]]
234
then err_exit 'export inside function not working for zzz'
235
fi
236
if [[ $(export | grep "zzz=") ]]
237
then err_exit 'zzz exported after function call'
238
fi
239
set -- foo/bar bam/yes last/file/done
240
if [[ ${@/*\/@(*)/\1} != 'bar yes done' ]]
241
then err_exit '\1 not working with $@'
242
fi
243
var=(foo/bar bam/yes last/file/done)
244
if [[ ${var[@]/*\/@(*)/\1} != 'bar yes done' ]]
245
then err_exit '\1 not working with ${var[@]}'
246
fi
247
var='abc_d2ef.462abc %%'
248
if [[ ${var/+(\w)/Q} != 'Q.462abc %%' ]]
249
then err_exit '${var/+(\w)/Q} not workding'
250
fi
251
if [[ ${var//+(\w)/Q} != 'Q.Q %%' ]]
252
then err_exit '${var//+(\w)/Q} not workding'
253
fi
254
if [[ ${var//+(\S)/Q} != 'Q Q' ]]
255
then err_exit '${var//+(\S)/Q} not workding'
256
fi
257
var=$($SHELL -c 'v=/vin:/usr/vin r=vin; : ${v//vin/${r//v/b}};typeset -p .sh.match') 2> /dev/null
258
[[ $var == 'typeset -a .sh.match=((vin vin) )' ]] || err_exit '.sh.match not correct when replacement pattern contains a substring match'
259
foo='foo+bar+'
260
[[ $(print -r -- ${foo//+/'|'}) != 'foo|bar|' ]] && err_exit "\${foobar//+/'|'}"
261
[[ $(print -r -- ${foo//+/"|"}) != 'foo|bar|' ]] && err_exit '${foobar//+/"|"}'
262
[[ $(print -r -- "${foo//+/'|'}") != 'foo|bar|' ]] && err_exit '"${foobar//+/'"'|'"'}"'
263
[[ $(print -r -- "${foo//+/"|"}") != 'foo|bar|' ]] && err_exit '"${foobar//+/"|"}"'
264
unset x
265
x=abcedfg
266
: ${x%@(d)f@(g)}
267
[[ ${.sh.match[0]} == dfg ]] || err_exit '.sh.match[0] not dfg'
268
[[ ${.sh.match[1]} == d ]] || err_exit '.sh.match[1] not d'
269
[[ ${.sh.match[2]} == g ]] || err_exit '.sh.match[2] not g'
270
x=abcedddfg
271
: ${x%%+(d)f@(g)}
272
[[ ${.sh.match[1]} == ddd ]] || err_exit '.sh.match[1] not ddd'
273
unset a b
274
a='\[abc @(*) def\]'
275
b='[abc 123 def]'
276
[[ ${b//$a/\1} == 123 ]] || err_exit "\${var/pattern} not working with \[ in pattern"
277
unset foo
278
foo='(win32.i386) '
279
[[ ${foo/'('/'(x11-'} == '(x11-win32.i386) ' ]] || err_exit "\${var/pattern} not working with ' in pattern"
280
$SHELL -c $'v=\'$(hello)\'; [[ ${v//\'$(\'/-I\'$(\'} == -I"$v" ]]' 2> /dev/null || err_exit "\${var/pattern} not working with \$( as pattern"
281
unset X
282
$SHELL -c '[[ ! ${X[@]:0:300} ]]' 2> /dev/null || err_exit '${X[@]:0:300} with X undefined fails'
283
$SHELL -c '[[ ${@:0:300} == "$0" ]]' 2> /dev/null || err_exit '${@:0:300} with no arguments fails'
284
i=20030704
285
[[ ${i#{6}(?)} == 04 ]] || err_exit '${i#{6}(?)} not working'
286
[[ ${i#{6,6}(?)} == 04 ]] || err_exit '${i#{6,6}(?)} not working'
287
LC_ALL=posix
288
i=" ."
289
[[ $(printf "<%s>\n" ${i#' '}) == '<.>' ]] || err_exit 'printf "<%s>\n" ${i#' '} failed'
290
unset x
291
x=foo
292
[[ "${x%o}(1)" == "fo(1)" ]] || err_exit 'print ${}() treated as pattern'
293
unset i pattern string
294
string=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz
295
integer i
296
for((i=0; i < ${#string}; i++))
297
do pattern+='@(?)'
298
done
299
[[ $(string=$string $SHELL -c ": \${string/$pattern/}; print \${.sh.match[26]}") == Z ]] || err_exit -u2 'sh.match[26] not Z'
300
: ${string/$pattern/}
301
(( ${#.sh.match[@]} == 53 )) || err_exit '.sh.match has wrong number of elements'
302
[[ ${.sh.match[@]:2:4} == 'B C D E' ]] || err_exit '${.sh.match[@]:2:4} incorrect'
303
304
D=$';' E=$'\\\\' Q=$'"' S=$'\'' M='nested pattern substitution failed'
305
306
x='-(-)-'
307
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
308
x='-(-)-)-'
309
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
310
x='-(-()-)-'
311
[[ ${x/*%(())*/\1} == '()' ]] || err_exit $M
312
x='-(-\)-)-'
313
[[ ${x/*%(())*/\1} == '(-\)' ]] || err_exit $M
314
x='-(-\\)-)-'
315
[[ ${x/*%(())*/\1} == '(-\\)' ]] || err_exit $M
316
x='-(-(-)-'
317
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
318
x='-(-(-)-)-'
319
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
320
x='-(-[-]-)-'
321
[[ ${x/*%(()[])*/\1} == '(-[-]-)' ]] || err_exit $M
322
x='-[-(-)-]-'
323
[[ ${x/*%(()[])*/\1} == '(-)' ]] || err_exit $M
324
x='-(-[-)-]-'
325
[[ ${x/*%(()[])*/\1} == '-(-[-)-]-' ]] || err_exit $M
326
x='-(-[-]-)-'
327
[[ ${x/*%([]())*/\1} == '[-]' ]] || err_exit $M
328
x='-[-(-)-]-'
329
[[ ${x/*%([]())*/\1} == '[-(-)-]' ]] || err_exit $M
330
x='-(-[-)-]-'
331
[[ ${x/*%([]())*/\1} == '-(-[-)-]-' ]] || err_exit $M
332
333
x='-((-))-'
334
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
335
x='-((-))-'
336
[[ ${x/~(-g)*%(())*/\1} == '((-))-' ]] || err_exit $M
337
x='-((-))-'
338
[[ ${x/~(-g:*)*%(())*/\1} == '(-)' ]] || err_exit $M
339
x='-((-))-'
340
[[ ${x/~(+g)*%(())*/\1} == '(-)' ]] || err_exit $M
341
x='-((-))-'
342
[[ ${x/~(+g:*)*%(())*/\1} == '(-)' ]] || err_exit $M
343
x='-((-))-'
344
[[ ${x/*(?)*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
345
x='-((-))-'
346
[[ ${x/~(-g)*(?)*%(())*(?)*/:\1:\2:\3:} == '::((-))::-' ]] || err_exit $M
347
x='-((-))-'
348
[[ ${x/~(-g:*(?))*%(())*(?)*/:\1:\2:\3:} == '::(-):)-:' ]] || err_exit $M
349
x='-((-))-'
350
[[ ${x/~(+g)*(?)*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
351
x='-((-))-'
352
[[ ${x/~(+g:*(?))*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
353
x='call(a+b,x/(c/d),(0));'
354
[[ ${x/+([[:alnum:]])*([[:space:]])@(*%(()))*/:\1:\2:\3:} == ':call::(a+b,x/(c/d),(0)):' ]] || err_exit $M
355
356
x='-(-;-)-'
357
[[ ${x/*%(()D${D})*/\1} == '-(-;-)-' ]] || err_exit $M
358
x='-(-);-'
359
[[ ${x/*%(()D${D})*/\1} == '(-)' ]] || err_exit $M
360
x='-(-)\;-'
361
[[ ${x/*%(()D${D})*/\1} == '(-)' ]] || err_exit $M
362
x='-(-\;-)-'
363
[[ ${x/*%(()D${D}E${E})*/\1} == '(-\;-)' ]] || err_exit $M
364
x='-(-)\;-'
365
[[ ${x/*%(()D${D}E${E})*/\1} == '(-)' ]] || err_exit $M
366
x='-(-(-)\;-)-'
367
[[ ${x/*%(()D${D}E${E})*/\1} == '(-)' ]] || err_exit $M
368
369
x='-(-")"-)-'
370
[[ ${x/*%(()Q${Q})*/\1} == '(-")"-)' ]] || err_exit $M
371
x='-(-\")"-)-'
372
[[ ${x/*%(()Q${Q})*/\1} == '(-\")"-)' ]] || err_exit $M
373
x='-(-\")\"-)-'
374
[[ ${x/*%(()Q${Q})*/\1} == '(-\")\"-)' ]] || err_exit $M
375
x=$'-(-\\\'")\\\'-)-'
376
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\\'")\\\'-)' ]] || err_exit $M
377
x=$'-(-\\\'")"-)-'
378
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
379
x=$'-(-\\\'")"\'-)-'
380
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
381
x=$'-(-\\"\')\'\\"-)-'
382
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
383
x=$'-(-\')\\\'\'-)-'
384
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
385
x=$'-(-\'")\'-)-'
386
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\'")\'-)' ]] || err_exit $M
387
x=$'-(-\\\'")"-)-'
388
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
389
x=$'-(-\\\'")"\'-)-'
390
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
391
x=$'-(-\\"\')\'\\"-)-'
392
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
393
x=$'-(-\')\\\'\'-)-'
394
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
395
x='-(-")"-)-'
396
[[ ${x/*%(()Q${Q})*/\1} == '(-")"-)' ]] || err_exit $M
397
x='-(-\")"-)-'
398
[[ ${x/*%(()Q${Q})*/\1} == '(-\")"-)' ]] || err_exit $M
399
x='-(-\")\"-)-'
400
[[ ${x/*%(()Q${Q})*/\1} == '(-\")\"-)' ]] || err_exit $M
401
402
x='-(-\)-)-'
403
[[ ${x/*%(()E${E})*/\1} == '(-\)-)' ]] || err_exit $M
404
x='-(-\\)-)-'
405
[[ ${x/*%(()E${E})*/\1} == '(-\\)' ]] || err_exit $M
406
x='-(-\")"-)-'
407
[[ ${x/*%(()E${E}Q${Q})*/\1} == '(-\")' ]] || err_exit $M
408
x='-(-\")\"-)-'
409
[[ ${x/*%(()E${E}Q${Q})*/\1} == '(-\")' ]] || err_exit $M
410
x=$'-(-\'")"-)-'
411
[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'-(-\'")"-)-' ]] || err_exit $M
412
x=$'-(-\\\'")"-)-'
413
[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
414
x=$'-(-\\"\')\'\\"-)-'
415
[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
416
x=$'-(-\\\'")"-)-'
417
[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
418
x=$'-(-\\"\')\'\\"-)-'
419
[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
420
x=$'-(-\\"\')\\\'\\"-)-'
421
[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\"\')\\\'\\"-)' ]] || err_exit $M
422
x=$'-(-\\"\')\\\'\\"\'-)-'
423
[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'-(-\\"\')\\\'\\"\'-)-' ]] || err_exit $M
424
425
x='-(-;-)-'
426
[[ ${x/*%(()D\;)*/\1} == '-(-;-)-' ]] || err_exit $M
427
x='-(-);-'
428
[[ ${x/*%(()D\;)*/\1} == '(-)' ]] || err_exit $M
429
x='-(-)\;-'
430
[[ ${x/*%(()D\;)*/\1} == '(-)' ]] || err_exit $M
431
x='-(-\;-)-'
432
[[ ${x/*%(()D\;E\\)*/\1} == '(-\;-)' ]] || err_exit $M
433
x='-(-);-'
434
[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
435
x='-(-)\;-'
436
[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
437
x='-(-(-)\;-)-'
438
[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
439
440
x='-(-")"-)-'
441
[[ ${x/*%(()Q\")*/\1} == '(-")"-)' ]] || err_exit $M
442
x='-(-\")"-)-'
443
[[ ${x/*%(()Q\")*/\1} == '(-\")"-)' ]] || err_exit $M
444
x='-(-\")\"-)-'
445
[[ ${x/*%(()Q\")*/\1} == '(-\")\"-)' ]] || err_exit $M
446
x=$'-(-\\\'")\\\'-)-'
447
[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\\'")\\\'-)' ]] || err_exit $M
448
x=$'-(-\\\'")"-)-'
449
[[ ${x/*%(()Q\'Q\")*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
450
x=$'-(-\\\'")"\'-)-'
451
[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
452
x=$'-(-\\"\')\'\\"-)-'
453
[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
454
x=$'-(-\')\\\'\'-)-'
455
[[ ${x/*%(()Q\'Q\")*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
456
x=$'-(-\'")\'-)-'
457
[[ ${x/*%(()L\'Q\")*/\1} == $'(-\'")\'-)' ]] || err_exit $M
458
x=$'-(-\\\'")"-)-'
459
[[ ${x/*%(()L\'Q\")*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
460
x=$'-(-\\\'")"\'-)-'
461
[[ ${x/*%(()L\'Q\")*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
462
x=$'-(-\\"\')\'\\"-)-'
463
[[ ${x/*%(()L\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
464
x=$'-(-\')\\\'\'-)-'
465
[[ ${x/*%(()L\'Q\")*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
466
x='-(-")"-)-'
467
[[ ${x/*%(()Q\")*/\1} == '(-")"-)' ]] || err_exit $M
468
x='-(-\")"-)-'
469
[[ ${x/*%(()Q\")*/\1} == '(-\")"-)' ]] || err_exit $M
470
x='-(-\")\"-)-'
471
[[ ${x/*%(()Q\")*/\1} == '(-\")\"-)' ]] || err_exit $M
472
473
x='-(-\)-)-'
474
[[ ${x/*%(()E\\)*/\1} == '(-\)-)' ]] || err_exit $M
475
x='-(-\\)-)-'
476
[[ ${x/*%(()E\\)*/\1} == '(-\\)' ]] || err_exit $M
477
x='-(-\")"-)-'
478
[[ ${x/*%(()E\\Q\")*/\1} == '(-\")' ]] || err_exit $M
479
x='-(-\")\"-)-'
480
[[ ${x/*%(()E\\Q\")*/\1} == '(-\")' ]] || err_exit $M
481
x=$'-(-\'")"-)-'
482
[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'-(-\'")"-)-' ]] || err_exit $M
483
x=$'-(-\\\'")"-)-'
484
[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
485
x=$'-(-\\"\')\'\\"-)-'
486
[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
487
x=$'-(-\\\'")"-)-'
488
[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
489
x=$'-(-\\"\')\'\\"-)-'
490
[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
491
x=$'-(-\\"\')\\\'\\"-)-'
492
[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\"\')\\\'\\"-)' ]] || err_exit $M
493
x=$'-(-\\"\')\\\'\\"\'-)-'
494
[[ ${x/*%(()E\\L\'Q\")*/\1} == $'-(-\\"\')\\\'\\"\'-)-' ]] || err_exit $M
495
496
pattern=00
497
var=100
498
[[ $( print $(( ${var%%00} )) ) == 1 ]] || err_exit "arithmetic with embeddded patterns fails"
499
[[ $( print $(( ${var%%$pattern} )) ) == 1 ]] || err_exit "arithmetic with embeddded pattern variables fails"
500
if [[ ax == @(a)* ]] && [[ ${.sh.match[1]:0:${#.sh.match[1]}} != a ]]
501
then err_exit '${.sh.match[1]:1:${#.sh.match[1]}} not expanding correctly'
502
fi
503
504
string='foo(d:\nt\box\something)bar'
505
expected='d:\nt\box\something'
506
[[ ${string/*\(+([!\)])\)*/\1} == "$expected" ]] || err_exit "substring expansion failed '${string/*\(+([!\)])\)*/\1}' returned -- '$expected' expected"
507
if [[ $($SHELL -c $'export LC_ALL=C.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 ]]
508
then LC_ALL=C.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'
509
fi
510
{ $SHELL -c 'unset x;[[ ${SHELL:$x} == $SHELL ]]';} 2> /dev/null || err_exit '${var:$x} fails when x is not set'
511
{ $SHELL -c 'x=;[[ ${SHELL:$x} == $SHELL ]]';} 2> /dev/null || err_exit '${var:$x} fails when x is null'
512
513
# subject mode pattern result #
514
set -- \
515
'a$z' 'E' '[$]|#' 'a($)z' \
516
'a#z' 'E' '[$]|#' 'a(#)z' \
517
'a$z' 'Elr' '[$]|#' 'a$z' \
518
'a#z' 'Elr' '[$]|#' 'a#z' \
519
'a$' 'E' '[$]|#' 'a($)' \
520
'a#' 'E' '[$]|#' 'a(#)' \
521
'a$' 'Elr' '[$]|#' 'a$' \
522
'a#' 'Elr' '[$]|#' 'a#' \
523
'$z' 'E' '[$]|#' '($)z' \
524
'#z' 'E' '[$]|#' '(#)z' \
525
'$z' 'Elr' '[$]|#' '$z' \
526
'#z' 'Elr' '[$]|#' '#z' \
527
'$' 'E' '[$]|#' '($)' \
528
'#' 'E' '[$]|#' '(#)' \
529
'$' 'Elr' '[$]|#' '($)' \
530
'#' 'Elr' '[$]|#' '(#)' \
531
'a$z' 'E' '\$|#' 'a$z()' \
532
'a$z' 'E' '\\$|#' 'a$z' \
533
'a$z' 'E' '\\\$|#' 'a($)z' \
534
'a#z' 'E' '\\\$|#' 'a(#)z' \
535
'a$z' 'Elr' '\\\$|#' 'a$z' \
536
'a#z' 'Elr' '\\\$|#' 'a#z' \
537
'a$' 'E' '\\\$|#' 'a($)' \
538
'a#' 'E' '\\\$|#' 'a(#)' \
539
'a$' 'Elr' '\\\$|#' 'a$' \
540
'a#' 'Elr' '\\\$|#' 'a#' \
541
'$z' 'E' '\\\$|#' '($)z' \
542
'#z' 'E' '\\\$|#' '(#)z' \
543
'$z' 'Elr' '\\\$|#' '$z' \
544
'#z' 'Elr' '\\\$|#' '#z' \
545
'$' 'E' '\\\$|#' '($)' \
546
'#' 'E' '\\\$|#' '(#)' \
547
'$' 'Elr' '\\\$|#' '($)' \
548
'#' 'Elr' '\\\$|#' '(#)' \
549
# do not delete this line #
550
unset i o
551
while (( $# >= 4 ))
552
do i=$1
553
eval o="\${i/~($2)$3/\\(\\0\\)}"
554
if [[ "$o" != "$4" ]]
555
then err_exit "i='$1'; \${i/~($2)$3/\\(\\0\\)} failed -- expected '$4', got '$o'"
556
fi
557
eval o="\${i/~($2)($3)/\\(\\1\\)}"
558
if [[ "$o" != "$4" ]]
559
then err_exit "i='$1'; \${i/~($2)($3)/\\(\\1\\)} failed -- expected '$4', got '$o'"
560
fi
561
shift 4
562
done
563
564
#multibyte locale tests
565
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:0:1}" == a || err_exit ${x:0:1} should be a'
566
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:1:1}" == "<2b|>" || err_exit ${x:1:1} should be <2b|>'
567
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:3:1}" == "<3d|\\>" || err_exit ${x:3:1} should be <3d|\>'
568
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:4:1}" == e || err_exit ${x:4:1} should bee'
569
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:1}" == "<2b|>c<3d|\\>e" || print -u2 ${x:1}" should be <2b|>c<3d|\>e'
570
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x: -1:1}" == e || err_exit ${x: -1:1} should be e'
571
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x: -2:1}" == "<3d|\\>" || err_exit ${x: -2:1} == <3d|\>'
572
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:1:3}" == "<2b|>c<3d|\\>" || err_exit ${x:1:3} should be <2b|>c<3d|\>'
573
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:1:20}" == "<2b|>c<3d|\\>e" || err_exit ${x:1:20} should be <2b|>c<3d|\>e'
574
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x#??}" == "c<3d|\\>e" || err_exit "${x#??} should be c<3d|\>e'
575
576
x='a one and a two'
577
[[ "${x//~(E)\<.\>/}" == ' one and two' ]] || err_exit "\< and \> not working in with ere's"
578
579
{
580
$SHELL -c 'typeset x="123" ; integer i=100 ; print -n "${x:i:5}"'
581
} 2> /dev/null || err_exit '${x:i:j} fails when i > strlen(x)'
582
583
got=$($SHELL -c 'A=""; B="B"; for I in ${A[@]} ${B[@]}; do echo "\"$I\""; done')
584
[[ $got == $'"B"' ]] || err_exit '"\"$I\"" fails when $I is empty string'
585
586
A='|'
587
[[ $A == $A ]] || err_exit 'With A="|", [[ $A == $A ]] does not match'
588
589
x="111 222 333 444 555 666"
590
[[ $x == ~(E)(...).(...).(...) ]]
591
[[ -v .sh.match[0] ]] || err_exit '[[ -v .sh.match[0] ]] should be true'
592
[[ -v .sh.match[3] ]] || err_exit '[[ -v .sh.match[3] ]] should be true'
593
[[ -v .sh.match[4] ]] && err_exit '[[ -v .sh.match[4] ]] should be false'
594
[[ ${#.sh.match[@]} == 4 ]] || err_exit "\${#.sh.match[@]} should be 4, not ${#.sh.match[@]}"
595
596
x="foo bar"
597
dummy=${x/~(E)(*)/}
598
[[ ${ print -v .sh.match;} ]] && err_exit 'print -v should show .sh.match empty when there are no matches'
599
600
if $SHELL -c 'set 1 2 3 4 5 6 7 8 9 10 11 12; : ${##[0-9]}' 2>/dev/null
601
then set 1 2 3 4 5 6 7 8 9 10 11 12
602
[[ ${##[0-9]} == 2 ]] || err_exit '${##[0-9]} should be 2 with $#==12'
603
[[ ${###[0-9]} == 2 ]] || err_exit '${###[0-9]} should be 2 with $#==12'
604
[[ ${#%[0-9]} == 1 ]] || err_exit '${#%[0-9]} should be 1 with $#==12'
605
[[ ${#%%[0-9]} == 1 ]] || err_exit '${#%%[0-9]} should be 1 with $#==12'
606
else err_exit '${##[0-9]} give syntax error'
607
fi
608
609
{
610
$SHELL -c 'x="a123 456 789z"; : ${x//{3}(\d)/ }' &
611
sleep .5; kill $!; wait $!
612
} 2> /dev/null || err_exit $'tokenizer can\'t handle ${var op {..} }'
613
614
615
function foo
616
{
617
typeset x="123 456 789 abc"
618
typeset dummy="${x/~(E-g)([[:digit:]][[:digit:]])((X)|([[:digit:]]))([[:blank:]])/_}"
619
exp=$'(\n\t[0]=\'123 \'\n\t[1]=12\n\t[2]=3\n\t[4]=3\n\t[5]=\' \'\n)'
620
[[ $(print -v .sh.match) == "$exp" ]] || err_exit '.sh.match not correct with alternations'
621
}
622
foo
623
624
x="a 1 b"
625
d=${x/~(E)(([[:digit:]])[[:space:]]*|([[:alpha:]]))/X}
626
[[ $(print -v .sh.match) == $'(\n\t[0]=a\n\t[1]=a\n\t[3]=a\n)' ]] || err_exit '.sh.match not sparse'
627
628
unset v
629
typeset -a arr=( 0 1 2 3 4 )
630
for v in "${arr[@]:5}"
631
do err_exit "\${arr[@]:5} should not generate $v"
632
break
633
done
634
for v in "${arr[@]:1:0}"
635
do err_exit "\${arr[@]:1:0} should not generate ${v:-empty_string}"
636
break
637
done
638
for v in "${arr[@]:0:-1}"
639
do err_exit "\${arr[@]:0:-1} should not generate ${v:-empty_string}"
640
break
641
done
642
643
set 1 2 3 4
644
for v in "${@:5}"
645
do err_exit "\${@:5} should not generate $v"
646
break
647
done
648
for v in "${@:1:0}"
649
do err_exit "\${@:1:0} should not generate ${v:-empty_string}"
650
break
651
done
652
for v in "${@:0:-1}"
653
do err_exit "\${@:0:-1} should not generate ${v:-empty_string}"
654
break
655
done
656
657
unset v d
658
v=abbbc
659
d="${v/~(E)b{2,4}/dummy}"
660
[[ ${.sh.match} == bbb ]] || err_exit '.sh.match wrong after ${s/~(E)b{2,4}/dummy}'
661
[[ $d == adummyc ]] || err_exit '${s/~(E)b{2,4}/dummy} not working'
662
663
664
exit $((Errors<125?Errors:125))
665
666