Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2003-10-29
18:10 • Ticket [832230ffff] Function to compile a pattern status still Closed with 4... artifact: b917e1f69d user: elfring
17:09 • Ticket [832230ffff]: 5 changes artifact: ea98c0c343 user: dkf
16:31 • Ticket [832230ffff]: 5 changes artifact: 5d34a41caa user: elfring
16:15 • Closed ticket [832230ffff]. artifact: e59a96e257 user: dkf
16:14 • Open ticket [832230ffff]. artifact: 93f7633681 user: elfring
16:03 • Closed ticket [832230ffff]. artifact: 7638768e7e user: hobbs
08:56 • New ticket [832230ffff]. artifact: df1878985e user: elfring

Function to compile a pattern

Closed / Invalid832230ffffffffffffffffffffffffffffffffffCreated 23 years agoUpdated 23 years ago
Descriptionplain2003-10-29 08:56:09
The class 
library "http://java.sun.com/j2se/1.4.2/docs/api/java/ut
il/regex/Pattern.html" provides a method "compile".
Can a similar function be offered for TCL?
How do you think about to make the 
function "Tcl_RegExpCompile" 
(http://tcl.tk/man/tcl8.4/TclLib/RegExp.htm) callable 
on the script level?

I would like to avoid the overhead for pattern 
preprocessing for the 
commands "http://tcl.tk/man/tcl8.4/TclCmd/regexp.htm
" and "http://tcl.tk/man/tcl8.4/TclCmd/regsub.htm" in 
loops.
Can we get more performance if the additional memory 
for the technique "Compile caching" will be saved?
hobbsplain2003-10-29 16:03:54
Logged In: YES 
user_id=72656

Tcl already does this if you keep the RE in an object (even a 
static object as part of a compiled body of code).  IOW, just 
set the RE to a var, and use that var just as an RE.
elfringplain2003-10-29 16:14:12
Logged In: YES 
user_id=572001

I assumed that "Compile caching" is performed already.
But how much processing does it need by TCL? This effort 
might not be needed for constant patterns that should be 
compiled only once before the matching is done in a loop.
dkfplain2003-10-29 16:15:26
Logged In: YES 
user_id=79902

Fleshing out with an example:

  set RE {the.*complex.*regexp}
  # Match once against the empty string; this forces compilation
  regexp $RE {}
  # Now use $RE instead of the literal and you'll use the
  # pre-compiled RE.

In fact, there's a few other places which do caching as
well. But IMHO the biggest advantage of the above technique
is that you can also give logical names to your RE (e.g.
$LinkTagMatcher instead {<a[^>]*?HREF="([^"]+)"}) and this
is a big win in maintenance and code-simplicity terms.
elfringplain2003-10-29 16:31:57
Logged In: YES 
user_id=572001

I'm sorry - I did not know that TCL variables can be marked 
that an instance contains a compiled regular expression.
Would you like to publish this behaviour on the 
page "http://mini.net/tcl/regexp" 
and "http://mini.net/tcl/2244"?
dkfplain2003-10-29 17:09:57
Logged In: YES 
user_id=79902

You do it Marcus.  You're obviously much keener on doing
this than I am especially as virtually everyone's already
reaping the benefits of precompilation anyway, one way or
another.
elfringplain2003-10-29 18:10:39
Logged In: YES 
user_id=572001

I have updated these wikis.
Log in (anonymous is fine) to add a comment.