Tcl Source Code

View Ticket
Login
Ticket UUID: 755335
Title: regsub won't substitute against an empty string
Type: Bug Version: obsolete: 8.4.3
Submitter: vincentdarley Created on: 2003-06-16 14:11:55
Subsystem: 18. Commands M-Z Assigned To: vincentdarley
Priority: 5 Medium Severity:
Status: Closed Last Modified: 2003-06-18 03:37:53
Resolution: Fixed Closed By: vincentdarley
    Closed on: 2003-06-17 20:37:53
Description:
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'.
User Comments: vincentdarley added on 2003-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...

dkf added on 2003-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.)

vincentdarley added on 2003-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"

vincentdarley added on 2003-06-17 16:39:45:

File Added - 53332: regsubfix.patch

vincentdarley added on 2003-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.

vincentdarley added on 2003-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.

Attachments: