Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 21951649f3451ac48a1d9143a668ac92bc49573d
Ticket: cafef7c5856ec8b1cbc0ea13c6c41d96d9982d75
Unexplained code in StringRplcCmd function (tclCmdMZ.c)
User & Date: FireTcl on 2015-02-18 20:37:02
Changes

  1. icomment:
    First of all, I am a newbie. I am learning Tcl.
    
    It would be nice a command like this: "string insert".
    
    Perhaps it's possible to change a little the implementation of  StringRplcCmd to allow this behaviour: inserting text without deleting nothing.
    
    For example when last < first or when last = first-1, no deletion is produced.
    
    The problem with this approach is that can break code, because when last < first, nothing happens: the provided string is returned.
    
    Another choice is perhaps another function implementing this new command, similar to this one:
    
    
    
    static int
    StringInsCmd(
        ClientData dummy,        /* Not used. */
        Tcl_Interp *interp,        /* Current interpreter. */
        int objc,            /* Number of arguments. */
        Tcl_Obj *const objv[])    /* Argument objects. */
    {
        Tcl_UniChar *ustring;
        int index, length;
    
        if (objc != 3 ) {
            Tcl_WrongNumArgs(interp, 1, objv, "string index insertion");
            return TCL_ERROR;
        }
    
        ustring = Tcl_GetUnicodeFromObj(objv[1], &length);
        length--;
    
        if (TclGetIntForIndexM(interp, objv[2], length, &index) != TCL_OK ){
            return TCL_ERROR;
        }
    
        Tcl_Obj *resultPtr;
    
        if (index < 0) {
            index = 0;
        } else if (index > length) {
            index = length
        }
    
        resultPtr = Tcl_NewUnicodeObj(ustring, index);
        
        Tcl_AppendObjToObj(resultPtr, objv[3]);
        
        Tcl_AppendUnicodeToObj(resultPtr, ustring + index, length - index);
        
        Tcl_SetObjResult(interp, resultPtr);
    
        return TCL_OK;
    }
    
    ___________________________
    
    It's necessary also to code tclCompileStringInsertCmd.
    
    The problem with this other approach is code duplication, because the same effect could be achieved changing a little "string replace".
    
  2. login: "FireTcl"
  3. mimetype: "text/plain"