Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2003-06-18
03:37 • Closed ticket [755335ffff]: regsub won't substitute against an empty string<... artifact: 250b723448 user: vincentdarley
2003-06-17
23:41 • Ticket [755335ffff]: 5 changes artifact: 15499e1b5d user: dkf
19:15 • Ticket [755335ffff]: 4 changes artifact: ac0c9b7136 user: vincentdarley
16:39 • Ticket [755335ffff]: 4 changes artifact: f60a536b13 user: vincentdarley
16:39 • Add attachment regsubfix.patch to ticket [755335ffff] artifact: 9b37b78404 user: vincentdarley
16:39 • Ticket [755335ffff] regsub won't substitute against an empty string stat... artifact: 8df6804532 user: vincentdarley
00:09 • Ticket [755335ffff]: 4 changes artifact: fa1bbaf5fa user: vincentdarley
2003-06-16
14:11 • New ticket [755335ffff]. artifact: b170814923 user: vincentdarley

regsub won't substitute against an empty string

Closed / Fixed755335ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-06-16 14:11:55
Despite the fact that patterns like '^' can and do match 
the empty string, 'regsub' will refuse to acknowledge 
them and refuse to perform substitutions.

For example:

GOOD:
% regexp -- ^ {}
1

BAD:

% regsub -- ^ {} {replace} var
0
% set var
<empty string>

Of course this should replace '{}' with 'replace'.
vincentdarleyplain2003-06-17 00:09:16
Logged In: YES 
user_id=32170

Here's another regsub issue:

% regsub -all -lineanchor -- {^#[^\n]*\n} \
       "#one\n#two\n#three\n" foo\n
foo
#two
foo

I believe the buggy line is tclCmdMZ.c:734:

match = Tcl_RegExpExecObj(interp, regExpr, objPtr, 
offset,
10 /* matches */, ((offset > 0) ? 
TCL_REG_NOTBOL : 0));

where 'TCL_REG_NOTBOL' should only be added as a flag if 
offset>0 _and_ the the 'offset-1'th character of objPtr is not 
a newline.
vincentdarleyplain2003-06-17 16:39:44
Logged In: YES 
user_id=32170

The attached patch I believe fixes these problems, although I 
can't test it since the windows build is still broken.
vincentdarleyplain2003-06-17 16:39:45
File Added - 53332: regsubfix.patch
vincentdarleyplain2003-06-17 19:15:44
Logged In: YES 
user_id=32170

Here are some new tests which ought to cover the problems 
seen.

test regexp-21.1 {regsub works with empty string} {
    regsub -- ^ {} foo
} {foo}

test regexp-21.2 {regsub works with empty string} {
    regsub -- \$ {} foo
} {foo}

test regexp-21.3 {regsub works with empty string offset} {
    regsub -start 0 -- ^ {} foo
} {foo}

test regexp-21.4 {regsub works with empty string offset} {
    regsub -start 0 -- \$ {} foo
} {foo}

test regexp-21.5 {regsub works with empty string offset} {
    regsub -start 3 -- \$ {123} foo
} {123foo}

test regexp-21.6 {regexp works with empty string} {
    regexp -- ^ {}
} {1}

test regexp-21.7 {regexp works with empty string} {
    regexp -start 0 -- ^ {}
} {1}

test regexp-21.8 {regexp works with empty string offset} {
    regexp -start 3 -- ^ {123}
} {0}

test regexp-21.9 {regexp works with empty string offset} {
    regexp -start 3 -- \$ {123}
} {1}

test regexp-21.10 {multiple matches handle newlines} {
    regsub -all -lineanchor -- {^#[^\n]
*\n} "#one\n#two\n#three\n" foo\n
} "foo\nfoo\nfoo\n"
dkfplain2003-06-17 23:41:27
Logged In: YES 
user_id=79902

Can't review the patch in the middle of this meeting, but as 
you've got tests to detect the problem and whether it is 
fixed, I'm perfectly happy for you to fix this.  Make sure the 
patch goes into the HEAD and the 8.4 branch, of course.  :^)

I assume this all depends on none of the various -line* 
options being specified?  Or do they need separate testing?  
(Sometimes its just nice to add extra tests even if you know 
they'll pass.)
vincentdarleyplain2003-06-18 03:37:53
Logged In: YES 
user_id=32170

Applied slightly modified patch to cvs head, so all of the 
quoted tests pass ok.

Will also apply to 8.4 branch...
Log in (anonymous is fine) to add a comment.

Attachments: