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