Tcl Source Code

Changes On Branch spjuth-unify
Login

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

Changes In Branch spjuth-unify Excluding Merge-Ins

This is equivalent to a diff from 9cee434074 to 730c630a8a

2015-03-08
12:13
Store the notes for this branch. Leaf check-in: 730c630a8a user: pspjuth tags: spjuth-unify
2011-04-06
23:35
* generic/tclExecute.c: fix for [Bug 3274728], making *catchTop an unsigned long. check-in: caf317ab68 user: mig tags: trunk
23:14
Merge from trunk check-in: 630366974c user: pspjuth tags: spjuth-unify (unpublished)
22:42
Merge from trunk check-in: 90a456061f user: pspjuth tags: spjuth-parse (unpublished)
22:30
New branch for list with NULL check-in: 489960922d user: pspjuth tags: spjuth-listhole (unpublished)
22:23
Branch for exec with pwd check-in: 3479d6ce29 user: pspjuth tags: spjuth-execpwd (unpublished)
22:05
New branch for complex experiment check-in: 604b1b2e26 user: pspjuth tags: spjuth-complex (unpublished)
21:53
Merge from trunk check-in: fcd805c812 user: pspjuth tags: spjuth-macro (unpublished)
18:18
merge trunk to feature branch check-in: af182bb0cf user: mig tags: mig-no280
18:16
merge trunk to feature branch check-in: 611c232145 user: mig tags: mig-alloc-reform
14:51
Don't use -fvisibility=hidden with static libraries (--disable-shared) check-in: 9cee434074 user: jan.nijtmans tags: trunk
13:10
Make symbols "main" and "Tcl_AppInit" MODULE_SCOPE: there is absolutely no reason for exporting them... check-in: 0a2eb3820d user: jan.nijtmans tags: trunk

Changes to generic/tclBinary.c.

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

#define BINARY_SCAN_MAX_CACHE	260

/*
 * Prototypes for local procedures defined in this file:
 */

static void		DupByteArrayInternalRep(Tcl_Obj *srcPtr,
			    Tcl_Obj *copyPtr);
static int		FormatNumber(Tcl_Interp *interp, int type,
			    Tcl_Obj *src, unsigned char **cursorPtr);
static void		FreeByteArrayInternalRep(Tcl_Obj *objPtr);
static int		GetFormatSpec(const char **formatPtr, char *cmdPtr,
			    int *countPtr, int *flagsPtr);
static Tcl_Obj *	ScanNumber(unsigned char *buffer, int type,
			    int flags, Tcl_HashTable **numberCachePtr);
static int		SetByteArrayFromAny(Tcl_Interp *interp,
			    Tcl_Obj *objPtr);
static void		UpdateStringOfByteArray(Tcl_Obj *listPtr);
static void		DeleteScanNumberCache(Tcl_HashTable *numberCachePtr);
static int		NeedReversing(int format);
static void		CopyNumber(const void *from, void *to,
			    unsigned length, int type);
/* Binary ensemble commands */
static int		BinaryFormatCmd(ClientData clientData,
			    Tcl_Interp *interp,







<
<


<




<
<
<







50
51
52
53
54
55
56


57
58

59
60
61
62



63
64
65
66
67
68
69

#define BINARY_SCAN_MAX_CACHE	260

/*
 * Prototypes for local procedures defined in this file:
 */



static int		FormatNumber(Tcl_Interp *interp, int type,
			    Tcl_Obj *src, unsigned char **cursorPtr);

static int		GetFormatSpec(const char **formatPtr, char *cmdPtr,
			    int *countPtr, int *flagsPtr);
static Tcl_Obj *	ScanNumber(unsigned char *buffer, int type,
			    int flags, Tcl_HashTable **numberCachePtr);



static void		DeleteScanNumberCache(Tcl_HashTable *numberCachePtr);
static int		NeedReversing(int format);
static void		CopyNumber(const void *from, void *to,
			    unsigned length, int type);
/* Binary ensemble commands */
static int		BinaryFormatCmd(ClientData clientData,
			    Tcl_Interp *interp,
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
 *
 * Converting a String to a ByteArray proceeds by getting the Unicode
 * representation of each character in the String, casting it to a byte by
 * truncating the upper 8 bits, and then storing the byte in the ByteArray.
 * Converting from ByteArray to String and back to ByteArray is not lossy, but
 * converting an arbitrary String to a ByteArray may be.
 */

const Tcl_ObjType tclByteArrayType = {
    "bytearray",
    FreeByteArrayInternalRep,
    DupByteArrayInternalRep,
    UpdateStringOfByteArray,
    SetByteArrayFromAny
};

/*
 * The following structure is the internal rep for a ByteArray object. Keeps
 * track of how much memory has been used and how much has been allocated for
 * the byte array to enable growing and shrinking of the ByteArray object with
 * fewer mallocs.
 */

typedef struct ByteArray {
    int used;			/* The number of bytes used in the byte
				 * array. */
    int allocated;		/* The amount of space actually allocated
				 * minus 1 byte. */
    unsigned char bytes[1];	/* The array of bytes. The actual size of this
				 * field depends on the 'allocated' field
				 * above. */
} ByteArray;

#define BYTEARRAY_SIZE(len) \
		((unsigned) (TclOffset(ByteArray, bytes) + (len)))
#define GET_BYTEARRAY(objPtr) \
		((ByteArray *) (objPtr)->internalRep.otherValuePtr)
#define SET_BYTEARRAY(objPtr, baPtr) \
		(objPtr)->internalRep.otherValuePtr = (void *) (baPtr)

/*
 *----------------------------------------------------------------------
 *
 * Tcl_NewByteArrayObj --
 *
 *	This procedure is creates a new ByteArray object and initializes it
 *	from the given array of bytes.
 *
 * Results:
 *	The newly create object is returned. This object will have no initial
 *	string representation. The returned object has a ref count of 0.
 *
 * Side effects:
 *	Memory allocated for new object and copy of byte array argument.
 *
 *----------------------------------------------------------------------
 */

#undef Tcl_NewByteArrayObj

Tcl_Obj *
Tcl_NewByteArrayObj(
    const unsigned char *bytes,	/* The array of bytes used to initialize the
				 * new object. */
    int length)			/* Length of the array of bytes, which must be
				 * >= 0. */
{
#ifdef TCL_MEM_DEBUG
    return Tcl_DbNewByteArrayObj(bytes, length, "unknown", 0);
#else /* if not TCL_MEM_DEBUG */
    Tcl_Obj *objPtr;

    TclNewObj(objPtr);
    Tcl_SetByteArrayObj(objPtr, bytes, length);
    return objPtr;
#endif /* TCL_MEM_DEBUG */
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_DbNewByteArrayObj --
 *
 *	This procedure is normally called when debugging: i.e., when
 *	TCL_MEM_DEBUG is defined. It is the same as the Tcl_NewByteArrayObj
 *	above except that it calls Tcl_DbCkalloc directly with the file name
 *	and line number from its caller. This simplifies debugging since then
 *	the [memory active] command will report the correct file name and line
 *	number when reporting objects that haven't been freed.
 *
 *	When TCL_MEM_DEBUG is not defined, this procedure just returns the
 *	result of calling Tcl_NewByteArrayObj.
 *
 * Results:
 *	The newly create object is returned. This object will have no initial
 *	string representation. The returned object has a ref count of 0.
 *
 * Side effects:
 *	Memory allocated for new object and copy of byte array argument.
 *
 *----------------------------------------------------------------------
 */

Tcl_Obj *
Tcl_DbNewByteArrayObj(
    const unsigned char *bytes,	/* The array of bytes used to initialize the
				 * new object. */
    int length,			/* Length of the array of bytes, which must be
				 * >= 0. */
    const char *file,		/* The name of the source file calling this
				 * procedure; used for debugging. */
    int line)			/* Line number in the source file; used for
				 * debugging. */
{
#ifdef TCL_MEM_DEBUG
    Tcl_Obj *objPtr;

    TclDbNewObj(objPtr, file, line);
    Tcl_SetByteArrayObj(objPtr, bytes, length);
    return objPtr;
#else /* if not TCL_MEM_DEBUG */
    return Tcl_NewByteArrayObj(bytes, length);
#endif /* TCL_MEM_DEBUG */
}

/*
 *---------------------------------------------------------------------------
 *
 * Tcl_SetByteArrayObj --
 *
 *	Modify an object to be a ByteArray object and to have the specified
 *	array of bytes as its value.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The object's old string rep and internal rep is freed. Memory
 *	allocated for copy of byte array argument.
 *
 *----------------------------------------------------------------------
 */

void
Tcl_SetByteArrayObj(
    Tcl_Obj *objPtr,		/* Object to initialize as a ByteArray. */
    const unsigned char *bytes,	/* The array of bytes to use as the new
				   value. May be NULL even if length > 0. */
    int length)			/* Length of the array of bytes, which must
				   be >= 0. */
{
    ByteArray *byteArrayPtr;

    if (Tcl_IsShared(objPtr)) {
	Tcl_Panic("%s called with shared object", "Tcl_SetByteArrayObj");
    }
    TclFreeIntRep(objPtr);
    Tcl_InvalidateStringRep(objPtr);

    length = (length < 0) ? 0 : length;
    byteArrayPtr = ckalloc(BYTEARRAY_SIZE(length));
    memset(byteArrayPtr, 0, BYTEARRAY_SIZE(length));
    byteArrayPtr->used = length;
    byteArrayPtr->allocated = length;
    if (bytes && length) {
	memcpy(byteArrayPtr->bytes, bytes, (size_t) length);
    }

    objPtr->typePtr = &tclByteArrayType;
    SET_BYTEARRAY(objPtr, byteArrayPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetByteArrayFromObj --
 *
 *	Attempt to get the array of bytes from the Tcl object. If the object
 *	is not already a ByteArray object, an attempt will be made to convert
 *	it to one.
 *
 * Results:
 *	Pointer to array of bytes representing the ByteArray object.
 *
 * Side effects:
 *	Frees old internal rep. Allocates memory for new internal rep.
 *
 *----------------------------------------------------------------------
 */

unsigned char *
Tcl_GetByteArrayFromObj(
    Tcl_Obj *objPtr,		/* The ByteArray object. */
    int *lengthPtr)		/* If non-NULL, filled with length of the
				 * array of bytes in the ByteArray object. */
{
    ByteArray *baPtr;

    if (objPtr->typePtr != &tclByteArrayType) {
	SetByteArrayFromAny(NULL, objPtr);
    }
    baPtr = GET_BYTEARRAY(objPtr);

    if (lengthPtr != NULL) {
	*lengthPtr = baPtr->used;
    }
    return (unsigned char *) baPtr->bytes;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_SetByteArrayLength --
 *
 *	This procedure changes the length of the byte array for this object.
 *	Once the caller has set the length of the array, it is acceptable to
 *	directly modify the bytes in the array up until Tcl_GetStringFromObj()
 *	has been called on this object.
 *
 * Results:
 *	The new byte array of the specified length.
 *
 * Side effects:
 *	Allocates enough memory for an array of bytes of the requested size.
 *	When growing the array, the old array is copied to the new array; new
 *	bytes are undefined. When shrinking, the old array is truncated to the
 *	specified length.
 *
 *----------------------------------------------------------------------
 */

unsigned char *
Tcl_SetByteArrayLength(
    Tcl_Obj *objPtr,		/* The ByteArray object. */
    int length)			/* New length for internal byte array. */
{
    ByteArray *byteArrayPtr;

    if (Tcl_IsShared(objPtr)) {
	Tcl_Panic("%s called with shared object", "Tcl_SetByteArrayLength");
    }
    if (objPtr->typePtr != &tclByteArrayType) {
	SetByteArrayFromAny(NULL, objPtr);
    }

    byteArrayPtr = GET_BYTEARRAY(objPtr);
    if (length > byteArrayPtr->allocated) {
	byteArrayPtr = ckrealloc(byteArrayPtr, BYTEARRAY_SIZE(length));
	byteArrayPtr->allocated = length;
	SET_BYTEARRAY(objPtr, byteArrayPtr);
    }
    Tcl_InvalidateStringRep(objPtr);
    byteArrayPtr->used = length;
    return byteArrayPtr->bytes;
}

/*
 *----------------------------------------------------------------------
 *
 * SetByteArrayFromAny --
 *
 *	Generate the ByteArray internal rep from the string rep.
 *
 * Results:
 *	The return value is always TCL_OK.
 *
 * Side effects:
 *	A ByteArray object is stored as the internal rep of objPtr.
 *
 *----------------------------------------------------------------------
 */

static int
SetByteArrayFromAny(
    Tcl_Interp *interp,		/* Not used. */
    Tcl_Obj *objPtr)		/* The object to convert to type ByteArray. */
{
    int length;
    const char *src, *srcEnd;
    unsigned char *dst;
    ByteArray *byteArrayPtr;
    Tcl_UniChar ch;

    if (objPtr->typePtr != &tclByteArrayType) {
	src = TclGetStringFromObj(objPtr, &length);
	srcEnd = src + length;

	byteArrayPtr = ckalloc(BYTEARRAY_SIZE(length));
	for (dst = byteArrayPtr->bytes; src < srcEnd; ) {
	    src += Tcl_UtfToUniChar(src, &ch);
	    *dst++ = UCHAR(ch);
	}

	byteArrayPtr->used = dst - byteArrayPtr->bytes;
	byteArrayPtr->allocated = length;

	TclFreeIntRep(objPtr);
	objPtr->typePtr = &tclByteArrayType;
	SET_BYTEARRAY(objPtr, byteArrayPtr);
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * FreeByteArrayInternalRep --
 *
 *	Deallocate the storage associated with a ByteArray data object's
 *	internal representation.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Frees memory.
 *
 *----------------------------------------------------------------------
 */

static void
FreeByteArrayInternalRep(
    Tcl_Obj *objPtr)		/* Object with internal rep to free. */
{
    ckfree(GET_BYTEARRAY(objPtr));
    objPtr->typePtr = NULL;
}

/*
 *----------------------------------------------------------------------
 *
 * DupByteArrayInternalRep --
 *
 *	Initialize the internal representation of a ByteArray Tcl_Obj to a
 *	copy of the internal representation of an existing ByteArray object.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Allocates memory.
 *
 *----------------------------------------------------------------------
 */

static void
DupByteArrayInternalRep(
    Tcl_Obj *srcPtr,		/* Object with internal rep to copy. */
    Tcl_Obj *copyPtr)		/* Object with internal rep to set. */
{
    int length;
    ByteArray *srcArrayPtr, *copyArrayPtr;

    srcArrayPtr = GET_BYTEARRAY(srcPtr);
    length = srcArrayPtr->used;

    copyArrayPtr = ckalloc(BYTEARRAY_SIZE(length));
    copyArrayPtr->used = length;
    copyArrayPtr->allocated = length;
    memcpy(copyArrayPtr->bytes, srcArrayPtr->bytes, (size_t) length);
    SET_BYTEARRAY(copyPtr, copyArrayPtr);

    copyPtr->typePtr = &tclByteArrayType;
}

/*
 *----------------------------------------------------------------------
 *
 * UpdateStringOfByteArray --
 *
 *	Update the string representation for a ByteArray data object. Note:
 *	This procedure does not invalidate an existing old string rep so
 *	storage will be lost if this has not already been done.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The object's string is set to a valid string that results from the
 *	ByteArray-to-string conversion.
 *
 *	The object becomes a string object -- the internal rep is discarded
 *	and the typePtr becomes NULL.
 *
 *----------------------------------------------------------------------
 */

static void
UpdateStringOfByteArray(
    Tcl_Obj *objPtr)		/* ByteArray object whose string rep to
				 * update. */
{
    int i, length, size;
    unsigned char *src;
    char *dst;
    ByteArray *byteArrayPtr;

    byteArrayPtr = GET_BYTEARRAY(objPtr);
    src = byteArrayPtr->bytes;
    length = byteArrayPtr->used;

    /*
     * How much space will string rep need?
     */

    size = length;
    for (i = 0; i < length && size >= 0; i++) {
	if ((src[i] == 0) || (src[i] > 127)) {
	    size++;
	}
    }
    if (size < 0) {
	Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX);
    }

    dst = ckalloc(size + 1);
    objPtr->bytes = dst;
    objPtr->length = size;

    if (size == length) {
	memcpy(dst, src, (size_t) size);
	dst[size] = '\0';
    } else {
	for (i = 0; i < length; i++) {
	    dst += Tcl_UniCharToUtf(src[i], dst);
	}
	*dst = '\0';
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclAppendBytesToByteArray --
 *
 *	This function appends an array of bytes to a byte array object. Note







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







141
142
143
144
145
146
147






































































































































































































































































































































































































































148
149
150
151
152
153
154
 *
 * Converting a String to a ByteArray proceeds by getting the Unicode
 * representation of each character in the String, casting it to a byte by
 * truncating the upper 8 bits, and then storing the byte in the ByteArray.
 * Converting from ByteArray to String and back to ByteArray is not lossy, but
 * converting an arbitrary String to a ByteArray may be.
 */







































































































































































































































































































































































































































/*
 *----------------------------------------------------------------------
 *
 * TclAppendBytesToByteArray --
 *
 *	This function appends an array of bytes to a byte array object. Note
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
		    if (src[size-1] != '\0' && src[size-1] != ' ') {
			break;
		    }
		    size--;
		}
	    }

	    /*
	     * Have to do this #ifdef-fery because (as part of defining
	     * Tcl_NewByteArrayObj) we removed the #def that hides this stuff
	     * normally. If this code ever gets copied to another file, it
	     * should be changed back to the simpler version.
	     */

#ifdef TCL_MEM_DEBUG
	    valuePtr = Tcl_DbNewByteArrayObj(src, size, __FILE__, __LINE__);
#else
	    valuePtr = Tcl_NewByteArrayObj(src, size);
#endif /* TCL_MEM_DEBUG */

	    resultPtr = Tcl_ObjSetVar2(interp, objv[arg], NULL, valuePtr,
		    TCL_LEAVE_ERR_MSG);
	    arg++;
	    if (resultPtr == NULL) {
		DeleteScanNumberCache(numberCachePtr);
		return TCL_ERROR;
	    }







<
<
<
<
<
<
<
<
<
<

<
<







887
888
889
890
891
892
893










894


895
896
897
898
899
900
901
		    if (src[size-1] != '\0' && src[size-1] != ' ') {
			break;
		    }
		    size--;
		}
	    }











	    valuePtr = Tcl_NewByteArrayObj(src, size);


	    resultPtr = Tcl_ObjSetVar2(interp, objv[arg], NULL, valuePtr,
		    TCL_LEAVE_ERR_MSG);
	    arg++;
	    if (resultPtr == NULL) {
		DeleteScanNumberCache(numberCachePtr);
		return TCL_ERROR;
	    }

Changes to generic/tclExecute.c.

4518
4519
4520
4521
4522
4523
4524

4525

4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538

4539

4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
	}
	TRACE(("%.20s %.20s => %s\n", O2S(valuePtr), O2S(value2Ptr),
		O2S(objResultPtr)));
	NEXT_INST_F(1, 2, 1);

    case INST_STR_LEN:
	valuePtr = OBJ_AT_TOS;

	length = Tcl_GetCharLength(valuePtr);

	TclNewIntObj(objResultPtr, length);
	TRACE(("%.20s => %d\n", O2S(valuePtr), length));
	NEXT_INST_F(1, 1, 1);

    case INST_STR_INDEX:
	value2Ptr = OBJ_AT_TOS;
	valuePtr = OBJ_UNDER_TOS;

	/*
	 * Get char length to calulate what 'end' means.
	 */

	length = Tcl_GetCharLength(valuePtr);

	if (TclGetIntForIndexM(interp, value2Ptr, length-1, &index)!=TCL_OK) {

	    goto gotError;
	}

	if ((index < 0) || (index >= length)) {
	    TclNewObj(objResultPtr);
	} else if (TclIsPureByteArray(valuePtr)) {
	    objResultPtr = Tcl_NewByteArrayObj(
		    Tcl_GetByteArrayFromObj(valuePtr, &length)+index, 1);
	} else if (valuePtr->bytes && length == valuePtr->length) {
	    objResultPtr = Tcl_NewStringObj((const char *)
		    valuePtr->bytes+index, 1);
	} else {
	    char buf[TCL_UTF_MAX];
	    Tcl_UniChar ch = Tcl_GetUniChar(valuePtr, index);

	    /*
	     * This could be: Tcl_NewUnicodeObj((const Tcl_UniChar *)&ch, 1)
	     * but creating the object as a string seems to be faster in
	     * practical use.
	     */

	    length = Tcl_UniCharToUtf(ch, buf);
	    objResultPtr = Tcl_NewStringObj(buf, length);
	}

	TRACE(("%.20s %.20s => %s\n", O2S(valuePtr), O2S(value2Ptr),
		O2S(objResultPtr)));
	NEXT_INST_F(1, 2, 1);

    case INST_STR_MATCH:







>

>









|



>
|
>
|


|
<
<
|
<
<
<
<

<
<
<
<
<
<
<
<
<
<
|







4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547


4548




4549










4550
4551
4552
4553
4554
4555
4556
4557
	}
	TRACE(("%.20s %.20s => %s\n", O2S(valuePtr), O2S(value2Ptr),
		O2S(objResultPtr)));
	NEXT_INST_F(1, 2, 1);

    case INST_STR_LEN:
	valuePtr = OBJ_AT_TOS;

	length = Tcl_GetCharLength(valuePtr);

	TclNewIntObj(objResultPtr, length);
	TRACE(("%.20s => %d\n", O2S(valuePtr), length));
	NEXT_INST_F(1, 1, 1);

    case INST_STR_INDEX:
	value2Ptr = OBJ_AT_TOS;
	valuePtr = OBJ_UNDER_TOS;

	/*
	 * Get Unicode char length to calulate what 'end' means.
	 */

	length = Tcl_GetCharLength(valuePtr);

	result = TclGetIntForIndexM(interp, value2Ptr, length - 1, &index);
	if (result != TCL_OK) {
	    goto checkForCatch;
	}

	if ((index >= 0) && (index < length)) {


	    objResultPtr = Tcl_GetRange(valuePtr, index, index);




	} else {










	    TclNewObj(objResultPtr);
	}

	TRACE(("%.20s %.20s => %s\n", O2S(valuePtr), O2S(value2Ptr),
		O2S(objResultPtr)));
	NEXT_INST_F(1, 2, 1);

    case INST_STR_MATCH:

Changes to generic/tclIO.c.

5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
    factor = UTF_EXPANSION_FACTOR;

    if (appendFlag == 0) {
	if (encoding == NULL) {
	    Tcl_SetByteArrayLength(objPtr, 0);
	} else {
	    Tcl_SetObjLength(objPtr, 0);

	    /*
	     * We're going to access objPtr->bytes directly, so we must ensure
	     * that this is actually a string object (otherwise it might have
	     * been pure Unicode).
	     */

	    TclGetString(objPtr);
	}
	offset = 0;
    } else {
	if (encoding == NULL) {
	    Tcl_GetByteArrayFromObj(objPtr, &offset);
	} else {
	    TclGetStringFromObj(objPtr, &offset);







<
<
<
<
<
<
<
<







5711
5712
5713
5714
5715
5716
5717








5718
5719
5720
5721
5722
5723
5724
    factor = UTF_EXPANSION_FACTOR;

    if (appendFlag == 0) {
	if (encoding == NULL) {
	    Tcl_SetByteArrayLength(objPtr, 0);
	} else {
	    Tcl_SetObjLength(objPtr, 0);








	}
	offset = 0;
    } else {
	if (encoding == NULL) {
	    Tcl_GetByteArrayFromObj(objPtr, &offset);
	} else {
	    TclGetStringFromObj(objPtr, &offset);

Changes to generic/tclInt.h.

3092
3093
3094
3095
3096
3097
3098

3099
3100
3101


3102
3103
3104
3105
3106
3107
3108
MODULE_SCOPE void	TclSetBignumIntRep(Tcl_Obj *objPtr,
			    mp_int *bignumValue);
MODULE_SCOPE void	TclSetCmdNameObj(Tcl_Interp *interp, Tcl_Obj *objPtr,
			    Command *cmdPtr);
MODULE_SCOPE void	TclSetDuplicateObj(Tcl_Obj *dupPtr, Tcl_Obj *objPtr);
MODULE_SCOPE void	TclSetProcessGlobalValue(ProcessGlobalValue *pgvPtr,
			    Tcl_Obj *newValue, Tcl_Encoding encoding);

MODULE_SCOPE void	TclSignalExitThread(Tcl_ThreadId id, int result);
MODULE_SCOPE void *	TclStackRealloc(Tcl_Interp *interp, void *ptr,
			    int numBytes);


MODULE_SCOPE int	TclStringMatch(const char *str, int strLen,
			    const char *pattern, int ptnLen, int flags);
MODULE_SCOPE int	TclStringMatchObj(Tcl_Obj *stringObj,
			    Tcl_Obj *patternObj, int flags);
MODULE_SCOPE Tcl_Obj *	TclStringObjReverse(Tcl_Obj *objPtr);
MODULE_SCOPE void	TclSubstCompile(Tcl_Interp *interp, const char *bytes,
			    int numBytes, int flags, int line,







>



>
>







3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
MODULE_SCOPE void	TclSetBignumIntRep(Tcl_Obj *objPtr,
			    mp_int *bignumValue);
MODULE_SCOPE void	TclSetCmdNameObj(Tcl_Interp *interp, Tcl_Obj *objPtr,
			    Command *cmdPtr);
MODULE_SCOPE void	TclSetDuplicateObj(Tcl_Obj *dupPtr, Tcl_Obj *objPtr);
MODULE_SCOPE void	TclSetProcessGlobalValue(ProcessGlobalValue *pgvPtr,
			    Tcl_Obj *newValue, Tcl_Encoding encoding);
MODULE_SCOPE Tcl_UniChar * TclSetUnicodeLength(Tcl_Obj *objPtr, int length);
MODULE_SCOPE void	TclSignalExitThread(Tcl_ThreadId id, int result);
MODULE_SCOPE void *	TclStackRealloc(Tcl_Interp *interp, void *ptr,
			    int numBytes);
MODULE_SCOPE int	TclStringCompare(Tcl_Obj *obj1Ptr, Tcl_Obj *obj2Ptr,
			    int reqlength, int nocase, int equal);
MODULE_SCOPE int	TclStringMatch(const char *str, int strLen,
			    const char *pattern, int ptnLen, int flags);
MODULE_SCOPE int	TclStringMatchObj(Tcl_Obj *stringObj,
			    Tcl_Obj *patternObj, int flags);
MODULE_SCOPE Tcl_Obj *	TclStringObjReverse(Tcl_Obj *objPtr);
MODULE_SCOPE void	TclSubstCompile(Tcl_Interp *interp, const char *bytes,
			    int numBytes, int flags, int line,

Changes to generic/tclStringObj.c.

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92


































93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118



119
120
121
122

123
124







125
126
127
128
129

130
131
132
133

134


135

136


137

138


139

140
141
142
143
144
145
146
			    const Tcl_UniChar *unicode, int numChars);
static void		AppendUtfToUnicodeRep(Tcl_Obj *objPtr,
			    const char *bytes, int numBytes);
static void		AppendUtfToUtfRep(Tcl_Obj *objPtr,
			    const char *bytes, int numBytes);
static void		DupStringInternalRep(Tcl_Obj *objPtr,
			    Tcl_Obj *copyPtr);
static int		ExtendStringRepWithUnicode(Tcl_Obj *objPtr,
			    const Tcl_UniChar *unicode, int numChars);
static void		ExtendUnicodeRepWithString(Tcl_Obj *objPtr,
			    const char *bytes, int numBytes,
			    int numAppendChars);
static void		FillUnicodeRep(Tcl_Obj *objPtr);
static void		FreeStringInternalRep(Tcl_Obj *objPtr);
static void		GrowStringBuffer(Tcl_Obj *objPtr, int needed, int flag);
static void		GrowUnicodeBuffer(Tcl_Obj *objPtr, int needed);
static int		SetStringFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr);
static void		SetUnicodeObj(Tcl_Obj *objPtr,
			    const Tcl_UniChar *unicode, int numChars);
static int		UnicodeLength(const Tcl_UniChar *unicode);
static void		UpdateStringOfString(Tcl_Obj *objPtr);

/*
 * The structure below defines the string Tcl object type by means of
 * functions that can be invoked by generic object code.
 */

const Tcl_ObjType tclStringType = {
    "string",			/* name */
    FreeStringInternalRep,	/* freeIntRepPro */
    DupStringInternalRep,	/* dupIntRepProc */
    UpdateStringOfString,	/* updateStringProc */
    SetStringFromAny		/* setFromAnyProc */
};



































/*
 * The following structure is the internal rep for a String object. It keeps
 * track of how much memory has been used and how much has been allocated for
 * the Unicode and UTF string to enable growing and shrinking of the UTF and
 * Unicode reps of the String object with fewer mallocs. To optimize string
 * length and indexing operations, this structure also stores the number of
 * characters (same of UTF and Unicode!) once that value has been computed.
 *
 * Under normal configurations, what Tcl calls "Unicode" is actually UTF-16
 * restricted to the Basic Multilingual Plane (i.e. U+00000 to U+0FFFF). This
 * can be officially modified by altering the definition of Tcl_UniChar in
 * tcl.h, but do not do that unless you are sure what you're doing!
 */

typedef struct String {
    int numChars;		/* The number of chars in the string. -1 means
				 * this value has not been calculated. >= 0
				 * means that there is a valid Unicode rep, or
				 * that the number of UTF bytes == the number
				 * of chars. */
    int allocated;		/* The amount of space actually allocated for
				 * the UTF string (minus 1 byte for the
				 * termination char). */
    int maxChars;		/* Max number of chars that can fit in the
				 * space allocated for the unicode array. */
    int hasUnicode;		/* Boolean determining whether the string has



				 * a Unicode representation. */
    Tcl_UniChar unicode[1];	/* The array of Unicode chars. The actual size
				 * of this field depends on the 'maxChars'
				 * field above. */

} String;








#define STRING_MAXCHARS \
	(int)(((size_t)UINT_MAX - sizeof(String))/sizeof(Tcl_UniChar))
#define STRING_SIZE(numChars) \
	(sizeof(String) + ((numChars) * sizeof(Tcl_UniChar)))
#define stringCheckLimits(numChars) \

    if ((numChars) < 0 || (numChars) > STRING_MAXCHARS) { \
	Tcl_Panic("max length for a Tcl unicode value (%d chars) exceeded", \
		STRING_MAXCHARS); \
    }

#define stringAlloc(numChars) \


	(String *) ckalloc((unsigned) STRING_SIZE(numChars) )

#define stringRealloc(ptr, numChars) \


    (String *) ckrealloc((ptr), (unsigned) STRING_SIZE(numChars) )

#define stringAttemptRealloc(ptr, numChars) \


    (String *) attemptckrealloc((ptr), (unsigned) STRING_SIZE(numChars) )

#define GET_STRING(objPtr) \
	((String *) (objPtr)->internalRep.otherValuePtr)
#define SET_STRING(objPtr, stringPtr) \
	((objPtr)->internalRep.otherValuePtr = (void *) (stringPtr))

/*
 * TCL STRING GROWTH ALGORITHM







|
<
<
<
<
|
<
<
<



















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
















|
<
<
|
|


|
|
|
>
>
>
|
|
|
|
>


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







58
59
60
61
62
63
64
65




66



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

168

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
			    const Tcl_UniChar *unicode, int numChars);
static void		AppendUtfToUnicodeRep(Tcl_Obj *objPtr,
			    const char *bytes, int numBytes);
static void		AppendUtfToUtfRep(Tcl_Obj *objPtr,
			    const char *bytes, int numBytes);
static void		DupStringInternalRep(Tcl_Obj *objPtr,
			    Tcl_Obj *copyPtr);
static int		SetByteArrayFromAny(Tcl_Interp *interp,




			    Tcl_Obj *objPtr);



static int		SetStringFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr);
static void		SetUnicodeObj(Tcl_Obj *objPtr,
			    const Tcl_UniChar *unicode, int numChars);
static int		UnicodeLength(const Tcl_UniChar *unicode);
static void		UpdateStringOfString(Tcl_Obj *objPtr);

/*
 * The structure below defines the string Tcl object type by means of
 * functions that can be invoked by generic object code.
 */

const Tcl_ObjType tclStringType = {
    "string",			/* name */
    FreeStringInternalRep,	/* freeIntRepPro */
    DupStringInternalRep,	/* dupIntRepProc */
    UpdateStringOfString,	/* updateStringProc */
    SetStringFromAny		/* setFromAnyProc */
};

/*
 * Keep the old bytearray type for backward compatibility with code
 * that uses it to distinguish binary data.
 */

const Tcl_ObjType tclByteArrayType = {
    "bytearray",
    FreeStringInternalRep,	/* freeIntRepPro */
    DupStringInternalRep,	/* dupIntRepProc */
    UpdateStringOfString,	/* updateStringProc */
    SetByteArrayFromAny		/* setFromAnyProc */
};

/*
 * To know what shortcuts can be taken with an object, it is important
 * to know if all characters are within certain ranges.
 * This enum defines the ranges used.
 */

typedef enum {
    Range_Ascii,  /* Characters within range 1-127   */
    Range_Byte,	  /* Characters within range 0-255   */
    Range_Unicode /* Characters within range 0-65535 */
} CharacterRange;

/*
 * An object can hold up to one fixed width storage of the string.
 * This enum defines which is currently present.
 */

typedef enum {
    Storage_None, Storage_Byte, Storage_Unicode
} StorageType;

/*
 * The following structure is the internal rep for a String object. It keeps
 * track of how much memory has been used and how much has been allocated for
 * the Unicode and UTF string to enable growing and shrinking of the UTF and
 * Unicode reps of the String object with fewer mallocs. To optimize string
 * length and indexing operations, this structure also stores the number of
 * characters (same of UTF and Unicode!) once that value has been computed.
 *
 * Under normal configurations, what Tcl calls "Unicode" is actually UTF-16
 * restricted to the Basic Multilingual Plane (i.e. U+00000 to U+0FFFF). This
 * can be officially modified by altering the definition of Tcl_UniChar in
 * tcl.h, but do not do that unless you are sure what you're doing!
 */

typedef struct String {
    int numChars;		/* The number of chars in the string. -1 means
				 * this value has not been calculated, and that


				 * the range field is invalid. */
    size_t allocated;		/* The amount of space actually allocated for
				 * the UTF string (minus 1 byte for the
				 * termination char). */
    size_t uallocated;		/* The amount of space actually allocated for
				 * the fixed width string (minus 2 bytes for
				 * the termination char if it is Unicode). */
    CharacterRange range;	/* The range that all chars in the string are
				 * within. */
    StorageType storageType;	/* Determines if there is a fixed width 
				 * representation stored. */
    union {			/* The array of fixed-width chars. The      */
	unsigned char bytes[4]; /* actual size of this field depends on the */
	Tcl_UniChar unicode[2]; /* 'uallocated' field above.		    */
    } data;
} String;

/*
 * Assertions about String struct:
 *
 * numChars can only be -1 if objPtr->bytes is filled in
 * range can only be Range_Ascii if objPtr->bytes is filled in
 */

#define STRING_UALLOC(numChars)	\
	((numChars) * sizeof(Tcl_UniChar))
#define STRING_SIZE(numBytes)					\
	(sizeof(String) - sizeof(Tcl_UniChar) + (numBytes))
#define BYTEARRAY_UALLOC(numChars) ((numChars) > 4 ? (numChars) : 4)
#define BYTEARRAY_SIZE(ualloc) ((unsigned) (sizeof(String) - 4 + (ualloc)))
#define STRING_NOMEM(numBytes) \

	(Tcl_Panic("unable to alloc %u bytes", STRING_SIZE(numBytes)), \

	 (char *) NULL)
#define stringAlloc(numBytes) \
	(String *) (((numBytes) > INT_MAX - STRING_SIZE(0)) \
	    ? STRING_NOMEM(numBytes) \
	    : ckalloc((unsigned) STRING_SIZE( \
		(numBytes) ? (numBytes) : sizeof(Tcl_UniChar)) ))
#define stringRealloc(ptr, numBytes) \
	(String *) (((numBytes) > INT_MAX - STRING_SIZE(0)) \
	    ? STRING_NOMEM(numBytes) \
	    : ckrealloc((char *) ptr, (unsigned) STRING_SIZE( \
		(numBytes) ? (numBytes) : sizeof(Tcl_UniChar)) ))
#define stringAttemptRealloc(ptr, numBytes) \
	(String *) (((numBytes) > INT_MAX - STRING_SIZE(0)) \
	    ? NULL \
	    : attemptckrealloc((char *) ptr, (unsigned) STRING_SIZE( \
		(numBytes) ? (numBytes) : sizeof(Tcl_UniChar)) ))
#define GET_STRING(objPtr) \
	((String *) (objPtr)->internalRep.otherValuePtr)
#define SET_STRING(objPtr, stringPtr) \
	((objPtr)->internalRep.otherValuePtr = (void *) (stringPtr))

/*
 * TCL STRING GROWTH ALGORITHM
443
444
445
446
447
448
449













































































































































































































































































































450
451
452
453
454
455
456
    SetUnicodeObj(objPtr, unicode, numChars);
    return objPtr;
}

/*
 *----------------------------------------------------------------------
 *













































































































































































































































































































 * Tcl_GetCharLength --
 *
 *	Get the length of the Unicode string from the Tcl object.
 *
 * Results:
 *	Pointer to unicode string representing the unicode object.
 *







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
    SetUnicodeObj(objPtr, unicode, numChars);
    return objPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_NewByteArrayObj --
 *
 *	This procedure is creates a new ByteArray object and initializes it
 *	from the given array of bytes.
 *
 * Results:
 *	The newly create object is returned. This object will have no initial
 *	string representation. The returned object has a ref count of 0.
 *
 * Side effects:
 *	Memory allocated for new object and copy of byte array argument.
 *
 *----------------------------------------------------------------------
 */

#ifdef TCL_MEM_DEBUG
#undef Tcl_NewByteArrayObj

Tcl_Obj *
Tcl_NewByteArrayObj(
    const unsigned char *bytes,	/* The array of bytes used to initialize the
				 * new object. */
    int length)			/* Length of the array of bytes, which must be
				 * >= 0. */
{
    return Tcl_DbNewByteArrayObj(bytes, length, "unknown", 0);
}

#else /* if not TCL_MEM_DEBUG */

Tcl_Obj *
Tcl_NewByteArrayObj(
    const unsigned char *bytes,	/* The array of bytes used to initialize the
				 * new object. */
    int length)			/* Length of the array of bytes, which must be
				 * >= 0. */
{
    Tcl_Obj *objPtr;

    TclNewObj(objPtr);
    Tcl_SetByteArrayObj(objPtr, bytes, length);
    return objPtr;
}
#endif /* TCL_MEM_DEBUG */

/*
 *----------------------------------------------------------------------
 *
 * Tcl_DbNewByteArrayObj --
 *
 *	This procedure is normally called when debugging: i.e., when
 *	TCL_MEM_DEBUG is defined. It is the same as the Tcl_NewByteArrayObj
 *	above except that it calls Tcl_DbCkalloc directly with the file name
 *	and line number from its caller. This simplifies debugging since then
 *	the [memory active] command will report the correct file name and line
 *	number when reporting objects that haven't been freed.
 *
 *	When TCL_MEM_DEBUG is not defined, this procedure just returns the
 *	result of calling Tcl_NewByteArrayObj.
 *
 * Results:
 *	The newly create object is returned. This object will have no initial
 *	string representation. The returned object has a ref count of 0.
 *
 * Side effects:
 *	Memory allocated for new object and copy of byte array argument.
 *
 *----------------------------------------------------------------------
 */

#ifdef TCL_MEM_DEBUG

Tcl_Obj *
Tcl_DbNewByteArrayObj(
    const unsigned char *bytes,	/* The array of bytes used to initialize the
				 * new object. */
    int length,			/* Length of the array of bytes, which must be
				 * >= 0. */
    const char *file,		/* The name of the source file calling this
				 * procedure; used for debugging. */
    int line)			/* Line number in the source file; used for
				 * debugging. */
{
    Tcl_Obj *objPtr;

    TclDbNewObj(objPtr, file, line);
    Tcl_SetByteArrayObj(objPtr, bytes, length);
    return objPtr;
}

#else /* if not TCL_MEM_DEBUG */

Tcl_Obj *
Tcl_DbNewByteArrayObj(
    const unsigned char *bytes,	/* The array of bytes used to initialize the
				 * new object. */
    int length,			/* Length of the array of bytes, which must be
				 * >= 0. */
    const char *file,		/* The name of the source file calling this
				 * procedure; used for debugging. */
    int line)			/* Line number in the source file; used for
				 * debugging. */
{
    return Tcl_NewByteArrayObj(bytes, length);
}
#endif /* TCL_MEM_DEBUG */

/*
 *---------------------------------------------------------------------------
 *
 * Tcl_SetByteArrayObj --
 *
 *	Modify an object to be a ByteArray object and to have the specified
 *	array of bytes as its value.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The object's old string rep and internal rep is freed. Memory
 *	allocated for copy of byte array argument.
 *
 *----------------------------------------------------------------------
 */

void
Tcl_SetByteArrayObj(
    Tcl_Obj *objPtr,		/* Object to initialize as a ByteArray. */
    const unsigned char *bytes,	/* The array of bytes to use as the new
				 * value. */
    int length)			/* Length of the array of bytes, which must be
				 * >= 0. */
{
    size_t uallocated;
    String *stringPtr;

    if (Tcl_IsShared(objPtr)) {
	Tcl_Panic("%s called with shared object", "Tcl_SetByteArrayObj");
    }

    TclFreeIntRep(objPtr);
    Tcl_InvalidateStringRep(objPtr);

    uallocated = BYTEARRAY_UALLOC(length);
    stringPtr = (String *) ckalloc(BYTEARRAY_SIZE(uallocated));
    stringPtr->numChars = length;
    stringPtr->uallocated = uallocated;
    stringPtr->storageType = Storage_Byte;
    stringPtr->range = Range_Byte;
    stringPtr->allocated = 0;
    memcpy(stringPtr->data.bytes, bytes, (size_t) length);

    objPtr->typePtr = &tclByteArrayType;
    SET_STRING(objPtr, stringPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetByteArrayFromObj --
 *
 *	Attempt to get the array of bytes from the Tcl object. If the object
 *	is not already a ByteArray object, an attempt will be made to convert
 *	it to one.
 *
 * Results:
 *	Pointer to array of bytes representing the ByteArray object.
 *
 * Side effects:
 *	Frees old internal rep. Allocates memory for new internal rep.
 *
 *----------------------------------------------------------------------
 */

unsigned char *
Tcl_GetByteArrayFromObj(
    Tcl_Obj *objPtr,		/* The ByteArray object. */
    int *lengthPtr)		/* If non-NULL, filled with length of the
				 * array of bytes in the ByteArray object. */
{
    String *stringPtr;

    SetByteArrayFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (lengthPtr != NULL) {
	*lengthPtr = stringPtr->numChars;
    }
    return stringPtr->data.bytes;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_SetByteArrayLength --
 *
 *	This procedure changes the length of the byte array for this object.
 *	Once the caller has set the length of the array, it is acceptable to
 *	directly modify the bytes in the array up until Tcl_GetStringFromObj()
 *	has been called on this object.
 *
 * Results:
 *	The new byte array of the specified length.
 *
 * Side effects:
 *	Allocates enough memory for an array of bytes of the requested size.
 *	When growing the array, the old array is copied to the new array; new
 *	bytes are undefined. When shrinking, the old array is truncated to the
 *	specified length.
 *
 *----------------------------------------------------------------------
 */

unsigned char *
Tcl_SetByteArrayLength(
    Tcl_Obj *objPtr,		/* The ByteArray object. */
    int length)			/* New length for internal byte array. */
{
    size_t uallocated;
    String *stringPtr;

    if (Tcl_IsShared(objPtr)) {
	Tcl_Panic("%s called with shared object", "Tcl_SetByteArrayLength");
    }
    if (objPtr->typePtr != &tclByteArrayType) {
	SetByteArrayFromAny(NULL, objPtr);
    }

    stringPtr = GET_STRING(objPtr);
    uallocated = BYTEARRAY_UALLOC(length);
    if (uallocated > stringPtr->uallocated) {
	stringPtr = (String *) ckrealloc(
		(char *) stringPtr, BYTEARRAY_SIZE(uallocated));
	stringPtr->uallocated = uallocated;
	SET_STRING(objPtr, stringPtr);
    }
    Tcl_InvalidateStringRep(objPtr);
    stringPtr->numChars = length;
    stringPtr->range = Range_Byte;
    return stringPtr->data.bytes;
}

/*
 *----------------------------------------------------------------------
 *
 * TclSetUnicodeLength --
 *
 *	This procedure changes the length of the unicode array for this object.
 *	Once the caller has set the length of the array, it is acceptable to
 *	directly modify the elements in the array up until Tcl_GetStringFromObj()
 *	has been called on this object.
 *
 * Results:
 *	The new unicode array of the specified length.
 *
 * Side effects:
 *	Allocates enough memory for an array of unichars of the requested size.
 *	When growing the array, the old array is copied to the new array; new
 *	bytes are undefined. When shrinking, the old array is truncated to the
 *	specified length.
 *
 *----------------------------------------------------------------------
 */

Tcl_UniChar *
TclSetUnicodeLength(
    Tcl_Obj *objPtr,		/* The Unicode object. */
    int length)			/* New length for internal unicode array. */
{
    size_t uallocated;
    String *stringPtr;

    if (Tcl_IsShared(objPtr)) {
	Tcl_Panic("%s called with shared object", "Tcl_SetUnicodeLength");
    }

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->storageType != Storage_Unicode) {
	FillUnicodeRep(objPtr);
	stringPtr = GET_STRING(objPtr);
    }

    uallocated = STRING_UALLOC(length);
    if (uallocated > stringPtr->uallocated) {
	stringPtr = (String *) ckrealloc(
		(char *) stringPtr, STRING_SIZE(uallocated));
	stringPtr->uallocated = uallocated;
	SET_STRING(objPtr, stringPtr);
    }
    Tcl_InvalidateStringRep(objPtr);
    stringPtr->numChars = length;
    stringPtr->range = Range_Unicode;
    stringPtr->data.unicode[length] = 0;
    return stringPtr->data.unicode;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetCharLength --
 *
 *	Get the length of the Unicode string from the Tcl object.
 *
 * Results:
 *	Pointer to unicode string representing the unicode object.
 *
487
488
489
490
491
492
493
494

495
496
497


498













499



500
501
502
503
504
505
506
507
508


509
510
511
512
513
514
515
516
     */

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);
    numChars = stringPtr->numChars;

    /*
     * If numChars is unknown, compute it.

     */

    if (numChars == -1) {


	TclNumUtfChars(numChars, objPtr->bytes, objPtr->length);













	stringPtr->numChars = numChars;




#if COMPAT
	if (numChars < objPtr->length) {
	    /*
	     * Since we've just computed the number of chars, and not all UTF
	     * chars are 1-byte long, go ahead and populate the unicode
	     * string.
	     */



	    FillUnicodeRep(objPtr);
	}
#endif
    }
    return numChars;
}

/*







|
>


|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
|
|
|

<
|
<


>
>
|







833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868

869

870
871
872
873
874
875
876
877
878
879
880
881
     */

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);
    numChars = stringPtr->numChars;

    /*
     * If numChars is unknown, then calculate the number of characaters while
     * populating the Unicode string.
     */

    if (stringPtr->numChars == -1) {
	register int i = objPtr->length;
	register unsigned char *str = (unsigned char *) objPtr->bytes;

	/*
	 * This is a speed sensitive function, so run specially over the
	 * string to count continuous ascii characters before resorting to the
	 * Tcl_NumUtfChars call. This is a long form of:
	 stringPtr->numChars = Tcl_NumUtfChars(objPtr->bytes,objPtr->length);
	 *
	 * TODO: Consider macro-izing this.
	 */

	while (i && (*str < 0xC0)) {
	    i--;
	    str++;
	}
	stringPtr->numChars = objPtr->length - i;
	if (i) {
	    stringPtr->numChars += Tcl_NumUtfChars(objPtr->bytes
		    + (objPtr->length - i), i);
	}

	if (stringPtr->numChars == objPtr->length) {
	    /*

	     * All UTF chars are 1-byte long.

	     */

	    stringPtr->range = Range_Ascii;
	} else {
	    stringPtr->range = Range_Unicode;
	}
#endif
    }
    return numChars;
}

/*
553
554
555
556
557
558
559
560




561
562


563
564
565
566
567
568
569
570

571
572


573
574
575
576
577
578
579
    /*
     * OK, need to work with the object as a string.
     */

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->hasUnicode == 0) {




	/*
	 * If numChars is unknown, compute it.


	 */

	if (stringPtr->numChars == -1) {
	    TclNumUtfChars(stringPtr->numChars, objPtr->bytes, objPtr->length);
	}
	if (stringPtr->numChars == objPtr->length) {
	    return (Tcl_UniChar) objPtr->bytes[index];
	}

	FillUnicodeRep(objPtr);
	stringPtr = GET_STRING(objPtr);


    }
    return stringPtr->unicode[index];
}

/*
 *----------------------------------------------------------------------
 *







|
>
>
>
>

<
>
>


|
|
<
|
|
|
>
|
|
>
>







918
919
920
921
922
923
924
925
926
927
928
929
930

931
932
933
934
935
936

937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
    /*
     * OK, need to work with the object as a string.
     */

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->numChars == -1) {
	Tcl_GetCharLength(objPtr);
    }

    if (stringPtr->range == Range_Ascii) {
	/*

	 * All of the characters in the Utf string are 1 byte chars.  We get
	 * the Utf string and convert the index'th byte to a Unicode character.
	 */

	unichar = (Tcl_UniChar) objPtr->bytes[index];
    } else if ((stringPtr->storageType == Storage_Byte) && 

	    (stringPtr->range == Range_Byte)) {
	unichar = (Tcl_UniChar) stringPtr->data.bytes[index];
    } else {
	if (stringPtr->storageType != Storage_Unicode) {
	    FillUnicodeRep(objPtr);
	    stringPtr = GET_STRING(objPtr);
	}
	unichar = stringPtr->data.unicode[index];
    }
    return stringPtr->unicode[index];
}

/*
 *----------------------------------------------------------------------
 *
594
595
596
597
598
599
600

601




















602
603
604
605
606
607
608
 */

Tcl_UniChar *
Tcl_GetUnicode(
    Tcl_Obj *objPtr)		/* The object to find the unicode string
				 * for. */
{

    return Tcl_GetUnicodeFromObj(objPtr, NULL);




















}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetUnicodeFromObj --
 *







>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
 */

Tcl_UniChar *
Tcl_GetUnicode(
    Tcl_Obj *objPtr)		/* The object to find the unicode string
				 * for. */
{
    String *stringPtr;

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->storageType != Storage_Unicode) {
	/*
	 * Since this function must return a unicode string, and one has
	 * not yet been stored, force the Unicode to be calculated and stored
	 * now.
	 */

	FillUnicodeRep(objPtr);

	/*
	 * We need to fetch the pointer again because we have just reallocated
	 * the structure to make room for the Unicode data.
	 */

	stringPtr = GET_STRING(objPtr);
    }
    return stringPtr->data.unicode;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetUnicodeFromObj --
 *
629
630
631
632
633
634
635
636






637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
				 * NULL, no length is stored. */
{
    String *stringPtr;

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->hasUnicode == 0) {






	FillUnicodeRep(objPtr);
	stringPtr = GET_STRING(objPtr);
    }

    if (lengthPtr != NULL) {
	*lengthPtr = stringPtr->numChars;
    }
    return stringPtr->unicode;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetRange --
 *







|
>
>
>
>
>
>







|







1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
				 * NULL, no length is stored. */
{
    String *stringPtr;

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->storageType != Storage_Unicode) {
	/*
	 * Since this function must return a unicode string, and one has
	 * not yet been stored, force the Unicode to be calculated and stored
	 * now.
	 */

	FillUnicodeRep(objPtr);
	stringPtr = GET_STRING(objPtr);
    }

    if (lengthPtr != NULL) {
	*lengthPtr = stringPtr->numChars;
    }
    return stringPtr->data.unicode;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetRange --
 *
687
688
689
690
691
692
693
694






695
696


697
698
699
700
701
702
703
704
705
706

707
708
709
710
711



712

713

714
715
716


717
718
719
720
721
722
723
724
725
    /*
     * OK, need to work with the object as a string.
     */

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->hasUnicode == 0) {






	/*
	 * If numChars is unknown, compute it.


	 */

	if (stringPtr->numChars == -1) {
	    TclNumUtfChars(stringPtr->numChars, objPtr->bytes, objPtr->length);
	}
	if (stringPtr->numChars == objPtr->length) {
	    newObjPtr = Tcl_NewStringObj(objPtr->bytes + first, last-first+1);

	    /*
	     * Since we know the char length of the result, store it.

	     */

	    SetStringFromAny(NULL, newObjPtr);
	    stringPtr = GET_STRING(newObjPtr);
	    stringPtr->numChars = newObjPtr->length;



	    return newObjPtr;

	}

	FillUnicodeRep(objPtr);
	stringPtr = GET_STRING(objPtr);
    }



    return Tcl_NewUnicodeObj(stringPtr->unicode + first, last-first+1);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_SetStringObj --
 *







|
>
>
>
>
>
>

<
>
>


<
<
<
<
|

|
|
>
|

|
|
|
>
>
>
|
>
|
>
|
|
|
>
>
|
|







1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100

1101
1102
1103
1104




1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
    /*
     * OK, need to work with the object as a string.
     */

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->numChars == -1) {
	Tcl_GetCharLength(objPtr);
    }

    if (stringPtr->range == Range_Ascii) {
	char *str = TclGetString(objPtr);

	/*

	 * All of the characters in the Utf string are 1 byte chars. Create a
	 * new string object containing the specified range of chars.
	 */





	newObjPtr = Tcl_NewStringObj(str+first, last-first+1);

	/*
	 * Since we know the new string only has 1-byte chars, we can set it's
	 * numChars field.
	 */

	SetStringFromAny(NULL, newObjPtr);
	stringPtr = GET_STRING(newObjPtr);
	stringPtr->numChars = last-first+1;
	stringPtr->range = Range_Ascii;
    } else if ((stringPtr->storageType == Storage_Byte) && 
	    (stringPtr->range == Range_Byte)) {
	newObjPtr = Tcl_NewByteArrayObj(stringPtr->data.bytes + first,
		last-first+1);
    } else {
	if (stringPtr->storageType != Storage_Unicode) {
	    FillUnicodeRep(objPtr);
	    stringPtr = GET_STRING(objPtr);
	}
	newObjPtr = Tcl_NewUnicodeObj(stringPtr->data.unicode + first,
		last-first+1);
    }
    return newObjPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_SetStringObj --
 *
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	If the size of objPtr's string representation is greater than length,
 *	then it is reduced to length and a new terminating null byte is stored
 *	in the strength. If the length of the string representation is greater
 *	than length, the storage space is reallocated to the given length; a
 *	null byte is stored at the end, but other bytes past the end of the
 *	original string representation are undefined. The object's internal
 *	representation is changed to "expendable string".
 *
 *----------------------------------------------------------------------
 */







|







1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	If the size of objPtr's string representation is greater than length,
 *	then it is reduced to length and a new terminating null byte is stored
 *	in the string. If the length of the string representation is greater
 *	than length, the storage space is reallocated to the given length; a
 *	null byte is stored at the end, but other bytes past the end of the
 *	original string representation are undefined. The object's internal
 *	representation is changed to "expendable string".
 *
 *----------------------------------------------------------------------
 */
817
818
819
820
821
822
823
824
825
826
827

828
829
830
831
832
833
834
835
836

837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876









877
878
879
880
881
882
883
	Tcl_Panic("%s called with shared object", "Tcl_SetObjLength");
    }

    if (objPtr->bytes && objPtr->length == length) {
	return;
    }

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (objPtr->bytes != NULL) {

	/*
	 * Change length of an existing string rep.
	 */
	if (length > stringPtr->allocated) {
	    /*
	     * Need to enlarge the buffer.
	     */
	    if (objPtr->bytes == tclEmptyStringRep) {
		objPtr->bytes = ckalloc(length + 1);

	    } else {
		objPtr->bytes = ckrealloc(objPtr->bytes, length + 1);
	    }
	    stringPtr->allocated = length;
	}

	objPtr->length = length;
	objPtr->bytes[length] = 0;

	/*
	 * Invalidate the unicode data.
	 */

	stringPtr->numChars = -1;
	stringPtr->hasUnicode = 0;
    } else {
	/*
	 * Changing length of pure unicode string.
	 */

	stringCheckLimits(length);
	if (length > stringPtr->maxChars) {
	    stringPtr = stringRealloc(stringPtr, length);
	    SET_STRING(objPtr, stringPtr);
	    stringPtr->maxChars = length;
	}

	/*
	 * Mark the new end of the unicode string
	 */

	stringPtr->numChars = length;
	stringPtr->unicode[length] = 0;
	stringPtr->hasUnicode = 1;

	/*
	 * Can only get here when objPtr->bytes == NULL. No need to invalidate
	 * the string rep.
	 */
    }









}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_AttemptSetObjLength --
 *







|
|
|
|
>



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

|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<




|
>
>
>
>
>
>
>
>
>







1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242



1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253



























1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
	Tcl_Panic("%s called with shared object", "Tcl_SetObjLength");
    }

    if (objPtr->bytes && objPtr->length == length) {
	return;
    }

    if (objPtr->bytes == NULL) {
	UpdateStringOfString(objPtr);
    }

    if (length > (int) stringPtr->allocated) {
	/*
	 * Change length of an existing string rep.
	 */




	if (objPtr->bytes != tclEmptyStringRep) {
	    objPtr->bytes = ckrealloc((char *)objPtr->bytes,
		    (unsigned)(length+1));
	} else {
	    objPtr->bytes = ckalloc((unsigned) (length+1));
	}
	stringPtr->allocated = length;
    }

    objPtr->length = length;
    if (objPtr->bytes != tclEmptyStringRep) {



























	/*
	 * Can only get here when objPtr->bytes == NULL. No need to invalidate
	 * the string rep.
	 */

	objPtr->bytes[length] = 0;
    }

    /*
     * Invalidate the unicode data.
     */

    stringPtr->numChars = -1;
    stringPtr->storageType = Storage_None;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_AttemptSetObjLength --
 *
923
924
925
926
927
928
929
930






931
932
933
934
935
936
937
938
939
940
941
942
943
944


945


946
947
948

949
950
951
952
953





954

955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
    if (objPtr->bytes && objPtr->length == length) {
	return 1;
    }

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (objPtr->bytes != NULL) {






	/*
	 * Change length of an existing string rep.
	 */
	if (length > stringPtr->allocated) {
	    /*
	     * Need to enlarge the buffer.
	     */

	    char *newBytes;

	    if (objPtr->bytes == tclEmptyStringRep) {
		newBytes = attemptckalloc(length + 1);
	    } else {
		newBytes = attemptckrealloc(objPtr->bytes, length + 1);


	    }


	    if (newBytes == NULL) {
		return 0;
	    }

	    objPtr->bytes = newBytes;
	    stringPtr->allocated = length;
	}

	objPtr->length = length;





	objPtr->bytes[length] = 0;


	/*
	 * Invalidate the unicode data.
	 */

	stringPtr->numChars = -1;
	stringPtr->hasUnicode = 0;
    } else {
	/*
	 * Changing length of pure unicode string.
	 */

	if (length > STRING_MAXCHARS) {
	    return 0;
	}
	if (length > stringPtr->maxChars) {
	    stringPtr = stringAttemptRealloc(stringPtr, length);
	    if (stringPtr == NULL) {
		return 0;
	    }
	    SET_STRING(objPtr, stringPtr);
	    stringPtr->maxChars = length;
	}

	/*
	 * Mark the new end of the unicode string.
	 */

	stringPtr->unicode[length] = 0;
	stringPtr->numChars = length;
	stringPtr->hasUnicode = 1;

	/*
	 * Can only get here when objPtr->bytes == NULL. No need to invalidate
	 * the string rep.
	 */
    }
    return 1;
}

/*
 *---------------------------------------------------------------------------
 *
 * Tcl_SetUnicodeObj --







|
>
>
>
>
>
>










|
<
<
|
>
>

>
>
|


>
|
|
|

|
>
>
>
>
>

>

|
|
|

|
|
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338


1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367




1368

























1369
1370
1371
1372
1373
1374
1375
    if (objPtr->bytes && objPtr->length == length) {
	return 1;
    }

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (objPtr->bytes == NULL) {
	UpdateStringOfString(objPtr);
    }

    if (length > (int) stringPtr->allocated) {
	char *new;

	/*
	 * Change length of an existing string rep.
	 */
	if (length > stringPtr->allocated) {
	    /*
	     * Need to enlarge the buffer.
	     */

	    char *newBytes;

	if (objPtr->bytes != tclEmptyStringRep) {


	    new = attemptckrealloc(objPtr->bytes, (unsigned)(length+1));
	    if (new == NULL) {
		return 0;
	    }
	} else {
	    new = attemptckalloc((unsigned) (length+1));
	    if (new == NULL) {
		return 0;
	    }
	}
	objPtr->bytes = new;
	stringPtr->allocated = length;
    }

    objPtr->length = length;
    if (objPtr->bytes != tclEmptyStringRep) {
	/*
	 * Ensure the string is NULL-terminated.
	 */

	objPtr->bytes[length] = 0;
    }

    /*
     * Invalidate the unicode data.
     */

    stringPtr->numChars = -1;
    stringPtr->storageType = Storage_None;






























    return 1;
}

/*
 *---------------------------------------------------------------------------
 *
 * Tcl_SetUnicodeObj --
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068


1069
1070
1071
1072
1073
1074
1075
     */

    stringCheckLimits(numChars);
    stringPtr = stringAlloc(numChars);
    SET_STRING(objPtr, stringPtr);
    objPtr->typePtr = &tclStringType;

    stringPtr->maxChars = numChars;
    memcpy(stringPtr->unicode, unicode, numChars * sizeof(Tcl_UniChar));
    stringPtr->unicode[numChars] = 0;
    stringPtr->numChars = numChars;
    stringPtr->hasUnicode = 1;



    TclInvalidateStringRep(objPtr);
    stringPtr->allocated = 0;
}

/*
 *----------------------------------------------------------------------







|
|
|
|
|
>
>







1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
     */

    stringCheckLimits(numChars);
    stringPtr = stringAlloc(numChars);
    SET_STRING(objPtr, stringPtr);
    objPtr->typePtr = &tclStringType;

    stringPtr->numChars = numChars;
    stringPtr->uallocated = uallocated;
    stringPtr->storageType = Storage_Unicode;
    stringPtr->range = Range_Unicode;
    stringPtr->allocated = 0;
    memcpy(stringPtr->data.unicode, unicode, uallocated);
    stringPtr->data.unicode[numChars] = 0;

    TclInvalidateStringRep(objPtr);
    stringPtr->allocated = 0;
}

/*
 *----------------------------------------------------------------------
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
     * If objPtr has a valid Unicode rep, then append the Unicode conversion
     * of "bytes" to the objPtr's Unicode rep, otherwise append "bytes" to
     * objPtr's string rep.
     */

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    if (stringPtr->hasUnicode && stringPtr->numChars > 0) {
	AppendUtfToUnicodeRep(objPtr, bytes, toCopy);
    } else {
	AppendUtfToUtfRep(objPtr, bytes, toCopy);
    }

    if (length <= limit) {
	return;
    }

    stringPtr = GET_STRING(objPtr);
    if (stringPtr->hasUnicode && stringPtr->numChars > 0) {
	AppendUtfToUnicodeRep(objPtr, ellipsis, strlen(ellipsis));
    } else {
	AppendUtfToUtfRep(objPtr, ellipsis, strlen(ellipsis));
    }
}

/*
 *----------------------------------------------------------------------







|
<










|
|







1509
1510
1511
1512
1513
1514
1515
1516

1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
     * If objPtr has a valid Unicode rep, then append the Unicode conversion
     * of "bytes" to the objPtr's Unicode rep, otherwise append "bytes" to
     * objPtr's string rep.
     */

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);
    if (stringPtr->storageType == Storage_Unicode) {

	AppendUtfToUnicodeRep(objPtr, bytes, toCopy);
    } else {
	AppendUtfToUtfRep(objPtr, bytes, toCopy);
    }

    if (length <= limit) {
	return;
    }

    stringPtr = GET_STRING(objPtr);
    if (stringPtr->storageType == Storage_Unicode) {
	AppendUtfToUnicodeRep(objPtr, ellipsis, -1);
    } else {
	AppendUtfToUtfRep(objPtr, ellipsis, strlen(ellipsis));
    }
}

/*
 *----------------------------------------------------------------------
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239

    /*
     * If objPtr has a valid Unicode rep, then append the "unicode" to the
     * objPtr's Unicode rep, otherwise the UTF conversion of "unicode" to
     * objPtr's string rep.
     */

    if (stringPtr->hasUnicode
#if COMPAT
		&& stringPtr->numChars > 0
#endif
	    ) {
	AppendUnicodeToUnicodeRep(objPtr, unicode, length);
    } else {
	AppendUnicodeToUtfRep(objPtr, unicode, length);
    }
}

/*







|
<
<
<
<







1599
1600
1601
1602
1603
1604
1605
1606




1607
1608
1609
1610
1611
1612
1613

    /*
     * If objPtr has a valid Unicode rep, then append the "unicode" to the
     * objPtr's Unicode rep, otherwise the UTF conversion of "unicode" to
     * objPtr's string rep.
     */

    if (stringPtr->storageType == Storage_Unicode) {




	AppendUnicodeToUnicodeRep(objPtr, unicode, length);
    } else {
	AppendUnicodeToUtfRep(objPtr, unicode, length);
    }
}

/*
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325






1326
1327

1328
1329

1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342

1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357

1358

1359
1360
1361
1362
1363
1364
1365
    stringPtr = GET_STRING(objPtr);

    /*
     * If objPtr has a valid Unicode rep, then get a Unicode string from
     * appendObjPtr and append it.
     */

    if (stringPtr->hasUnicode 
#if COMPAT
		&& stringPtr->numChars > 0
#endif
	    ) {
	/*
	 * If appendObjPtr is not of the "String" type, don't convert it.
	 */

	if (appendObjPtr->typePtr == &tclStringType) {






	    Tcl_UniChar *unicode =
		    Tcl_GetUnicodeFromObj(appendObjPtr, &numChars);


	    AppendUnicodeToUnicodeRep(objPtr, unicode, numChars);

	} else {
	    bytes = TclGetStringFromObj(appendObjPtr, &length);
	    AppendUtfToUnicodeRep(objPtr, bytes, length);
	}
	return;
    }

    /*
     * Append to objPtr's UTF string rep. If we know the number of characters
     * in both objects before appending, then set the combined number of
     * characters in the final (appended-to) object.
     */


    bytes = TclGetStringFromObj(appendObjPtr, &length);

    numChars = stringPtr->numChars;
    if ((numChars >= 0) && (appendObjPtr->typePtr == &tclStringType)) {
	String *appendStringPtr = GET_STRING(appendObjPtr);
	appendNumChars = appendStringPtr->numChars;
    }

    AppendUtfToUtfRep(objPtr, bytes, length);

    if (numChars >= 0 && appendNumChars >= 0
#if COMPAT
		&& appendNumChars == length
#endif
	    ) {

	stringPtr->numChars = numChars + appendNumChars;

    }
}

/*
 *----------------------------------------------------------------------
 *
 * AppendUnicodeToUnicodeRep --







|
<
|
<
<





>
>
>
>
>
>
|
|
>
|
|
>













>










<
<
<
<
|
>
|
>







1683
1684
1685
1686
1687
1688
1689
1690

1691


1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732




1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
    stringPtr = GET_STRING(objPtr);

    /*
     * If objPtr has a valid Unicode rep, then get a Unicode string from
     * appendObjPtr and append it.
     */

    stringPtr = GET_STRING(objPtr);

    if (stringPtr->storageType == Storage_Unicode) {


	/*
	 * If appendObjPtr is not of the "String" type, don't convert it.
	 */

	if (appendObjPtr->typePtr == &tclStringType) {
	    stringPtr = GET_STRING(appendObjPtr);
	    if (stringPtr->storageType != Storage_Unicode) {
		/*
		 * If appendObjPtr is a string obj with no valid Unicode rep,
		 * then fill its unicode rep.
		 */

		FillUnicodeRep(appendObjPtr);
		stringPtr = GET_STRING(appendObjPtr);
	    }
	    AppendUnicodeToUnicodeRep(objPtr, stringPtr->data.unicode,
		    stringPtr->numChars);
	} else {
	    bytes = TclGetStringFromObj(appendObjPtr, &length);
	    AppendUtfToUnicodeRep(objPtr, bytes, length);
	}
	return;
    }

    /*
     * Append to objPtr's UTF string rep. If we know the number of characters
     * in both objects before appending, then set the combined number of
     * characters in the final (appended-to) object.
     */

    Tcl_GetString(objPtr);
    bytes = TclGetStringFromObj(appendObjPtr, &length);

    numChars = stringPtr->numChars;
    if ((numChars >= 0) && (appendObjPtr->typePtr == &tclStringType)) {
	String *appendStringPtr = GET_STRING(appendObjPtr);
	appendNumChars = appendStringPtr->numChars;
    }

    AppendUtfToUtfRep(objPtr, bytes, length);





    if (allOneByteChars) {
	stringPtr = GET_STRING(objPtr);
	stringPtr->numChars = numChars;
	stringPtr->range = Range_Ascii;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * AppendUnicodeToUnicodeRep --
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
    }

    /*
     * Copy the new string onto the end of the old string, then add the
     * trailing null.
     */

    memcpy(stringPtr->unicode + stringPtr->numChars, unicode,
	    appendNumChars * sizeof(Tcl_UniChar));
    stringPtr->unicode[numChars] = 0;
    stringPtr->numChars = numChars;
    stringPtr->allocated = 0;

    TclInvalidateStringRep(objPtr);
}

/*
 *----------------------------------------------------------------------
 *







|

|

|







1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
    }

    /*
     * Copy the new string onto the end of the old string, then add the
     * trailing null.
     */

    memcpy(stringPtr->data.unicode + stringPtr->numChars, unicode,
	    appendNumChars * sizeof(Tcl_UniChar));
    stringPtr->data.unicode[numChars] = 0;
    stringPtr->numChars = numChars;
    stringPtr->range = Range_Unicode;

    TclInvalidateStringRep(objPtr);
}

/*
 *----------------------------------------------------------------------
 *
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
    }

    /*
     * Invalidate the unicode data.
     */

    stringPtr->numChars = -1;
    stringPtr->hasUnicode = 0;

    memcpy(objPtr->bytes + oldLength, bytes, numBytes);
    objPtr->bytes[newLength] = 0;
    objPtr->length = newLength;
}

/*







|







1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
    }

    /*
     * Invalidate the unicode data.
     */

    stringPtr->numChars = -1;
    stringPtr->storageType = Storage_None;

    memcpy(objPtr->bytes + oldLength, bytes, numBytes);
    objPtr->bytes[newLength] = 0;
    objPtr->length = newLength;
}

/*
2631
2632
2633
2634
2635
2636
2637

































































































2638
2639
2640
2641
2642
2643
2644

    va_start(argList, format);
    AppendPrintfToObjVA(objPtr, format, argList);
    va_end(argList);
    return objPtr;
}


































































































/*
 *---------------------------------------------------------------------------
 *
 * TclStringObjReverse --
 *
 *	Implements the [string reverse] operation.
 *







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119

    va_start(argList, format);
    AppendPrintfToObjVA(objPtr, format, argList);
    va_end(argList);
    return objPtr;
}

int
TclStringCompare(
    Tcl_Obj *obj1Ptr,
    Tcl_Obj *obj2Ptr,
    int reqlength,
    int nocase,
    int equal)
{
    /*
     * Remember to keep code here in some sync with the byte-compiled
     * versions in tclExecute.c (INST_STR_EQ, INST_STR_NEQ and
     * INST_STR_CMP as well as the expr string comparison in
     * INST_EQ/INST_NEQ/INST_LT/...).
     */

    int match, length;
    typedef int (*strCmpFn_t)(const char *, const char *, unsigned int);
    strCmpFn_t strCmpFn;
    char *string1, *string2;
    int length1, length2;

    if ((reqlength == 0) || (obj1Ptr == obj2Ptr)) {
	/*
	 * Always match at 0 chars of if it is the same obj.
	 */

	return 0;
    } else if (!nocase && obj1Ptr->typePtr == &tclByteArrayType &&
	    obj2Ptr->typePtr == &tclByteArrayType) {
	/*
	 * Use binary versions of comparisons since that won't cause undue
	 * type conversions and it is much faster. Only do this if we're
	 * case-sensitive (which is all that really makes sense with byte
	 * arrays anyway, and we have no memcasecmp() for some
	 * reason... :^)
	 */

	string1 = (char *) Tcl_GetByteArrayFromObj(obj1Ptr, &length1);
	string2 = (char *) Tcl_GetByteArrayFromObj(obj2Ptr, &length2);
	strCmpFn = (strCmpFn_t) memcmp;
    } else if ((obj1Ptr->typePtr == &tclStringType)
	    && (obj2Ptr->typePtr == &tclStringType)) {
	/*
	 * Do a unicode-specific comparison if both of the args are of
	 * String type. In benchmark testing this proved the most
	 * efficient check between the unicode and string comparison
	 * operations.
	 */

	string1 = (char *) Tcl_GetUnicodeFromObj(obj1Ptr, &length1);
	string2 = (char *) Tcl_GetUnicodeFromObj(obj2Ptr, &length2);
	strCmpFn = (strCmpFn_t)
		(nocase ? Tcl_UniCharNcasecmp : Tcl_UniCharNcmp);
    } else {
	/*
	 * As a catch-all we will work with UTF-8. We cannot use memcmp()
	 * as that is unsafe with any string containing NULL (\xC0\x80 in
	 * Tcl's utf rep). We can use the more efficient TclpUtfNcmp2 if
	 * we are case-sensitive and no specific length was requested.
	 */

	string1 = (char *) Tcl_GetStringFromObj(obj1Ptr, &length1);
	string2 = (char *) Tcl_GetStringFromObj(obj2Ptr, &length2);
	if ((reqlength < 0) && !nocase) {
	    strCmpFn = (strCmpFn_t) TclpUtfNcmp2;
	} else {
	    length1 = Tcl_NumUtfChars(string1, length1);
	    length2 = Tcl_NumUtfChars(string2, length2);
	    strCmpFn = (strCmpFn_t)
		    (nocase ? Tcl_UtfNcasecmp : Tcl_UtfNcmp);
	}
    }

    if (equal && (reqlength < 0) && (length1 != length2)) {
	return 1;
    }

    length = (length1 < length2) ? length1 : length2;
    if (reqlength > 0 && reqlength < length) {
	length = reqlength;
    } else if (reqlength < 0) {
	/*
	 * The requested length is negative, so we ignore it by
	 * setting it to length + 1 so we correct the match var.
	 */

	reqlength = length + 1;
    }

    match = strCmpFn(string1, string2, (unsigned) length);
    if ((match == 0) && (reqlength > length)) {
	match = length1 - length2;
    }

    return (match > 0) ? 1 : (match < 0) ? -1 : 0;
}

/*
 *---------------------------------------------------------------------------
 *
 * TclStringObjReverse --
 *
 *	Implements the [string reverse] operation.
 *
2661
2662
2663
2664
2665
2666
2667





2668




2669

2670










2671
2672
2673
2674
2675
2676
2677
    char *src = NULL, *dest = NULL;
    Tcl_UniChar *usrc = NULL, *udest = NULL;
    Tcl_Obj *resultPtr = NULL;

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);






    if (stringPtr->hasUnicode == 0) {




	if (stringPtr->numChars == -1) {

	    TclNumUtfChars(stringPtr->numChars, objPtr->bytes, objPtr->length);










	}
	if (stringPtr->numChars <= 1) {
	    return objPtr;
	}
	if (stringPtr->numChars == objPtr->length) {
	    /*
	     * All one-byte chars. Reverse in objPtr->bytes.







>
>
>
>
>
|
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>







3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
    char *src = NULL, *dest = NULL;
    Tcl_UniChar *usrc = NULL, *udest = NULL;
    Tcl_Obj *resultPtr = NULL;

    SetStringFromAny(NULL, objPtr);
    stringPtr = GET_STRING(objPtr);

    /*
     * Make sure we have either fixed width UTF data or Unicode data present.
     */

    if ((stringPtr->storageType != Storage_Unicode) &&
	    (stringPtr->range != Range_Ascii)) {
	FillUnicodeRep(objPtr);
	stringPtr = GET_STRING(objPtr);
    }

    if (stringPtr->storageType == Storage_Unicode) {
	Tcl_UniChar *source = stringPtr->data.unicode;

	if (Tcl_IsShared(objPtr)) {
	    Tcl_UniChar *dest;

	    Tcl_Obj *resultPtr = Tcl_NewObj();
	    dest = TclSetUnicodeLength(resultPtr, numChars);

	    while (i < numChars) {
		dest[i++] = source[lastCharIdx--];
	    }
	    return resultPtr;
	}
	if (stringPtr->numChars <= 1) {
	    return objPtr;
	}
	if (stringPtr->numChars == objPtr->length) {
	    /*
	     * All one-byte chars. Reverse in objPtr->bytes.
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790

2791


2792

2793
2794




2795

2796
2797












2798







2799

2800



2801













2802
2803
2804

2805



2806



2807


2808
2809
2810
2811
2812
2813
2814
2815

/*
 *---------------------------------------------------------------------------
 *
 * FillUnicodeRep --
 *
 *	Populate the Unicode internal rep with the Unicode form of its string
 *	rep. The object must alread have a "String" internal rep.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Reallocates the String internal rep.
 *
 *---------------------------------------------------------------------------
 */

static void
FillUnicodeRep(
    Tcl_Obj *objPtr)		/* The object in which to fill the unicode
				 * rep. */
{
    String *stringPtr = GET_STRING(objPtr);

    ExtendUnicodeRepWithString(objPtr, objPtr->bytes, objPtr->length,
	    stringPtr->numChars);
}

static void
ExtendUnicodeRepWithString(
    Tcl_Obj *objPtr,
    const char *bytes,
    int numBytes,
    int numAppendChars)
{
    String *stringPtr = GET_STRING(objPtr);
    int needed, numOrigChars = 0;
    Tcl_UniChar *dst;


    if (stringPtr->hasUnicode) {


	numOrigChars = stringPtr->numChars;

    }
    if (numAppendChars == -1) {




	TclNumUtfChars(numAppendChars, bytes, numBytes);

    }
    needed = numOrigChars + numAppendChars;












    stringCheckLimits(needed);







	

    if (needed > stringPtr->maxChars) {



	GrowUnicodeBuffer(objPtr, needed);













	stringPtr = GET_STRING(objPtr);
    }


    stringPtr->hasUnicode = 1;



    stringPtr->numChars = needed;



    for (dst=stringPtr->unicode + numOrigChars; numAppendChars-- > 0; dst++) {


	bytes += TclUtfToUniChar(bytes, dst);
    }
    *dst = 0;
}

/*
 *----------------------------------------------------------------------
 *







|















|
|
<
<
<
|
<
<
<
|
<
<
<
<
<


>
|
>
>
|
>

|
>
>
>
>
|
>

|
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
|
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
|
>
>
>
|
>
>
>
|
>
>
|







3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270



3271



3272





3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353

/*
 *---------------------------------------------------------------------------
 *
 * FillUnicodeRep --
 *
 *	Populate the Unicode internal rep with the Unicode form of its string
 *	rep. The object must already have a "String" internal rep.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Reallocates the String internal rep.
 *
 *---------------------------------------------------------------------------
 */

static void
FillUnicodeRep(
    Tcl_Obj *objPtr)		/* The object in which to fill the unicode
				 * rep. */
{
    String *stringPtr;
    size_t uallocated;



    char *srcEnd, *src = objPtr->bytes;



    unsigned char *srcByte;





    Tcl_UniChar *dst;

    stringPtr = GET_STRING(objPtr);
    if (stringPtr->numChars == -1) {
	Tcl_GetCharLength(objPtr);
    }
    if (stringPtr->storageType == Storage_Unicode) {
	return;
    }

    if (stringPtr->storageType == Storage_Byte) {
	/*
	 * Convert ualloc count to unicode
	 */

	stringPtr->uallocated = stringPtr->uallocated - 2;
    }

    uallocated = STRING_UALLOC(stringPtr->numChars);
    if (uallocated > stringPtr->uallocated) {
	/*
	 * If not enough space has been allocated for the unicode rep,
	 * reallocate the internal rep object.
	 *
	 * There isn't currently enough space in the Unicode representation so
	 * allocate additional space. If the current Unicode representation
	 * isn't empty (i.e. it looks like we've done some appends) then
	 * overallocate the space so that we won't have to do as much
	 * reallocation in the future.
	 */

	if (stringPtr->uallocated > 0) {
	    uallocated *= 2;
	}
	stringPtr = stringRealloc(stringPtr, uallocated);
	stringPtr->uallocated = uallocated;
	SET_STRING(objPtr, stringPtr);
    }

    if (stringPtr->storageType == Storage_Byte) {
	if (stringPtr->range <= Range_Byte) {
	    /*
	     * Convert byte to unicode directly.
	     */

	    srcByte = stringPtr->data.bytes + stringPtr->numChars - 1;
	    dst = stringPtr->data.unicode + stringPtr->numChars - 1;
	    while (srcByte >= stringPtr->data.bytes) {
		*dst = (Tcl_UniChar) *srcByte;
		dst--;
		srcByte--;
	    }
	    stringPtr->data.unicode[stringPtr->numChars] = 0;
	    stringPtr->storageType = Storage_Unicode;
	    objPtr->typePtr = &tclStringType;
	    return;
	} else {
	    if (objPtr->bytes == NULL) {
		Tcl_GetString(objPtr);
	    }
	}
    }

    src = objPtr->bytes;
    stringPtr->storageType = Storage_Unicode;
    objPtr->typePtr = &tclStringType;

    /*
     * Convert src to Unicode and store the coverted data in "unicode".
     */

    srcEnd = src + objPtr->length;
    for (dst = stringPtr->data.unicode; src < srcEnd; dst++) {
	src += TclUtfToUniChar(src, dst);
    }
    *dst = 0;
}

/*
 *----------------------------------------------------------------------
 *
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891




2892




2893
2894

2895

2896
2897

2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
    /*
     * If the src obj is a string of 1-byte Utf chars, then copy the string
     * rep of the source object and create an "empty" Unicode internal rep for
     * the new object. Otherwise, copy Unicode internal rep, and invalidate
     * the string rep of the new object.
     */

    if (srcStringPtr->hasUnicode && srcStringPtr->numChars > 0) {
	/*
	 * Copy the full allocation for the Unicode buffer.
	 */









	copyStringPtr = stringAlloc(srcStringPtr->maxChars);
	copyStringPtr->maxChars = srcStringPtr->maxChars;

	memcpy(copyStringPtr->unicode, srcStringPtr->unicode,

		srcStringPtr->numChars * sizeof(Tcl_UniChar));
	copyStringPtr->unicode[srcStringPtr->numChars] = 0;

	copyStringPtr->allocated = 0;
    } else {
	copyStringPtr = stringAlloc(0);
	copyStringPtr->unicode[0] = 0;
	copyStringPtr->maxChars = 0;

	/*
	 * Tricky point: the string value was copied by generic object
	 * management code, so it doesn't contain any extra bytes that might
	 * exist in the source object.
	 */

	copyStringPtr->allocated = copyPtr->length;
    }
    copyStringPtr->numChars = srcStringPtr->numChars;
    copyStringPtr->hasUnicode = srcStringPtr->hasUnicode;
#endif /* COMPAT==0 */

    SET_STRING(copyPtr, copyStringPtr);
    copyPtr->typePtr = &tclStringType;
}

/*
 *----------------------------------------------------------------------







|
|
|
<
>
>
>
>

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

|
|
|
|
|

|
<
<
<
<







3419
3420
3421
3422
3423
3424
3425
3426
3427
3428

3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446

3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457




3458
3459
3460
3461
3462
3463
3464
    /*
     * If the src obj is a string of 1-byte Utf chars, then copy the string
     * rep of the source object and create an "empty" Unicode internal rep for
     * the new object. Otherwise, copy Unicode internal rep, and invalidate
     * the string rep of the new object.
     */

    if (srcStringPtr->storageType == Storage_None) {
	copyStringPtr = (String *) ckalloc(STRING_SIZE(STRING_UALLOC(0)));
	copyStringPtr->uallocated = STRING_UALLOC(0);

    } else if (srcStringPtr->storageType == Storage_Byte) {
	copyStringPtr = (String *) ckalloc(
		BYTEARRAY_SIZE(srcStringPtr->uallocated));
	copyStringPtr->uallocated = srcStringPtr->uallocated;

	memcpy((void *) copyStringPtr->data.bytes,
		(void *) srcStringPtr->data.bytes,
		(size_t) srcStringPtr->numChars);
    } else {
	copyStringPtr = stringAlloc(srcStringPtr->uallocated);
	copyStringPtr->uallocated = srcStringPtr->uallocated;

	memcpy(copyStringPtr->data.unicode,
		srcStringPtr->data.unicode,
		(size_t) srcStringPtr->numChars * sizeof(Tcl_UniChar));
	copyStringPtr->data.unicode[srcStringPtr->numChars] = 0;
    }
    copyStringPtr->numChars = srcStringPtr->numChars;

    copyStringPtr->storageType = srcStringPtr->storageType;
    copyStringPtr->allocated = srcStringPtr->allocated;
    copyStringPtr->range = srcStringPtr->range;

    /*
     * Tricky point: the string value was copied by generic object management
     * code, so it doesn't contain any extra bytes that might exist in the
     * source object.
     */

    copyStringPtr->allocated = copyPtr->length;





    SET_STRING(copyPtr, copyStringPtr);
    copyPtr->typePtr = &tclStringType;
}

/*
 *----------------------------------------------------------------------
2935
2936
2937
2938
2939
2940
2941






2942

2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957




2958
2959

2960

2961
2962
2963
2964
2965









































































































2966
2967
2968
2969
2970
2971
2972
 */

static int
SetStringFromAny(
    Tcl_Interp *interp,		/* Used for error reporting if not NULL. */
    Tcl_Obj *objPtr)		/* The object to convert. */
{






    if (objPtr->typePtr != &tclStringType) {

	String *stringPtr = stringAlloc(0);

	/*
	 * Convert whatever we have into an untyped value. Just A String.
	 */

	(void) TclGetString(objPtr);
	TclFreeIntRep(objPtr);

	/*
	 * Create a basic String intrep that just points to the UTF-8 string
	 * already in place at objPtr->bytes.
	 */

	stringPtr->numChars = -1;




	stringPtr->allocated = objPtr->length;
	stringPtr->maxChars = 0;

	stringPtr->hasUnicode = 0;

	SET_STRING(objPtr, stringPtr);
	objPtr->typePtr = &tclStringType;
    }
    return TCL_OK;
}










































































































/*
 *----------------------------------------------------------------------
 *
 * UpdateStringOfString --
 *
 *	Update the string representation for an object whose internal







>
>
>
>
>
>
|
>
|














>
>
>
>
|
|
>
|
>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
 */

static int
SetStringFromAny(
    Tcl_Interp *interp,		/* Used for error reporting if not NULL. */
    Tcl_Obj *objPtr)		/* The object to convert. */
{
    /*
     * The Unicode object is optimized for the case where each UTF char in a
     * string is only one byte. In this case, we store the value of numChars,
     * but we don't copy the bytes to the unicodeObj->unicode.
     */

    if ((objPtr->typePtr != &tclStringType) && 
	    (objPtr->typePtr != &tclByteArrayType)) {
	String *stringPtr;

	/*
	 * Convert whatever we have into an untyped value. Just A String.
	 */

	(void) TclGetString(objPtr);
	TclFreeIntRep(objPtr);

	/*
	 * Create a basic String intrep that just points to the UTF-8 string
	 * already in place at objPtr->bytes.
	 */

	stringPtr->numChars = -1;
	stringPtr->uallocated = STRING_UALLOC(0);
	stringPtr->storageType = Storage_None;

	if (objPtr->bytes != NULL) {
	    stringPtr->allocated = objPtr->length;
	    objPtr->bytes[objPtr->length] = 0;
	} else {
	    objPtr->length = 0;
	}
	SET_STRING(objPtr, stringPtr);
	objPtr->typePtr = &tclStringType;
    }
    return TCL_OK;
}

static int
SetByteArrayFromAny(
    Tcl_Interp *interp,		/* Used for error reporting if not NULL. */
    register Tcl_Obj *objPtr)	/* The object to convert. */
{
    String *stringPtr;
    CharacterRange range = Range_Unicode;
    int length, count;

    if (objPtr->typePtr == &tclByteArrayType) {
	return TCL_OK;
    }
    if (objPtr->typePtr != &tclStringType) {
	SetStringFromAny(interp, objPtr);
    }
    stringPtr = GET_STRING(objPtr);

    objPtr->typePtr = &tclByteArrayType;
    if (stringPtr->storageType == Storage_Byte) {
	return TCL_OK;
    }

    length = Tcl_GetCharLength(objPtr);

    if (stringPtr->storageType != Storage_Unicode) {
	/*
	 * Since the unicode representation is larger than the byte rep
	 * we only need to allocate storage when there is none.
	 */
	    
	size_t uallocated = BYTEARRAY_UALLOC(length);
	stringPtr = (String *) ckrealloc((char*) stringPtr,
					 BYTEARRAY_SIZE(uallocated));
	SET_STRING(objPtr, stringPtr);
	stringPtr->uallocated = uallocated;
    } else {
	/*
	 * Change allocation count from unicode to bytes.
	 */

	stringPtr->uallocated = stringPtr->uallocated + 2;
    }

    if (stringPtr->range == Range_Ascii) {
	memcpy((void *) stringPtr->data.bytes, objPtr->bytes, length);
	stringPtr->storageType = Storage_Byte;
	return TCL_OK;
    }

    if (stringPtr->storageType == Storage_Unicode) {
	Tcl_UniChar *srcPtr = stringPtr->data.unicode;
	unsigned char *dstPtr = stringPtr->data.bytes;
	
	/*
	 * Look for any char outside of byte range.
	 */

	count = length;
	range = Range_Byte;
	while (count-- > 0) {
	    if (*srcPtr++ > 255) {
		range = Range_Unicode;
		break;
	    }
	}

	/* 
	 * If a pure Unicode object contains chars outside the byte range we
	 * must fill in the UTF representation to not lose information.
	 */

	if (range == Range_Unicode) {
	    Tcl_GetString(objPtr);
	}

	count = length;
	srcPtr = stringPtr->data.unicode;
	while (count-- > 0) {
	    *dstPtr = (unsigned char) *srcPtr;
	    dstPtr++;
	    srcPtr++;
	}

	stringPtr->storageType = Storage_Byte;
	stringPtr->range = range;
    } else {
	Tcl_UniChar ch;
	char *srcPtr = Tcl_GetStringFromObj(objPtr, &length);
	unsigned char *dstPtr = stringPtr->data.bytes;
	char *srcEnd = srcPtr + length;

	range = Range_Byte;
	while (srcPtr < srcEnd) {
	    srcPtr += Tcl_UtfToUniChar(srcPtr, &ch);
	    *dstPtr++ = (unsigned char) ch;
	    if (ch > 255) {
		range = Range_Unicode;
	    }
	}
	stringPtr->storageType = Storage_Byte;
	stringPtr->range = range;
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * UpdateStringOfString --
 *
 *	Update the string representation for an object whose internal
2982
2983
2984
2985
2986
2987
2988





2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999

3000

3001
3002
3003
3004

3005
3006
3007
3008
3009
3010
3011



3012
3013
3014
3015
3016

3017
3018


3019






3020
3021
3022
3023

3024
3025
3026
3027


3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038

3039


3040


3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
 *----------------------------------------------------------------------
 */

static void
UpdateStringOfString(
    Tcl_Obj *objPtr)		/* Object with string rep to update. */
{





    String *stringPtr = GET_STRING(objPtr);

    if (stringPtr->numChars == 0) {
	TclInitStringRep(objPtr, tclEmptyStringRep, 0);
    } else {
	(void) ExtendStringRepWithUnicode(objPtr, stringPtr->unicode,
		stringPtr->numChars);
    }
}

static int

ExtendStringRepWithUnicode(

    Tcl_Obj *objPtr,
    const Tcl_UniChar *unicode,
    int numChars)
{

    /*
     * Pre-condition: this is the "string" Tcl_ObjType.
     */

    int i, origLength, size = 0;	
    char *dst, buf[TCL_UTF_MAX];
    String *stringPtr = GET_STRING(objPtr);




    if (numChars < 0) {
	numChars = UnicodeLength(unicode);
    }


    if (numChars == 0) {
	return 0;


    }







    if (objPtr->bytes == NULL) {
	objPtr->length = 0;
    }

    size = origLength = objPtr->length;
    
    /*
     * Quick cheap check in case we have more than enough room.


     */

    if (numChars <= (INT_MAX - size)/TCL_UTF_MAX 
	    && stringPtr->allocated >= size + numChars * TCL_UTF_MAX) {
	goto copyBytes;
    }

    for (i = 0; i < numChars && size >= 0; i++) {
	size += Tcl_UniCharToUtf((int) unicode[i], buf);
    }
    if (size < 0) {

	Tcl_Panic("max size for a Tcl value (%d bytes) exceeded", INT_MAX);


    }



    /*
     * Grow space if needed.
     */

    if (size > stringPtr->allocated) {
	GrowStringBuffer(objPtr, size, 1);
    }

  copyBytes:
    dst = objPtr->bytes + origLength;
    for (i = 0; i < numChars; i++) {
	dst += Tcl_UniCharToUtf((int) unicode[i], dst);
    }







>
>
>
>
>
|

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

|
<
|
>
>
>
|
<
<
|

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

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







3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658


3659


3660

3661
3662
3663
3664


3665
3666
3667
3668
3669
3670
3671

3672
3673
3674
3675
3676


3677
3678
3679
3680

3681
3682
3683
3684
3685
3686
3687
3688
3689
3690

3691
3692
3693
3694
3695
3696

3697
3698
3699
3700




3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714


3715


3716
3717
3718
3719
3720
3721
3722
 *----------------------------------------------------------------------
 */

static void
UpdateStringOfString(
    Tcl_Obj *objPtr)		/* Object with string rep to update. */
{
    int i, size;
    Tcl_UniChar *unicode;
    char dummy[TCL_UTF_MAX];
    unsigned char *src;
    char *dst;
    String *stringPtr;

    stringPtr = GET_STRING(objPtr);
    if ((objPtr->bytes == NULL) || (stringPtr->allocated == 0)) {


	if (stringPtr->storageType == Storage_None) {


	    /*

	     * If there is no Unicode rep, or the string has 0 chars, then set
	     * the string rep to an empty string.
	     */
    String *stringPtr = GET_STRING(objPtr);



	if (stringPtr->storageType == Storage_Byte) {
	    /*
	     * How much space will string rep need?
	     */

	    size = stringPtr->numChars;

	    src = stringPtr->data.bytes;
	    for (i = 0; i < stringPtr->numChars; i++) {
		if ((src[i] == 0) || (src[i] > 127)) {
		    size++;
		}


	    }

	    dst = (char *) ckalloc((unsigned) (size + 1));
	    objPtr->bytes = dst;

	    objPtr->length = size;
	    stringPtr->allocated = size;

	    if (size == stringPtr->numChars) {
		memcpy(dst, src, (size_t) size);
		dst[size] = '\0';
	    } else {
		for (i = 0; i < stringPtr->numChars; i++) {
		    dst += Tcl_UniCharToUtf(src[i], dst);
		}

		*dst = '\0';
	    }
	} else {
	    unicode = stringPtr->data.unicode;

	    /*

	     * Translate the Unicode string to UTF. "size" will hold the amount
	     * of space the UTF string needs.
	     */





	    size = 0;
	    for (i = 0; i < stringPtr->numChars; i++) {
		size += Tcl_UniCharToUtf((int) unicode[i], dummy);
	    }

	    dst = (char *) ckalloc((unsigned) (size + 1));
	    objPtr->bytes = dst;
	    objPtr->length = size;
	    stringPtr->allocated = size;

	    for (i = 0; i < stringPtr->numChars; i++) {
		dst += Tcl_UniCharToUtf(unicode[i], dst);
	    }
	    *dst = '\0';


	}


    }

  copyBytes:
    dst = objPtr->bytes + origLength;
    for (i = 0; i < numChars; i++) {
	dst += Tcl_UniCharToUtf((int) unicode[i], dst);
    }

Changes to generic/tclTestObj.c.

53
54
55
56
57
58
59
60

61

62
63
64
65
66
67
68
static int		TestobjCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		TeststringobjCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);

typedef struct TestString {
    int numChars;
    int allocated;

    int maxChars;

    Tcl_UniChar unicode[2];
} TestString;

/*
 *----------------------------------------------------------------------
 *
 * TclObjTest_Init --







|
>
|
>







53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
static int		TestobjCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		TeststringobjCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);

typedef struct TestString {
    int numChars;
    size_t allocated;
    size_t uallocated;
    int range;
    int storageType;
    Tcl_UniChar unicode[2];
} TestString;

/*
 *----------------------------------------------------------------------
 *
 * TclObjTest_Init --
1024
1025
1026
1027
1028
1029
1030













1031

1032
1033
1034
1035
1036
1037
1038
	index = Tcl_GetString(objv[2]);
	if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (CheckIfVarUnset(interp, varIndex)) {
	    return TCL_ERROR;
	}













	Tcl_SetObjResult(interp, Tcl_NewIntObj(varPtr[varIndex]->refCount));

    } else if (strcmp(subCmd, "type") == 0) {
	if (objc != 3) {
	    goto wrongNumArgs;
	}
	index = Tcl_GetString(objv[2]);
	if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) {
	    return TCL_ERROR;







>
>
>
>
>
>
>
>
>
>
>
>
>
|
>







1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
	index = Tcl_GetString(objv[2]);
	if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) {
	    return TCL_ERROR;
	}
	if (CheckIfVarUnset(interp, varIndex)) {
	    return TCL_ERROR;
	}
	TclFormatInt(buf, varPtr[varIndex]->refCount);
        Tcl_SetResult(interp, buf, TCL_VOLATILE);
     } else if (strcmp(subCmd, "pure") == 0) {
        if (objc != 3) {
            goto wrongNumArgs;
        }
        index = Tcl_GetString(objv[2]);
        if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) {
            return TCL_ERROR;
        }
        if (CheckIfVarUnset(interp, varIndex)) {
	    return TCL_ERROR;
	}
	Tcl_SetObjResult(interp,
		Tcl_NewBooleanObj(varPtr[varIndex]->bytes == NULL));
    } else if (strcmp(subCmd, "type") == 0) {
	if (objc != 3) {
	    goto wrongNumArgs;
	}
	index = Tcl_GetString(objv[2]);
	if (GetVariableIndex(interp, index, &varIndex) != TCL_OK) {
	    return TCL_ERROR;
1090
1091
1092
1093
1094
1095
1096


1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    Tcl_UniChar *unicode;
    int varIndex, option, i, length;
#define MAX_STRINGS 11
    const char *index, *string, *strings[MAX_STRINGS+1];
    TestString *strPtr;


    static const char *const options[] = {
	"append", "appendstrings", "get", "get2", "length", "length2",
	"set", "set2", "setlength", "maxchars", "getunicode",
	"appendself", "appendself2", NULL
    };

    if (objc < 3) {
	wrongNumArgs:
	Tcl_WrongNumArgs(interp, 1, objv, "option arg ?arg ...?");
	return TCL_ERROR;
    }







>
>
|

|
|







1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    Tcl_UniChar *unicode;
    int varIndex, option, i, length;
#define MAX_STRINGS 11
    const char *index, *string, *strings[MAX_STRINGS+1];
    TestString *strPtr;
    Tcl_UniChar uniChar, *uniPtr;
    Tcl_Obj *objPtr;
    static const char *options[] = {
	"append", "appendstrings", "get", "get2", "length", "length2",
	"set", "set2", "setlength", "ualloc", "getunicode", "range", "getunichar",
	"setunicode", "appendunicode", NULL
    };

    if (objc < 3) {
	wrongNumArgs:
	Tcl_WrongNumArgs(interp, 1, objv, "option arg ?arg ...?");
	return TCL_ERROR;
    }
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274

1275



1276
1277
1278
1279
1280
1281
1282

1283
1284
1285
1286
1287
1288
1289
1290
1291
1292

1293
1294
1295



1296
1297
1298

1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
	    break;
	case 10:			/* getunicode */
	    if (objc != 3) {
		goto wrongNumArgs;
	    }
	    Tcl_GetUnicodeFromObj(varPtr[varIndex], NULL);
	    break;
	case 11:			/* appendself */
	    if (objc != 4) {
		goto wrongNumArgs;
	    }
	    if (varPtr[varIndex] == NULL) {

		SetVarToObj(varIndex, Tcl_NewObj());



	    }

	    /*
	     * If the object bound to variable "varIndex" is shared, we must
	     * "copy on write" and append to a copy of the object.
	     */


	    if (Tcl_IsShared(varPtr[varIndex])) {
		SetVarToObj(varIndex, Tcl_DuplicateObj(varPtr[varIndex]));
	    }

	    string = Tcl_GetStringFromObj(varPtr[varIndex], &length);

	    if (Tcl_GetIntFromObj(interp, objv[3], &i) != TCL_OK) {
		return TCL_ERROR;
	    }
	    if ((i < 0) || (i > length)) {

		Tcl_SetObjResult(interp, Tcl_NewStringObj(
			"index value out of range", -1));
		return TCL_ERROR;



	    }

	    Tcl_AppendToObj(varPtr[varIndex], string + i, length - i);

	    Tcl_SetObjResult(interp, varPtr[varIndex]);
	    break;
	case 12:			/* appendself2 */
	    if (objc != 4) {
		goto wrongNumArgs;
	    }
	    if (varPtr[varIndex] == NULL) {
		SetVarToObj(varIndex, Tcl_NewObj());
	    }

	    /*
	     * If the object bound to variable "varIndex" is shared, we must
	     * "copy on write" and append to a copy of the object.
	     */

	    if (Tcl_IsShared(varPtr[varIndex])) {
		SetVarToObj(varIndex, Tcl_DuplicateObj(varPtr[varIndex]));
	    }

	    unicode = Tcl_GetUnicodeFromObj(varPtr[varIndex], &length);

	    if (Tcl_GetIntFromObj(interp, objv[3], &i) != TCL_OK) {
		return TCL_ERROR;
	    }
	    if ((i < 0) || (i > length)) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj(
			"index value out of range", -1));
		return TCL_ERROR;
	    }

	    Tcl_AppendUnicodeToObj(varPtr[varIndex], unicode + i, length - i);
	    Tcl_SetObjResult(interp, varPtr[varIndex]);
	    break;
    }

    return TCL_OK;
}








|
|


|
>
|
>
>
>

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



<
>
|
<
|
>
>
>

|
|
>
|

|






<
<
<
<
<
<
<
<
<
<
|
|
<
<
<
<
<
<
<
<
<
|







1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299




1300
1301
1302


1303

1304
1305
1306
1307

1308
1309

1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326










1327
1328









1329
1330
1331
1332
1333
1334
1335
1336
	    break;
	case 10:			/* getunicode */
	    if (objc != 3) {
		goto wrongNumArgs;
	    }
	    Tcl_GetUnicodeFromObj(varPtr[varIndex], NULL);
	    break;
	case 11:				/* range */
	    if (objc != 3) {
		goto wrongNumArgs;
	    }
	    if (varPtr[varIndex] != NULL) {
		strPtr = (TestString *)
		    (varPtr[varIndex])->internalRep.otherValuePtr;
		length = (int) strPtr->range;
	    } else {
		length = -1;
	    }
	    Tcl_SetIntObj(Tcl_GetObjResult(interp), length);




	    break;
	case 12:				/* getunichar */
	    if (objc != 4) {


		goto wrongNumArgs;

	    }
	    if (Tcl_GetIntFromObj(interp, objv[3], &i) != TCL_OK) {
		return TCL_ERROR;
	    }

	    uniChar = Tcl_GetUniChar(varPtr[varIndex], i);
	    Tcl_SetObjResult(interp, Tcl_NewUnicodeObj(&uniChar, 1));

	    break;
	case 13:				/* setunicode */
	    if (objc != 4) {
		goto wrongNumArgs;
	    }
	    uniPtr = Tcl_GetUnicodeFromObj(objv[3], &length);
	    objPtr = Tcl_NewObj();
	    Tcl_SetUnicodeObj(objPtr, uniPtr, length);
	    SetVarToObj(varIndex, objPtr);
	    break;
	case 14:				/* appendunicode */
	    if (objc != 4) {
		goto wrongNumArgs;
	    }
	    if (varPtr[varIndex] == NULL) {
		SetVarToObj(varIndex, Tcl_NewObj());
	    }










	    uniPtr = Tcl_GetUnicodeFromObj(objv[3], &length);
	    objPtr = Tcl_NewObj();









	    Tcl_AppendUnicodeToObj(varPtr[varIndex], uniPtr, length);
	    Tcl_SetObjResult(interp, varPtr[varIndex]);
	    break;
    }

    return TCL_OK;
}


Added stringbyte.txt.





























































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
This is the original write down of the Unify idea, from Sep 2007...


Currently there are two internal reps called "string" and "bytearray".
They share some common properties including storing the length of the
string and a fixed-sized-per-char array allowing O(1) indexing.

Currently any place that handles strings, such as indexing, ranging,
comparing etc, needs to handle both intreps to avoid shimmering
and preserve efficiency.

Unifying these into one intrep might help simplifying the code
and maybe make things more efficient.
It will also make shimmering more efficient when needed.

Properties of "string":
 Stores length in characters.
 Stores info about extra allocated utf-8 space.
 Optionally stores 16-bit unicode array, handling extra space.
 Knows if utf-8 is one-byte-per-char.

Properties of "bytearray":
 Stores length in characters.
 Stores 8-bit byte array, handling extra space.
 
A current bug in bytearray is that it does not handle if a char
outside of 0-255 is present in the utf-8 string, i.e. if indexing
into the bytearray is really valid.  This does not matter in any
sane code, but to be picky it is a bug.


Does a unified version need the ability to keep both a byte and a
unicode rep?
If not, the data can be in a union at the end of the struct keeping
the entire intrep in one block as the current intreps do.
If yes, the intrep could need three memory blocks, the struct, the
byte data and the unicode data. This would make handling generally
trickier.
I would say no to this currently.


A combined structure could contain:
 The length in characters.
 Size of utf-8 allocation.
 If utf-8 is one-byte-per-char (all chars within 1-127).
 If all chars are within 0-255 (pure bytearray).
 If stored array is not present, byte or unicode.
 Size of array allocation.
 Data Union

The property "utf-8 is one-byte-per-char" is currently
indicated by numChars != -1 and hasUnicode == 0.
A special field for this combined with a value for a pure bytearray
covers all needs.  This could be expanded in the future if
"Unicode" is split into UTF-16 and real Unicode.

typedef enum {
    Ascii, Byte, Unicode
} CharacterRange;

The presence of data can be indicated with an enum.  This
can also be expanded if UTF-16/Unicode needs to be separated.

typedef enum {
    None, Byte, Unicode
} StorageType;

Giving something like this:

typedef struct String {
    int numChars;
    size_t allocated;
    CharacterRange range;
    StorageType storageType;
    size_t allocatedStorage;
    union {
        unsigned char bytes[4];
        Tcl_UniChar unicode[2];
    } data;
} String;

This only expands the String array with one integer.


Implementation:
All ByteArray APIs move to tclStringObj.c
The binary command stays in tclBinary.c

Shimmering from unicode to byte is a rather cheap operation
since it can be done in-place.

Compatibility:
If anyone accesses through e.g. Tcl_GetObjType("bytearray") it gets
tricker to maintain full compatiblity.  I wonder if this is needed?

Changes to tests/stringObj.test.

393
394
395
396
397
398
399


















400
401
402
403
404
405
406
test stringObj-12.5 {Tcl_GetUniChar} testobj {
    set x "\u00efa\u00bfb\u00aec\u00ae\u00bfd\u00ef"
    list [string index $x 4] [string index $x 0]
} "\u00ae \u00ef"
test stringObj-12.6 {Tcl_GetUniChar} testobj {
    string index "\u00efa\u00bfb\u00aec\u00ef\u00bfd\u00ae" end
} "\u00ae"



















test stringObj-13.1 {Tcl_GetCharLength with byte-size chars} testobj {
    set a ""
    list [string length $a] [string length $a]
} {0 0}
test stringObj-13.2 {Tcl_GetCharLength with byte-size chars} testobj {
    string length "a"







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
test stringObj-12.5 {Tcl_GetUniChar} testobj {
    set x "\u00efa\u00bfb\u00aec\u00ae\u00bfd\u00ef"
    list [string index $x 4] [string index $x 0]
} "\u00ae \u00ef"
test stringObj-12.6 {Tcl_GetUniChar} testobj {
    string index "\u00efa\u00bfb\u00aec\u00ef\u00bfd\u00ae" end
} "\u00ae"

test stringObj-12.7 {Tcl_GetUniChar with byte-size chars} testobj {
    teststringobj set2 1 "abcdefghi"
    list [teststringobj getunichar 1 0] [teststringobj getunichar 1 1]
} {a b}

test stringObj-12.8 {Tcl_GetUniChar with unicode rep} testobj {
    set a "abcd\u129fefghi"
    teststringobj set2 1 $a 
    list [teststringobj getunichar 1 4] [teststringobj getunichar 1 6]
} [list \u129f f]

test stringObj-12.9 {Tcl_GetUniChar with byte rep} testobj {
    set a "abcd\u009fefghi"
    binary scan $a I dummy
    teststringobj set2 1 $a 
    list [teststringobj getunichar 1 4] [teststringobj getunichar 1 6]
} [list \u009f f]

test stringObj-13.1 {Tcl_GetCharLength with byte-size chars} testobj {
    set a ""
    list [string length $a] [string length $a]
} {0 0}
test stringObj-13.2 {Tcl_GetCharLength with byte-size chars} testobj {
    string length "a"
473
474
475
476
477
478
479







480

























































































































481
482
483
484
485
486
487
    teststringobj appendself2 1 2
} fooo
test stringObj-15.8 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself2 1 3
} foo


































































































































if {[testConstraint testobj]} {
    testobj freeallvars
}

# cleanup
::tcltest::cleanupTests
return







>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
    teststringobj appendself2 1 2
} fooo
test stringObj-15.8 {Tcl_Append*ToObj: self appends} {
    teststringobj set 1 foo
    teststringobj appendself2 1 3
} foo

test stringObj-15.1 {byte/unicode shimmering, start from byte} testobj {
    set res ""
    # Pure byte object
    teststringobj set2 1 [binary format c* {1 5 8 9 78 178 250 180}]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Still byte object
    string length [teststringobj get 1]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Unicode object
    regexp {.*} [teststringobj get 1]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Byte object
    binary scan [teststringobj get 1] I dummy
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Unicode object
    regexp {.*} [teststringobj get 1]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

} {bytearray 8 1 bytearray 8 1 string 32 1 bytearray 34 1 string 32 1}

test stringObj-15.2 {byte/unicode shimmering, start from unicode} testobj {
    set res ""
    # Pure unicode object
    teststringobj set 1 a\u0095cdef\u00EFh
    teststringobj getunicode 1
    testobj invalidateStringRep 1
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Still unicode object
    string length [teststringobj get 1]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Byte object
    binary scan [teststringobj get 1] I dummy
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Unicode object
    regexp {.*} [teststringobj get 1]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Byte object
    binary scan [teststringobj get 1] I dummy
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

} {string 16 1 string 16 1 bytearray 18 1 string 16 1 bytearray 18 1}

test stringObj-15.3 {byte/unicode shimmering, out of byte range} testobj {
    set res ""
    # Pure unicode object
    teststringobj set 1 a\u1295cdef\u00EFh
    teststringobj getunicode 1
    testobj invalidateStringRep 1
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]
    lappend res [string range [teststringobj get 1] 0 end]

    # Byte object
    binary scan [teststringobj get 1] I dummy
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Reading byte object with out-of-range data
    lappend res [string range [teststringobj get 1] 0 end]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Byte object, without utf
    binary scan [teststringobj get 1] I dummy
    testobj invalidateStringRep 1
    teststringobj getunicode 1
    lappend res [string range [teststringobj get 1] 0 end]

} [list string 16 1 a\u1295cdef\u00EFh bytearray 18 0 a\u1295cdef\u00EFh string 16 0 a\u0095cdef\u00EFh]

test stringObj-15.4 {byte/unicode shimmering, one-char} testobj {
    set res ""
    # Pure byte object
    teststringobj set2 1 [binary format c 178]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]

    # Unicode object
    regexp {.*} [teststringobj get 1]
    lappend res [testobj type 1]
    lappend res [teststringobj ualloc 1]
    lappend res [testobj pure 1]
} {bytearray 4 1 string 2 1}

test stringObj-16.1 {Tcl_SetUnicodeObj} testobj {
    teststringobj setunicode 1 foo\u1234bar
    testobj type 1
} {string}

test stringObj-17.1 {Tcl_AppendUnicodeToObj} testobj {
    set res ""
    teststringobj set 1 apa
    teststringobj appendunicode 1 foo\u1234bar
    lappend res [testobj type 1]
    lappend res [teststringobj get 1]
} [list string apafoo\u1234bar]

if {[testConstraint testobj]} {
    testobj freeallvars
}

# cleanup
::tcltest::cleanupTests
return