Tcl Source Code

View Ticket
Login
Ticket UUID: 652953
Title: regsub -code (equivalent to Perl's /e)
Type: RFE Version: None
Submitter: nobody Created on: 2002-12-12 21:51:56
Subsystem: 18. Commands M-Z Assigned To: dkf
Priority: 7 High Severity:
Status: Open Last Modified: 2003-11-12 07:25:24
Resolution: None Closed By:
    Closed on:
Description:
(Greg Beauchesne, [email protected])

I hope I didn't miss something, but I wanted an option
in "regsub" that was equivalent to Perl's /e modifier
on substitutions, and I couldn't find one. /e in Perl
evaluates the subtitution string as code rather than as
just a string. So, I added a -code flag to "regsub" for
this purpose.

For example, to decode a URL-encoded string, you can do:

regsub -nocase -all -code -- {%([0-9a-f]{2})} $string
{format %c 0x$1}

During the substitution code, the variables $0, $1, $2,
etc. are set to the match. If there were actual
variables $0, $1, $2, etc. beforehand, they are
restored at the end of the regsub command, but they are
not accessible during the code execution. Matches which
are unused are set to empty strings (to avoid errors).
The replacement is done for TCL_OK and TCL_RETURN
codes. TCL_BREAK will stop replacement prematurely (Not
much use without -all) and TCL_ERROR or TCL_CONTINUE
will cause the command to return TCL_ERROR.
User Comments: hobbs added on 2003-11-12 07:25:24:
Logged In: YES 
user_id=72656

also consider subst instead of eval: 550797

nobody added on 2002-12-15 11:20:06:
Logged In: NO 

Yeah, after I submitted it, I saw your comment on the
feature request list. It would be a small change to switch
the use of Tcl_EvalObjEx with Tcl_SubstObj, though, yes,
that wouldn't be compatible with the \1 \2 \3 etc. format.
It sort of seems to me like $1 $2 $3 ... is more "natural"
in the context of evaluated expressions, however. (as
opposed to {[expr {\1 + \2}]} or whatever)

It might be inconsistent, but it wouldn't break the
non-substituted version.

-- Greg

dkf added on 2002-12-13 16:58:12:
Logged In: YES 
user_id=79902

This sort of thing has been on my to-do list for ages,
though I've been thinking more in terms of passing things
through the (subst) engine.

nobody added on 2002-12-13 04:51:57:

File Added - 37472: regsub-code.patch

Attachments: