docs: aio: Document unix domain datagram support
[jimtcl.git] / tests / regexp.test
blob2e9b13e0e76a86f97752d99ffe15d3da74e323be
1 # Commands covered:  regexp, regsub
3 # This file contains a collection of tests for one or more of the Tcl
4 # built-in commands.  Sourcing this file into Tcl runs the tests and
5 # generates output for errors.  No output means no errors were found.
7 # Copyright (c) 1991-1993 The Regents of the University of California.
8 # Copyright (c) 1998 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
11 # See the file "license.terms" for information on usage and redistribution
12 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 # RCS: @(#) $Id: regexp.test,v 1.30.2.1 2008/08/21 23:19:06 hobbs Exp $
16 source [file dirname [info script]]/testing.tcl
18 needs cmd regexp
19 testConstraint regexp_are [regexp {\d} 1]
21 catch {unset foo}
22 test regexp-1.1 {basic regexp operation} {
23     regexp ab*c abbbc
24 } 1
25 test regexp-1.2 {basic regexp operation} {
26     regexp ab*c ac
27 } 1
28 test regexp-1.3 {basic regexp operation} {
29     regexp ab*c ab
30 } 0
31 test regexp-1.4 {basic regexp operation} {
32     regexp -- -gorp abc-gorpxxx
33 } 1
34 test regexp-1.5 {basic regexp operation} {
35     regexp {^([^ ]*)[ ]*([^ ]*)} "" a
36 } 1
37 test regexp-1.6 {basic regexp operation} regexp_are {
38     list [catch {regexp {} abc} msg] $msg
39 } {0 1}
40 #test regexp-1.7 {regexp utf compliance} {
41 #    # if not UTF-8 aware, result is "0 1"
42 #    set foo "\u4e4eb q"
43 #    regexp "\u4e4eb q" "a\u4e4eb qw\u5e4e\x4e wq" bar
44 #    list [string compare $foo $bar] [regexp 4 $bar]
45 #} {0 0}
48 test regexp-2.1 {getting substrings back from regexp} {
49     set foo {}
50     list [regexp ab*c abbbbc foo] $foo
51 } {1 abbbbc}
52 test regexp-2.2 {getting substrings back from regexp} {
53     set foo {}
54     set f2 {}
55     list [regexp a(b*)c abbbbc foo f2] $foo $f2
56 } {1 abbbbc bbbb}
57 test regexp-2.3 {getting substrings back from regexp} {
58     set foo {}
59     set f2 {}
60     list [regexp a(b*)(c) abbbbc foo f2] $foo $f2
61 } {1 abbbbc bbbb}
62 test regexp-2.4 {getting substrings back from regexp} {
63     set foo {}
64     set f2 {}
65     set f3 {}
66     list [regexp a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
67 } {1 abbbbc bbbb c}
68 test regexp-2.5 {getting substrings back from regexp} {
69     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
70     set f6 {}; set f7 {}; set f8 {}; set f9 {}; set fa {}; set fb {};
71     list [regexp (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*)(a*)(b*) \
72               12223345556789999aabbb \
73             foo f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb] $foo $f1 $f2 $f3 $f4 $f5 \
74             $f6 $f7 $f8 $f9 $fa $fb
75 } {1 12223345556789999aabbb 1 222 33 4 555 6 7 8 9999 aa bbb}
76 test regexp-2.6 {getting substrings back from regexp} {
77     set foo 2; set f2 2; set f3 2; set f4 2
78     list [regexp (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
79 } {1 a a {} {}}
80 test regexp-2.7 {getting substrings back from regexp} {
81     set foo 1; set f2 1; set f3 1; set f4 1
82     list [regexp (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
83 } {1 ac a {} c}
84 test regexp-2.8 {getting substrings back from regexp} {
85     set match {}
86     list [regexp {^a*b} aaaab match] $match
87 } {1 aaaab}
88 test regexp-2.9 {getting substrings back from regexp} {
89     set foo {}
90     set f2 {}
91     list [regexp f\352te(b*)c f\352tebbbbc foo f2] $foo $f2
92 } [list 1 f\352tebbbbc bbbb]
93 test regexp-2.10 {getting substrings back from regexp} {
94     set foo {}
95     set f2 {}
96     list [regexp f\352te(b*)c eff\352tebbbbc foo f2] $foo $f2
97 } [list 1 f\352tebbbbc bbbb]
99 test regexp-3.1 {-indices option to regexp} {
100     set foo {}
101     list [regexp -indices ab*c abbbbc foo] $foo
102 } {1 {0 5}}
103 test regexp-3.2 {-indices option to regexp} {
104     set foo {}
105     set f2 {}
106     list [regexp -indices a(b*)c abbbbc foo f2] $foo $f2
107 } {1 {0 5} {1 4}}
108 test regexp-3.3 {-indices option to regexp} {
109     set foo {}
110     set f2 {}
111     list [regexp -indices a(b*)(c) abbbbc foo f2] $foo $f2
112 } {1 {0 5} {1 4}}
113 test regexp-3.4 {-indices option to regexp} {
114     set foo {}
115     set f2 {}
116     set f3 {}
117     list [regexp -indices a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
118 } {1 {0 5} {1 4} {5 5}}
119 test regexp-3.5 {-indices option to regexp} {
120     set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
121     set f6 {}; set f7 {}; set f8 {}; set f9 {}
122     list [regexp -indices (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) \
123             12223345556789999 \
124             foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
125             $f6 $f7 $f8 $f9
126 } {1 {0 16} {0 0} {1 3} {4 5} {6 6} {7 9} {10 10} {11 11} {12 12} {13 16}}
127 test regexp-3.6 {getting substrings back from regexp} {
128     set foo 2; set f2 2; set f3 2; set f4 2
129     list [regexp -indices (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
130 } {1 {1 1} {1 1} {-1 -1} {-1 -1}}
131 test regexp-3.7 {getting substrings back from regexp} {
132     set foo 1; set f2 1; set f3 1; set f4 1
133     list [regexp -indices (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
134 } {1 {1 2} {1 1} {-1 -1} {2 2}}
136 test regexp-4.1 {-nocase option to regexp} {
137     regexp -nocase foo abcFOo
138 } 1
139 test regexp-4.2 {-nocase option to regexp} {
140     set f1 22
141     set f2 33
142     set f3 44
143     list [regexp -nocase {a(b*)([xy]*)z} aBbbxYXxxZ22 f1 f2 f3] $f1 $f2 $f3
144 } {1 aBbbxYXxxZ Bbb xYXxx}
145 test regexp-4.3 {-nocase option to regexp} {
146     regexp -nocase FOo abcFOo
147 } 1
148 set x abcdefghijklmnopqrstuvwxyz1234567890
149 set x $x$x$x$x$x$x$x$x$x$x$x$x
150 test regexp-4.4 {case conversion in regexp} {
151     list [regexp -nocase $x $x foo] $foo
152 } "1 $x"
153 catch {unset x}
155 test regexp-5.1 {exercise cache of compiled expressions} {
156     regexp .*a b
157     regexp .*b c
158     regexp .*c d
159     regexp .*d e
160     regexp .*e f
161     regexp .*a bbba
162 } 1
163 test regexp-5.2 {exercise cache of compiled expressions} {
164     regexp .*a b
165     regexp .*b c
166     regexp .*c d
167     regexp .*d e
168     regexp .*e f
169     regexp .*b xxxb
170 } 1
171 test regexp-5.3 {exercise cache of compiled expressions} {
172     regexp .*a b
173     regexp .*b c
174     regexp .*c d
175     regexp .*d e
176     regexp .*e f
177     regexp .*c yyyc
178 } 1
179 test regexp-5.4 {exercise cache of compiled expressions} {
180     regexp .*a b
181     regexp .*b c
182     regexp .*c d
183     regexp .*d e
184     regexp .*e f
185     regexp .*d 1d
186 } 1
187 test regexp-5.5 {exercise cache of compiled expressions} {
188     regexp .*a b
189     regexp .*b c
190     regexp .*c d
191     regexp .*d e
192     regexp .*e f
193     regexp .*e xe
194 } 1
196 test regexp-6.1 {regexp errors} {
197     list [catch {regexp a} msg] $msg
198 } {1 {wrong # args: should be "regexp ?-switch ...? exp string ?matchVar? ?subMatchVar ...?"}}
199 test regexp-6.2 {regexp errors} {
200     list [catch {regexp -nocase a} msg] $msg
201 } {1 {wrong # args: should be "regexp ?-switch ...? exp string ?matchVar? ?subMatchVar ...?"}}
202 test regexp-6.3 {regexp errors} jim {
203     list [catch {regexp -gorp a} msg] $msg
204 } {1 {bad switch "-gorp": must be --, -all, -indices, -inline, -line, -nocase, or -start}}
205 test regexp-6.4 {regexp errors} {
206     catch {regexp a( b} msg
207 } 1
208 test regexp-6.5 {regexp errors} regexp_are {
209     list [catch {regexp a) b} msg] [string match *parentheses* $msg]
210 } {1 1}
211 test regexp-6.6 {regexp errors} {
212     list [catch {regexp a a f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1 f1} msg] $msg
213 } {0 1}
214 test regexp-6.7 {regexp errors} {
215     list [catch {regexp (x)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.) xyzzy} msg] $msg
216 } {0 0}
217 test regexp-6.8 {regexp errors} jim {
218     catch {unset f1}
219     set f1 44
220     list [catch {regexp abc abc f1(f2)} msg] $msg
221 } {1 {can't set "f1(f2)": variable isn't array}}
222 test regexp-6.9 {regexp errors, -start bad int check} {
223     list [catch {regexp -start bogus {^$} {}} msg] $msg
224 } {1 {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}}
225 test regexp-6.10 {regexp errors, -start too few args} {
226     list [catch {regexp -all -start} msg] $msg
227 } {1 {wrong # args: should be "regexp ?-switch ...? exp string ?matchVar? ?subMatchVar ...?"}}
230 test regexp-7.1 {basic regsub operation} {
231     list [regsub aa+ xaxaaaxaa 111&222 foo] $foo
232 } {1 xax111aaa222xaa}
233 test regexp-7.2 {basic regsub operation} {
234     list [regsub aa+ aaaxaa &111 foo] $foo
235 } {1 aaa111xaa}
236 test regexp-7.3 {basic regsub operation} {
237     list [regsub aa+ xaxaaa 111& foo] $foo
238 } {1 xax111aaa}
239 test regexp-7.4 {basic regsub operation} {
240     list [regsub aa+ aaa 11&2&333 foo] $foo
241 } {1 11aaa2aaa333}
242 test regexp-7.5 {basic regsub operation} {
243     list [regsub aa+ xaxaaaxaa &2&333 foo] $foo
244 } {1 xaxaaa2aaa333xaa}
245 test regexp-7.6 {basic regsub operation} {
246     list [regsub aa+ xaxaaaxaa 1&22& foo] $foo
247 } {1 xax1aaa22aaaxaa}
248 test regexp-7.7 {basic regsub operation} {
249     list [regsub a(a+) xaxaaaxaa {1\122\1} foo] $foo
250 } {1 xax1aa22aaxaa}
251 test regexp-7.8 {basic regsub operation} {
252     list [regsub a(a+) xaxaaaxaa {1\\\122\1} foo] $foo
253 } "1 {xax1\\aa22aaxaa}"
254 test regexp-7.9 {basic regsub operation} {
255     list [regsub a(a+) xaxaaaxaa {1\\122\1} foo] $foo
256 } "1 {xax1\\122aaxaa}"
257 test regexp-7.10 {basic regsub operation} {
258     list [regsub a(a+) xaxaaaxaa {1\\&\1} foo] $foo
259 } "1 {xax1\\aaaaaxaa}"
260 test regexp-7.11 {basic regsub operation} {
261     list [regsub a(a+) xaxaaaxaa {1\&\1} foo] $foo
262 } {1 xax1&aaxaa}
263 test regexp-7.12 {basic regsub operation} {
264     list [regsub a(a+) xaxaaaxaa {\1\1\1\1&&} foo] $foo
265 } {1 xaxaaaaaaaaaaaaaaxaa}
266 test regexp-7.13 {basic regsub operation} {
267     set foo xxx
268     list [regsub abc xyz 111 foo] $foo
269 } {0 xyz}
270 test regexp-7.14 {basic regsub operation} {
271     set foo xxx
272     list [regsub ^ xyz "111 " foo] $foo
273 } {1 {111 xyz}}
274 test regexp-7.15 {basic regsub operation} {
275     set foo xxx
276     list [regsub -- -foo abc-foodef "111 " foo] $foo
277 } {1 {abc111 def}}
278 test regexp-7.16 {basic regsub operation} {
279     set foo xxx
280     list [regsub x "" y foo] $foo
281 } {0 {}}
282 #test regexp-7.17 {regsub utf compliance} {
283 #    # if not UTF-8 aware, result is "0 1"
284 #    set foo "xyz555ijka\u4e4ebpqr"
285 #    regsub a\u4e4eb xyza\u4e4ebijka\u4e4ebpqr 555 bar
286 #    list [string compare $foo $bar] [regexp 4 $bar]
287 #} {0 0}
289 test regexp-8.1 {case conversion in regsub} {
290     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
291 } {1 xaAAaAAay}
292 test regexp-8.2 {case conversion in regsub} {
293     list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
294 } {1 xaAAaAAay}
295 test regexp-8.3 {case conversion in regsub} {
296     set foo 123
297     list [regsub a(a+) xaAAaAAay & foo] $foo
298 } {0 xaAAaAAay}
299 test regexp-8.4 {case conversion in regsub} {
300     set foo 123
301     list [regsub -nocase a CaDE b foo] $foo
302 } {1 CbDE}
303 test regexp-8.5 {case conversion in regsub} {
304     set foo 123
305     list [regsub -nocase XYZ CxYzD b foo] $foo
306 } {1 CbD}
307 test regexp-8.6 {case conversion in regsub} {
308     set x abcdefghijklmnopqrstuvwxyz1234567890
309     set x $x$x$x$x$x$x$x$x$x$x$x$x
310     set foo 123
311     list [regsub -nocase $x $x b foo] $foo
312 } {1 b}
314 test regexp-9.1 {-all option to regsub} {
315     set foo 86
316     list [regsub -all x+ axxxbxxcxdx |&| foo] $foo
317 } {4 a|xxx|b|xx|c|x|d|x|}
318 test regexp-9.2 {-all option to regsub} {
319     set foo 86
320     list [regsub -nocase -all x+ aXxXbxxcXdx |&| foo] $foo
321 } {4 a|XxX|b|xx|c|X|d|x|}
322 test regexp-9.3 {-all option to regsub} {
323     set foo 86
324     list [regsub x+ axxxbxxcxdx |&| foo] $foo
325 } {1 a|xxx|bxxcxdx}
326 test regexp-9.4 {-all option to regsub} {
327     set foo 86
328     list [regsub -all bc axxxbxxcxdx |&| foo] $foo
329 } {0 axxxbxxcxdx}
330 test regexp-9.5 {-all option to regsub} {
331     set foo xxx
332     list [regsub -all node "node node more" yy foo] $foo
333 } {2 {yy yy more}}
334 test regexp-9.6 {-all option to regsub} {
335     set foo xxx
336     list [regsub -all ^ xxx 123 foo] $foo
337 } {1 123xxx}
339 test regexp-10.2 {newline sensitivity in regsub} {
340     set foo xxx
341     list [regsub -line {^a.*b$} "dabc\naxyb\n" 123 foo] $foo
342 } "1 {dabc\n123\n}"
343 test regexp-10.3 {newline sensitivity in regsub} {
344     set foo xxx
345     list [regsub -line {^a.*b$} "dabc\naxyb\nxb" 123 foo] $foo
346 } "1 {dabc\n123\nxb}"
347 #test regexp-10.4 {partial newline sensitivity in regsub} {
348 #    set foo xxx
349 #    list [regsub -lineanchor {^a.*b$} "da\naxyb\nxb" 123 foo] $foo
350 #} "1 {da\n123}"
351 #test regexp-10.5 {inverse partial newline sensitivity in regsub} {
352 #    set foo xxx
353 #    list [regsub -linestop {a.*b} "da\nbaxyb\nxb" 123 foo] $foo
354 #} "1 {da\nb123\nxb}"
356 test regexp-11.1 {regsub errors} {
357     list [catch {regsub a b} msg] $msg
358 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
359 test regexp-11.2 {regsub errors} {
360     list [catch {regsub -nocase a b} msg] $msg
361 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
362 test regexp-11.3 {regsub errors} {
363     list [catch {regsub -nocase -all a b} msg] $msg
364 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
365 test regexp-11.4 {regsub errors} {
366     list [catch {regsub a b c d e f} msg] $msg
367 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
368 test regexp-11.5 {regsub errors} jim {
369     list [catch {regsub -gorp a b c} msg] $msg
370 } {1 {bad switch "-gorp": must be --, -all, -line, -nocase, or -start}}
371 test regexp-11.6 {regsub errors} {
372     catch {regsub -nocase a( b c d} msg
373 } 1
374 test regexp-11.7 {regsub errors} jim {
375     catch {unset f1}
376     set f1 44
377     list [catch {regsub -nocase aaa aaa xxx f1(f2)} msg] $msg
378 } {1 {can't set "f1(f2)": variable isn't array}}
379 test regexp-11.8 {regsub errors, -start bad int check} {
380     list [catch {regsub -start bogus pattern string rep var} msg] $msg
381 } {1 {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}}
382 test regexp-11.9 {regsub without final variable name returns value} {
383     regsub b abaca X
384 } {aXaca}
385 test regexp-11.10 {regsub without final variable name returns value} {
386     regsub -all a abaca X
387 } {XbXcX}
389 test regexp-11.11 {regsub without final variable name returns value} {
390     regsub b(\[^d\]*)d abcdeabcfde {,&,\1,}
391 } {a,bcd,c,eabcfde}
392 test regexp-11.12 {regsub without final variable name returns value} {
393     regsub -all b(\[^d\]*)d abcdeabcfde {,&,\1,}
394 } {a,bcd,c,ea,bcfd,cf,e}
395 test regexp-11.13 {regsub errors, -start too few args} {
396     list [catch {regsub -all -nocase -nocase -start} msg] $msg
397 } {1 {wrong # args: should be "regsub ?-switch ...? exp string subSpec ?varName?"}}
400 # This test crashes on the Mac unless you increase the Stack Space to about 1
401 # Meg.  This is probably bigger than most users want... 
402 # 8.2.3 regexp reduced stack space requirements, but this should be
403 # tested again
404 test regexp-12.1 {Tcl_RegExpExec: large number of subexpressions} {
405     list [regexp (.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.) abcdefghijklmnopqrstuvwxyz all a b c d e f g h i j k l m n o p q r s t u v w x y z] $all $a $b $c $d $e $f $g $h $i $j $k $l $m $n $o $p $q $r $s $t $u $v $w $x $y $z
406 } {1 abcdefghijklmnopqrstuvwxyz a b c d e f g h i j k l m n o p q r s t u v w x y z}
408 test regexp-13.1 {regsub of a very large string} {
409     # This test is designed to stress the memory subsystem in order
410     # to catch Bug #933.  It only fails if the Tcl memory allocator
411     # is in use.
413     set line {BEGIN_TABLE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END_TABLE}
414     set filedata [string repeat $line 200]
415     for {set i 1} {$i<10} {incr i} {
416         regsub -all "BEGIN_TABLE " $filedata "" newfiledata
417     }
418     set x done
419 } {done}
421 test regexp-14.1 {CompileRegexp: regexp cache} {
422     regexp .*a b
423     regexp .*b c
424     regexp .*c d
425     regexp .*d e
426     regexp .*e f
427     set x .
428     append x *a
429     regexp $x bbba
430 } 1
431 test regexp-14.2 {CompileRegexp: regexp cache, different flags} {
432     regexp .*a b
433     regexp .*b c
434     regexp .*c d
435     regexp .*d e
436     regexp .*e f
437     set x .
438     append x *a
439     regexp -nocase $x bbba
440 } 1
442 test regexp-15.1 {regexp -start} regexp_are {
443     catch {unset x}
444     list [regexp -start -10 {\d} 1abc2de3 x] $x
445 } {1 1}
446 test regexp-15.2 {regexp -start} regexp_are {
447     catch {unset x}
448     list [regexp -start 2 {\d} 1abc2de3 x] $x
449 } {1 2}
450 test regexp-15.3 {regexp -start} regexp_are {
451     catch {unset x}
452     list [regexp -start 4 {\d} 1abc2de3 x] $x
453 } {1 2}
454 test regexp-15.4 {regexp -start} regexp_are {
455     catch {unset x}
456     list [regexp -start 5 {\d} 1abc2de3 x] $x
457 } {1 3}
458 test regexp-15.5 {regexp -start, over end of string} regexp_are {
459     catch {unset x}
460     list [regexp -start [string length 1abc2de3] {\d} 1abc2de3 x] [info exists x]
461 } {0 0}
462 test regexp-15.6 {regexp -start, loss of ^$ behavior} {
463     list [regexp -start 2 {^$} {}]
464 } {0}
465 test regexp-15.7 {regexp -start, double option} {
466     regexp -start 2 -start 0 a abc
467 } 1
468 test regexp-15.8 {regexp -start, double option} {
469     regexp -start 0 -start 2 a abc
470 } 0
471 test regexp-15.9 {regexp -start, end relative index} {
472     catch {unset x}
473     list [regexp -start end {\d} 1abc2de3 x] [info exists x]
474 } {0 0}
475 test regexp-15.10 {regexp -start, end relative index} regexp_are {
476     catch {unset x}
477     list [regexp -start end-1 {\d} 1abc2de3 x] [info exists x] $x
478 } {1 1 3}
480 test regexp-16.1 {regsub -start} regexp_are {
481     catch {unset x}
482     list [regsub -all -start 2 {\d} a1b2c3d4e5 {/&} x] $x
483 } {4 a1b/2c/3d/4e/5}
484 test regexp-16.2 {regsub -start} {
485     catch {unset x}
486     list [regsub -all -start -25 {z} hello {/&} x] $x
487 } {0 hello}
488 test regexp-16.3 {regsub -start} {
489     catch {unset x}
490     list [regsub -all -start 3 {z} hello {/&} x] $x
491 } {0 hello}
492 #test regexp-16.4 {regsub -start, \A behavior} {
493 #    set out {}
494 #    lappend out [regsub -start 0 -all {\A(\w)} {abcde} {/\1} x] $x
495 #    lappend out [regsub -start 2 -all {\A(\w)} {abcde} {/\1} x] $x
496 #} {5 /a/b/c/d/e 3 ab/c/d/e}
497 test regexp-16.5 {regsub -start, double option} {
498     list [regsub -start 2 -start 0 a abc c x] $x
499 } {1 cbc}
500 test regexp-16.6 {regsub -start, double option} {
501     list [regsub -start 0 -start 2 a abc c x] $x
502 } {0 abc}
503 test regexp-16.7 {regexp -start, end relative index} {
504     list [regsub -start end a aaa b x] $x
505 } {0 aaa}
506 test regexp-16.8 {regexp -start, end relative index} {
507     list [regsub -start end-1 a aaa b x] $x
508 } {1 aab}
510 test regexp-17.1 {regexp -inline} {
511     regexp -inline b ababa
512 } {b}
513 test regexp-17.2 {regexp -inline} {
514     regexp -inline (b) ababa
515 } {b b}
516 test regexp-17.3 {regexp -inline -indices} {
517     regexp -inline -indices (b) ababa
518 } {{1 1} {1 1}}
519 test regexp-17.4 {regexp -inline} regexp_are {
520     regexp -inline {\w(\d+)\w} "   hello 23 there456def "
521 } {e456d 456}
522 test regexp-17.5 {regexp -inline no matches} {
523     regexp -inline {\w(\d+)\w} ""
524 } {}
525 test regexp-17.6 {regexp -inline no matches} {
526     regexp -inline hello goodbye
527 } {}
528 test regexp-17.7 {regexp -inline, no matchvars allowed} {
529     list [catch {regexp -inline b abc match} msg] $msg
530 } {1 {regexp match variables not allowed when using -inline}}
531 test regexp-17.8 {\W, \S and \D} regexp_are {
532     regexp -inline {(\W+)(\D+)(\S+)} " #!word  abc 13123  "
533 } {{ #!word  abc 13123} { #!} {word  abc } 13123}
535 test regexp-18.1 {regexp -all} {
536     regexp -all b bbbbb
537 } {5}
538 test regexp-18.2 {regexp -all} {
539     regexp -all b abababbabaaaaaaaaaab
540 } {6}
541 test regexp-18.3 {regexp -all -inline} {
542     regexp -all -inline b abababbabaaaaaaaaaab
543 } {b b b b b b}
544 test regexp-18.4 {regexp -all -inline} regexp_are {
545     regexp -all -inline {\w(\w)} abcdefg
546 } {ab b cd d ef f}
547 test regexp-18.5 {regexp -all -inline} regexp_are {
548     regexp -all -inline {\w(\w)$} abcdefg
549 } {fg g}
550 test regexp-18.6 {regexp -all -inline} regexp_are {
551     regexp -all -inline {\d+} 10:20:30:40
552 } {10 20 30 40}
553 test regexp-18.7 {regexp -all -inline} {
554     list [catch {regexp -all -inline b abc match} msg] $msg
555 } {1 {regexp match variables not allowed when using -inline}}
556 test regexp-18.8 {regexp -all} {
557     # This should not cause an infinite loop
558     regexp -all -inline {a*} a
559 } {a}
560 test regexp-18.9 {regexp -all} {
561     # Yes, the expected result is {a {}}.  Here's why:
562     # Start at index 0; a* matches the "a" there then stops.
563     # Go to index 1; a* matches the lambda (or {}) there then stops.  Recall
564     #   that a* matches zero or more "a"'s; thus it matches the string "b", as
565     #   there are zero or more "a"'s there.
566     # Go to index 2; this is past the end of the string, so stop.
567     regexp -all -inline {a*} ab
568 } {a {}}
569 test regexp-18.10 {regexp -all} {
570     # Yes, the expected result is {a {} a}.  Here's why:
571     # Start at index 0; a* matches the "a" there then stops.
572     # Go to index 1; a* matches the lambda (or {}) there then stops.   Recall
573     #   that a* matches zero or more "a"'s; thus it matches the string "b", as
574     #   there are zero or more "a"'s there.
575     # Go to index 2; a* matches the "a" there then stops.
576     # Go to index 3; this is past the end of the string, so stop.
577     regexp -all -inline {a*} aba
578 } {a {} a}
579 test regexp-18.11 {regexp -all} {
580     regexp -all -inline {^a} aaaa
581 } {a}
582 test regexp-18.12 {regexp -all -inline -indices} {
583     regexp -all -inline -indices a(b(c)d|e(f)g)h abcdhaefgh
584 } {{0 4} {1 3} {2 2} {-1 -1} {5 9} {6 8} {-1 -1} {7 7}}
586 test regexp-19.1 {regsub null replacement} {
587     regsub -all {@} {@hel@lo@} "\0a\0" result
588     list $result [string length $result]
589 } "\0a\0hel\0a\0lo\0a\0 14"
591 #test regexp-20.1 {regsub shared object shimmering} {
592 #    # Bug #461322
593 #    set a abcdefghijklmnopqurstuvwxyz 
594 #    set b $a 
595 #    set c abcdefghijklmnopqurstuvwxyz0123456789 
596 #    regsub $a $c $b d 
597 #    list $d [string length $d] [string bytelength $d]
598 #} [list abcdefghijklmnopqurstuvwxyz0123456789 37 37]
599 #test regexp-20.2 {regsub shared object shimmering with -about} {
600 #    eval regexp -about abc
601 #} {0 {}}
603 test regexp-21.1 {regsub works with empty string} {
604     regsub -- ^ {} foo
605 } {foo}
607 test regexp-21.2 {regsub works with empty string} {
608     regsub -- \$ {} foo
609 } {foo}
611 test regexp-21.3 {regsub works with empty string offset} {
612     regsub -start 0 -- ^ {} foo
613 } {foo}
615 test regexp-21.4 {regsub works with empty string offset} {
616     regsub -start 0 -- \$ {} foo
617 } {foo}
619 test regexp-21.5 {regsub works with empty string offset} {
620     regsub -start 3 -- \$ {123} foo
621 } {123foo}
623 test regexp-21.6 {regexp works with empty string} {
624     regexp -- ^ {}
625 } {1}
627 test regexp-21.7 {regexp works with empty string} {
628     regexp -start 0 -- ^ {}
629 } {1}
631 test regexp-21.8 {regexp works with empty string offset} {
632     regexp -start 3 -- ^ {123}
633 } {0}
635 test regexp-21.9 {regexp works with empty string offset} {
636     regexp -start 3 -- \$ {123}
637 } {1}
639 #test regexp-21.10 {multiple matches handle newlines} {
640 #    regsub -all -lineanchor -- {^#[^\n]*\n} "#one\n#two\n#three\n" foo\n
641 #} "foo\nfoo\nfoo\n"
643 test regexp-21.11 {multiple matches handle newlines} {
644     regsub -all -line -- ^ "a\nb\nc" \#
645 } "\#a\n\#b\n\#c"
647 test regexp-21.12 {multiple matches handle newlines} {
648     regsub -all -line -- ^ "\n\n" \#
649 } "\#\n\#\n\#"
651 test regexp-21.13 {multiple matches handle newlines} {
652     regexp -all -inline -indices -line -- ^ "a\nb\nc"
653 } {{0 -1} {2 1} {4 3}}
655 test regexp-21.14 {Literal newline in pattern} {
656     regexp -all -inline "\n(\[ \t\]+)" "\n\t\t# This  is  a test"
657 } "{\n\t\t} {\t\t}"
659 test regexp-21.15 {Replace literal backslash} {
660     regsub -all <bs> {<bs>abc<bs>def} \\ value
661     set value
662 } "\\abc\\def"
664 # Tests resulting from bugs reported by users
665 test reg-31.1 {[[:xdigit:]] behaves correctly when followed by [[:space:]]} {
666     set str {2:::DebugWin32}
667     set re {([[:xdigit:]])([[:space:]]*)}
668     list [regexp $re $str match xdigit spaces] $match $xdigit $spaces
669     # Code used to produce {1 2:::DebugWin32 2 :::DebugWin32} !!!
670 } {1 2 2 {}}
672 testreport