<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://tcl.activestate.com/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu Feb 09 11:23:34 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='36'>
<header><title>Library Access to &apos;Subst&apos; Functionality</title><author address="mailto:fellowsd@cs.man.ac.uk">Donal K. Fellows</author><status type='project' state='final' tclversion="8.4" vote='after'>$Revision: 1.4 $</status><history></history><created day='13' month='jun' year='2001' /></header>
<abstract>Some applications make very heavy use of the <emph style="italic">subst</emph> command - it seems particularly popular in the active-content-generation field - and for them it is important to optimise this as much as possible. This TIP adds a direct interface to these capabilities to the Tcl library, allowing programmers to avoid the modest overheads of even <emph style="italic">Tcl_EvalObjv</emph> and the option parser for the <emph style="italic">subst</emph> command implementation.</abstract>
<body><section title="Functionality Changes">
<para>There will be one script-visible functionality change from the current implementation; if the evaluation of any command substitution returns TCL_BREAK, then the result of the <emph style="italic">subst</emph> command will be the string up to that point and no further. This contrasts with the current behaviour where TCL_BREAK (like TCL_CONTINUE) just causes the current command substitution to finish early.</para>
</section>
<section title="Design Decisions">
<para>The code should be created by effectively splitting <emph style="italic">Tcl_SubstObjCmd</emph> in the current <emph style="italic">.../generic/tclCmdMZ.c</emph> into two pieces. One of these pieces will have the same interface as the present code and will contain the argument parser. The other piece will be the implementation of the <emph style="italic">subst</emph> behaviour and will be separately exposed at the C level as well as being called by the front-end code.</para>
<para>The code should take positive flags stating what kinds of substitutions should be performed, as this is closest to the current internal implementation of the <emph style="italic">subst</emph> command. These flags will be named with the prefix TCL_SUBST_*. For programming convenience, the flag TCL_SUBST_ALL will also be provided allowing the common case of wanting all substitutions to be performed with a minimum of fuss.</para>
<para>The string to be substituted will be passed in as a <emph style="italic">Tcl_Obj *</emph> too, as this is both easiest to do from the point-of-view of the front-end code and permits additional optimisation of the core at some future point if it proves necessary and/or desirable. By contrast, passing in a standard C string or a <emph style="italic">Tcl_DString *</emph> does not permit any such optimisations in the future.</para>
<para>The code should return a newly-allocated <emph style="italic">Tcl_Obj *</emph> as this allows for the efficient implementation of the front-end involving no re-copying of the resulting string. It also allows error conditions to be represented by NULL (with an error message in the interpreter result) and does not force a Tcl_DString reference to be passed in as an <emph style="italic">out</emph> parameter; returning the result gives a much clearer call semantics. Another advantage of using <emph style="italic">Tcl_Obj</emph>s to build the result is the fact that they have a more sophisticated memory allocation algorithm that copes more efficiently with very large strings; when large and small strings are being combined together (as is easily the case in <emph style="italic">subst</emph>) this can make a substantial difference.</para>
</section>
<section title="Public Interface">
<para><emph style="italic">Added to .../generic/tcl.h</emph></para>
<verbatim><vline encoding='base64'>I2RlZmluZSBUQ0xfU1VCU1RfQ09NTUFORFMgICAgMHgwMQ==</vline><vline encoding='base64'>I2RlZmluZSBUQ0xfU1VCU1RfVkFSSUFCTEVTICAgMHgwMg==</vline><vline encoding='base64'>I2RlZmluZSBUQ0xfU1VCU1RfQkFDS1NMQVNIRVMgMHgwNA==</vline><vline encoding='base64'>I2RlZmluZSBUQ0xfU1VCU1RfQUxMICAgICAgICAgMHgwNw==</vline></verbatim>
<para><emph style="italic">Added to .../generic/tcl.decls</emph></para>
<verbatim><vline encoding='base64'>ZGVjbGFyZSBzb21lTnVtYmVyIGdlbmVyaWMgew==</vline><vline encoding='base64'>ICAgIFRjbF9PYmogKiBUY2xfU3Vic3RPYmooIFRjbF9JbnRlcnAgKmludGVycCw=</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgIFRjbF9PYmogKm9ialB0ciw=</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgIGludCBmbGFncyk=</vline><vline encoding='base64'>fQ==</vline></verbatim>
</section>
<section title="Implementation">
<para>The implementation is to be developed upon acceptance of this TIP, but will involve <emph style="italic">Tcl_AppendToObj</emph> and <emph style="italic">Tcl_AppendObjToObj</emph>.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>

