Tcl Source Code

Check-in [c0036938dd]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Reimplement NON-BYTECODED [string replace] in terms of new TclStringReplace() function
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | amg-string-insert
Files: files | file ages | folders
SHA1: c0036938ddeb5da232d4a50cfe585918e0306564
User & Date: andy 2017-08-20 03:21:27
Context
2017-08-20
03:45
Add string-14.18 to test that [string replace] with last<first does not insert a string, even though... check-in: de104ef5ab user: andy tags: amg-string-insert
03:21
Reimplement NON-BYTECODED [string replace] in terms of new TclStringReplace() function check-in: c0036938dd user: andy tags: amg-string-insert
03:20
Correct NULL dereference, and optimize short-circuit logical operation check-in: fa3278ec35 user: andy tags: amg-string-insert
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to generic/tclCmdMZ.c.

2352
2353
2354
2355
2356
2357
2358
2359
2360




2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383





2384
2385
2386


2387
2388
2389
2390


2391
2392
2393

2394
2395
2396
2397
2398
2399
2400
static int
StringRplcCmd(
    ClientData dummy,		/* Not used. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    Tcl_UniChar *ustring;
    int first, last, length;





    if (objc < 4 || objc > 5) {
	Tcl_WrongNumArgs(interp, 1, objv, "string first last ?string?");
	return TCL_ERROR;
    }

    ustring = Tcl_GetUnicodeFromObj(objv[1], &length);
    length--;

    if (TclGetIntForIndexM(interp, objv[2], length, &first) != TCL_OK ||
	    TclGetIntForIndexM(interp, objv[3], length, &last) != TCL_OK){
	return TCL_ERROR;
    }

    if ((last < first) || (last < 0) || (first > length)) {
	Tcl_SetObjResult(interp, objv[1]);
    } else {
	Tcl_Obj *resultPtr;

	if (first < 0) {
	    first = 0;
	}






	resultPtr = Tcl_NewUnicodeObj(ustring, first);
	if (objc == 5) {
	    Tcl_AppendObjToObj(resultPtr, objv[4]);


	}
	if (last < length) {
	    Tcl_AppendUnicodeToObj(resultPtr, ustring + last + 1,
		    length - last);


	}
	Tcl_SetObjResult(interp, resultPtr);
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * StringRevCmd --







<
|
>
>
>
>






|
|
<
|
|



<
<
<
<
<
|
|
|

>
>
>
>
>
|
|
|
>
>
|
|
<
<
>
>
|
<
|
>







2352
2353
2354
2355
2356
2357
2358

2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371

2372
2373
2374
2375
2376





2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392


2393
2394
2395

2396
2397
2398
2399
2400
2401
2402
2403
2404
static int
StringRplcCmd(
    ClientData dummy,		/* Not used. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{

    int first, last, length;	/* Replacement indexes and string length */
    int del;			/* Number of characters to delete */
    Tcl_Obj *strObj;		/* String being modified */
    Tcl_Obj *insObj;		/* Substring to insert, may be NULL */
    Tcl_Obj *outObj;		/* Output object */

    if (objc < 4 || objc > 5) {
	Tcl_WrongNumArgs(interp, 1, objv, "string first last ?string?");
	return TCL_ERROR;
    }

    strObj = objv[1];
    length = Tcl_GetCharLength(strObj) - 1;

    if (TclGetIntForIndexM(interp, objv[2], length, &first) != TCL_OK
     || TclGetIntForIndexM(interp, objv[3], length, &last) != TCL_OK) {
	return TCL_ERROR;
    }






    if (first < 0) {
	first = 0;
    }

    if (last >= first) {
	del = last - first + 1;
    } else {
	del = 0;
    }

    if (objc == 5) {
	insObj = objv[4];
    } else {
	insObj = NULL;
    }



    if (!(outObj = TclStringReplace(interp, strObj, first, del, insObj))) {
	return TCL_ERROR;
    }


    Tcl_SetObjResult(interp, outObj);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * StringRevCmd --