summaryrefslogtreecommitdiffstats
path: root/generic/tclUnicodeObj.c
blob: 315644d6e6b08eb271c67c0c7b4b8ac34482f255 (plain)
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
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
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
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
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
/* 
 * tclUnicodeObj.c --
 *
 *	This file contains the implementation of the Unicode internal
 *	representation of Tcl objects.
 *
 * Copyright (c) 1999 by Scriptics Corporation.
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: tclUnicodeObj.c,v 1.5 1999/06/10 04:28:51 stanton Exp $
 */

#include <math.h>
#include "tclInt.h"
#include "tclPort.h"

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

static int		AllSingleByteChars _ANSI_ARGS_((Tcl_Obj *objPtr));
static void		AppendUniCharStrToObj _ANSI_ARGS_((Tcl_Obj *objPtr,
			    Tcl_UniChar *unichars, int numNewChars));	
static void		DupUnicodeInternalRep _ANSI_ARGS_((Tcl_Obj *srcPtr,
			    Tcl_Obj *copyPtr));
static void		FreeUnicodeInternalRep _ANSI_ARGS_((Tcl_Obj *objPtr));
static void		UpdateStringOfUnicode _ANSI_ARGS_((Tcl_Obj *objPtr));
static void		SetOptUnicodeFromAny _ANSI_ARGS_((Tcl_Obj *objPtr,
			    int numChars));
static void		SetFullUnicodeFromAny _ANSI_ARGS_((Tcl_Obj *objPtr,
			    char *src, int numBytes, int numChars));
static int		SetUnicodeFromAny _ANSI_ARGS_((Tcl_Interp *interp,
			    Tcl_Obj *objPtr));

/*
 * The following object type represents a Unicode string.  A Unicode string
 * is an internationalized string.  Conceptually, a Unicode string is an
 * array of 16-bit quantities organized as a sequence of properly formed
 * UTF-8 characters.  There is a one-to-one map between Unicode and UTF
 * characters.  The Unicode ojbect is opitmized 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->chars.  Before
 * accessing obj->chars, check if unicodeObj->numChars == obj->length.
 */

Tcl_ObjType tclUnicodeType = {
    "unicode",
    FreeUnicodeInternalRep,
    DupUnicodeInternalRep,
    UpdateStringOfUnicode,
    SetUnicodeFromAny
};

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

typedef struct Unicode {
    int numChars;		/* The number of chars in the unicode
				 * string. */
    size_t allocated;		/* The amount of space actually allocated. */
    Tcl_UniChar chars[2];	/* The array of chars.  The actual size of
				 * this field depends on the 'allocated' field
				 * above. */
} Unicode;

#define UNICODE_SIZE(len)	\
		((unsigned) (sizeof(Unicode) - (sizeof(Tcl_UniChar)*2) + (len)))
#define GET_UNICODE(objPtr) \
		((Unicode *) (objPtr)->internalRep.otherValuePtr)
#define SET_UNICODE(objPtr, unicodePtr) \
		(objPtr)->internalRep.otherValuePtr = (VOID *) (unicodePtr)


/*
 *----------------------------------------------------------------------
 *
 * TclGetUnicodeFromObj --
 *
 *	Get the index'th Unicode character from the Unicode object.  If
 *	the object is not already a Unicode object, an attempt will be
 *	made to convert it to one.  The index is assumed to be in the
 *	appropriate range.
 *
 * Results:
 *	Returns a pointer to the object's internal unicode string.
 *
 * Side effects:
 *	Converts the object to have the Unicode internal rep.
 *
 *----------------------------------------------------------------------
 */

Tcl_UniChar *
TclGetUnicodeFromObj(objPtr)
    Tcl_Obj *objPtr;	/* The object to find the unicode string for. */
{
    Unicode *unicodePtr;
    int numBytes;
    char *src;
    
    SetUnicodeFromAny(NULL, objPtr);
    unicodePtr = GET_UNICODE(objPtr);
    
    if (unicodePtr->allocated == 0) {

	/*
	 * If all of the characters in the Utf string are 1 byte chars,
	 * we don't normally store the unicode str.  Since this
	 * function must return a unicode string, and one has not yet
	 * been stored, force the Unicode to be calculated and stored
	 * now.
	 */
	
	src = Tcl_GetStringFromObj(objPtr, &numBytes);
	SetFullUnicodeFromAny(objPtr, src, numBytes, unicodePtr->numChars);

	/*
	 * We need to fetch the pointer again because we have just
	 * reallocated the structure to make room for the Unicode data.
	 */
	
	unicodePtr = GET_UNICODE(objPtr);
    }
    return unicodePtr->chars;
}

/*
 *----------------------------------------------------------------------
 *
 * TclGetUnicodeLengthFromObj --
 *
 *	Get the length of the Unicode string from the Tcl object.  If
 *	the object is not already a Unicode object, an attempt will be
 *	made to convert it to one.
 *
 * Results:
 *	Pointer to unicode string representing the unicode object.
 *
 * Side effects:
 *	Frees old internal rep.  Allocates memory for new internal rep.
 *
 *----------------------------------------------------------------------
 */

int
TclGetUnicodeLengthFromObj(objPtr)
    Tcl_Obj *objPtr;		/* The Unicode object. */
{
    int length;
    Unicode *unicodePtr;
    
    SetUnicodeFromAny(NULL, objPtr);
    unicodePtr = GET_UNICODE(objPtr);

    length = unicodePtr->numChars;
    return length;
}

/*
 *----------------------------------------------------------------------
 *
 * TclGetUniCharFromObj --
 *
 *	Get the index'th Unicode character from the Unicode object.  If
 *	the object is not already a Unicode object, an attempt will be
 *	made to convert it to one.  The index is assumed to be in the
 *	appropriate range.
 *
 * Results:
 *	Returns the index'th Unicode character in the Object.
 *
 * Side effects:
 *	Fills unichar with the index'th Unicode character.
 *
 *----------------------------------------------------------------------
 */

Tcl_UniChar
TclGetUniCharFromObj(objPtr, index)
    Tcl_Obj *objPtr;		/* The Unicode object. */
    int index;			/* Get the index'th character. */
{
    Tcl_UniChar unichar;
    Unicode *unicodePtr;
    int length;
    
    SetUnicodeFromAny(NULL, objPtr);
    unicodePtr = GET_UNICODE(objPtr);
    length = objPtr->length;
    
    if (AllSingleByteChars(objPtr)) {
	int length;
	char *str;

	/*
	 * All of the characters in the Utf string are 1 byte chars,
	 * so we don't store the unicode char.  We get the Utf string
	 * and convert the index'th byte to a Unicode character.
	 */
	
	str = Tcl_GetStringFromObj(objPtr, &length);
	Tcl_UtfToUniChar(&str[index], &unichar);	
    } else {
	unichar = unicodePtr->chars[index];
    }
    return unichar;
}

/*
 *----------------------------------------------------------------------
 *
 * TclGetRangeFromObj --
 *
 *	Create a Tcl Object that contains the chars between first and last
 *	of the object indicated by "objPtr".  If the object is not already
 *	a Unicode object, an attempt will be made to convert it to one.
 *	The first and last indices are assumed to be in the appropriate
 *	range.
 *
 * Results:
 *	Returns a new Tcl Object of either "string" or "unicode" type,
 *	containing the range of chars.
 *
 * Side effects:
 *	Changes the internal rep of "objPtr" to unicode.
 *
 *----------------------------------------------------------------------
 */

Tcl_Obj*
TclGetRangeFromObj(objPtr, first, last)
   
 Tcl_Obj *objPtr;		/* The Tcl object to find the range of. */
    int first;			/* First index of the range. */
    int last;			/* Last index of the range. */
{
    Tcl_Obj *newObjPtr;		/* The Tcl object to find the range of. */
    Unicode *unicodePtr;
    int length;
    
    SetUnicodeFromAny(NULL, objPtr);
    unicodePtr = GET_UNICODE(objPtr);
    length = objPtr->length;
    
    if (unicodePtr->numChars != length) {
	newObjPtr = TclNewUnicodeObj(unicodePtr->chars + first, last-first+1);
    } else {
	int length;
	char *str;

	/*
	 * All of the characters in the Utf string are 1 byte chars,
	 * so we don't store the unicode char.  Create a new string
	 * object containing the specified range of chars.
	 */
	
	str = Tcl_GetStringFromObj(objPtr, &length);
	newObjPtr = Tcl_NewStringObj(&str[first], last-first+1);	
    }
    return newObjPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * TclAppendObjToUnicodeObj --
 *
 *	This procedure appends the contents of "srcObjPtr" to the Unicode
 *	object "destPtr".
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	If srcObjPtr doesn't have an internal rep, then it is given a
 *	Unicode internal rep.
 *
 *----------------------------------------------------------------------
 */

Tcl_Obj *
TclAppendObjToUnicodeObj(targetObjPtr, srcObjPtr)
    register Tcl_Obj *targetObjPtr;	/* Points to the object to
					 * append to. */
    register Tcl_Obj *srcObjPtr;	/* Points to the object to
					 * append from. */
{
    int numBytes, numChars;
    Tcl_Obj *resultObjPtr;
    char *utfSrcStr;
    Tcl_UniChar *unicharSrcStr;
    Unicode *unicodePtr;
    Tcl_DString dsPtr;
    
    /*
     * Duplicate the target if it is shared.
     * Change the result's internal rep to Unicode object.
     */
    
    if (Tcl_IsShared(targetObjPtr)) {
	resultObjPtr = Tcl_DuplicateObj(targetObjPtr);
    } else {
	resultObjPtr = targetObjPtr;
    }
    SetUnicodeFromAny(NULL, resultObjPtr);

    /*
     * Case where target chars are 1 byte long:
     * If src obj is of "string" or null type, then convert it to "unicode"
     * type.  Src objs of other types (such as int) are left in tact to keep
     * them from shimmering between types.  If the src obj is a unichar obj,
     * and all src chars are also 1 byte long, the src string is appended to
     * the target "unicode" obj, and the target obj maintains its "optimized"
     * status.
     */

    if (AllSingleByteChars(resultObjPtr)) {

	int length;
	char *stringRep;

	if (srcObjPtr->typePtr == &tclStringType
		|| srcObjPtr->typePtr == NULL) {
	    SetUnicodeFromAny(NULL, srcObjPtr);
	}

	stringRep = Tcl_GetStringFromObj(srcObjPtr, &length);
	Tcl_AppendToObj(resultObjPtr, stringRep, length);

	if ((srcObjPtr->typePtr == &tclUnicodeType)
		&& (AllSingleByteChars(srcObjPtr))) {
	    SetOptUnicodeFromAny(resultObjPtr, resultObjPtr->length);
	}
	return resultObjPtr;
    }

    /*
     * Extract a unicode string from "unicode" or "string" type objects.
     * Extract the utf string from non-unicode objects, and convert the
     * utf string to unichar string locally.
     * If the src obj is a "string" obj, convert it to "unicode" type.
     * Src objs of other types (such as int) are left in tact to keep
     * them from shimmering between types.
     */

    Tcl_DStringInit(&dsPtr);
    if (srcObjPtr->typePtr == &tclStringType || srcObjPtr->typePtr == NULL) {
	SetUnicodeFromAny(NULL, srcObjPtr);
    }
    if (srcObjPtr->typePtr == &tclUnicodeType) {
	if (AllSingleByteChars(srcObjPtr)) {

	    unicodePtr = GET_UNICODE(srcObjPtr);
	    numChars = unicodePtr->numChars;

	    utfSrcStr = Tcl_GetStringFromObj(srcObjPtr, &numBytes);
	    unicharSrcStr = (Tcl_UniChar *)Tcl_UtfToUniCharDString(utfSrcStr,
		    numBytes, &dsPtr);
	} else {
	    unicodePtr = GET_UNICODE(srcObjPtr);
	    numChars = unicodePtr->numChars;
	    unicharSrcStr = unicodePtr->chars;
	}
    } else {
	utfSrcStr = Tcl_GetStringFromObj(srcObjPtr, &numBytes);
	numChars = Tcl_NumUtfChars(utfSrcStr, numBytes);
	unicharSrcStr = (Tcl_UniChar *)Tcl_UtfToUniCharDString(utfSrcStr,
		numBytes, &dsPtr);
    }
    if (numChars == 0) {
	return resultObjPtr;
    }

    /*
     * Append the unichar src string to the result object.
     */

    AppendUniCharStrToObj(resultObjPtr, unicharSrcStr, numChars);
    Tcl_DStringFree(&dsPtr);
    return resultObjPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * AppendUniCharStrToObj --
 *
 *	This procedure appends the contents of "srcObjPtr" to the
 *	Unicode object "objPtr".
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	If srcObjPtr doesn't have an internal rep, then it is given a
 *	Unicode internal rep.
 *
 *----------------------------------------------------------------------
 */

static void
AppendUniCharStrToObj(objPtr, unichars, numNewChars)
    register Tcl_Obj *objPtr;	/* Points to the object to append to. */
    Tcl_UniChar *unichars;	/* The unicode string to append to the
			         * object. */
    int numNewChars;		/* Number of chars in "unichars". */
{
    Unicode *unicodePtr;
    int numChars;
    size_t numBytes;

    SetUnicodeFromAny(NULL, objPtr);
    unicodePtr = GET_UNICODE(objPtr);
    
    numChars = numNewChars + unicodePtr->numChars;
    numBytes = (numChars + 1) * sizeof(Tcl_UniChar);
    
    if (unicodePtr->allocated < numBytes) {
	int allocatedBytes = numBytes * 2;
    
	/*
	 * There isn't currently enough space in the Unicode
	 * representation so allocate additional space.  In fact,
	 * overallocate so that there is room for future growth without
	 * having to reallocate again.
	 */

	unicodePtr = (Unicode *) ckrealloc((char*) unicodePtr,
		UNICODE_SIZE(allocatedBytes));
	unicodePtr->allocated = allocatedBytes;	
	unicodePtr = SET_UNICODE(objPtr, unicodePtr);
    }
    memcpy((VOID *) (unicodePtr->chars + unicodePtr->numChars),
	    (VOID *) unichars, (size_t) numNewChars * sizeof(Tcl_UniChar));
    unicodePtr->chars[numChars] = 0;
    unicodePtr->numChars = numChars;

    /*
     * Invalidate the StringRep.
     */

    Tcl_InvalidateStringRep(objPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * TclAppendUnicodeToObj --
 *
 *	This procedure appends a Unicode string to an object in the
 *	most efficient manner possible.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Invalidates the string rep and creates a new Unicode string.
 *
 *----------------------------------------------------------------------
 */

void
TclAppendUnicodeToObj(objPtr, unichars, length)
    register Tcl_Obj *objPtr;	/* Points to the object to append to. */
    Tcl_UniChar *unichars;	/* The unicode string to append to the
			         * object. */
    int length;			/* Number of chars in "unichars". */
{
    Unicode *unicodePtr;
    int numChars, i;
    size_t newSize;
    char *src;
    Tcl_UniChar *dst;

    if (Tcl_IsShared(objPtr)) {
	panic("TclAppendUnicodeToObj called with shared object");
    }

    SetUnicodeFromAny(NULL, objPtr);
    unicodePtr = GET_UNICODE(objPtr);
    
    /*
     * Make the buffer big enough for the result.
     */

    numChars = unicodePtr->numChars + length;
    newSize = (numChars + 1) * sizeof(Tcl_UniChar);

    if (newSize > unicodePtr->allocated) {
	int allocated = newSize * 2;
    
	unicodePtr = (Unicode *) ckrealloc((char*)unicodePtr,
		UNICODE_SIZE(allocated));

	if (unicodePtr->allocated == 0) {
	    /*
	     * If the original string was not in Unicode form, add it to the
	     * beginning of the buffer.
	     */

	    src = objPtr->bytes;
	    dst = unicodePtr->chars;
	    for (i = 0; i < unicodePtr->numChars; i++) {
		src += Tcl_UtfToUniChar(src, dst++);
	    }
	}
	unicodePtr->allocated = allocated;
    }

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

    memcpy((VOID*) (unicodePtr->chars + unicodePtr->numChars), unichars,
	    length * sizeof(Tcl_UniChar));
    unicodePtr->numChars = numChars;
    unicodePtr->chars[numChars] = 0;

    SET_UNICODE(objPtr, unicodePtr);

    Tcl_InvalidateStringRep(objPtr);
}

/*
 *---------------------------------------------------------------------------
 *
 * TclNewUnicodeObj --
 *
 *	This procedure is creates a new Unicode object and initializes
 *	it from the given Utf String.  If the Utf String is the same size
 *	as the Unicode string, don't duplicate the data.
 *
 * Results:
 *	The newly created 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 Unicode argument.
 *
 *---------------------------------------------------------------------------
 */

Tcl_Obj *
TclNewUnicodeObj(unichars, numChars)
    Tcl_UniChar *unichars;	/* The unicode string used to initialize
				 * the new object. */
    int numChars;		/* Number of characters in the unicode
				 * string. */
{
    Tcl_Obj *objPtr;
    Unicode *unicodePtr;
    int numBytes, allocated;

    numBytes = numChars * sizeof(Tcl_UniChar);

    /*
     * Allocate extra space for the null character
     */

    allocated = numBytes + sizeof(Tcl_UniChar);
    
    TclNewObj(objPtr);
    objPtr->bytes = NULL;
    objPtr->typePtr = &tclUnicodeType;

    unicodePtr = (Unicode *) ckalloc(UNICODE_SIZE(allocated));
    unicodePtr->numChars = numChars;
    unicodePtr->allocated = allocated;
    memcpy((VOID *) unicodePtr->chars, (VOID *) unichars, (size_t) numBytes);
    unicodePtr->chars[numChars] = 0;
    SET_UNICODE(objPtr, unicodePtr);
    return objPtr;
}

/*
 *---------------------------------------------------------------------------
 *
 * TclAllSingleByteChars --
 *
 *	Initialize the internal representation of a Unicode Tcl_Obj
 *	to a copy of the internal representation of an existing Unicode
 *	object. 
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Allocates memory.
 *
 *---------------------------------------------------------------------------
 */

static int
AllSingleByteChars(objPtr)
    Tcl_Obj *objPtr;		/* Object whose char lengths to check. */
{
    Unicode *unicodePtr;
    int numBytes, numChars;

    unicodePtr = GET_UNICODE(objPtr);
    numChars = unicodePtr->numChars;
    numBytes = objPtr->length;

    if (numChars == numBytes) {
	return 1;
    } else {
	return 0;
    }
}

/*
 *---------------------------------------------------------------------------
 *
 * DupUnicodeInternalRep --
 *
 *	Initialize the internal representation of a Unicode Tcl_Obj
 *	to a copy of the internal representation of an existing Unicode
 *	object. 
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Allocates memory.
 *
 *---------------------------------------------------------------------------
 */

static void
DupUnicodeInternalRep(srcPtr, copyPtr)
    Tcl_Obj *srcPtr;		/* Object with internal rep to copy. */
    Tcl_Obj *copyPtr;		/* Object with internal rep to set. */
{
    Unicode *srcUnicodePtr = GET_UNICODE(srcPtr);
    Unicode *copyUnicodePtr; /*GET_UNICODE(copyPtr);*/
    
    /*
     * 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 (AllSingleByteChars(srcPtr)) {
	copyUnicodePtr = (Unicode *) ckalloc(sizeof(Unicode));
	copyUnicodePtr->allocated = 0;
    } else {
	int allocated = srcUnicodePtr->allocated;

	copyUnicodePtr = (Unicode *) ckalloc(UNICODE_SIZE(allocated));

	copyUnicodePtr->allocated = allocated;
	memcpy((VOID *) copyUnicodePtr->chars,
		(VOID *) srcUnicodePtr->chars,
		(size_t) (srcUnicodePtr->numChars + 1) * sizeof(Tcl_UniChar));
    }
    copyUnicodePtr->numChars = srcUnicodePtr->numChars;
    SET_UNICODE(copyPtr, copyUnicodePtr);
}

/*
 *---------------------------------------------------------------------------
 *
 * UpdateStringOfUnicode --
 *
 *	Update the string representation for a Unicode 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 Unicode-to-string conversion.
 *
 *	The object becomes a string object -- the internal rep is
 *	discarded and the typePtr becomes NULL.
 *
 *---------------------------------------------------------------------------
 */

static void
UpdateStringOfUnicode(objPtr)
    Tcl_Obj *objPtr;		/* Unicode object whose string rep to
				 * update. */
{
    int i, length, size;
    Tcl_UniChar *src;
    char dummy[TCL_UTF_MAX];
    char *dst;
    Unicode *unicodePtr;

    unicodePtr = GET_UNICODE(objPtr);
    src = unicodePtr->chars;
    length = unicodePtr->numChars * sizeof(Tcl_UniChar);

    /*
     * How much space will string rep need?
     */
     
    size = 0;
    for (i = 0; i < unicodePtr->numChars; i++) {
	size += Tcl_UniCharToUtf((int) src[i], dummy);
    }

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

    for (i = 0; i < unicodePtr->numChars; i++) {
	dst += Tcl_UniCharToUtf(src[i], dst);
    }
    *dst = '\0';
}

/*
 *---------------------------------------------------------------------------
 *
 * SetOptUnicodeFromAny --
 *
 *	Generate the optimized Unicode internal rep from the string rep.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The Unicode ojbect is opitmized 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->chars.
 *	Before accessing obj->chars, check if all chars are 1 byte long.
 *
 *---------------------------------------------------------------------------
 */

static void
SetOptUnicodeFromAny(objPtr, numChars)
    Tcl_Obj *objPtr;		/* The object to convert to type Unicode. */
    int numChars;
{
    Tcl_ObjType *typePtr;
    Unicode *unicodePtr;

    typePtr = objPtr->typePtr;
    if ((typePtr != NULL) && (typePtr->freeIntRepProc) != NULL) {
	(*typePtr->freeIntRepProc)(objPtr);
    }
    objPtr->typePtr = &tclUnicodeType;

    /*
     * Allocate enough space for the basic Unicode structure.
     */

    unicodePtr = (Unicode *) ckalloc(sizeof(Unicode));
    unicodePtr->numChars = numChars;
    unicodePtr->allocated = 0;
    SET_UNICODE(objPtr, unicodePtr);
}

/*
 *---------------------------------------------------------------------------
 *
 * SetFullUnicodeFromAny --
 *
 *	Generate the full (non-optimized) Unicode internal rep from the
 *	string rep.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The Unicode internal rep will contain a copy of the string "src" in
 *	unicode format.
 *
 *---------------------------------------------------------------------------
 */

static void
SetFullUnicodeFromAny(objPtr, src, numBytes, numChars)
    Tcl_Obj *objPtr;		/* The object to convert to type Unicode. */
    char *src;
    int numBytes;
    int numChars;
{
    Tcl_ObjType *typePtr;
    Unicode *unicodePtr;
    char *srcEnd;
    Tcl_UniChar *dst;
    size_t length = (numChars + 1) * sizeof(Tcl_UniChar);
    
    unicodePtr = (Unicode *) ckalloc(UNICODE_SIZE(length));
    srcEnd = src + numBytes;
	
    for (dst = unicodePtr->chars; src < srcEnd; dst++) {
	src += Tcl_UtfToUniChar(src, dst);
    }
    *dst = 0;

    unicodePtr->numChars = numChars;
    unicodePtr->allocated = length;
    
    typePtr = objPtr->typePtr;
    if ((typePtr != NULL) && (typePtr->freeIntRepProc) != NULL) {
	(*typePtr->freeIntRepProc)(objPtr);
    }
    objPtr->typePtr = &tclUnicodeType;
    SET_UNICODE(objPtr, unicodePtr);
}

/*
 *---------------------------------------------------------------------------
 *
 * SetUnicodeFromAny --
 *
 *	Generate the Unicode internal rep from the string rep.
 *
 * Results:
 *	The return value is always TCL_OK.
 *
 * Side effects:
 *	A Unicode object is stored as the internal rep of objPtr.  The Unicode
 *	object is opitmized 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->chars.  Before accessing
 *	obj->chars, check if all chars are 1 byte long.
 *
 *---------------------------------------------------------------------------
 */

static int
SetUnicodeFromAny(interp, objPtr)
    Tcl_Interp *interp;		/* Not used. */
    Tcl_Obj *objPtr;		/* The object to convert to type Unicode. */
{
    int numBytes, numChars;
    char *src;
    
    if (objPtr->typePtr != &tclUnicodeType) {
	src = Tcl_GetStringFromObj(objPtr, &numBytes);

	numChars = Tcl_NumUtfChars(src, numBytes);
	if (numChars == numBytes) {
	    SetOptUnicodeFromAny(objPtr, numChars);
	} else {
	    SetFullUnicodeFromAny(objPtr, src, numBytes, numChars);
	}
    }
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * FreeUnicodeInternalRep --
 *
 *	Deallocate the storage associated with a Unicode data object's
 *	internal representation.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Frees memory. 
 *
 *----------------------------------------------------------------------
 */

static void
FreeUnicodeInternalRep(objPtr)
    Tcl_Obj *objPtr;		/* Object with internal rep to free. */
{
    ckfree((char *) GET_UNICODE(objPtr));
}