1# Commands covered:  regexp, regsub
2#
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.
6#
7# Copyright © 1991-1993 The Regents of the University of California.
8# Copyright © 1998 Sun Microsystems, Inc.
9# Copyright © 1998-1999 Scriptics Corporation.
10#
11# See the file "license.terms" for information on usage and redistribution
12# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13
14if {"::tcltest" ni [namespace children]} {
15    package require tcltest 2.5
16    namespace import -force ::tcltest::*
17}
18
19unset -nocomplain foo
20
21testConstraint exec [llength [info commands exec]]
22testConstraint nodep [info exists tcl_precision]
23
24# Used for constraining memory leak tests
25testConstraint memory [llength [info commands memory]]
26if {[testConstraint memory]} {
27    proc memtest script {
28	set end [lindex [split [memory info] \n] 3 3]
29	for {set i 0} {$i < 5} {incr i} {
30	    uplevel 1 $script
31	    set tmp $end
32	    set end [lindex [split [memory info] \n] 3 3]
33	}
34	expr {$end - $tmp}
35    }
36}
37
38test regexp-1.1 {basic regexp operation} {
39    regexp ab*c abbbc
40} 1
41test regexp-1.2 {basic regexp operation} {
42    regexp ab*c ac
43} 1
44test regexp-1.3 {basic regexp operation} {
45    regexp ab*c ab
46} 0
47test regexp-1.4 {basic regexp operation} {
48    regexp -- -gorp abc-gorpxxx
49} 1
50test regexp-1.5 {basic regexp operation} {
51    regexp {^([^ ]*)[ ]*([^ ]*)} "" a
52} 1
53test regexp-1.6 {basic regexp operation} {
54    list [catch {regexp {} abc} msg] $msg
55} {0 1}
56test regexp-1.7 {regexp utf compliance} {
57    # if not UTF-8 aware, result is "0 1"
58    set foo "乎b q"
59    regexp "乎b q" "a乎b qw幎N wq" bar
60    list [string compare $foo $bar] [regexp 4 $bar]
61} {0 0}
62test regexp-1.8 {regexp ***= metasyntax} {
63    regexp -- "***=o" "aeiou"
64} 1
65test regexp-1.9 {regexp ***= metasyntax} {
66    set string "aeiou"
67    regexp -- "***=o" $string
68} 1
69test regexp-1.10 {regexp ***= metasyntax} {
70    set string "aeiou"
71    set re "***=o"
72    regexp -- $re $string
73} 1
74test regexp-1.11 {regexp ***= metasyntax} {
75    regexp -- "***=y" "aeiou"
76} 0
77test regexp-1.12 {regexp ***= metasyntax} {
78    set string "aeiou"
79    regexp -- "***=y" $string
80} 0
81test regexp-1.13 {regexp ***= metasyntax} {
82    set string "aeiou"
83    set re "***=y"
84    regexp -- $re $string
85} 0
86
87test regexp-2.1 {getting substrings back from regexp} {
88    set foo {}
89    list [regexp ab*c abbbbc foo] $foo
90} {1 abbbbc}
91test regexp-2.2 {getting substrings back from regexp} {
92    set foo {}
93    set f2 {}
94    list [regexp a(b*)c abbbbc foo f2] $foo $f2
95} {1 abbbbc bbbb}
96test regexp-2.3 {getting substrings back from regexp} {
97    set foo {}
98    set f2 {}
99    list [regexp a(b*)(c) abbbbc foo f2] $foo $f2
100} {1 abbbbc bbbb}
101test regexp-2.4 {getting substrings back from regexp} {
102    set foo {}
103    set f2 {}
104    set f3 {}
105    list [regexp a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
106} {1 abbbbc bbbb c}
107test regexp-2.5 {getting substrings back from regexp} {
108    set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
109    set f6 {}; set f7 {}; set f8 {}; set f9 {}; set fa {}; set fb {};
110    list [regexp (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*)(a*)(b*) \
111	      12223345556789999aabbb \
112	    foo f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb] $foo $f1 $f2 $f3 $f4 $f5 \
113	    $f6 $f7 $f8 $f9 $fa $fb
114} {1 12223345556789999aabbb 1 222 33 4 555 6 7 8 9999 aa bbb}
115test regexp-2.6 {getting substrings back from regexp} {
116    set foo 2; set f2 2; set f3 2; set f4 2
117    list [regexp (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
118} {1 a a {} {}}
119test regexp-2.7 {getting substrings back from regexp} {
120    set foo 1; set f2 1; set f3 1; set f4 1
121    list [regexp (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
122} {1 ac a {} c}
123test regexp-2.8 {getting substrings back from regexp} {
124    set match {}
125    list [regexp {^a*b} aaaab match] $match
126} {1 aaaab}
127test regexp-2.9 {getting substrings back from regexp} {
128    set foo {}
129    set f2 {}
130    list [regexp f\352te(b*)c f\352tebbbbc foo f2] $foo $f2
131} [list 1 f\352tebbbbc bbbb]
132test regexp-2.10 {getting substrings back from regexp} {
133    set foo {}
134    set f2 {}
135    list [regexp f\352te(b*)c eff\352tebbbbc foo f2] $foo $f2
136} [list 1 f\352tebbbbc bbbb]
137test regexp-2.11 {non-capturing subgroup} {
138    set foo {}
139    set f2 {}
140    list [regexp {str(?:a+)} straa foo f2] $foo $f2
141} [list 1 straa {}]
142test regexp-2.12 {non-capturing subgroup with -inline} {
143    regexp -inline {str(?:a+)} straa
144} {straa}
145test regexp-2.13 {non-capturing and capturing subgroups} {
146    set foo {}
147    set f2 {}
148    set f3 {}
149    list [regexp {str(?:a+)(c+)} straacc foo f2 f3] $foo $f2 $f3
150} [list 1 straacc cc {}]
151test regexp-2.14 {non-capturing and capturing subgroups} {
152    regexp -inline {str(?:a+)(c+)} straacc
153} {straacc cc}
154test regexp-2.15 {getting substrings back from regexp} {
155    set foo NA
156    set f2 NA
157    list [regexp {str(?:a+)} straa foo f2] $foo $f2
158} [list 1 straa {}]
159
160test regexp-3.1 {-indices option to regexp} {
161    set foo {}
162    list [regexp -indices ab*c abbbbc foo] $foo
163} {1 {0 5}}
164test regexp-3.2 {-indices option to regexp} {
165    set foo {}
166    set f2 {}
167    list [regexp -indices a(b*)c abbbbc foo f2] $foo $f2
168} {1 {0 5} {1 4}}
169test regexp-3.3 {-indices option to regexp} {
170    set foo {}
171    set f2 {}
172    list [regexp -indices a(b*)(c) abbbbc foo f2] $foo $f2
173} {1 {0 5} {1 4}}
174test regexp-3.4 {-indices option to regexp} {
175    set foo {}
176    set f2 {}
177    set f3 {}
178    list [regexp -indices a(b*)(c) abbbbc foo f2 f3] $foo $f2 $f3
179} {1 {0 5} {1 4} {5 5}}
180test regexp-3.5 {-indices option to regexp} {
181    set foo {}; set f1 {}; set f2 {}; set f3 {}; set f4 {}; set f5 {};
182    set f6 {}; set f7 {}; set f8 {}; set f9 {}
183    list [regexp -indices (1*)(2*)(3*)(4*)(5*)(6*)(7*)(8*)(9*) \
184	    12223345556789999 \
185	    foo f1 f2 f3 f4 f5 f6 f7 f8 f9] $foo $f1 $f2 $f3 $f4 $f5 \
186	    $f6 $f7 $f8 $f9
187} {1 {0 16} {0 0} {1 3} {4 5} {6 6} {7 9} {10 10} {11 11} {12 12} {13 16}}
188test regexp-3.6 {getting substrings back from regexp} {
189    set foo 2; set f2 2; set f3 2; set f4 2
190    list [regexp -indices (a)(b)? xay foo f2 f3 f4] $foo $f2 $f3 $f4
191} {1 {1 1} {1 1} {-1 -1} {-1 -1}}
192test regexp-3.7 {getting substrings back from regexp} {
193    set foo 1; set f2 1; set f3 1; set f4 1
194    list [regexp -indices (a)(b)?(c) xacy foo f2 f3 f4] $foo $f2 $f3 $f4
195} {1 {1 2} {1 1} {-1 -1} {2 2}}
196test regexp-3.8a {-indices by multi-byte utf-8} {
197    regexp -inline -indices {(\w+)-(\w+)} \
198	"grüß-привет"
199} {{0 10} {0 3} {5 10}}
200test regexp-3.8b {-indices by multi-byte utf-8, from -start position} {
201    list\
202	[regexp -inline -indices -start 3 {(\w+)-(\w+)} \
203	"grüß-привет"] \
204	[regexp -inline -indices -start 4 {(\w+)-(\w+)} \
205	"grüß-привет"]
206} {{{3 10} {3 3} {5 10}} {}}
207
208test regexp-4.1 {-nocase option to regexp} {
209    regexp -nocase foo abcFOo
210} 1
211test regexp-4.2 {-nocase option to regexp} {
212    set f1 22
213    set f2 33
214    set f3 44
215    list [regexp -nocase {a(b*)([xy]*)z} aBbbxYXxxZ22 f1 f2 f3] $f1 $f2 $f3
216} {1 aBbbxYXxxZ Bbb xYXxx}
217test regexp-4.3 {-nocase option to regexp} {
218    regexp -nocase FOo abcFOo
219} 1
220set x abcdefghijklmnopqrstuvwxyz1234567890
221set x $x$x$x$x$x$x$x$x$x$x$x$x
222test regexp-4.4 {case conversion in regexp} {
223    list [regexp -nocase $x $x foo] $foo
224} "1 $x"
225unset -nocomplain x
226
227test regexp-5.1 {exercise cache of compiled expressions} {
228    regexp .*a b
229    regexp .*b c
230    regexp .*c d
231    regexp .*d e
232    regexp .*e f
233    regexp .*a bbba
234} 1
235test regexp-5.2 {exercise cache of compiled expressions} {
236    regexp .*a b
237    regexp .*b c
238    regexp .*c d
239    regexp .*d e
240    regexp .*e f
241    regexp .*b xxxb
242} 1
243test regexp-5.3 {exercise cache of compiled expressions} {
244    regexp .*a b
245    regexp .*b c
246    regexp .*c d
247    regexp .*d e
248    regexp .*e f
249    regexp .*c yyyc
250} 1
251test regexp-5.4 {exercise cache of compiled expressions} {
252    regexp .*a b
253    regexp .*b c
254    regexp .*c d
255    regexp .*d e
256    regexp .*e f
257    regexp .*d 1d
258} 1
259test regexp-5.5 {exercise cache of compiled expressions} {
260    regexp .*a b
261    regexp .*b c
262    regexp .*c d
263    regexp .*d e
264    regexp .*e f
265    regexp .*e xe
266} 1
267
268test regexp-6.1 {regexp errors} {
269    list [catch {regexp a} msg] $msg
270} {1 {wrong # args: should be "regexp ?-option ...? exp string ?matchVar? ?subMatchVar ...?"}}
271test regexp-6.2 {regexp errors} {
272    list [catch {regexp -nocase a} msg] $msg
273} {1 {wrong # args: should be "regexp ?-option ...? exp string ?matchVar? ?subMatchVar ...?"}}
274test regexp-6.3 {regexp errors} {
275    list [catch {regexp -gorp a} msg] $msg
276} {1 {bad option "-gorp": must be -all, -about, -indices, -inline, -expanded, -line, -linestop, -lineanchor, -nocase, -start, or --}}
277test regexp-6.4 {regexp errors} {
278    list [catch {regexp a( b} msg] $msg
279} {1 {couldn't compile regular expression pattern: parentheses () not balanced}}
280test regexp-6.5 {regexp errors} {
281    list [catch {regexp a( b} msg] $msg
282} {1 {couldn't compile regular expression pattern: parentheses () not balanced}}
283test regexp-6.6 {regexp errors} {
284    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
285} {0 1}
286test regexp-6.7 {regexp errors} {
287    list [catch {regexp (x)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.) xyzzy} msg] $msg
288} {0 0}
289test regexp-6.8 {regexp errors} -setup {
290    unset -nocomplain f1
291} -body {
292    set f1 44
293    regexp abc abc f1(f2)
294} -returnCodes error -result {can't set "f1(f2)": variable isn't array}
295test regexp-6.9 {regexp errors, -start bad int check} {
296    list [catch {regexp -start bogus {^$} {}} msg] $msg
297} {1 {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}}
298test regexp-6.10 {regexp errors} {
299    list [catch {regexp {a[} b} msg] $msg
300} {1 {couldn't compile regular expression pattern: brackets [] not balanced}}
301
302test regexp-7.1 {basic regsub operation} {
303    list [regsub aa+ xaxaaaxaa 111&222 foo] $foo
304} {1 xax111aaa222xaa}
305test regexp-7.2 {basic regsub operation} {
306    list [regsub aa+ aaaxaa &111 foo] $foo
307} {1 aaa111xaa}
308test regexp-7.3 {basic regsub operation} {
309    list [regsub aa+ xaxaaa 111& foo] $foo
310} {1 xax111aaa}
311test regexp-7.4 {basic regsub operation} {
312    list [regsub aa+ aaa 11&2&333 foo] $foo
313} {1 11aaa2aaa333}
314test regexp-7.5 {basic regsub operation} {
315    list [regsub aa+ xaxaaaxaa &2&333 foo] $foo
316} {1 xaxaaa2aaa333xaa}
317test regexp-7.6 {basic regsub operation} {
318    list [regsub aa+ xaxaaaxaa 1&22& foo] $foo
319} {1 xax1aaa22aaaxaa}
320test regexp-7.7 {basic regsub operation} {
321    list [regsub a(a+) xaxaaaxaa {1\122\1} foo] $foo
322} {1 xax1aa22aaxaa}
323test regexp-7.8 {basic regsub operation} {
324    list [regsub a(a+) xaxaaaxaa {1\\\122\1} foo] $foo
325} "1 {xax1\\aa22aaxaa}"
326test regexp-7.9 {basic regsub operation} {
327    list [regsub a(a+) xaxaaaxaa {1\\122\1} foo] $foo
328} "1 {xax1\\122aaxaa}"
329test regexp-7.10 {basic regsub operation} {
330    list [regsub a(a+) xaxaaaxaa {1\\&\1} foo] $foo
331} "1 {xax1\\aaaaaxaa}"
332test regexp-7.11 {basic regsub operation} {
333    list [regsub a(a+) xaxaaaxaa {1\&\1} foo] $foo
334} {1 xax1&aaxaa}
335test regexp-7.12 {basic regsub operation} {
336    list [regsub a(a+) xaxaaaxaa {\1\1\1\1&&} foo] $foo
337} {1 xaxaaaaaaaaaaaaaaxaa}
338test regexp-7.13 {basic regsub operation} {
339    set foo xxx
340    list [regsub abc xyz 111 foo] $foo
341} {0 xyz}
342test regexp-7.14 {basic regsub operation} {
343    set foo xxx
344    list [regsub ^ xyz "111 " foo] $foo
345} {1 {111 xyz}}
346test regexp-7.15 {basic regsub operation} {
347    set foo xxx
348    list [regsub -- -foo abc-foodef "111 " foo] $foo
349} {1 {abc111 def}}
350test regexp-7.16 {basic regsub operation} {
351    set foo xxx
352    list [regsub x "" y foo] $foo
353} {0 {}}
354test regexp-7.17 {regsub utf compliance} {
355    # if not UTF-8 aware, result is "0 1"
356    set foo "xyz555ijka乎bpqr"
357    regsub a乎b xyza乎bijka乎bpqr 555 bar
358    list [string compare $foo $bar] [regexp 4 $bar]
359} {0 0}
360test regexp-7.18 {basic regsub replacement} {
361    list [regsub a+ aaa {&} foo] $foo
362} {1 aaa}
363test regexp-7.19 {basic regsub replacement} {
364    list [regsub a+ aaa {\&} foo] $foo
365} {1 &}
366test regexp-7.20 {basic regsub replacement} {
367    list [regsub a+ aaa {\\&} foo] $foo
368} {1 {\aaa}}
369test regexp-7.21 {basic regsub replacement} {
370    list [regsub a+ aaa {\\\&} foo] $foo
371} {1 {\&}}
372test regexp-7.22 {basic regsub replacement} {
373    list [regsub a+ aaa {\0} foo] $foo
374} {1 aaa}
375test regexp-7.23 {basic regsub replacement} {
376    list [regsub a+ aaa {\\0} foo] $foo
377} {1 {\0}}
378test regexp-7.24 {basic regsub replacement} {
379    list [regsub a+ aaa {\\\0} foo] $foo
380} {1 {\aaa}}
381test regexp-7.25 {basic regsub replacement} {
382    list [regsub a+ aaa {\\\\0} foo] $foo
383} {1 {\\0}}
384test regexp-7.26 {dollar zero is not a backslash replacement} {
385    list [regsub a+ aaa {$0} foo] $foo
386} {1 {$0}}
387test regexp-7.27 {dollar zero is not a backslash replacement} {
388    list [regsub a+ aaa {\0$0} foo] $foo
389} {1 {aaa$0}}
390test regexp-7.28 {dollar zero is not a backslash replacement} {
391    list [regsub a+ aaa {\$0} foo] $foo
392} {1 {\$0}}
393test regexp-7.29 {dollar zero is not a backslash replacement} {
394    list [regsub a+ aaa {\\} foo] $foo
395} {1 \\}
396
397test regexp-8.1 {case conversion in regsub} {
398    list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
399} {1 xaAAaAAay}
400test regexp-8.2 {case conversion in regsub} {
401    list [regsub -nocase a(a+) xaAAaAAay & foo] $foo
402} {1 xaAAaAAay}
403test regexp-8.3 {case conversion in regsub} {
404    set foo 123
405    list [regsub a(a+) xaAAaAAay & foo] $foo
406} {0 xaAAaAAay}
407test regexp-8.4 {case conversion in regsub} {
408    set foo 123
409    list [regsub -nocase a CaDE b foo] $foo
410} {1 CbDE}
411test regexp-8.5 {case conversion in regsub} {
412    set foo 123
413    list [regsub -nocase XYZ CxYzD b foo] $foo
414} {1 CbD}
415test regexp-8.6 {case conversion in regsub} {
416    set x abcdefghijklmnopqrstuvwxyz1234567890
417    set x $x$x$x$x$x$x$x$x$x$x$x$x
418    set foo 123
419    list [regsub -nocase $x $x b foo] $foo
420} {1 b}
421
422test regexp-9.1 {-all option to regsub} {
423    set foo 86
424    list [regsub -all x+ axxxbxxcxdx |&| foo] $foo
425} {4 a|xxx|b|xx|c|x|d|x|}
426test regexp-9.2 {-all option to regsub} {
427    set foo 86
428    list [regsub -nocase -all x+ aXxXbxxcXdx |&| foo] $foo
429} {4 a|XxX|b|xx|c|X|d|x|}
430test regexp-9.3 {-all option to regsub} {
431    set foo 86
432    list [regsub x+ axxxbxxcxdx |&| foo] $foo
433} {1 a|xxx|bxxcxdx}
434test regexp-9.4 {-all option to regsub} {
435    set foo 86
436    list [regsub -all bc axxxbxxcxdx |&| foo] $foo
437} {0 axxxbxxcxdx}
438test regexp-9.5 {-all option to regsub} {
439    set foo xxx
440    list [regsub -all node "node node more" yy foo] $foo
441} {2 {yy yy more}}
442test regexp-9.6 {-all option to regsub} {
443    set foo xxx
444    list [regsub -all ^ xxx 123 foo] $foo
445} {1 123xxx}
446
447test regexp-10.1 {expanded syntax in regsub} {
448    set foo xxx
449    list [regsub -expanded ". \#comment\n  . \#comment2" abc def foo] $foo
450} {1 defc}
451test regexp-10.2 {newline sensitivity in regsub} {
452    set foo xxx
453    list [regsub -line {^a.*b$} "dabc\naxyb\n" 123 foo] $foo
454} "1 {dabc\n123\n}"
455test regexp-10.3 {newline sensitivity in regsub} {
456    set foo xxx
457    list [regsub -line {^a.*b$} "dabc\naxyb\nxb" 123 foo] $foo
458} "1 {dabc\n123\nxb}"
459test regexp-10.4 {partial newline sensitivity in regsub} {
460    set foo xxx
461    list [regsub -lineanchor {^a.*b$} "da\naxyb\nxb" 123 foo] $foo
462} "1 {da\n123}"
463test regexp-10.5 {inverse partial newline sensitivity in regsub} {
464    set foo xxx
465    list [regsub -linestop {a.*b} "da\nbaxyb\nxb" 123 foo] $foo
466} "1 {da\nb123\nxb}"
467
468test regexp-11.1 {regsub errors} {
469    list [catch {regsub a b} msg] $msg
470} {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}}
471test regexp-11.2 {regsub errors} {
472    list [catch {regsub -nocase a b} msg] $msg
473} {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}}
474test regexp-11.3 {regsub errors} {
475    list [catch {regsub -nocase -all a b} msg] $msg
476} {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}}
477test regexp-11.4 {regsub errors} {
478    list [catch {regsub a b c d e f} msg] $msg
479} {1 {wrong # args: should be "regsub ?-option ...? exp string subSpec ?varName?"}}
480test regexp-11.5 {regsub errors} {
481    list [catch {regsub -gorp a b c} msg] $msg
482} {1 {bad option "-gorp": must be -all, -command, -expanded, -line, -linestop, -lineanchor, -nocase, -start, or --}}
483test regexp-11.6 {regsub errors} {
484    list [catch {regsub -nocase a( b c d} msg] $msg
485} {1 {couldn't compile regular expression pattern: parentheses () not balanced}}
486test regexp-11.7 {regsub errors} -setup {
487    unset -nocomplain f1
488} -body {
489    set f1 44
490    regsub -nocase aaa aaa xxx f1(f2)
491} -returnCodes error -result {can't set "f1(f2)": variable isn't array}
492test regexp-11.8 {regsub errors, -start bad int check} {
493    list [catch {regsub -start bogus pattern string rep var} msg] $msg
494} {1 {bad index "bogus": must be integer?[+-]integer? or end?[+-]integer?}}
495test regexp-11.9 {regsub without final variable name returns value} {
496    regsub b abaca X
497} {aXaca}
498test regexp-11.10 {regsub without final variable name returns value} {
499    regsub -all a abaca X
500} {XbXcX}
501test regexp-11.11 {regsub without final variable name returns value} {
502    regsub b(.*?)d abcdeabcfde {,&,\1,}
503} {a,bcd,c,eabcfde}
504test regexp-11.12 {regsub without final variable name returns value} {
505    regsub -all b(.*?)d abcdeabcfde {,&,\1,}
506} {a,bcd,c,ea,bcfd,cf,e}
507
508# This test crashes on the Mac unless you increase the Stack Space to about 1
509# Meg.  This is probably bigger than most users want...
510# 8.2.3 regexp reduced stack space requirements, but this should be
511# tested again
512test regexp-12.1 {Tcl_RegExpExec: large number of subexpressions} {macCrash} {
513    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
514} {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}
515
516test regexp-13.1 {regsub of a very large string} {
517    # This test is designed to stress the memory subsystem in order to catch
518    # Bug #933.  It only fails if the Tcl memory allocator is in use.
519    set line {BEGIN_TABLE ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END_TABLE}
520    set filedata [string repeat $line 200]
521    for {set i 1} {$i<10} {incr i} {
522	regsub -all "BEGIN_TABLE " $filedata "" newfiledata
523    }
524    set x done
525} {done}
526
527test regexp-14.1 {CompileRegexp: regexp cache} {
528    regexp .*a b
529    regexp .*b c
530    regexp .*c d
531    regexp .*d e
532    regexp .*e f
533    set x .
534    append x *a
535    regexp $x bbba
536} 1
537test regexp-14.2 {CompileRegexp: regexp cache, different flags} {
538    regexp .*a b
539    regexp .*b c
540    regexp .*c d
541    regexp .*d e
542    regexp .*e f
543    set x .
544    append x *a
545    regexp -nocase $x bbba
546} 1
547test regexp-14.3 {CompileRegexp: regexp cache, empty regexp and empty cache} -constraints {
548    exec
549} -setup {
550    set junk [makeFile {puts [regexp {} foo]} junk.tcl]
551} -body {
552    exec [interpreter] $junk
553} -cleanup {
554    removeFile junk.tcl
555} -result 1
556
557test regexp-15.1 {regexp -start} -body {
558    unset -nocomplain x
559    list [regexp -start -10 {\d} 1abc2de3 x] $x
560} -result {1 1}
561test regexp-15.2 {regexp -start} -body {
562    unset -nocomplain x
563    list [regexp -start 2 {\d} 1abc2de3 x] $x
564} -result {1 2}
565test regexp-15.3 {regexp -start} -body {
566    unset -nocomplain x
567    list [regexp -start 4 {\d} 1abc2de3 x] $x
568} -result {1 2}
569test regexp-15.4 {regexp -start} -body {
570    unset -nocomplain x
571    list [regexp -start 5 {\d} 1abc2de3 x] $x
572} -result {1 3}
573test regexp-15.5 {regexp -start, over end of string} -body {
574    unset -nocomplain x
575    list [regexp -start [string length 1abc2de3] {\d} 1abc2de3 x] [info exists x]
576} -result {0 0}
577test regexp-15.6 {regexp -start, loss of ^$ behavior} -body {
578    list [regexp -start 2 {^$} {}]
579} -result {0}
580test regexp-15.7 {regexp -start, double option} -body {
581    regexp -start 2 -start 0 a abc
582} -result 1
583test regexp-15.8 {regexp -start, double option} -body {
584    regexp -start 0 -start 2 a abc
585} -result 0
586test regexp-15.9 {regexp -start, end relative index} -body {
587    unset -nocomplain x
588    list [regexp -start end {\d} 1abc2de3 x] [info exists x]
589} -result {0 0}
590test regexp-15.10 {regexp -start, end relative index} -body {
591    unset -nocomplain x
592    list [regexp -start end-1 {\d} 1abc2de3 x] [info exists x] $x
593} -result {1 1 3}
594test regexp-15.11 {regexp -start, over end of string} -body {
595    set x NA
596    list [regexp -start 2 {.*} ab x] $x
597} -result {1 {}}
598
599test regexp-16.1 {regsub -start} -body {
600    unset -nocomplain x
601    list [regsub -all -start 2 {\d} a1b2c3d4e5 {/&} x] $x
602} -result {4 a1b/2c/3d/4e/5}
603test regexp-16.2 {regsub -start} -body {
604    unset -nocomplain x
605    list [regsub -all -start -25 {z} hello {/&} x] $x
606} -result {0 hello}
607test regexp-16.3 {regsub -start} -body {
608    unset -nocomplain x
609    list [regsub -all -start 3 {z} hello {/&} x] $x
610} -result {0 hello}
611test regexp-16.4 {regsub -start, \A behavior} -body {
612    set out {}
613    lappend out [regsub -start 0 -all {\A(\w)} {abcde} {/\1} x] $x
614    lappend out [regsub -start 2 -all {\A(\w)} {abcde} {/\1} x] $x
615} -result {5 /a/b/c/d/e 3 ab/c/d/e}
616test regexp-16.5 {regsub -start, double option} -body {
617    list [regsub -start 2 -start 0 a abc c x] $x
618} -result {1 cbc}
619test regexp-16.6 {regsub -start, double option} -body {
620    list [regsub -start 0 -start 2 a abc c x] $x
621} -result {0 abc}
622test regexp-16.7 {regexp -start, end relative index} -body {
623    list [regsub -start end a aaa b x] $x
624} -result {0 aaa}
625test regexp-16.8 {regexp -start, end relative index} -body {
626    list [regsub -start end-1 a aaa b x] $x
627} -result {1 aab}
628test regexp-16.9 {regsub -start and -all} -body {
629    set foo {}
630    list [regsub -start 0 -all x+ axxxbxx |&| foo] $foo
631} -result {2 a|xxx|b|xx|}
632test regexp-16.10 {regsub -start and -all} -body {
633    set foo {}
634    list [regsub -start 1 -all x+ axxxbxx |&| foo] $foo
635} -result {2 a|xxx|b|xx|}
636test regexp-16.11 {regsub -start and -all} -body {
637    set foo {}
638    list [regsub -start 4 -all x+ axxxbxx |&| foo] $foo
639} -result {1 axxxb|xx|}
640test regexp-16.12 {regsub -start} -body {
641    set foo {}
642    list [regsub -start 4 x+ axxxbxx |&| foo] $foo
643} -result {1 axxxb|xx|}
644test regexp-16.13 {regsub -start and -all} -body {
645    set foo {}
646    list [regsub -start 1 -all a+ "" & foo] $foo
647} -result {0 {}}
648test regexp-16.14 {regsub -start} -body {
649    set foo {}
650    list [regsub -start 1 a+ "" & foo] $foo
651} -result {0 {}}
652test regexp-16.15 {regsub -start and -all} -body {
653    set foo {}
654    list [regsub -start 2 -all a+ "xy" & foo] $foo
655} -result {0 xy}
656test regexp-16.16 {regsub -start} -body {
657    set foo {}
658    list [regsub -start 2 a+ "xy" & foo] $foo
659} -result {0 xy}
660test regexp-16.17 {regsub -start and -all} -body {
661    set foo {}
662    list [regsub -start 1 -all y+ "xy" & foo] $foo
663} -result {1 xy}
664test regexp-16.18 {regsub -start} -body {
665    set foo {}
666    list [regsub -start 1 y+ "xy" & foo] $foo
667} -result {1 xy}
668test regexp-16.19 {regsub -start} -body {
669    set foo {}
670    list [regsub -start -1 a+ "" & foo] $foo
671} -result {0 {}}
672test regexp-16.20 {regsub -start, loss of ^$ behavior} -body {
673    set foo NA
674    list [regsub -start 1 {^$} {} & foo] $foo
675} -result {0 {}}
676test regexp-16.21 {regsub -start, loss of ^$ behavior} -body {
677    set foo NA
678    list [regsub -start 1 {^.*$} abc & foo] $foo
679} -result {0 abc}
680test regexp-16.22 {regsub -start, loss of ^$ behavior} -body {
681    set foo NA
682    list [regsub -all -start 1 {^.*$} abc & foo] $foo
683} -result {0 abc}
684
685test regexp-17.1 {regexp -inline} {
686    regexp -inline b ababa
687} {b}
688test regexp-17.2 {regexp -inline} {
689    regexp -inline (b) ababa
690} {b b}
691test regexp-17.3 {regexp -inline -indices} {
692    regexp -inline -indices (b) ababa
693} {{1 1} {1 1}}
694test regexp-17.4 {regexp -inline} {
695    regexp -inline {\w(\d+)\w} "   hello 23 there456def "
696} {e456d 456}
697test regexp-17.5 {regexp -inline no matches} {
698    regexp -inline {\w(\d+)\w} ""
699} {}
700test regexp-17.6 {regexp -inline no matches} {
701    regexp -inline hello goodbye
702} {}
703test regexp-17.7 {regexp -inline, no matchvars allowed} {
704    list [catch {regexp -inline b abc match} msg] $msg
705} {1 {regexp match variables not allowed when using -inline}}
706
707test regexp-18.1 {regexp -all} {
708    regexp -all b bbbbb
709} {5}
710test regexp-18.2 {regexp -all} {
711    regexp -all b abababbabaaaaaaaaaab
712} {6}
713test regexp-18.3 {regexp -all -inline} {
714    regexp -all -inline b abababbabaaaaaaaaaab
715} {b b b b b b}
716test regexp-18.4 {regexp -all -inline} {
717    regexp -all -inline {\w(\w)} abcdefg
718} {ab b cd d ef f}
719test regexp-18.5 {regexp -all -inline} {
720    regexp -all -inline {\w(\w)$} abcdefg
721} {fg g}
722test regexp-18.6 {regexp -all -inline} {
723    regexp -all -inline {\d+} 10:20:30:40
724} {10 20 30 40}
725test regexp-18.7 {regexp -all -inline} {
726    list [catch {regexp -all -inline b abc match} msg] $msg
727} {1 {regexp match variables not allowed when using -inline}}
728test regexp-18.8 {regexp -all} {
729    # This should not cause an infinite loop
730    regexp -all -inline {a*} a
731} {a}
732test regexp-18.9 {regexp -all} {
733    # Yes, the expected result is {a {}}.  Here's why:
734    # Start at index 0; a* matches the "a" there then stops.
735    # Go to index 1; a* matches the lambda (or {}) there then stops.  Recall
736    #   that a* matches zero or more "a"'s; thus it matches the string "b", as
737    #   there are zero or more "a"'s there.
738    # Go to index 2; this is past the end of the string, so stop.
739    regexp -all -inline {a*} ab
740} {a {}}
741test regexp-18.10 {regexp -all} {
742    # Yes, the expected result is {a {} a}.  Here's why:
743    # Start at index 0; a* matches the "a" there then stops.
744    # Go to index 1; a* matches the lambda (or {}) there then stops.   Recall
745    #   that a* matches zero or more "a"'s; thus it matches the string "b", as
746    #   there are zero or more "a"'s there.
747    # Go to index 2; a* matches the "a" there then stops.
748    # Go to index 3; this is past the end of the string, so stop.
749    regexp -all -inline {a*} aba
750} {a {} a}
751test regexp-18.11 {regexp -all} {
752    regexp -all -inline {^a} aaaa
753} {a}
754test regexp-18.12 {regexp -all -inline -indices} {
755    regexp -all -inline -indices a(b(c)d|e(f)g)h abcdhaefgh
756} {{0 4} {1 3} {2 2} {-1 -1} {5 9} {6 8} {-1 -1} {7 7}}
757
758test regexp-19.1 {regsub null replacement} {
759    regsub -all {@} {@hel@lo@} "\0a\0" result
760    list $result [string length $result]
761} "\0a\0hel\0a\0lo\0a\0 14"
762
763test regexp-19.2 {regsub null replacement} {
764    regsub -all {@} {@hel@lo@} "\0a\0" result
765    set expected "\0a\0hel\0a\0lo\0a\0"
766    string equal $result $expected
767} 1
768
769test regexp-20.1 {regsub shared object shimmering} -constraints nodep -body {
770    # Bug #461322
771    set a abcdefghijklmnopqurstuvwxyz
772    set b $a
773    set c abcdefghijklmnopqurstuvwxyz0123456789
774    regsub $a $c $b d
775    list $d [string length $d] [string bytelength $d]
776} -result [list abcdefghijklmnopqurstuvwxyz0123456789 37 37]
777test regexp-20.2 {regsub shared object shimmering with -about} -body {
778    eval regexp -about abc
779} -result {0 {}}
780
781test regexp-21.1 {regsub works with empty string} -body {
782    regsub -- ^ {} foo
783} -result {foo}
784test regexp-21.2 {regsub works with empty string} -body {
785    regsub -- \$ {} foo
786} -result {foo}
787test regexp-21.3 {regsub works with empty string offset} -body {
788    regsub -start 0 -- ^ {} foo
789} -result {foo}
790test regexp-21.4 {regsub works with empty string offset} -body {
791    regsub -start 0 -- \$ {} foo
792} -result {foo}
793test regexp-21.5 {regsub works with empty string offset} -body {
794    regsub -start 3 -- \$ {123} foo
795} -result {123foo}
796test regexp-21.6 {regexp works with empty string} -body {
797    regexp -- ^ {}
798} -result {1}
799test regexp-21.7 {regexp works with empty string} -body {
800    regexp -start 0 -- ^ {}
801} -result {1}
802test regexp-21.8 {regexp works with empty string offset} -body {
803    regexp -start 3 -- ^ {123}
804} -result {0}
805test regexp-21.9 {regexp works with empty string offset} -body {
806    regexp -start 3 -- \$ {123}
807} -result {1}
808test regexp-21.10 {multiple matches handle newlines} {
809    regsub -all -lineanchor -- {^#[^\n]*\n} "#one\n#two\n#three\n" foo\n
810} "foo\nfoo\nfoo\n"
811test regexp-21.11 {multiple matches handle newlines} {
812    regsub -all -line -- ^ "a\nb\nc" \#
813} "\#a\n\#b\n\#c"
814test regexp-21.12 {multiple matches handle newlines} {
815    regsub -all -line -- ^ "\n\n" \#
816} "\#\n\#\n\#"
817test regexp-21.13 {multiple matches handle newlines} {
818    regexp -all -inline -indices -line -- ^ "a\nb\nc"
819} {{0 -1} {2 1} {4 3}}
820test regexp-21.14 {regsub works with empty string} {
821    regsub -- ^ {} &
822} {}
823test regexp-21.15 {regsub works with empty string} {
824    regsub -- ^ {} foo&
825} {foo}
826test regexp-21.16 {regsub works with empty string} {
827    regsub -all -- ^ {} foo&
828} {foo}
829test regexp-21.17 {regsub works with empty string} {
830    regsub -- ^ {} {foo\0}
831} {foo}
832test regexp-21.18 {regsub works with empty string} {
833    regsub -- ^.* {} {foo$0}
834} {foo$0}
835test regexp-21.19 {regsub works with empty string} {
836    regsub -- ^ {input} {}
837} {input}
838test regexp-21.20 {regsub works with empty string} {
839    regsub -- x {} {foo}
840} {}
841
842test regexp-22.1 {Bug 1810038} {
843    regexp ($|^X)* {}
844} 1
845test regexp-22.2 {regexp compile and backrefs, Bug 1857126} {
846    regexp -- {([bc])\1} bb
847} 1
848test regexp-22.3 {Bug 3604074} {
849    # This will hang in interps where the bug is not fixed
850    regexp ((((((((a)*)*)*)*)*)*)*)* a
851} 1
852test regexp-22.4 {Bug 3606139} -setup {
853    interp alias {} a {} string repeat a
854} -body {
855    # This crashes in interps where the bug is not fixed
856    regexp [join [list [a 160]([a 55])[a 668]([a 55])[a 669]([a 55]) \
857	[a 668]([a 55])[a 649]([a 55])[a 668]([a 55])[a 668]([a 55]) \
858	[a 672]([a 55])[a 669]([a 55])[a 671]([a 55])[a 671]([a 55]) \
859	[a 672]([a 55])[a 652]([a 55])[a 672]([a 55])[a 671]([a 55]) \
860	[a 671]([a 55])[a 671]([a 55])[a 653]([a 55])[a 672]([a 55]) \
861	[a 653]([a 55])[a 672]([a 55])[a 672]([a 55])[a 652]([a 55]) \
862	[a 671]([a 55])[a 652]([a 55])[a 652]([a 55])[a 672]([a 55]) \
863	[a 672]([a 55])[a 672]([a 55])[a 653]([a 55])[a 671]([a 55]) \
864	[a 669]([a 55])[a 649]([a 55])[a 668]([a 55])[a 668]([a 55]) \
865	[a 668]([a 55])[a 650]([a 55])[a 650]([a 55])[a 672]([a 55]) \
866	[a 669]([a 55])[a 669]([a 55])[a 668]([a 55])[a 668]([a 55]) \
867	[a 668]([a 55])[a 669]([a 55])[a 672]([a 55])[a 669]([a 55]) \
868	[a 669]([a 55])[a 669]([a 55])[a 669]([a 55])[a 672]([a 55]) \
869	[a 670]([a 55])[a 671]([a 55])[a 672]([a 55])[a 672]([a 55]) \
870	[a 671]([a 55])[a 671]([a 55])[a 672]([a 55])[a 669]([a 55]) \
871	[a 668]([a 55])[a 668]([a 55])[a 669]([a 55])[a 668]([a 55]) \
872	[a 669]([a 55])[a 668]([a 55])[a 669]([a 55])[a 669]([a 55]) \
873	[a 668]([a 55])[a 668]([a 55])[a 669]([a 55])[a 668]([a 55]) \
874	[a 669]([a 55])[a 669]([a 55])[a 669]([a 55])[a 669]([a 55]) \
875	[a 668]([a 55])[a 669]([a 55])[a 672]([a 55])[a 669]([a 55]) \
876	[a 669]([a 55])[a 669]([a 55])[a 669]([a 55])[a 668]([a 55]) \
877	[a 669]([a 55])[a 669]([a 55])[a 668]([a 55])[a 668]([a 55]) \
878	[a 668]([a 55])[a 669]([a 55])[a 668]([a 55])[a 669]([a 55]) \
879	[a 672]([a 55])[a 669]([a 55])[a 669]([a 55])[a 710]([a 55]) \
880	[a 668]([a 55])[a 669]([a 55])[a 668]([a 55])[a 669]([a 55]) \
881	[a 668]([a 55])[a 669]([a 55])[a 668]([a 55])[a 668]([a 55]) \
882	[a 668]([a 55])[a 668]([a 55])[a 668]([a 55])[a 669]([a 55]) \
883	[a 672]([a 55])[a 669]([a 55])[a 669]([a 55])[a 668]([a 55]) \
884	[a 669]([a 55])[a 669]([a 55])[a 668]([a 55])[a 668]([a 55]) \
885	[a 668]([a 55])[a 668]([a 55])[a 668]([a 55])[a 668]([a 55]) \
886	[a 667]([a 55])[a 668]([a 55])[a 669]([a 55])[a 668]([a 55]) \
887	[a 671]([a 55])[a 669]([a 55])[a 668]([a 55])[a 669]([a 55]) \
888	[a 669]([a 55])[a 669]([a 55])[a 668]([a 55])[a 669]([a 55]) \
889	[a 668]([a 55])[a 710]([a 55])[a 668]([a 55])[a 668]([a 55]) \
890	[a 668]([a 55])[a 668]([a 55])[a 668]([a 55])[a 511]] {}] a
891} -cleanup {
892    rename a {}
893} -returnCodes 1 -match glob -result {couldn't compile regular expression pattern: *}
894test regexp-22.5 {Bug 3610026} -setup {
895    set e {}
896    set cp 99
897    while {$cp < 32864} {
898	append e [format %c [incr cp]]
899    }
900} -body {
901    regexp -about $e
902} -cleanup {
903    unset -nocomplain e cp
904} -returnCodes error  -match glob -result {*too many colors*}
905test regexp-22.6 {Bug 6585b21ca8} {
906    expr {[regexp {(\w).*?\1} Programmer m] ? $m : "<NONE>"}
907} rogr
908
909
910test regexp-23.1 {regexp -all and -line} {
911    set string ""
912    list \
913	[regexp -all -inline -indices -line -- {^} $string] \
914	[regexp -all -inline -indices -line -- {^$} $string] \
915	[regexp -all -inline -indices -line -- {$} $string]
916} {{{0 -1}} {{0 -1}} {{0 -1}}}
917test regexp-23.2 {regexp -all and -line} {
918    set string "\n"
919    list \
920	[regexp -all -inline -indices -line -- {^} $string] \
921	[regexp -all -inline -indices -line -- {^$} $string] \
922	[regexp -all -inline -indices -line -- {$} $string]
923} {{{0 -1}} {{0 -1}} {{0 -1}}}
924test regexp-23.3 {regexp -all and -line} {
925    set string "\n\n"
926    list \
927	[regexp -all -inline -indices -line -- {^} $string] \
928	[regexp -all -inline -indices -line -- {^$} $string] \
929	[regexp -all -inline -indices -line -- {$} $string]
930} {{{0 -1} {1 0}} {{0 -1} {1 0}} {{0 -1} {1 0}}}
931test regexp-23.4 {regexp -all and -line} {
932    set string "a"
933    list \
934	[regexp -all -inline -indices -line -- {^} $string] \
935	[regexp -all -inline -indices -line -- {^.*$} $string] \
936	[regexp -all -inline -indices -line -- {$} $string]
937} {{{0 -1}} {{0 0}} {{1 0}}}
938test regexp-23.5 {regexp -all and -line} {knownBug} {
939    set string "a\n"
940    list \
941	[regexp -all -inline -indices -line -- {^} $string] \
942	[regexp -all -inline -indices -line -- {^.*$} $string] \
943	[regexp -all -inline -indices -line -- {$} $string]
944} {{{0 -1} {2 1}} {{0 0} {2 1}} {{1 0} {2 1}}}
945test regexp-23.6 {regexp -all and -line} {
946    set string "\na"
947    list \
948	[regexp -all -inline -indices -line -- {^} $string] \
949	[regexp -all -inline -indices -line -- {^.*$} $string] \
950	[regexp -all -inline -indices -line -- {$} $string]
951} {{{0 -1} {1 0}} {{0 -1} {1 1}} {{0 -1} {2 1}}}
952test regexp-23.7 {regexp -all and -line} {knownBug} {
953    set string "ab\n"
954    list \
955	[regexp -all -inline -indices -line -- {^} $string] \
956	[regexp -all -inline -indices -line -- {^.*$} $string] \
957	[regexp -all -inline -indices -line -- {$} $string]
958} {{{0 -1} {3 2}} {{0 1} {3 2}} {{2 1} {3 2}}}
959test regexp-23.8 {regexp -all and -line} {
960    set string "a\nb"
961    list \
962	[regexp -all -inline -indices -line -- {^} $string] \
963	[regexp -all -inline -indices -line -- {^.*$} $string] \
964	[regexp -all -inline -indices -line -- {$} $string]
965} {{{0 -1} {2 1}} {{0 0} {2 2}} {{1 0} {3 2}}}
966test regexp-23.9 {regexp -all and -line} {knownBug} {
967    set string "a\nb\n"
968    list \
969	[regexp -all -inline -indices -line -- {^} $string] \
970	[regexp -all -inline -indices -line -- {^.*$} $string] \
971	[regexp -all -inline -indices -line -- {$} $string]
972} {{{0 -1} {2 1} {4 3}} {{0 0} {2 2} {4 3}} {{1 0} {3 2} {4 3}}}
973test regexp-23.10 {regexp -all and -line} {
974    set string "a\nb\nc"
975    list \
976	[regexp -all -inline -indices -line -- {^} $string] \
977	[regexp -all -inline -indices -line -- {^.*$} $string] \
978	[regexp -all -inline -indices -line -- {$} $string]
979} {{{0 -1} {2 1} {4 3}} {{0 0} {2 2} {4 4}} {{1 0} {3 2} {5 4}}}
980test regexp-23.11 {regexp -all and -line} {
981    regexp -all -inline -indices -line -- {b} "abb\nb"
982} {{1 1} {2 2} {4 4}}
983
984test regexp-24.1 {regsub -all and -line} {
985    foreach {v1 v2 v3} {{} {} {}} {}
986    set string ""
987    list \
988	[regsub -line -all {^} $string {<&>} v1] $v1 \
989	[regsub -line -all {^$} $string {<&>} v2] $v2 \
990	[regsub -line -all {$} $string {<&>} v3] $v3
991} {1 <> 1 <> 1 <>}
992test regexp-24.2 {regsub -all and -line} {
993    foreach {v1 v2 v3} {{} {} {}} {}
994    set string "\n"
995    list \
996	[regsub -line -all {^} $string {<&>} v1] $v1 \
997	[regsub -line -all {^$} $string {<&>} v2] $v2 \
998	[regsub -line -all {$} $string {<&>} v3] $v3
999} [list 2 "<>\n<>" 2 "<>\n<>" 2 "<>\n<>"]
1000test regexp-24.3 {regsub -all and -line} {
1001    foreach {v1 v2 v3} {{} {} {}} {}
1002    set string "\n\n"
1003    list \
1004	[regsub -line -all {^} $string {<&>} v1] $v1 \
1005	[regsub -line -all {^$} $string {<&>} v2] $v2 \
1006	[regsub -line -all {$} $string {<&>} v3] $v3
1007} [list 3 "<>\n<>\n<>" 3 "<>\n<>\n<>" 3 "<>\n<>\n<>"]
1008test regexp-24.4 {regsub -all and -line} {
1009    foreach {v1 v2 v3} {{} {} {}} {}
1010    set string "a"
1011    list \
1012	[regsub -line -all {^} $string {<&>} v1] $v1 \
1013	[regsub -line -all {^.*$} $string {<&>} v2] $v2 \
1014	[regsub -line -all {$} $string {<&>} v3] $v3
1015} [list 1 "<>a" 1 "<a>" 1 "a<>"]
1016test regexp-24.5 {regsub -all and -line} {
1017    foreach {v1 v2 v3} {{} {} {}} {}
1018    set string "a\n"
1019    list \
1020	[regsub -line -all {^} $string {<&>} v1] $v1 \
1021	[regsub -line -all {^.*$} $string {<&>} v2] $v2 \
1022	[regsub -line -all {$} $string {<&>} v3] $v3
1023} [list 2 "<>a\n<>" 2 "<a>\n<>" 2 "a<>\n<>"]
1024test regexp-24.6 {regsub -all and -line} {
1025    foreach {v1 v2 v3} {{} {} {}} {}
1026    set string "\na"
1027    list \
1028	[regsub -line -all {^} $string {<&>} v1] $v1 \
1029	[regsub -line -all {^.*$} $string {<&>} v2] $v2 \
1030	[regsub -line -all {$} $string {<&>} v3] $v3
1031} [list 2 "<>\n<>a" 2 "<>\n<a>" 2 "<>\na<>"]
1032test regexp-24.7 {regsub -all and -line} {
1033    foreach {v1 v2 v3} {{} {} {}} {}
1034    set string "ab\n"
1035    list \
1036	[regsub -line -all {^} $string {<&>} v1] $v1 \
1037	[regsub -line -all {^.*$} $string {<&>} v2] $v2 \
1038	[regsub -line -all {$} $string {<&>} v3] $v3
1039} [list 2 "<>ab\n<>" 2 "<ab>\n<>" 2 "ab<>\n<>"]
1040test regexp-24.8 {regsub -all and -line} {
1041    foreach {v1 v2 v3} {{} {} {}} {}
1042    set string "a\nb"
1043    list \
1044	[regsub -line -all {^} $string {<&>} v1] $v1 \
1045	[regsub -line -all {^.*$} $string {<&>} v2] $v2 \
1046	[regsub -line -all {$} $string {<&>} v3] $v3
1047} [list 2 "<>a\n<>b" 2 "<a>\n<b>" 2 "a<>\nb<>"]
1048test regexp-24.9 {regsub -all and -line} {
1049    foreach {v1 v2 v3} {{} {} {}} {}
1050    set string "a\nb\n"
1051    list \
1052	[regsub -line -all {^} $string {<&>} v1] $v1 \
1053	[regsub -line -all {^.*$} $string {<&>} v2] $v2 \
1054	[regsub -line -all {$} $string {<&>} v3] $v3
1055} [list 3 "<>a\n<>b\n<>" 3 "<a>\n<b>\n<>" 3 "a<>\nb<>\n<>"]
1056test regexp-24.10 {regsub -all and -line} {
1057    foreach {v1 v2 v3} {{} {} {}} {}
1058    set string "a\nb\nc"
1059    list \
1060	[regsub -line -all {^} $string {<&>} v1] $v1 \
1061	[regsub -line -all {^.*$} $string {<&>} v2] $v2 \
1062	[regsub -line -all {$} $string {<&>} v3] $v3
1063} [list 3 "<>a\n<>b\n<>c" 3 "<a>\n<b>\n<c>" 3 "a<>\nb<>\nc<>"]
1064test regexp-24.11 {regsub -all and -line} {
1065    regsub -line -all {b} "abb\nb" {<&>}
1066} "a<b><b>\n<b>"
1067
1068test regexp-25.1 {regexp without -line option} {
1069    set foo ""
1070    list [regexp {a.*b} "dabc\naxyb\n" foo] $foo
1071} [list 1 abc\naxyb]
1072test regexp-25.2 {regexp without -line option} {
1073    set foo ""
1074    list [regexp {^a.*b$} "dabc\naxyb\n" foo] $foo
1075} {0 {}}
1076test regexp-25.3 {regexp with -line option} {
1077    set foo ""
1078    list [regexp -line {^a.*b$} "dabc\naxyb\n" foo] $foo
1079} {1 axyb}
1080test regexp-25.4 {regexp with -line option} {
1081    set foo ""
1082    list [regexp -line {^a.*b$} "dabc\naxyb\nxb" foo] $foo
1083} {1 axyb}
1084test regexp-25.5 {regexp without -line option} {
1085    set foo ""
1086    list [regexp {^a.*b$} "dabc\naxyb\nxb" foo] $foo
1087} {0 {}}
1088test regexp-25.6 {regexp without -line option} {
1089    set foo ""
1090    list [regexp {a.*b$} "dabc\naxyb\nxb" foo] $foo
1091} "1 {abc\naxyb\nxb}"
1092test regexp-25.7 {regexp with -lineanchor option} {
1093    set foo ""
1094    list [regexp -lineanchor {^a.*b$} "dabc\naxyb\nxb" foo] $foo
1095} "1 {axyb\nxb}"
1096test regexp-25.8 {regexp with -lineanchor and -linestop option} {
1097    set foo ""
1098    list [regexp -lineanchor -linestop {^a.*b$} "dabc\naxyb\nxb" foo] $foo
1099} {1 axyb}
1100test regexp-25.9 {regexp with -linestop option} {
1101    set foo ""
1102    list [regexp -linestop {a.*b} "ab\naxyb\nxb" foo] $foo
1103} {1 ab}
1104
1105test regexp-26.1 {matches start of line 1 time} {
1106    regexp -all -inline -- {^a+} "aab\naaa"
1107} {aa}
1108test regexp-26.2 {matches start of line(s) 2 times} {
1109    regexp -all -inline -line -- {^a+} "aab\naaa"
1110} {aa aaa}
1111test regexp-26.3 {effect of -line -all and -start} -body {
1112    list \
1113	[regexp -all -inline -line -start 0 -- {^a+} "aab\naaa"] \
1114	[regexp -all -inline -line -start 1 -- {^a+} "aab\naaa"] \
1115	[regexp -all -inline -line -start 3 -- {^a+} "aab\naaa"] \
1116	[regexp -all -inline -line -start 4 -- {^a+} "aab\naaa"] \
1117} -result {{aa aaa} aaa aaa aaa}
1118# No regexp-26.4
1119test regexp-26.5 {match length 0, match length 1} {
1120    regexp -all -inline -line -- {^b*} "a\nb"
1121} {{} b}
1122test regexp-26.6 {non reporting capture group} {
1123    regexp -all -inline -line -- {^(?:a+|b)} "aab\naaa"
1124} {aa aaa}
1125test regexp-26.7 {Tcl bug 2826551: -line sensitive regexp and -start} {
1126    set match1 {}
1127    set match2 {}
1128    list \
1129	[regexp -start 0 -indices -line {^a} "\nab" match1] $match1 \
1130	[regexp -start 1 -indices -line {^a} "\nab" match2] $match2
1131} {1 {1 1} 1 {1 1}}
1132test regexp-26.8 {Tcl bug 2826551: diff regexp with -line option} {
1133    set data "@1\n2\n+3\n@4\n-5\n+6\n7\n@8\n9\n"
1134    regexp -all -inline -line {^@.*\n(?:[^@].*\n?)*} $data
1135} [list "@1\n2\n+3\n" "@4\n-5\n+6\n7\n" "@8\n9\n"]
1136test regexp-26.9 {Tcl bug 2826551: diff regexp with embedded -line option} {
1137    set data "@1\n2\n+3\n@4\n-5\n+6\n7\n@8\n9\n"
1138    regexp -all -inline {(?n)^@.*\n(?:[^@].*\n?)*} $data
1139} [list "@1\n2\n+3\n" "@4\n-5\n+6\n7\n" "@8\n9\n"]
1140test regexp-26.10 {regexp with -line option} {
1141    regexp -all -inline -line -- {a*} "a\n"
1142} {a {}}
1143test regexp-26.11 {regexp without -line option} {
1144    regexp -all -inline -- {a*} "a\n"
1145} {a {}}
1146test regexp-26.12 {regexp with -line option} {
1147    regexp -all -inline -line -- {a*} "b\n"
1148} {{} {}}
1149test regexp-26.13 {regexp without -line option} {
1150    regexp -all -inline -- {a*} "b\n"
1151} {{} {}}
1152
1153test regexp-27.1 {regsub -command} {
1154    regsub -command {.x.} {abcxdef} {string length}
1155} ab3ef
1156test regexp-27.2 {regsub -command} {
1157    regsub -command {.x.} {abcxdefxghi} {string length}
1158} ab3efxghi
1159test regexp-27.3 {regsub -command} {
1160    set x 0
1161    regsub -all -command {(?=.)} abcde {apply {args {incr ::x}}}
1162} 1a2b3c4d5e
1163test regexp-27.4 {regsub -command} -body {
1164    regsub -command {.x.} {abcxdef} error
1165} -returnCodes error -result cxd
1166test regexp-27.5 {regsub -command} {
1167    regsub -command {(.)(.)} {abcdef} {list ,}
1168} {, ab a bcdef}
1169test regexp-27.6 {regsub -command} {
1170    regsub -command -all {(.)(.)} {abcdef} {list ,}
1171} {, ab a b, cd c d, ef e f}
1172test regexp-27.7 {regsub -command representation smash} {
1173    set ::s {123=456 789}
1174    regsub -command -all {\d+} $::s {apply {n {
1175	expr {[llength $::s] + $n}
1176    }}}
1177} {125=458 791}
1178test regexp-27.8 {regsub -command representation smash} {
1179    set ::t {apply {n {
1180	expr {[llength [lindex $::t 1 1 1]] + $n}
1181    }}}
1182    regsub -command -all {\d+} "123=456 789" $::t
1183} {131=464 797}
1184test regexp-27.9 {regsub -command memory leak testing} memory {
1185    set ::s "123=456 789"
1186    set ::t {apply {n {
1187	expr {[llength [lindex $::t 1 1 1]] + [llength $::s] + $n}
1188    }}}
1189    memtest {
1190	regsub -command -all {\d+} $::s $::t
1191    }
1192} 0
1193test regexp-27.10 {regsub -command error cases} -returnCodes error -body {
1194    regsub -command . abc "def \{ghi"
1195} -result {unmatched open brace in list}
1196test regexp-27.11 {regsub -command error cases} -returnCodes error -body {
1197    regsub -command . abc {}
1198} -result {command prefix must be a list of at least one element}
1199test regexp-27.12 {regsub -command representation smash} {
1200    set s {list (.+)}
1201    regsub -command $s {list list} $s
1202} {(.+) {list list} list}
1203
1204# cleanup
1205::tcltest::cleanupTests
1206return
1207
1208# Local Variables:
1209# mode: tcl
1210# End:
1211