Diff
Not logged in

Differences From Artifact [721bf4662a]:

To Artifact [611210fab5]:


101
102
103
104
105
106
107
108

109
110
111
112
113
114
115
101
102
103
104
105
106
107

108
109
110
111
112
113
114
115







-
+







 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

size_t
int
TclUtfCount(
    int ch)			/* The Unicode character whose size is returned. */
{
    if ((unsigned)(ch - 1) < (UNICODE_SELF - 1)) {
	return 1;
    }
    if (ch <= 0x7FF) {
201
202
203
204
205
206
207
208

209
210
211
212
213
214
215
201
202
203
204
205
206
207

208
209
210
211
212
213
214
215







-
+







 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

#undef Tcl_UniCharToUtf
size_t
Tcl_Size
Tcl_UniCharToUtf(
    int ch,	/* The Tcl_UniChar to be stored in the
		 * buffer. Can be or'ed with flag TCL_COMBINE
		 */
    char *buf)	/* Buffer in which the UTF-8 representation of
		 * ch is stored. Must be large enough to hold the UTF-8
		 * character (at most 4 bytes).
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
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







-
+
+





-
+








-
+







 *---------------------------------------------------------------------------
 */

#undef Tcl_UniCharToUtfDString
char *
Tcl_UniCharToUtfDString(
    const int *uniStr,	/* Unicode string to convert to UTF-8. */
    size_t uniLength,		/* Length of Unicode string. */
    Tcl_Size uniLength,		/* Length of Unicode string. Negative for nul
    				 * nul terminated string */
    Tcl_DString *dsPtr)		/* UTF-8 representation of string is appended
				 * to this previously initialized DString. */
{
    const int *w, *wEnd;
    char *p, *string;
    size_t oldLength;
    Tcl_Size oldLength;

    /*
     * UTF-8 string length in bytes will be <= Unicode string length * 4.
     */

    if (uniStr == NULL) {
	return NULL;
    }
    if (uniLength == TCL_INDEX_NONE) {
    if (uniLength < 0) {
	uniLength = 0;
	w = uniStr;
	while (*w != '\0') {
	    uniLength++;
	    w++;
	}
    }
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
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







-
+















-
+








    return string;
}

char *
Tcl_Char16ToUtfDString(
    const unsigned short *uniStr,/* Utf-16 string to convert to UTF-8. */
    size_t uniLength,		/* Length of Utf-16 string. */
    Tcl_Size uniLength,		/* Length of Utf-16 string. */
    Tcl_DString *dsPtr)		/* UTF-8 representation of string is appended
				 * to this previously initialized DString. */
{
    const unsigned short *w, *wEnd;
    char *p, *string;
    size_t oldLength;
    int len = 1;

    /*
     * UTF-8 string length in bytes will be <= Utf16 string length * 3.
     */

    if (uniStr == NULL) {
	return NULL;
    }
    if (uniLength == TCL_INDEX_NONE) {
    if (uniLength < 0) {

	uniLength = 0;
	w = uniStr;
	while (*w != '\0') {
	    uniLength++;
	    w++;
	}
449
450
451
452
453
454
455
456

457
458
459
460
461
462
463
450
451
452
453
454
455
456

457
458
459
460
461
462
463
464







-
+







  0x20AC,   0x81, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
  0x02C6, 0x2030, 0x0160, 0x2039, 0x0152,   0x8D, 0x017D,   0x8F,
    0x90, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
   0x2DC, 0x2122, 0x0161, 0x203A, 0x0153,   0x9D, 0x017E, 0x0178
};

#undef Tcl_UtfToUniChar
size_t
Tcl_Size
Tcl_UtfToUniChar(
    const char *src,	/* The UTF-8 string. */
    int *chPtr)/* Filled with the Unicode character represented by
				 * the UTF-8 string. */
{
    int byte;

532
533
534
535
536
537
538
539

540
541
542
543
544
545
546
533
534
535
536
537
538
539

540
541
542
543
544
545
546
547







-
+







	 */
    }

    *chPtr = byte;
    return 1;
}

size_t
Tcl_Size
Tcl_UtfToChar16(
    const char *src,	/* The UTF-8 string. */
    unsigned short *chPtr)/* Filled with the Tcl_UniChar represented by
				 * the UTF-8 string. This could be a surrogate too. */
{
    unsigned short byte;

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
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







-
+







-
+








-
+







 *---------------------------------------------------------------------------
 */

#undef Tcl_UtfToUniCharDString
int *
Tcl_UtfToUniCharDString(
    const char *src,		/* UTF-8 string to convert to Unicode. */
    size_t length,			/* Length of UTF-8 string in bytes, or -1 for
    Tcl_Size length,		/* Length of UTF-8 string in bytes, or -1 for
				 * strlen(). */
    Tcl_DString *dsPtr)		/* Unicode representation of string is
				 * appended to this previously initialized
				 * DString. */
{
    int ch = 0, *w, *wString;
    const char *p;
    size_t oldLength;
    Tcl_Size oldLength;
    /* Pointer to the end of string. Never read endPtr[0] */
    const char *endPtr = src + length;
    /* Pointer to last byte where optimization still can be used */
    const char *optPtr = endPtr - TCL_UTF_MAX;

    if (src == NULL) {
	return NULL;
    }
    if (length == TCL_INDEX_NONE) {
    if (length < 0) {
	length = strlen(src);
    }

    /*
     * Unicode string length in Tcl_UniChars will be <= UTF-8 string length in
     * bytes.
     */
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
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







-
+







-
+








-
+








    return wString;
}

unsigned short *
Tcl_UtfToChar16DString(
    const char *src,		/* UTF-8 string to convert to Unicode. */
    size_t length,			/* Length of UTF-8 string in bytes, or -1 for
    Tcl_Size length,		/* Length of UTF-8 string in bytes, or -1 for
				 * strlen(). */
    Tcl_DString *dsPtr)		/* Unicode representation of string is
				 * appended to this previously initialized
				 * DString. */
{
    unsigned short ch = 0, *w, *wString;
    const char *p;
    size_t oldLength;
    Tcl_Size oldLength;
    /* Pointer to the end of string. Never read endPtr[0] */
    const char *endPtr = src + length;
    /* Pointer to last byte where optimization still can be used */
    const char *optPtr = endPtr - TCL_UTF_MAX;

    if (src == NULL) {
	return NULL;
    }
    if (length == TCL_INDEX_NONE) {
    if (length < 0) {
	length = strlen(src);
    }

    /*
     * Unicode string length in WCHARs will be <= UTF-8 string length in
     * bytes.
     */
788
789
790
791
792
793
794
795

796
797
798
799
800
801
802
789
790
791
792
793
794
795

796
797
798
799
800
801
802
803







-
+







 *---------------------------------------------------------------------------
 */

int
Tcl_UtfCharComplete(
    const char *src,		/* String to check if first few bytes contain
				 * a complete UTF-8 character. */
    size_t length)			/* Length of above string in bytes. */
    Tcl_Size length)		/* Length of above string in bytes. */
{
    return length >= complete[UCHAR(*src)];
}

/*
 *---------------------------------------------------------------------------
 *
811
812
813
814
815
816
817
818

819
820
821
822


823
824
825

826
827

828
829
830
831
832
833
834
812
813
814
815
816
817
818

819
820
821


822
823
824
825

826
827

828
829
830
831
832
833
834
835







-
+


-
-
+
+


-
+

-
+







 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

size_t
Tcl_Size
Tcl_NumUtfChars(
    const char *src,	/* The UTF-8 string to measure. */
    size_t length)	/* The length of the string in bytes, or
			 * TCL_INDEX_NONE for strlen(src). */
    Tcl_Size length)	/* The length of the string in bytes, or
			 * negative value for strlen(src). */
{
    Tcl_UniChar ch = 0;
    size_t i = 0;
    Tcl_Size i = 0;

    if (length == TCL_INDEX_NONE) {
    if (length < 0) {
	/* string is NUL-terminated, so TclUtfToUniChar calls are safe. */
	while (*src != '\0') {
	    src += TclUtfToUniChar(src, &ch);
	    i++;
	}
    } else {
	/* Will return value between 0 and length. No overflow checks. */
863
864
865
866
867
868
869
870

871
872
873
874


875
876
877

878
879

880
881
882
883
884
885
886
864
865
866
867
868
869
870

871
872
873


874
875
876
877

878
879

880
881
882
883
884
885
886
887







-
+


-
-
+
+


-
+

-
+







	    }
	    i++;
	}
    }
    return i;
}

size_t
Tcl_Size
TclNumUtfChars(
    const char *src,	/* The UTF-8 string to measure. */
    size_t length)	/* The length of the string in bytes, or
			 * TCL_INDEX_NONE for strlen(src). */
    Tcl_Size length)	/* The length of the string in bytes, or
			 * negative for strlen(src). */
{
    unsigned short ch = 0;
    size_t i = 0;
    Tcl_Size i = 0;

    if (length == TCL_INDEX_NONE) {
    if (length < 0) {
	/* string is NUL-terminated, so TclUtfToUniChar calls are safe. */
	while (*src != '\0') {
	    src += Tcl_UtfToChar16(src, &ch);
	    i++;
	}
    } else {
	/* Will return value between 0 and length. No overflow checks. */
1190
1191
1192
1193
1194
1195
1196
1197

1198
1199
1200
1201
1202

1203
1204
1205
1206
1207
1208
1209
1191
1192
1193
1194
1195
1196
1197

1198
1199
1200
1201
1202

1203
1204
1205
1206
1207
1208
1209
1210







-
+




-
+







 *
 *---------------------------------------------------------------------------
 */

int
Tcl_UniCharAtIndex(
    const char *src,	/* The UTF-8 string to dereference. */
    size_t index)		/* The position of the desired character. */
    Tcl_Size index)	/* The position of the desired character. */
{
    Tcl_UniChar ch = 0;
    int i = 0;

    if (index == TCL_INDEX_NONE) {
    if (index < 0) {
	return -1;
    }
    while (index--) {
	i = TclUtfToUniChar(src, &ch);
	src += i;
    }
#if TCL_UTF_MAX < 4
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
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







-
+



-
+











-
+


-
+

-
+







 *
 *---------------------------------------------------------------------------
 */

const char *
Tcl_UtfAtIndex(
    const char *src,	/* The UTF-8 string. */
    size_t index)		/* The position of the desired character. */
    Tcl_Size index)	/* The position of the desired character. */
{
    int ch = 0;

    if (index != TCL_INDEX_NONE) {
    if (index > 0) {
	while (index--) {
	    /* Make use of the #undef Tcl_UtfToUniChar above, which already handles UCS4. */
	    src += Tcl_UtfToUniChar(src, &ch);
	}
    }
    return src;
}

const char *
TclUtfAtIndex(
    const char *src,	/* The UTF-8 string. */
    size_t index)		/* The position of the desired character. */
    Tcl_Size index)	/* The position of the desired character. */
{
    unsigned short ch = 0;
    size_t len = 0;
    Tcl_Size len = 0;

    if (index != TCL_INDEX_NONE) {
    if (index > 0) {
	while (index--) {
	    src += (len = Tcl_UtfToChar16(src, &ch));
	}
	if ((ch >= 0xD800) && (len < 3)) {
	    /* Index points at character following high Surrogate */
	    src += Tcl_UtfToChar16(src, &ch);
	}
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
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







-
+









+
-
+







 *	that represent the Unicode character is at least as large as the
 *	source buffer from which the backslashed sequence was extracted, no
 *	buffer overruns should occur.
 *
 *---------------------------------------------------------------------------
 */

size_t
Tcl_Size
Tcl_UtfBackslash(
    const char *src,		/* Points to the backslash character of a
				 * backslash sequence. */
    int *readPtr,		/* Fill in with number of characters read from
				 * src, unless NULL. */
    char *dst)			/* Filled with the bytes represented by the
				 * backslash sequence. */
{
#define LINE_LENGTH 128
    Tcl_Size numRead;
    size_t numRead, result;
    int result;

    result = TclParseBackslash(src, LINE_LENGTH, &numRead, dst);
    if (numRead == LINE_LENGTH) {
	/*
	 * We ate a whole line. Pay the price of a strlen()
	 */

1337
1338
1339
1340
1341
1342
1343
1344

1345
1346
1347
1348
1349
1350

1351
1352
1353
1354
1355
1356
1357
1339
1340
1341
1342
1343
1344
1345

1346
1347
1348
1349
1350
1351

1352
1353
1354
1355
1356
1357
1358
1359







-
+





-
+







 *
 * Side effects:
 *	Writes a terminating null after the last converted character.
 *
 *----------------------------------------------------------------------
 */

size_t
Tcl_Size
Tcl_UtfToUpper(
    char *str)			/* String to convert in place. */
{
    int ch, upChar;
    char *src, *dst;
    size_t len;
    Tcl_Size len;

    /*
     * Iterate over the string until we hit the terminating null.
     */

    src = dst = str;
    while (*src) {
1390
1391
1392
1393
1394
1395
1396
1397

1398
1399
1400
1401
1402
1403

1404
1405
1406
1407
1408
1409
1410
1392
1393
1394
1395
1396
1397
1398

1399
1400
1401
1402
1403
1404

1405
1406
1407
1408
1409
1410
1411
1412







-
+





-
+







 *
 * Side effects:
 *	Writes a terminating null after the last converted character.
 *
 *----------------------------------------------------------------------
 */

size_t
Tcl_Size
Tcl_UtfToLower(
    char *str)			/* String to convert in place. */
{
    int ch, lowChar;
    char *src, *dst;
    size_t len;
    Tcl_Size len;

    /*
     * Iterate over the string until we hit the terminating null.
     */

    src = dst = str;
    while (*src) {
1444
1445
1446
1447
1448
1449
1450
1451

1452
1453
1454
1455
1456
1457

1458
1459
1460
1461
1462
1463
1464
1446
1447
1448
1449
1450
1451
1452

1453
1454
1455
1456
1457
1458

1459
1460
1461
1462
1463
1464
1465
1466







-
+





-
+







 *
 * Side effects:
 *	Writes a terminating null after the last converted character.
 *
 *----------------------------------------------------------------------
 */

size_t
Tcl_Size
Tcl_UtfToTitle(
    char *str)			/* String to convert in place. */
{
    int ch, titleChar, lowChar;
    char *src, *dst;
    size_t len;
    Tcl_Size len;

    /*
     * Capitalize the first character and then lowercase the rest of the
     * characters until we get to a null.
     */

    src = dst = str;
1866
1867
1868
1869
1870
1871
1872
1873

1874
1875
1876
1877

1878
1879
1880
1881
1882
1883
1884
1868
1869
1870
1871
1872
1873
1874

1875
1876
1877
1878

1879
1880
1881
1882
1883
1884
1885
1886







-
+



-
+







 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

size_t
Tcl_Size
Tcl_Char16Len(
    const unsigned short *uniStr)	/* Unicode string to find length of. */
{
    size_t len = 0;
    Tcl_Size len = 0;

    while (*uniStr != '\0') {
	len++;
	uniStr++;
    }
    return len;
}
1897
1898
1899
1900
1901
1902
1903
1904

1905
1906
1907
1908

1909
1910
1911
1912
1913
1914
1915
1899
1900
1901
1902
1903
1904
1905

1906
1907
1908
1909

1910
1911
1912
1913
1914
1915
1916
1917







-
+



-
+







 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

#undef Tcl_UniCharLen
size_t
Tcl_Size
Tcl_UniCharLen(
    const int *uniStr)	/* Unicode string to find length of. */
{
    size_t len = 0;
    Tcl_Size len = 0;

    while (*uniStr != '\0') {
	len++;
	uniStr++;
    }
    return len;
}
2559
2560
2561
2562
2563
2564
2565
2566

2567
2568
2569

2570
2571
2572
2573
2574
2575
2576
2561
2562
2563
2564
2565
2566
2567

2568
2569
2570

2571
2572
2573
2574
2575
2576
2577
2578







-
+


-
+







 *
 *----------------------------------------------------------------------
 */

int
TclUniCharMatch(
    const Tcl_UniChar *string,	/* Unicode String. */
    size_t strLen,			/* Length of String */
    Tcl_Size strLen,		/* Length of String */
    const Tcl_UniChar *pattern,	/* Pattern, which may contain special
				 * characters. */
    size_t ptnLen,			/* Length of Pattern */
    Tcl_Size ptnLen,		/* Length of Pattern */
    int nocase)			/* 0 for case sensitive, 1 for insensitive */
{
    const Tcl_UniChar *stringEnd, *patternEnd;
    Tcl_UniChar p;

    stringEnd = string + strLen;
    patternEnd = pattern + ptnLen;