Changes On Branch wtf-8-experiment
Not logged in

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

Changes In Branch wtf-8-experiment Excluding Merge-Ins

This is equivalent to a diff from f358a5f413 to 6aeb87c775

2024-04-30
16:51
merge with trunk Leaf check-in: 6aeb87c775 user: chw tags: wtf-8-experiment
16:50
add selected tk upstream changes Leaf check-in: f358a5f413 user: chw tags: trunk
16:39
add selected tcl upstream changes check-in: 103bb0978a user: chw tags: trunk
2024-04-22
18:15
merge with trunk check-in: 46b8eecdce user: chw tags: wtf-8-experiment

Changes to jni/rl_json/generic/parser.c.

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
145
146
147
148
149
150
151




152
153
154
155
156
157
158
159

160
161
162
163
164
165
166







-
-
-
-








-







			(*p)--;
			// Have to check for this here - other unescaped control chars are handled by the < 0x1F
			// test, but 0x00 is transformed to 0xC0 0x80 by Tcl (MUTF-8 rather than UTF-8)
			return CHAR_ADVANCE_UNESCAPED_NULL;
		}
		if (first < 0xe0 /* 0b11100000 */) {
			eat = 1;
#if TCL_UTF_MAX == 3
		} else {
			eat = 2;
#else
		} else if (first < 0xf0 /* 0b11110000 */) {
			eat = 2;
		} else if (first < 0xf8 /* 0b11111000 */) {
			eat = 3;
		} else if (first < 0xfc /* 0b11111100 */) {
			eat = 4;
		} else {
			eat = 5;
#endif
		}
		*p += eat;
		*char_adj += eat;
	}

	return CHAR_ADVANCE_OK;
}
316
317
318
319
320
321
322
323

324
325
326
327
328
329
330
311
312
313
314
315
316
317

318
319
320
321
322
323
324
325







-
+







						case 'r': mapped='\r'; goto append_mapped;
						case 't': mapped='\t';
append_mapped:				Tcl_AppendToObj(out, &mapped, 1);		// Weird, but arranged this way the compiler optimizes it to a jump table
							break;

						case 'u':
							{
								Tcl_UniChar	acc = 0;
								unsigned int	acc = 0;
								char		utfbuf[6];
								int			i=4, digit;

								if (unlikely(e-p-2 < i)) {	// -2 is for the "u" and the close quote
									err_at = e-1;
									if (err_at < e && *err_at != '"')
										err_at++;	// Bodge the case where the doc was truncated without a closing quote
350
351
352
353
354
355
356
357
358
359
360

361
362
363
364
365
366
367
345
346
347
348
349
350
351

352
353

354
355
356
357
358
359
360
361







-


-
+







											goto err;
										}
									}

									acc <<= 4;
									acc += digit;
								}
#if TCL_UTF_MAX > 4
								if ((acc & 0xFFFFFC00) == 0xD800 && p[1] == '\\' && p[2] == 'u') {
									const unsigned char *pp = p + 2;
									Tcl_UniChar	acc2=0;
									unsigned int	acc2=0;

									i=4;
									if (unlikely(e-pp-2 < i)) {	// -2 is for the "u" and the close quote
										err_at = e-1;
										if (err_at < e && *err_at != '"')
											err_at++;	// Bodge the case where the doc was truncated without a closing quote
										errmsg = "Unicode sequence too short";
392
393
394
395
396
397
398








399
400
401
402
403
404
405
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407







+
+
+
+
+
+
+
+







									}

									if ((acc2 & 0xFFFFFC00) == 0xDC00) {
										acc = ((acc & 0x3FF) << 10) + 0x10000 + (acc2 & 0x3FF);
										p = pp;
									}
								}
#if TCL_UTF_MAX == 3
								if (acc > 0xFFFF) {
									acc -= 0x10000;
									Tcl_UniChar uch = ((acc >> 10) & 0x3FF) | 0xD800;
									acc = (acc & 0x3FF) | 0xDC00;
									const int len0 = Tcl_UniCharToUtf(uch, utfbuf);
									Tcl_AppendToObj(out, utfbuf, len0);
								}
#endif
								//const unsigned char* utfend = output_utf8(acc, utfbuf);
								const int len = Tcl_UniCharToUtf(acc, utfbuf);
								Tcl_AppendToObj(out, utfbuf, len);
							}
							break;

Changes to jni/sdl2tk/generic/tkCanvText.c.

1020
1021
1022
1023
1024
1025
1026







1027











1028
1029
1030
1031
1032
1033
1034
1020
1021
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
1051
1052







+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+







	index = textPtr->numChars;
    }
    byteIndex = Tcl_UtfAtIndex(text, index) - text;
    byteCount = strlen(string);
    if (byteCount == 0) {
	return;
    }
#if TCL_UTF_MAX < 4
    /*
     * Don't insert within surrogate pairs.
     */
    if (byteIndex) {
	Tcl_UniChar ch;
	const char *prevPtr;

	Tcl_UtfToUniChar(string + byteIndex, &ch);
	if ((ch & 0xfc00) == 0xdc00) {
	    prevPtr = Tcl_UtfPrev(string + byteIndex, string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xfc00) == 0xd800) {
		byteIndex = prevPtr - string;
		index -= 1;
	    }
	}
    }
#endif
    newStr = (char *) ckalloc(textPtr->numBytes + byteCount + 1);
    memcpy(newStr, text, byteIndex);
    strcpy(newStr + byteIndex, string);
    strcpy(newStr + byteIndex + byteCount, text + byteIndex);

    ckfree(text);
    textPtr->text = newStr;
1099
1100
1101
1102
1103
1104
1105





























1106
1107

















1108
1109
1110
1111
1112
1113
1114
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
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
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    }
    if (first > last) {
	return;
    }
    charsRemoved = last + 1 - first;

    byteIndex = Tcl_UtfAtIndex(text, first) - text;
#if TCL_UTF_MAX < 4
    /*
     * Delete complete surrogate pairs.
     */
    if (byteIndex >= 0) {
	Tcl_UniChar ch;
	const char *prevPtr, *nextPtr;

	Tcl_UtfToUniChar(text + byteIndex, &ch);
	if ((ch & 0xfc00) == 0xdc00) {
	    prevPtr = Tcl_UtfPrev(text + byteIndex, text);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xfc00) == 0xd800) {
		byteIndex = prevPtr - text;
		first--;
	    }
	} else if (charsRemoved == 1 && (ch & 0xfc00) == 0xd800) {
	    nextPtr = Tcl_UtfNext(text + byteIndex);
	    Tcl_UtfToUniChar(nextPtr, &ch);
	    if ((ch & 0xfc00) == 0xdc00) {
		last++;
            }
        }
	if (last >= textPtr->numChars) {
	    last = textPtr->numChars - 1;
	}
	charsRemoved = last + 1 - first;
    }
#endif
    byteCount = Tcl_UtfAtIndex(text + byteIndex, charsRemoved)
	- (text + byteIndex);
#if TCL_UTF_MAX < 4
    if (byteCount) {
	int len;
	Tcl_UniChar ch;
	const char *prevPtr;

	len = Tcl_UtfToUniChar(text + byteIndex + byteCount, &ch);
	if ((ch & 0xfc00) == 0xdc00) {
	    prevPtr = Tcl_UtfPrev(text + byteIndex + byteCount, text);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xfc00) == 0xd800) {
		byteCount += len;
		charsRemoved++;
	    }
	}
    }
#endif

    newStr = (char *) ckalloc(textPtr->numBytes + 1 - byteCount);
    memcpy(newStr, text, byteIndex);
    strcpy(newStr + byteIndex, text + byteIndex + byteCount);

    ckfree(text);
    textPtr->text = newStr;
1340
1341
1342
1343
1344
1345
1346




1347
1348
1349
1350
1351
1352
1353
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421







+
+
+
+







				 * index. */
{
    TextItem *textPtr = (TextItem *) itemPtr;
    int length;
    int c;
    Tk_CanvasTextInfo *textInfoPtr = textPtr->textInfoPtr;
    const char *string = Tcl_GetStringFromObj(obj, &length);
#if TCL_UTF_MAX < 4
    int roundUp = 0;
    int oldInsertPos = textPtr->insertPos;
#endif

    c = string[0];

    if ((c == 'e') && (strncmp(string, "end", length) == 0)) {
	*indexPtr = textPtr->numChars;
    } else if ((c == 'i')
	    && (strncmp(string, "insert", length) == 0)) {
1394
1395
1396
1397
1398
1399
1400



1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411































1412
1413
1414
1415
1416
1417
1418
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520







+
+
+











+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







		(int) (x*cs - y*s), (int) (y*cs + x*s));
    } else if (Tcl_GetIntFromObj(NULL, obj, indexPtr) == TCL_OK) {
	if (*indexPtr < 0) {
	    *indexPtr = 0;
	} else if (*indexPtr > textPtr->numChars) {
	    *indexPtr = textPtr->numChars;
	}
#if TCL_UTF_MAX < 4
	roundUp = (*indexPtr == (oldInsertPos + 1));
#endif
    } else {
	/*
	 * Some of the paths here leave messages in the interp's result, so we
	 * have to clear it out before storing our own message.
	 */

    badIndex:
	Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad index \"%s\"", string));
	Tcl_SetErrorCode(interp, "TK", "CANVAS", "ITEM_INDEX", "TEXT", NULL);
	return TCL_ERROR;
    }
#if TCL_UTF_MAX < 4
    /*
     * Enforce index on start or end of surrogate pair.
     */
    if (*indexPtr) {
	int byteIndex;
	Tcl_UniChar ch;
	const char *prevPtr, *nextPtr;

	string = textPtr->text;
	byteIndex = Tcl_UtfAtIndex(string, *indexPtr) - string;
	Tcl_UtfToUniChar(string + byteIndex, &ch);
	if ((ch & 0xfc00) == 0xdc00) {
	    if (roundUp) {
		*indexPtr += 1;
	    } else {
		prevPtr = Tcl_UtfPrev(string + byteIndex, string);
		Tcl_UtfToUniChar(prevPtr, &ch);
		if ((ch & 0xfc00) == 0xd800) {
		    *indexPtr -= 1;
		}
	    }
	} else if (roundUp && (ch & 0xfc00) == 0xd800) {
	    nextPtr = Tcl_UtfNext(string + byteIndex);
	    Tcl_UtfToUniChar(nextPtr, &ch);
	    if ((ch & 0xfc00) == 0xdc00) {
		*indexPtr += 2;
	    }
	}
    }
#endif
    return TCL_OK;
}

/*
 *--------------------------------------------------------------
 *
 * SetTextCursor --

Changes to jni/sdl2tk/generic/tkEntry.c.

1982
1983
1984
1985
1986
1987
1988
1989
1990


1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001


2002
2003
2004
2005
2006
2007
2008
1982
1983
1984
1985
1986
1987
1988


1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999


2000
2001
2002
2003
2004
2005
2006
2007
2008







-
-
+
+









-
-
+
+








    /*
     * If we're displaying a special character instead of the value of the
     * entry, recompute the displayString.
     */

    if (entryPtr->showChar != NULL) {
	Tcl_UniChar ch;
	char buf[TCL_UTF_MAX];
	int ch;
	char buf[6];
	int size;

	/*
	 * Normalize the special character so we can safely duplicate it in
	 * the display string. If we didn't do this, then two malformed
	 * characters might end up looking like one valid UTF character in the
	 * resulting string.
	 */

	Tcl_UtfToUniChar(entryPtr->showChar, &ch);
	size = Tcl_UniCharToUtf(ch, buf);
	TkUtfToUniChar(entryPtr->showChar, &ch);
	size = TkUniCharToUtf(ch, buf);

	entryPtr->numDisplayBytes = entryPtr->numChars * size;
	p = (char *) ckalloc(entryPtr->numDisplayBytes + 1);
	entryPtr->displayString = p;

	for (i = entryPtr->numChars; --i >= 0; ) {
	    memcpy(p, buf, size);
2159
2160
2161
2162
2163
2164
2165



2166
2167
2168
2169
2170
2171
2172




















2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186






















2187
2188
2189
2190
2191
2192
2193
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238







+
+
+







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+














+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







				 * string). */
{
    ptrdiff_t byteIndex;
    size_t byteCount, newByteCount;
    int oldChars, charsAdded;
    const char *string;
    char *newStr;
#if TCL_UTF_MAX < 4
    Tcl_UniChar ch;
#endif

    string = entryPtr->string;
    byteIndex = Tcl_UtfAtIndex(string, index) - string;
    byteCount = strlen(value);
    if (byteCount == 0) {
	return TCL_OK;
    }

#if TCL_UTF_MAX < 4
    /*
     * Don't insert within surrogate pairs.
     */
    if (byteIndex) {
	Tcl_UniChar ch;
	const char *prevPtr;

	Tcl_UtfToUniChar(string + byteIndex, &ch);
	if ((ch & 0xFC00) == 0xDC00) {
	    prevPtr = Tcl_UtfPrev(string + byteIndex, string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xFC00) == 0xD800) {
		byteIndex = prevPtr - string;
		index -= 1;
	    }
	}
    }
#endif

    newByteCount = entryPtr->numBytes + byteCount + 1;
    newStr = (char *) ckalloc(newByteCount);
    memcpy(newStr, string, byteIndex);
    strcpy(newStr + byteIndex, value);
    strcpy(newStr + byteIndex + byteCount, string + byteIndex);

    if ((entryPtr->validate == VALIDATE_KEY ||
	    entryPtr->validate == VALIDATE_ALL) &&
	    EntryValidateChange(entryPtr, value, newStr, index,
		    VALIDATE_INSERT) != TCL_OK) {
	ckfree(newStr);
	return TCL_OK;
    }

#if TCL_UTF_MAX < 4
    /*
     * Account for high surrogate at end of inserted string.
     */
    if (byteCount > 1) {
	const char *lastPtr = Tcl_UtfPrev(newStr+byteIndex+byteCount, newStr);

	Tcl_UtfToUniChar(lastPtr, &ch);
	if ((ch & 0xFC00) == 0xD800) {
	    /*
	     * A high surrogate. If followed by low surrogate,
	     * adjust index after it.
	     */
	    lastPtr = Tcl_UtfNext(lastPtr);
	    Tcl_UtfToUniChar(lastPtr, &ch);
	    if ((ch & 0xFC00) == 0xDC00) {
		index++;
	    }
	}
    }
#endif

    ckfree((char *)string);
    entryPtr->string = newStr;

    /*
     * The following construction is used because inserting improperly formed
     * UTF-8 sequences between other improperly formed UTF-8 sequences could
2266
2267
2268
2269
2270
2271
2272































2273


















2274
2275
2276
2277
2278
2279
2280
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    }
    if (count <= 0) {
	return TCL_OK;
    }

    string = entryPtr->string;
    byteIndex = Tcl_UtfAtIndex(string, index) - string;

#if TCL_UTF_MAX < 4
    /*
     * Delete complete surrogate pairs.
     */
    if (byteIndex >= 0) {
	Tcl_UniChar ch;
	const char *prevPtr, *nextPtr;

	Tcl_UtfToUniChar(string + byteIndex, &ch);
	if ((ch & 0xFC00) == 0xDC00) {
	    prevPtr = Tcl_UtfPrev(string + byteIndex, string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xFC00) == 0xD800) {
		byteIndex = prevPtr - string;
		count++;
		index--;
	    }
	} else if ((count == 1) && ((ch & 0xFC00) == 0xD800)) {
	    nextPtr = Tcl_UtfNext(string + byteIndex);
	    Tcl_UtfToUniChar(nextPtr, &ch);
	    if ((ch & 0xFC00) == 0xDC00) {
		count++;
	    }
	}
	if ((index + count) > entryPtr->numChars) {
	    count = entryPtr->numChars - index;
	}
    }
#endif

    byteCount = Tcl_UtfAtIndex(string + byteIndex, count) - (string+byteIndex);

#if TCL_UTF_MAX < 4
    if (byteCount) {
	int len;
	Tcl_UniChar ch;
	const char *prevPtr;

	len = Tcl_UtfToUniChar(string + byteIndex + byteCount, &ch);
	if ((ch & 0xFC00) == 0xDC00) {
	    prevPtr = Tcl_UtfPrev(string + byteIndex + byteCount, string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xFC00) == 0xD800) {
		byteCount += len;
		count++;
	    }
	}
    }
#endif

    newByteCount = entryPtr->numBytes + 1 - byteCount;
    newStr = (char *) ckalloc(newByteCount);
    memcpy(newStr, string, (size_t) byteIndex);
    strcpy(newStr + byteIndex, string + byteIndex + byteCount);

    toDelete = (char *) ckalloc(byteCount + 1);
2673
2674
2675
2676
2677
2678
2679




2680
2681
2682
2683
2684
2685
2686
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784







+
+
+
+







    Entry *entryPtr,		/* Entry for which the index is being
				 * specified. */
    Tcl_Obj *indexObj,	/* Specifies character in entryPtr. */
    int *indexPtr)		/* Where to store converted character index */
{
    const char *string = Tcl_GetString(indexObj);
    size_t length = indexObj->length;
    int roundUp = 0;
#if TCL_UTF_MAX < 4
    int oldInsertPos = entryPtr->insertPos;
#endif

    switch (string[0]) {
    case 'a':
	if (strncmp(string, "anchor", length) != 0) {
	    goto badIndex;
	}
	*indexPtr = entryPtr->selectAnchor;
2716
2717
2718
2719
2720
2721
2722
2723

2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2814
2815
2816
2817
2818
2819
2820

2821
2822
2823
2824
2825
2826
2827
2828

2829
2830
2831
2832
2833
2834
2835







-
+







-







	} else if (strncmp(string, "sel.last", length) == 0) {
	    *indexPtr = entryPtr->selectLast;
	} else {
	    goto badIndex;
	}
	break;
    case '@': {
	int x, roundUp, maxWidth;
	int x, maxWidth;

	if (Tcl_GetInt(NULL, string + 1, &x) != TCL_OK) {
	    goto badIndex;
	}
	if (x < entryPtr->inset) {
	    x = entryPtr->inset;
	}
	roundUp = 0;
	maxWidth = Tk_Width(entryPtr->tkwin) - entryPtr->inset
		- entryPtr->xWidth - 1;
	if (x > maxWidth) {
	    x = maxWidth;
	    roundUp = 1;
	}
	*indexPtr = Tk_PointToChar(entryPtr->textLayout,
2755
2756
2757
2758
2759
2760
2761




2762



























2763
2764
2765
2766
2767
2768
2769
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897







+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	    goto badIndex;
	}
	if (*indexPtr < 0){
	    *indexPtr = 0;
	} else if (*indexPtr > entryPtr->numChars) {
	    *indexPtr = entryPtr->numChars;
	}
#if TCL_UTF_MAX < 4
	roundUp = (indexPtr == &entryPtr->insertPos) &&
		(*indexPtr == (oldInsertPos + 1));
#endif
    }

#if TCL_UTF_MAX < 4
    /*
     * Enforce index on start or end of surrogate pair.
     */
    if (*indexPtr) {
	int byteIndex;
	Tcl_UniChar ch;
	const char *prevPtr;

	string = entryPtr->string;
	byteIndex = Tcl_UtfAtIndex(string, *indexPtr) - string;
	Tcl_UtfToUniChar(string + byteIndex, &ch);
	if ((ch & 0xFC00) == 0xDC00) {
	    if (roundUp) {
		*indexPtr += 1;
	    } else {
		prevPtr = Tcl_UtfPrev(string + byteIndex, string);
		Tcl_UtfToUniChar(prevPtr, &ch);
		if ((ch & 0xFC00) == 0xD800) {
		    *indexPtr -= 1;
		}
	    }
	}
    }
#endif

    return TCL_OK;

  badIndex:
    Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad %s index \"%s\"",
	    (entryPtr->type == TK_ENTRY) ? "entry" : "spinbox", string));
    Tcl_SetErrorCode(interp, "TK",
	    (entryPtr->type == TK_ENTRY) ? "ENTRY" : "SPINBOX",
2869
2870
2871
2872
2873
2874
2875





















2876
2877
2878
2879
2880
2881
2882
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







     * Pick new starting and ending points for the selection.
     */

    if (entryPtr->selectAnchor > entryPtr->numChars) {
	entryPtr->selectAnchor = entryPtr->numChars;
    }
    if (entryPtr->selectAnchor <= index) {
#if TCL_UTF_MAX < 4
	/*
	 * Correct ending point for surrogate pair.
	 */

	Tcl_UniChar ch;
	const char *string;

	string = Tcl_UtfAtIndex(entryPtr->string, index);
	string += Tcl_UtfToUniChar(string, &ch);
	if (((ch & 0xFC00) == 0xDC00) && (index + 1 < entryPtr->numChars)) {
	    index += 1;
	} else if (((ch & 0xFC00) == 0xD800) &&
			(index + 1 < entryPtr->numChars) &&
			(index == entryPtr->insertPos)) {
	    Tcl_UtfToUniChar(string, &ch);
	    if ((ch & 0xFC00) == 0xDC00) {
		index += 2;
	    }
	}
#endif
	newFirst = entryPtr->selectAnchor;
	newLast = index;
    } else {
	newFirst = index;
	newLast = entryPtr->selectAnchor;
	if (newLast < 0) {
	    newFirst = newLast = -1;
3546
3547
3548
3549
3550
3551
3552
3553

3554
3555
3556
3557
3558
3559
3560
3695
3696
3697
3698
3699
3700
3701

3702
3703
3704
3705
3706
3707
3708
3709







-
+







     Tcl_DString *dsPtr)	/* Dynamic string in which to append new
				 * command. */
{
    int spaceNeeded, cvtFlags;	/* Used to substitute string as proper Tcl
				 * list element. */
    int number, length;
    const char *string;
    Tcl_UniChar ch;
    int ch;
    char numStorage[2*TCL_INTEGER_SPACE];

    while (1) {
	if (*before == '\0') {
	    break;
	}
	/*
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
3728
3729
3730
3731
3732
3733
3734

3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754

3755
3756
3757
3758
3759
3760
3761
3762







-
+



















-
+








	/*
	 * There's a percent sequence here. Process it.
	 */

	before++; /* skip over % */
	if (*before != '\0') {
	    before += Tcl_UtfToUniChar(before, &ch);
	    before += TkUtfToUniChar(before, &ch);
	} else {
	    ch = '%';
	}
	if (type == VALIDATE_BUTTON) {
	    /*
	     * -command %-substitution
	     */

	    switch (ch) {
	    case 's':		/* Current string value of spinbox */
		string = entryPtr->string;
		break;
	    case 'd':		/* direction, up or down */
		string = change;
		break;
	    case 'W':		/* widget name */
		string = Tk_PathName(entryPtr->tkwin);
		break;
	    default:
		length = Tcl_UniCharToUtf(ch, numStorage);
		length = TkUniCharToUtf(ch, numStorage);
		numStorage[length] = '\0';
		string = numStorage;
		break;
	    }
	} else {
	    /*
	     * -validatecommand / -invalidcommand %-substitution
3659
3660
3661
3662
3663
3664
3665
3666

3667
3668
3669
3670
3671
3672
3673
3808
3809
3810
3811
3812
3813
3814

3815
3816
3817
3818
3819
3820
3821
3822







-
+







		    break;
		}
		break;
	    case 'W': /* widget name */
		string = Tk_PathName(entryPtr->tkwin);
		break;
	    default:
		length = Tcl_UniCharToUtf(ch, numStorage);
		length = TkUniCharToUtf(ch, numStorage);
		numStorage[length] = '\0';
		string = numStorage;
		break;
	    }
	}

	spaceNeeded = Tcl_ScanCountedElement(string, -1, &cvtFlags);
4159
4160
4161
4162
4163
4164
4165
4166

4167
4168
4169
4170
4171
4172
4173
4308
4309
4310
4311
4312
4313
4314

4315
4316
4317
4318
4319
4320
4321
4322







-
+







		goto error;
	    }
	    if (GetEntryIndex(interp, entryPtr,
		    objv[3], &index) != TCL_OK) {
		goto error;
	    }
	    if (GetEntryIndex(interp, entryPtr,
		    objv[4],& index2) != TCL_OK) {
		    objv[4], &index2) != TCL_OK) {
		goto error;
	    }
	    if (index >= index2) {
		entryPtr->selectFirst = -1;
		entryPtr->selectLast = -1;
	    } else {
		entryPtr->selectFirst = index;

Changes to jni/sdl2tk/generic/tkFont.c.

560
561
562
563
564
565
566
567

568
569
570
571
572
573
574
560
561
562
563
564
565
566

567
568
569
570
571
572
573
574







-
+








    switch ((enum options) index) {
    case FONT_ACTUAL: {
	int skip, result, n;
	const char *s;
	Tk_Font tkfont;
	Tcl_Obj *optPtr, *charPtr, *resultPtr;
	Tcl_UniChar uniChar = 0;
	int uniChar = 0;
	const TkFontAttributes *faPtr;
	TkFontAttributes fa;

	/*
	 * Params 0 and 1 are 'font actual'. Param 2 is the font name. 3-4 may
	 * be '-displayof $window'
	 */
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
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







+
+
-
+
+


-
+






-







	}

	/*
	 * The 'charPtr' arg must be a single Unicode.
	 */

	if (charPtr != NULL) {
	    const char *string = Tcl_GetString(charPtr);
	    int len = TkUtfToUniChar(string, &uniChar);
	    if (Tcl_GetCharLength(charPtr) != 1) {

	    if (len != charPtr->length) {
		resultPtr = Tcl_NewStringObj(
			"expected a single character but got \"", -1);
		Tcl_AppendLimitedToObj(resultPtr, Tcl_GetString(charPtr),
		Tcl_AppendLimitedToObj(resultPtr, string,
			-1, 40, "...");
		Tcl_AppendToObj(resultPtr, "\"", -1);
		Tcl_SetObjResult(interp, resultPtr);
		Tcl_SetErrorCode(interp, "TK", "VALUE", "FONT_SAMPLE", NULL);
		return TCL_ERROR;
	    }
	    uniChar = Tcl_GetUniChar(charPtr, 0);
	}

	/*
	 * Find the font.
	 */

	tkfont = Tk_AllocFontFromObj(interp, tkwin, objv[2]);
1803
1804
1805
1806
1807
1808
1809
1810

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

1829
1830
1831
1832
1833
1834
1835

1836
1837
1838
1839
1840
1841
1842
1805
1806
1807
1808
1809
1810
1811

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

1830
1831
1832
1833
1834
1835
1836

1837
1838
1839
1840
1841
1842
1843
1844







-
+

















-
+






-
+







    } else if (strcasecmp(family, "AvantGarde") == 0) {
	family = "AvantGarde";
    } else if (strcasecmp(family, "ZapfChancery") == 0) {
	family = "ZapfChancery";
    } else if (strcasecmp(family, "ZapfDingbats") == 0) {
	family = "ZapfDingbats";
    } else {
	Tcl_UniChar ch;
	int ch;

	/*
	 * Inline, capitalize the first letter of each word, lowercase the
	 * rest of the letters in each word, and then take out the spaces
	 * between the words. This may make the DString shorter, which is safe
	 * to do.
	 */

	Tcl_DStringAppend(dsPtr, family, -1);

	src = dest = Tcl_DStringValue(dsPtr) + len;
	upper = 1;
	for (; *src != '\0'; ) {
	    while (isspace(UCHAR(*src))) { /* INTL: ISO space */
		src++;
		upper = 1;
	    }
	    src += Tcl_UtfToUniChar(src, &ch);
	    src += TkUtfToUniChar(src, &ch);
	    if (upper) {
		ch = Tcl_UniCharToUpper(ch);
		upper = 0;
	    } else {
		ch = Tcl_UniCharToLower(ch);
	    }
	    dest += Tcl_UniCharToUtf(ch, dest);
	    dest += TkUniCharToUtf(ch, dest);
	}
	*dest = '\0';
	Tcl_DStringSetLength(dsPtr, dest - Tcl_DStringValue(dsPtr));
	family = Tcl_DStringValue(dsPtr) + len;
    }
    if (family != Tcl_DStringValue(dsPtr) + len) {
	Tcl_DStringAppend(dsPtr, family, -1);
2862
2863
2864
2865
2866
2867
2868
2869

2870
2871
2872
2873
2874
2875
2876
2864
2865
2866
2867
2868
2869
2870

2871
2872
2873
2874
2875
2876
2877
2878







-
+







	    end = Tcl_UtfAtIndex(chunkPtr->start, index);
	    if (xPtr != NULL) {
		Tk_MeasureChars(tkfont, chunkPtr->start,
			end - chunkPtr->start, -1, 0, &x);
		x += chunkPtr->x;
	    }
	    if (widthPtr != NULL) {
		Tk_MeasureChars(tkfont, end, Tcl_UtfNext(end) - end,
		Tk_MeasureChars(tkfont, end, TkUtfNext(end) - end,
			-1, 0, &w);
	    }
	    goto check;
	}
	index -= chunkPtr->numChars;
	chunkPtr++;
    }
3385
3386
3387
3388
3389
3390
3391
3392

3393
3394
3395
3396
3397
3398
3399
3387
3388
3389
3390
3391
3392
3393

3394
3395
3396
3397
3398
3399
3400
3401







-
+







    TextLayout *layoutPtr = (TextLayout *) layout;
    LayoutChunk *chunkPtr = layoutPtr->chunks;
    int baseline = chunkPtr->y;
    Tcl_Obj *psObj = Tcl_NewObj();
    int i, j, len;
    const char *p, *glyphname;
    char uindex[5], c, *ps;
    Tcl_UniChar ch;
    int ch;

    Tcl_AppendToObj(psObj, "[(", -1);
    for (i = 0; i < layoutPtr->numChunks; i++, chunkPtr++) {
	if (baseline != chunkPtr->y) {
	    Tcl_AppendToObj(psObj, ")]\n[(", -1);
	    baseline = chunkPtr->y;
	}
3408
3409
3410
3411
3412
3413
3414
3415

3416
3417
3418
3419
3420
3421
3422
3410
3411
3412
3413
3414
3415
3416

3417
3418
3419
3420
3421
3422
3423
3424







-
+







	    /*
	     * INTL: We only handle symbols that have an encoding as a glyph
	     * from the standard set defined by Adobe. The rest get punted.
	     * Eventually this should be revised to handle more sophsticiated
	     * international postscript fonts.
	     */

	    p += Tcl_UtfToUniChar(p, &ch);
	    p += TkUtfToUniChar(p, &ch);
	    if ((ch == '(') || (ch == ')') || (ch == '\\') || (ch < 0x20)) {
		/*
		 * Tricky point: the "03" is necessary in the sprintf below,
		 * so that a full three digits of octal are always generated.
		 * Without the "03", a number following this sequence could be
		 * interpreted by Postscript as part of this sequence.
		 */

Changes to jni/sdl2tk/generic/tkInt.h.

1321
1322
1323
1324
1325
1326
1327













1328
1329
1330
1331
1332
1333
1334
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







+
+
+
+
+
+
+
+
+
+
+
+
+







#if !defined(__cplusplus) && !defined(c_plusplus)
# define c_class class
#endif

#ifdef TK_USE_POLL
MODULE_SCOPE TkDisplay * TkGetDisplayListExt(struct pollfd **pollTablePtr);
#endif

#if TCL_UTF_MAX > 3
#   define TkUtfToUniChar(src, chPtr) \
	Tcl_UtfToUniChar((src), (Tcl_UniChar *)(chPtr))
#   define TkUniCharToUtf Tcl_UniCharToUtf
#   define TkUtfPrev Tcl_UtfPrev
#   define TkUtfNext Tcl_UtfNext
#else
    MODULE_SCOPE int TkUtfToUniChar(const char *, int *);
    MODULE_SCOPE int TkUniCharToUtf(int, char *);
    MODULE_SCOPE const char *TkUtfPrev(const char *, const char *);
    MODULE_SCOPE const char *TkUtfNext(const char *);
#endif

/*
 * Unsupported commands.
 */

MODULE_SCOPE int	TkUnsupported1ObjCmd(ClientData clientData,
			    Tcl_Interp *interp, int objc,

Changes to jni/sdl2tk/generic/tkSelect.c.

1290
1291
1292
1293
1294
1295
1296
















1297

1298
1299
1300
1301
1302
1303
1304
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+







SelGetProc(
    ClientData clientData,	/* Dynamic string holding partially assembled
				 * selection. */
    Tcl_Interp *interp,		/* Interpreter used for error reporting (not
				 * used). */
    const char *portion)	/* New information to be appended. */
{
#if TCL_UTF_MAX < 4
    /*
     * Ensure WTF-8 without 4-byte UTF-8 sequences.
     */
    Tcl_Encoding encoding;
    Tcl_DString tmp;

    encoding = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_ExternalToUtfDString(encoding, portion, -1, &tmp);
    Tcl_DStringAppend((Tcl_DString *) clientData, Tcl_DStringValue(&tmp),
	Tcl_DStringLength(&tmp));
    Tcl_DStringFree(&tmp);
    if (encoding) {
	Tcl_FreeEncoding(encoding);
    }
#else
    Tcl_DStringAppend((Tcl_DString *) clientData, portion, -1);
#endif
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * HandleTclCommand --
1396
1397
1398
1399
1400
1401
1402
1403

1404
1405
1406
1407
1408
1409
1410
1413
1414
1415
1416
1417
1418
1419

1420
1421
1422
1423
1424
1425
1426
1427







-
+







	 */

	if (cmdInfoPtr->interp != NULL) {
	    if (length <= maxBytes) {
		cmdInfoPtr->charOffset += Tcl_NumUtfChars(string, -1);
		cmdInfoPtr->buffer[0] = '\0';
	    } else {
		Tcl_UniChar ch = 0;
		Tcl_UniChar ch;

		p = string;
		string += count;
		numChars = 0;
		while (p < string) {
		    p += Tcl_UtfToUniChar(p, &ch);
		    numChars++;

Changes to jni/sdl2tk/generic/tkText.c.

4447
4448
4449
4450
4451
4452
4453
4454

4455
4456
4457
4458
4459
4460
4461
4447
4448
4449
4450
4451
4452
4453

4454
4455
4456
4457
4458
4459
4460
4461







-
+







    Tcl_Obj *stringPtr)		/* Description of the tab stops. See the text
				 * manual entry for details. */
{
    int objc, i, count;
    Tcl_Obj **objv;
    TkTextTabArray *tabArrayPtr;
    TkTextTab *tabPtr;
    Tcl_UniChar ch;
    int ch;
    double prevStop, lastStop;
    /*
     * Map these strings to TkTextTabAlign values.
     */
    static const char *const tabOptionStrings[] = {
	"left", "right", "center", "numeric", NULL
    };
4555
4556
4557
4558
4559
4560
4561
4562

4563
4564
4565
4566
4567
4568
4569
4555
4556
4557
4558
4559
4560
4561

4562
4563
4564
4565
4566
4567
4568
4569







-
+







	    continue;
	}

	/*
	 * There may be a more efficient way of getting this.
	 */

	Tcl_UtfToUniChar(Tcl_GetString(objv[i+1]), &ch);
	TkUtfToUniChar(Tcl_GetString(objv[i+1]), &ch);
	if (!Tcl_UniCharIsAlpha(ch)) {
	    continue;
	}
	i += 1;

	if (Tcl_GetIndexFromObjStruct(interp, objv[i], tabOptionStrings,
		sizeof(char *), "tab alignment", 0, &index) != TCL_OK) {
5870
5871
5872
5873
5874
5875
5876
5877

5878
5879
5880
5881
5882
5883
5884
5870
5871
5872
5873
5874
5875
5876

5877
5878
5879
5880
5881
5882
5883
5884







-
+








	if (searchSpecPtr->exact) {
	    int maxExtraLines = 0;
	    const char *startOfLine = Tcl_GetString(theLine);

	    CLANG_ASSERT(pattern);
	    do {
		Tcl_UniChar ch;
		int ch;
		const char *p;
		int lastFullLine = lastOffset;

		if (firstNewLine == -1) {
		    if (searchSpecPtr->strictLimits
			    && (firstOffset + matchLength > lastOffset)) {
			/*
6104
6105
6106
6107
6108
6109
6110
6111

6112
6113
6114
6115
6116
6117
6118
6104
6105
6106
6107
6108
6109
6110

6111
6112
6113
6114
6115
6116
6117
6118







-
+







		    if (searchSpecPtr->backwards) {
			alreadySearchOffset = p - startOfLine - 1;
			if (alreadySearchOffset < 0) {
			    break;
			}
		    } else {
			firstOffset = p - startOfLine +
				Tcl_UtfToUniChar(startOfLine+matchOffset,&ch);
				TkUtfToUniChar(startOfLine+matchOffset,&ch);
		    }
		}
	    } while (searchSpecPtr->all);
	} else {
	    int maxExtraLines = 0;
	    int matches = 0;
	    int lastNonOverlap = -1;

Changes to jni/sdl2tk/generic/tkTextDisp.c.

7705
7706
7707
7708
7709
7710
7711
7712
7713


7714
7715
7716
7717
7718
7719
7720
7705
7706
7707
7708
7709
7710
7711


7712
7713
7714
7715
7716
7717
7718
7719
7720







-
-
+
+







#else /* !TK_LAYOUT_WITH_BASE_CHUNKS */
    bytesThatFit = CharChunkMeasureChars(chunkPtr, p, maxBytes, 0, -1,
	    chunkPtr->x, maxX, TK_ISOLATE_END, &nextX);
#endif /* TK_LAYOUT_WITH_BASE_CHUNKS */

    if (bytesThatFit < maxBytes) {
	if ((bytesThatFit == 0) && noCharsYet) {
	    Tcl_UniChar ch;
	    int chLen = Tcl_UtfToUniChar(p, &ch);
	    int ch;
	    int chLen = TkUtfToUniChar(p, &ch);

#ifdef TK_LAYOUT_WITH_BASE_CHUNKS
	    bytesThatFit = CharChunkMeasureChars(chunkPtr, line,
		    lineOffset+chLen, lineOffset, -1, chunkPtr->x, -1, 0,
		    &nextX);
#else /* !TK_LAYOUT_WITH_BASE_CHUNKS */
	    bytesThatFit = CharChunkMeasureChars(chunkPtr, p, chLen, 0, -1,

Changes to jni/sdl2tk/generic/tkTextIndex.c.

383
384
385
386
387
388
389
390

391
392
393
394
395
396
397
383
384
385
386
387
388
389

390
391
392
393
394
395
396
397







-
+







				 * of text). */
    int byteIndex,		/* Byte index of desired character. */
    TkTextIndex *indexPtr)	/* Structure to fill in. */
{
    TkTextSegment *segPtr;
    int index;
    const char *p, *start;
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;

    indexPtr->tree = tree;
    if (lineIndex < 0) {
	lineIndex = 0;
	byteIndex = 0;
    }
    if (byteIndex < 0) {
476
477
478
479
480
481
482
483

484
485
486
487
488
489
490
476
477
478
479
480
481
482

483
484
485
486
487
488
489
490







-
+







				 * of text). */
    int charIndex,		/* Index of desired character. */
    TkTextIndex *indexPtr)	/* Structure to fill in. */
{
    TkTextSegment *segPtr;
    char *p, *start, *end;
    int index, offset;
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;

    indexPtr->tree = tree;
    if (lineIndex < 0) {
	lineIndex = 0;
	charIndex = 0;
    }
    if (charIndex < 0) {
1548
1549
1550
1551
1552
1553
1554
1555

1556
1557
1558
1559
1560
1561
1562
1548
1549
1550
1551
1552
1553
1554

1555
1556
1557
1558
1559
1560
1561
1562







-
+







    TkTextCountType type)	/* The type of item to count */
{
    TkTextLine *linePtr;
    TkTextSegment *segPtr;
    TkTextElideInfo *infoPtr = NULL;
    int byteOffset;
    char *start, *end, *p;
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;
    int elide = 0;
    int checkElided = (type & COUNT_DISPLAY);

    if (charCount < 0) {
	TkTextIndexBackChars(textPtr, srcPtr, -charCount, dstPtr, type);
	return;
    }
1648
1649
1650
1651
1652
1653
1654













1655
1656
1657
1658
1659
1660
1661
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674







+
+
+
+
+
+
+
+
+
+
+
+
+







	    }

	    if (!elide) {
		if (segPtr->typePtr == &tkTextCharType) {
		    start = segPtr->body.chars + byteOffset;
		    end = segPtr->body.chars + segPtr->size;
		    for (p = start; p < end; p += Tcl_UtfToUniChar(p, &ch)) {
#if TCL_UTF_MAX < 4
			if (((ch & 0xFC00) == 0xD800) && (p < end)) {
			    char *pp = p;

			    pp += Tcl_UtfToUniChar(pp, &ch);
			    if ((ch & 0xFC00) == 0xDC00) {
				p = pp;
				if (charCount > 0) {
				    charCount--;
				} 
			    }
			}
#endif
			if (charCount == 0) {
			    dstPtr->byteIndex += (p - start);
			    goto forwardCharDone;
			}
			charCount--;
		    }
		} else if (type & COUNT_INDICES) {
2184
2185
2186
2187
2188
2189
2190


















2191
2192
2193
2194
2195
2196
2197
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	}

	if (!elide) {
	    if (segPtr->typePtr == &tkTextCharType) {
		start = segPtr->body.chars;
		end = segPtr->body.chars + segSize;
		for (p = end; ; p = Tcl_UtfPrev(p, start)) {
#if TCL_UTF_MAX < 4
		    Tcl_UniChar ch;

		    Tcl_UtfToUniChar(p, &ch);
		    if (((ch & 0xFC00) == 0xDC00) && (p > start)) {
			const char *pp = Tcl_UtfPrev(p, start);

			if (pp != NULL) {
			    Tcl_UtfToUniChar(pp, &ch);
			    if ((ch & 0xFC00) == 0xD800) {
				p = pp;
				if (charCount > 0) {
				    charCount--;
				} 
			    }
			}
		    }
#endif
		    if (charCount == 0) {
			dstPtr->byteIndex -= (end - p);
			goto backwardCharDone;
		    }
		    if (p == start) {
			break;
		    }
2371
2372
2373
2374
2375
2376
2377
2378

2379
2380

2381
2382
2383
2384
2385
2386
2387
2402
2403
2404
2405
2406
2407
2408

2409
2410

2411
2412
2413
2414
2415
2416
2417
2418







-
+

-
+







		    COUNT_DISPLAY_INDICES);
	}
	segPtr = TkTextIndexToSeg(indexPtr, &offset);
	while (1) {
	    int chSize = 1;

	    if (segPtr->typePtr == &tkTextCharType) {
		Tcl_UniChar ch = 0;
		int ch;

		chSize = Tcl_UtfToUniChar(segPtr->body.chars + offset, &ch);
		chSize = TkUtfToUniChar(segPtr->body.chars + offset, &ch);
		if (!Tcl_UniCharIsWordChar(ch)) {
		    break;
		}
		firstChar = 0;
	    }
	    offset += chSize;
	    indexPtr->byteIndex += chSize;
2416
2417
2418
2419
2420
2421
2422
2423

2424
2425

2426
2427
2428
2429
2430
2431

2432
2433
2434
2435
2436
2437
2438
2447
2448
2449
2450
2451
2452
2453

2454
2455

2456
2457
2458
2459
2460
2461

2462
2463
2464
2465
2466
2467
2468
2469







-
+

-
+





-
+







	 */

	segPtr = TkTextIndexToSeg(indexPtr, &offset);
	while (1) {
	    int chSize = 1;

	    if (segPtr->typePtr == &tkTextCharType) {
		Tcl_UniChar ch = 0;
		int ch;

		Tcl_UtfToUniChar(segPtr->body.chars + offset, &ch);
		TkUtfToUniChar(segPtr->body.chars + offset, &ch);
		if (!Tcl_UniCharIsWordChar(ch)) {
		    break;
		}
		if (offset > 0) {
		    chSize = (segPtr->body.chars + offset
			    - Tcl_UtfPrev(segPtr->body.chars + offset,
			    - TkUtfPrev(segPtr->body.chars + offset,
			    segPtr->body.chars));
		}
		firstChar = 0;
	    }
            if (offset == 0) {
		if (indexPtr->byteIndex == 0) {
		    goto done;

Changes to jni/sdl2tk/generic/tkUtil.c.

1189
1190
1191
1192
1193
1194
1195




























































































































































1196
1197
1198
1199
1200
1201
1202
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
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
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
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    if (detail != NULL) {
	/* Event code will call Tcl_DecrRefCount(). */
	Tcl_IncrRefCount(detail);
    }

    Tk_QueueWindowEvent(&event.general, TCL_QUEUE_TAIL);
}

#if TCL_UTF_MAX < 4
/*
 *---------------------------------------------------------------------------
 *
 * TkUtfToUniChar --
 *
 *	Almost the same as Tcl_UtfToUniChar but using int instead of Tcl_UniChar.
 *	This function is capable of collapsing a upper/lower surrogate pair to a
 *	single unicode character. So, up to 6 bytes might be consumed.
 *
 * Results:
 *	*chPtr is filled with the Tcl_UniChar, and the return value is the
 *	number of bytes from the UTF-8 string that were consumed.
 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

int
TkUtfToUniChar(
    const char *src,	/* The UTF-8 string. */
    int *chPtr)		/* Filled with the Tcl_UniChar represented by
			 * the UTF-8 string. */
{
    Tcl_UniChar uniChar = 0;
    int len = Tcl_UtfToUniChar(src, &uniChar);

    if ((uniChar & 0xfc00) == 0xd800) {
	int high = uniChar;
	int len2 = Tcl_UtfToUniChar(src+len, &uniChar);

	if ((uniChar & 0xfc00) == 0xdc00) {
	    *chPtr = (((high & 0x3ff) << 10) | (uniChar & 0x3ff)) + 0x10000;
	    len += len2;
	} else {
	    *chPtr = high;
	}
    } else {
	*chPtr = uniChar;
    }
    return len;
}

/*
 *---------------------------------------------------------------------------
 *
 * TkUniCharToUtf --
 *
 *	Almost the same as Tcl_UniCharToUtf but producing surrogates if
 *	TCL_UTF_MAX==3. So, up to 6 bytes might be produced.
 *
 * Results:
 *	*buf is filled with the UTF-8 string, and the return value is the
 *	number of bytes produced.
 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

int
TkUniCharToUtf(
    int ch,
    char *buf)
{
    int size;

    if ((ch > 0xffff) && (ch <= 0x10ffff)) {
	ch -= 0x10000;
	size = Tcl_UniCharToUtf(((ch >> 10) | 0xd800), buf);
	size += Tcl_UniCharToUtf(((ch & 0x3ff) | 0xdc00), buf+size);
    } else {
	size = Tcl_UniCharToUtf(ch, buf);
    }
    return size;
}

/*
 *---------------------------------------------------------------------------
 *
 * TkUtfPrev --
 *
 *	Almost the same as Tcl_UtfPrev but check for surrogates if
 *	TCL_UTF_MAX==3. So, the pointer might move up to 6 bytes.
 *
 * Results:
 *	Return a pointer to the previous unicode character.
 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

const char *
TkUtfPrev(
    const char *start,
    const char *source)
{
    const char *p = Tcl_UtfPrev(start, source);

    if ((p == source-3) && ((p[0]&0xFF) == 0xED)
	    && ((p[1]&0xF0) == 0xB0) && ((p[2]&0xC0) == 0x80)) {
	/* We are pointing to a low surrogate. If the previous
	 * codepoint is a high surrogate, we want that in stead. */
	const char *q = p - 3;

	if ((q >= start) && ((q[0]&0xFF) == 0xED)
		&& ((q[1]&0xF0) == 0xA0) && ((q[2]&0xC0) == 0x80)) {
	    p = q;
	}
    }
    return p;
}

/*
 *---------------------------------------------------------------------------
 *
 * TkUtfNext --
 *
 *	Almost the same as Tcl_UtfNext but check for surrogates if
 *	TCL_UTF_MAX==3. So, the pointer might move up to 6 bytes.
 *
 * Results:
 *	Return a pointer to the next unicode character.
 *
 * Side effects:
 *	None.
 *
 *---------------------------------------------------------------------------
 */

const char *
TkUtfNext(
    const char *source)
{
    const char *p = Tcl_UtfNext(source);

    if ((p == source+3) && ((source[0]&0xFF) == 0xED)
	    && ((source[1]&0xF0) == 0xA0) && ((source[2]&0xC0) == 0x80)) {
	/* We were pointing to a high surrogate. If the next
	 * codepoint is a low surrogate, we want to advance one more. */
	if (((p[0]&0xFF) == 0xED) && ((p[1]&0xF0) == 0xB0)
		&& ((p[2]&0xC0) == 0x80)) {

	    p += 3;
	}
    }
    return p;
}

#endif
/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */

Changes to jni/sdl2tk/generic/ttk/ttkEntry.c.

289
290
291
292
293
294
295
296
297


298
299
300


301
302
303
304
305
306
307
289
290
291
292
293
294
295


296
297
298


299
300
301
302
303
304
305
306
307







-
-
+
+

-
-
+
+







 * 	of (the first character in the string) 'showChar'.
 * 	Used to compute the displayString if -show is non-NULL.
 */
static char *EntryDisplayString(const char *showChar, int numChars)
{
    char *displayString, *p;
    int size;
    Tcl_UniChar ch;
    char buf[TCL_UTF_MAX];
    int ch;
    char buf[6];

    Tcl_UtfToUniChar(showChar, &ch);
    size = Tcl_UniCharToUtf(ch, buf);
    TkUtfToUniChar(showChar, &ch);
    size = TkUniCharToUtf(ch, buf);
    p = displayString = ckalloc(numChars * size + 1);

    while (numChars--) {
	memcpy(p, buf, size);
	p += size;
    }
    *p = '\0';
426
427
428
429
430
431
432
433

434
435
436
437
438
439
440
426
427
428
429
430
431
432

433
434
435
436
437
438
439
440







-
+







     VREASON reason,		/* Reason for change */
     Tcl_DString *dsPtr)	/* Result of %-substitutions */
{
    int spaceNeeded, cvtFlags;
    int number, length;
    const char *string;
    int stringLength;
    Tcl_UniChar ch;
    int ch;
    char numStorage[2*TCL_INTEGER_SPACE];

    while (*templ) {
	/* Find everything up to the next % character and append it
	 * to the result string.
	 */
	string = Tcl_UtfFindFirst(templ, '%');
450
451
452
453
454
455
456
457

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

457
458
459
460
461
462
463
464







-
+







	    templ = string;
	}

	/* There's a percent sequence here.  Process it.
	 */
	++templ; /* skip over % */
	if (*templ != '\0') {
	    templ += Tcl_UtfToUniChar(templ, &ch);
	    templ += TkUtfToUniChar(templ, &ch);
	} else {
	    ch = '%';
	}

	stringLength = -1;
	switch (ch) {
	    case 'd': /* Type of call that caused validation */
500
501
502
503
504
505
506
507

508
509
510
511
512
513
514
500
501
502
503
504
505
506

507
508
509
510
511
512
513
514







-
+







	    case 'V': /* type of validation in effect */
		string = validateReasonStrings[reason];
		break;
	    case 'W': /* widget name */
		string = Tk_PathName(entryPtr->core.tkwin);
		break;
	    default:
		length = Tcl_UniCharToUtf(ch, numStorage);
		length = TkUniCharToUtf(ch, numStorage);
		numStorage[length] = '\0';
		string = numStorage;
		break;
	}

	spaceNeeded = Tcl_ScanCountedElement(string, stringLength, &cvtFlags);
	length = Tcl_DStringLength(dsPtr);
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
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910







+
+
+




+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    char *string = entryPtr->entry.string;
    size_t byteIndex = Tcl_UtfAtIndex(string, index) - string;
    size_t byteCount = strlen(value);
    int charsAdded = Tcl_NumUtfChars(value, byteCount);
    size_t newByteCount = entryPtr->entry.numBytes + byteCount + 1;
    char *newBytes;
    int code;
#if TCL_UTF_MAX < 4
    Tcl_UniChar ch;
#endif

    if (byteCount == 0) {
	return TCL_OK;
    }

#if TCL_UTF_MAX < 4
    /*
     * Don't insert within surrogate pairs.
     */
    if (byteIndex) {
	const char *prevPtr;

	Tcl_UtfToUniChar(string + byteIndex, &ch);
	if ((ch & 0xFC00) == 0xDC00) {
	    prevPtr = Tcl_UtfPrev(string + byteIndex, string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xFC00) == 0xD800) {
		byteIndex = prevPtr - string;
		index -= 1;
	    }
	}
    }
#endif

    newBytes = (char *) ckalloc(newByteCount);
    memcpy(newBytes, string, byteIndex);
    strcpy(newBytes + byteIndex, value);
    strcpy(newBytes + byteIndex + byteCount, string + byteIndex);

    code = EntryValidateChange(
	    entryPtr, newBytes, index, charsAdded, VALIDATE_INSERT);

#if TCL_UTF_MAX < 4
    /*
     * Account for high surrogate at end of inserted string.
     */
    if (byteCount > 1) {
	const char *lastPtr = Tcl_UtfPrev(newBytes+byteIndex+byteCount, newBytes);

	Tcl_UtfToUniChar(lastPtr, &ch);
	if ((ch & 0xFC00) == 0xD800) {
	    /*
	     * A high surrogate. If followed by low surrogate,
	     * adjust index after it.
	     */
	    lastPtr = Tcl_UtfNext(lastPtr);
	    Tcl_UtfToUniChar(lastPtr, &ch);
	    if ((ch & 0xFC00) == 0xDC00) {
		index++;
		charsAdded++;
	    }
	}
    }
#endif

    if (code == TCL_OK) {
	AdjustIndices(entryPtr, index, charsAdded);
	code = EntrySetValue(entryPtr, newBytes);
    } else if (code == TCL_BREAK) {
	code = TCL_OK;
    }
877
878
879
880
881
882
883





884
885
886
887
888
889
890
891
892
893
894
895


























896














897
898
899
900
901
902
903
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
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







+
+
+
+
+












+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+







    int index,			/* Index of first character to delete. */
    int count)			/* How many characters to delete. */
{
    char *string = entryPtr->entry.string;
    size_t byteIndex, byteCount, newByteCount;
    char *newBytes;
    int code;
#if TCL_UTF_MAX < 4
    Tcl_UniChar ch;
    int len;
    const char *prevPtr, *nextPtr;
#endif

    if (index < 0) {
	index = 0;
    }
    if (count + index > entryPtr->entry.numChars) {
	count = entryPtr->entry.numChars - index;
    }
    if (count <= 0) {
	return TCL_OK;
    }

    byteIndex = Tcl_UtfAtIndex(string, index) - string;

#if TCL_UTF_MAX < 4
    /*
     * Delete complete surrogate pairs.
     */
    Tcl_UtfToUniChar(string + byteIndex, &ch);
    if ((ch & 0xFC00) == 0xDC00) {
	prevPtr = Tcl_UtfPrev(string + byteIndex, string);
	Tcl_UtfToUniChar(prevPtr, &ch);
	if ((ch & 0xFC00) == 0xD800) {
	    byteIndex = prevPtr - string;
	    count++;
	    index--;
	}
    } else if ((count == 1) && ((ch & 0xFC00) == 0xD800)) {
	nextPtr = Tcl_UtfNext(string + byteIndex);
	Tcl_UtfToUniChar(nextPtr, &ch);
	if ((ch & 0xFC00) == 0xDC00) {
	    count++;
	}
    }
    if (count > entryPtr->entry.numChars - index) {
	count = entryPtr->entry.numChars - index;
    }
#endif

    byteCount = Tcl_UtfAtIndex(string+byteIndex, count) - (string+byteIndex);

#if TCL_UTF_MAX < 4
    if (byteCount) {
	len = Tcl_UtfToUniChar(string + byteIndex + byteCount, &ch);
	if ((ch & 0xFC00) == 0xDC00) {
	    prevPtr = Tcl_UtfPrev(string + byteIndex + byteCount, string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xFC00) == 0xD800) {
		byteCount += len;
		count++;
	    }
	}
    }
#endif

    newByteCount = entryPtr->entry.numBytes + 1 - byteCount;
    newBytes = (char *) ckalloc(newByteCount);
    memcpy(newBytes, string, byteIndex);
    strcpy(newBytes + byteIndex, string + byteIndex + byteCount);

    code = EntryValidateChange(
1389
1390
1391
1392
1393
1394
1395




1396
1397
1398
1399
1400
1401
1402
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496







+
+
+
+







    Entry *entryPtr,		/* Entry widget to query */
    Tcl_Obj *indexObj,		/* Symbolic index name */
    int *indexPtr)		/* Return value */
{
#   define EntryWidth(e) (Tk_Width(entryPtr->core.tkwin)) /* Not Right */
    const char *string = Tcl_GetString(indexObj);
    size_t length = indexObj->length;
    int roundUp = 0;
#if TCL_UTF_MAX < 4
    int oldPos = entryPtr->entry.insertPos;
#endif

    if (strncmp(string, "end", length) == 0) {
	*indexPtr = entryPtr->entry.numChars;
    } else if (strncmp(string, "insert", length) == 0) {
	*indexPtr = entryPtr->entry.insertPos;
    } else if (strncmp(string, "left", length) == 0) {	/* for debugging */
	*indexPtr = entryPtr->entry.xscroll.first;
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1508
1509
1510
1511
1512
1513
1514

1515
1516
1517
1518
1519
1520
1521







-







	    *indexPtr = entryPtr->entry.selectFirst;
	} else if (strncmp(string, "sel.last", length) == 0) {
	    *indexPtr = entryPtr->entry.selectLast;
	} else {
	    goto badIndex;
	}
    } else if (string[0] == '@') {
	int roundUp = 0;
	int maxWidth = EntryWidth(entryPtr);
	int x;

	if (Tcl_GetInt(interp, string + 1, &x) != TCL_OK) {
	    goto badIndex;
	}
	if (x > maxWidth) {
1452
1453
1454
1455
1456
1457
1458




1459



























1460
1461
1462
1463
1464
1465
1466
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590







+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	    goto badIndex;
	}
	if (*indexPtr < 0) {
	    *indexPtr = 0;
	} else if (*indexPtr > entryPtr->entry.numChars) {
	    *indexPtr = entryPtr->entry.numChars;
	}
#if TCL_UTF_MAX < 4
	roundUp = (indexPtr == &entryPtr->entry.insertPos) && 
		(*indexPtr == (oldPos + 1));
#endif
    }

#if TCL_UTF_MAX < 4
    /*
     * Enforce index on start or end of surrogate pair.
     */
    if (*indexPtr) {
	int byteIndex;
	Tcl_UniChar ch;
	const char *prevPtr;

	string = entryPtr->entry.string;
	byteIndex = Tcl_UtfAtIndex(string, *indexPtr) - string;
	Tcl_UtfToUniChar(string + byteIndex, &ch);
	if ((ch & 0xFC00) == 0xDC00) {
	    if (roundUp) {
		*indexPtr += 1;
	    } else {
		prevPtr = Tcl_UtfPrev(string + byteIndex, string);
		Tcl_UtfToUniChar(prevPtr, &ch);
		if ((ch & 0xFC00) == 0xD800) {
		    *indexPtr -= 1;
		}
	    }
	}
    }
#endif

    return TCL_OK;

badIndex:
    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "bad entry index \"%s\"", string));
    Tcl_SetErrorCode(interp, "TTK", "ENTRY", "INDEX", NULL);
    return TCL_ERROR;

Changes to jni/sdl2tk/library/entry.tcl.

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
81
82
83
84
85
86
87

88
89
90
91
92
93
94







-







bind Entry <B1-Motion> {
    set tk::Priv(x) %x
    tk::EntryMouseSelect %W %x
}
bind Entry <Double-1> {
    set tk::Priv(selectMode) word
    tk::EntryMouseSelect %W %x
    catch {%W icursor sel.last}
}
bind Entry <Triple-1> {
    set tk::Priv(selectMode) line
    tk::EntryMouseSelect %W %x
    catch {%W icursor sel.last}
}
bind Entry <Shift-1> {
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
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







+



+






+








+
+
+
+
+
+
+
+







		}
	    }
	}
	word {
	    if {$cur < $anchor} {
		set before [tcl_wordBreakBefore [$w get] $cur]
		set after [tcl_wordBreakAfter [$w get] $anchor-1]
		set icur before
	    } elseif {$cur > $anchor} {
		set before [tcl_wordBreakBefore [$w get] $anchor]
		set after [tcl_wordBreakAfter [$w get] $cur-1]
		set icur after
	    } else {
		if {[$w index @$Priv(pressX)] < $anchor} {
		      incr anchor -1
		}
		set before [tcl_wordBreakBefore [$w get] $anchor]
		set after [tcl_wordBreakAfter [$w get] $anchor]
		set icur after
	    }
	    if {$before < 0} {
		set before 0
	    }
	    if {$after < 0} {
		set after end
	    }
	    $w selection range $before $after
	    if {$icur eq "after"} {
		$w icursor end
		$w icursor $after
	    } else {
		$w icursor 0
		$w icursor $before
	    }
	    set Priv(mouseMoved) 0
	}
	line {
	    $w selection range 0 end
	}
    }
    if {$Priv(mouseMoved)} {
        $w icursor $cur

Changes to jni/sdl2tk/library/spinbox.tcl.

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







+



+








+
+
+
+
+
+
+
+
+







		}
	    }
	}
	word {
	    if {$cur < [$w index anchor]} {
		set before [tcl_wordBreakBefore [$w get] $cur]
		set after [tcl_wordBreakAfter [$w get] $anchor-1]
		set icur before
	    } else {
		set before [tcl_wordBreakBefore [$w get] $anchor]
		set after [tcl_wordBreakAfter [$w get] $cur-1]
		set icur after
	    }
	    if {$before < 0} {
		set before 0
	    }
	    if {$after < 0} {
		set after end
	    }
	    $w selection range $before $after
	    if {$icur eq "after"} {
		$w icursor end
		$w icursor $after
	    } else {
		$w icursor 0
		$w icursor $before
	    }
	    set Priv(mouseMoved) 0
	    set cursor ignore
	}
	line {
	    $w selection range 0 end
	}
    }
    if {$cursor ne {} && $cursor ne "ignore"} {
	catch {$w icursor $cursor}

Changes to jni/sdl2tk/library/ttk/entry.tcl.

310
311
312
313
314
315
316








317
318
319
320
321
322
323
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331







+
+
+
+
+
+
+
+







    }
    return $pos
}

## RelIndex -- Compute character/word/line-relative index.
#
proc ttk::entry::RelIndex {w where {index insert}} {
    if {$index eq "insert" && $where eq "nextchar"} {
	# Needed for surrogate pairs:
	set icur [$w index insert]
	$w icursor [expr {$icur + 1}]
	set ret [expr {[$w index insert]}]
	$w icursor $icur
	return $ret
    }
    switch -- $where {
	prevchar	{ expr {[$w index $index] - 1} }
    	nextchar	{ expr {[$w index $index] + 1} }
	prevword	{ PrevWord $w $index }
	nextword	{ NextWord $w $index }
	home		{ return 0 }
	end		{ $w index end }

Changes to jni/sdl2tk/macosx/tkMacOSXBitmap.c.

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







+
+

+
+








+
-
+


+


+
-
+

+


+
-
+



+


-
+


+
-
+

+


+
-
+



+






+







    int *height)
{
    Tcl_HashEntry *hPtr;
    Pixmap pixmap = None;
    NSString *string;
    NSImage *image = nil;
    NSSize size = { .width = builtInIconSize, .height = builtInIconSize };
    Tcl_Encoding utf8;
    Tcl_DString ds;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_DStringInit(&ds);
    if (iconBitmapTable.buckets &&
	    (hPtr = Tcl_FindHashEntry(&iconBitmapTable, name))) {
	OSType type;
	IconBitmap *iconBitmap = (IconBitmap *) Tcl_GetHashValue(hPtr);
	name = NULL;
	size = NSMakeSize(iconBitmap->width, iconBitmap->height);
	switch (iconBitmap->kind) {
	case ICON_FILE:
	    name = Tcl_UtfToExternalDString(utf8, iconBitmap->value, -1, &ds);
	    string = [[NSString stringWithUTF8String:iconBitmap->value]
	    string = [[NSString stringWithUTF8String:name]
		    stringByExpandingTildeInPath];
	    image = [[NSWorkspace sharedWorkspace] iconForFile:string];
	    name = NULL;
	    break;
	case ICON_FILETYPE:
	    name = Tcl_UtfToExternalDString(utf8, iconBitmap->value, -1, &ds);
	    string = [NSString stringWithUTF8String:iconBitmap->value];
	    string = [NSString stringWithUTF8String:name];
	    image = [[NSWorkspace sharedWorkspace] iconForFileType:string];
	    name = NULL;
	    break;
	case ICON_OSTYPE:
	    name = Tcl_UtfToExternalDString(utf8, iconBitmap->value, -1, &ds);
	    if (OSTypeFromString(iconBitmap->value, &type) == TCL_OK) {
	    if (OSTypeFromString(name, &type) == TCL_OK) {
		string = NSFileTypeForHFSTypeCode(type);
		image = [[NSWorkspace sharedWorkspace] iconForFileType:string];
	    }
	    name = NULL;
	    break;
	case ICON_SYSTEMTYPE:
	    name = iconBitmap->value;
	    name = Tcl_UtfToExternalDString(utf8, iconBitmap->value, -1, &ds);
	    break;
	case ICON_NAMEDIMAGE:
	    name = Tcl_UtfToExternalDString(utf8, iconBitmap->value, -1, &ds);
	    string = [NSString stringWithUTF8String:iconBitmap->value];
	    string = [NSString stringWithUTF8String:name];
	    image = [NSImage imageNamed:string];
	    name = NULL;
	    break;
	case ICON_IMAGEFILE:
	    name = Tcl_UtfToExternalDString(utf8, iconBitmap->value, -1, &ds);
	    string = [[NSString stringWithUTF8String:iconBitmap->value]
	    string = [[NSString stringWithUTF8String:name]
		    stringByExpandingTildeInPath];
	    image = [[[NSImage alloc] initWithContentsOfFile:string]
		    autorelease];
	    name = NULL;
	    break;
	}
	if (image) {
	    [image setSize:size];
	}
    } else {
	name = Tcl_UtfToExternalDString(utf8, name, -1, &ds);
	string = [NSString stringWithUTF8String:name];
	image = [NSImage imageNamed:string];
	if (!image) {
	    NSURL *url = [NSURL fileURLWithPath:string];
	    if (url) {
		image = [[[NSImage alloc] initWithContentsOfURL:url]
			autorelease];
315
316
317
318
319
320
321


322
323
324
325
326
327
328
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345







+
+







	    NSImage *iconImage = [[NSWorkspace sharedWorkspace]
				     iconForFileType: iconUTI];
	    pixmap = PixmapFromImage(display, iconImage, NSSizeToCGSize(size));
	}
    }
    *width = (int) size.width;
    *height = (int) size.height;
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
    return pixmap;
}

/*
 *----------------------------------------------------------------------
 *
 * TkMacOSXIconBitmapObjCmd --

Changes to jni/sdl2tk/macosx/tkMacOSXCursor.c.

227
228
229
230
231
232
233


234


235
236
237
238
239
240
241
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245







+
+

+
+







    const char *name)
{
    NSString *path = nil;
    NSImage *image = nil;
    NSPoint hotSpot = NSZeroPoint;
    int haveHotSpot = 0, result = TCL_ERROR;
    NSCursor *macCursor = nil;
    Tcl_Encoding utf8;
    Tcl_DString ds;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    name = Tcl_UtfToExternalDString(utf8, name, -1, &ds);
    if (name[0] == '@') {
	/*
	 * System cursor of type @filename
	 */

	macCursorPtr->type = IMAGEPATH;
	path = [NSString stringWithUTF8String:&name[1]];
350
351
352
353
354
355
356


357
358
359
360
361
362
363
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369







+
+







	    hotSpot.y = size.height * 0.5;
	}
	hotSpot.y = -hotSpot.y;
	macCursor = [[NSCursor alloc] initWithImage:image hotSpot:hotSpot];
	[image release];
    }
    macCursorPtr->macCursor = macCursor;
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
}

/*
 *----------------------------------------------------------------------
 *
 * TkGetCursorByName --
 *

Changes to jni/sdl2tk/macosx/tkMacOSXDialog.c.

217
218
219
220
221
222
223


224

225
226
227


228
229



230
231


232



233

234
235
236
237
238
239
240
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







+
+

+



+
+

-
+
+
+


+
+
-
+
+
+

+







		returnCode: (NSModalResponse) returnCode
	       contextInfo: (void *) contextInfo
{
    FilePanelCallbackInfo *callbackInfo = (FilePanelCallbackInfo *) contextInfo;

    if (returnCode == modalOK) {
	Tcl_Obj *resultObj;
	Tcl_Encoding utf8;
	Tcl_DString ds;

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	if (callbackInfo->multiple) {
	    resultObj = Tcl_NewListObj(0, NULL);
	    for (NSURL *url in [(NSOpenPanel*)panel URLs]) {
		Tcl_ExternalToUtfDString(utf8, [[url path] UTF8String], -1,
			&ds);
		Tcl_ListObjAppendElement(callbackInfo->interp, resultObj,
			Tcl_NewStringObj([[url path] UTF8String], -1));
			Tcl_NewStringObj(Tcl_DStringValue(&ds),
			    Tcl_DStringLength(&ds)));
		Tcl_DStringFree(&ds);
	    }
	} else {
	    Tcl_ExternalToUtfDString(utf8,
		    [[[panel URL]path] UTF8String], -1, &ds);
	    resultObj = Tcl_NewStringObj([[[panel URL]path] UTF8String], -1);
	    resultObj = Tcl_NewStringObj(Tcl_DStringValue(&ds),
			    Tcl_DStringLength(&ds));
	    Tcl_DStringFree(&ds);
	}
	Tcl_FreeEncoding(utf8);
	if (callbackInfo->cmdObj) {
	    Tcl_Obj **objv, **tmpv;
	    int objc, result = Tcl_ListObjGetElements(callbackInfo->interp,
		    callbackInfo->cmdObj, &objc, &objv);

	    if (result == TCL_OK && objc) {
		tmpv = (Tcl_Obj **)ckalloc(sizeof(Tcl_Obj *) * (objc + 2));
454
455
456
457
458
459
460





461

462
463
464


465
466
467
468
469
470
471
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







+
+
+
+
+
-
+



+
+







    colorPanel = [NSColorPanel sharedColorPanel];
    [colorPanel orderOut:NSApp];
    [colorPanel setContinuous:NO];
    [colorPanel setBecomesKeyOnlyIfNeeded:NO];
    [colorPanel setShowsAlpha: NO];
    [colorPanel _setUseModalAppearance:YES];
    if (title) {
	NSString *s;
	Tcl_Encoding utf8 = Tcl_GetEncoding(NULL, "utf-8");
	Tcl_DString ds;

	title = Tcl_ExternalToUtfDString(utf8, title, -1, &ds);
	NSString *s = [[NSString alloc] initWithUTF8String:title];
	s = [[NSString alloc] initWithUTF8String:title];

	[colorPanel setTitle:s];
	[s release];
	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(utf8);
    }
    if (initialColor) {
	[colorPanel setColor:initialColor];
    }
    returnCode = [NSApp runModalForWindow:colorPanel];
    if (returnCode == modalOK) {
	color = [[colorPanel color] colorUsingColorSpace:
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
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







+






-



















+
+
+
+
+
+


-
+
+
+
+

+
+


+
-
-
+
+
-






-
+




+
+
+
-
+








+








static int
parseFileFilters(
    Tcl_Interp *interp,
    Tcl_Obj *fileTypesPtr,
    Tcl_Obj *typeVariablePtr)
{
    FileFilterList fl;

    if (!fileTypesPtr) {
	filterInfo.doFileTypes = false;
	return TCL_OK;
    }

    FileFilterList fl;

    TkInitFileFilters(&fl);
    if (TkGetFileFilters(interp, &fl, fileTypesPtr, 0) != TCL_OK) {
	TkFreeFileFilters(&fl);
	return TCL_ERROR;
    }

    filterInfo.doFileTypes = (fl.filters != NULL);

    filterInfo.fileTypeIndex = 0;
    filterInfo.fileTypeExtensions = [NSMutableArray array];
    filterInfo.fileTypeNames = [NSMutableArray array];
    filterInfo.fileTypeLabels = [NSMutableArray array];
    filterInfo.fileTypeAllowsAll = [NSMutableArray array];

    filterInfo.allowedExtensions = [NSMutableArray array];
    filterInfo.allowedExtensionsAllowAll = NO;

    if (filterInfo.doFileTypes) {
	Tcl_Encoding utf8;
	Tcl_DString ds;
	const char *str;

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	Tcl_DStringInit(&ds);
	for (FileFilter *filterPtr = fl.filters; filterPtr;
		filterPtr = filterPtr->next) {
	    NSString *name = [[NSString alloc] initWithUTF8String: filterPtr->name];
	    NSString *name;
	    NSMutableArray *clauseextensions;
	    NSMutableArray *displayextensions;
	    bool allowsAll = NO;

	    str = Tcl_UtfToExternalDString(utf8, filterPtr->name, -1, &ds);
	    name = [[NSString alloc] initWithUTF8String:str];
	    [filterInfo.fileTypeNames addObject:name];
	    [name release];
	    Tcl_DStringFree(&ds);
	    NSMutableArray *clauseextensions = [NSMutableArray array];
	    NSMutableArray *displayextensions = [NSMutableArray array];
	    clauseextensions = [NSMutableArray array];
	    displayextensions = [NSMutableArray array];
	    bool allowsAll = NO;

	    for (FileFilterClause *clausePtr = filterPtr->clauses; clausePtr;
		    clausePtr = clausePtr->next) {

		for (GlobPattern *globPtr = clausePtr->patterns; globPtr;
			globPtr = globPtr->next) {
		    const char *str = globPtr->pattern;
		    str = globPtr->pattern;
		    while (*str && (*str == '*' || *str == '.')) {
		    	str++;
		    }
		    if (*str) {
			NSString *extension;

			str = Tcl_UtfToExternalDString(utf8, str, -1, &ds);
			NSString *extension = [[NSString alloc] initWithUTF8String:str];
			extension = [[NSString alloc] initWithUTF8String:str];
			if (![filterInfo.allowedExtensions containsObject:extension]) {
			    [filterInfo.allowedExtensions addObject:extension];
			}

			[clauseextensions addObject:extension];
			[displayextensions addObject:[@"." stringByAppendingString:extension]];

			[extension release];
			Tcl_DStringFree(&ds);
		    } else {
			/*
			 * It is the all pattern (*, .* or *.*)
			 */

			allowsAll = YES;
			filterInfo.allowedExtensionsAllowAll = YES;
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
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







-
-
+
+
+
+
+
-
-
+
-
-
+








+
+







		    typeVariablePtr, NULL, TCL_GLOBAL_ONLY);

	    /*
	     * Check that the typevariable exists.
	     */

	    if (selectedFileTypeObj != NULL) {
		const char *selectedFileType =
			Tcl_GetString(selectedFileTypeObj);
		NSString *selectedFileTypeStr;
		NSUInteger index;

		str = Tcl_UtfToExternalDString(utf8,
			    Tcl_GetString(selectedFileTypeObj), -1, &ds);
		NSString *selectedFileTypeStr =
			[[NSString alloc] initWithUTF8String:selectedFileType];
		selectedFileTypeStr = [[NSString alloc] initWithUTF8String:str];
		NSUInteger index =
			[filterInfo.fileTypeNames indexOfObject:selectedFileTypeStr];
		index = [filterInfo.fileTypeNames indexOfObject:selectedFileTypeStr];

		if (index != NSNotFound) {
		    filterInfo.fileTypeIndex = index;
		    filterInfo.preselectFilter = true;
		}
	    }
	}

	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(utf8);
    }

    TkFreeFileFilters(&fl);
    return TCL_OK;
}

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







+
+

+
+




















+


+





+


+



+
+
-
-
+
+
















+
+
-
-
+
+







    Tcl_Obj *cmdObj = NULL, *typeVariablePtr = NULL, *fileTypesPtr = NULL;
    NSString *directory = nil, *filename = nil;
    NSString *message = nil, *title = nil;
    NSWindow *parent;
    openpanel =  [NSOpenPanel openPanel];
    NSInteger modalReturnCode = modalError;
    BOOL parentIsKey = NO;
    Tcl_Encoding utf8;
    Tcl_DString ds;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_DStringInit(&ds);
    for (i = 1; i < objc; i += 2) {
	if (Tcl_GetIndexFromObjStruct(interp, objv[i], openOptionStrings,
		sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
	    goto end;
	}
	if (i + 1 == objc) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "value for \"%s\" missing", Tcl_GetString(objv[i])));
	    Tcl_SetErrorCode(interp, "TK", "FILEDIALOG", "VALUE", NULL);
	    goto end;
	}
	switch (index) {
	case OPEN_DEFAULT:
	    break;
	case OPEN_FILETYPES:
	    fileTypesPtr = objv[i + 1];
	    break;
	case OPEN_INITDIR:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    if (len) {
		str = Tcl_UtfToExternalDString(utf8, str, -1, &ds);
		directory = [[[NSString alloc] initWithUTF8String:str]
			autorelease];
		Tcl_DStringFree(&ds);
	    }
	    break;
	case OPEN_INITFILE:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    if (len) {
		str = Tcl_UtfToExternalDString(utf8, str, -1, &ds);
		filename = [[[NSString alloc] initWithUTF8String:str]
			autorelease];
		Tcl_DStringFree(&ds);
	    }
	    break;
	case OPEN_MESSAGE:
	    str = Tcl_UtfToExternalDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    message = [[NSString alloc] initWithUTF8String:
		    Tcl_GetString(objv[i + 1])];
	    message = [[NSString alloc] initWithUTF8String:str];
	    Tcl_DStringFree(&ds);
	    break;
	case OPEN_MULTIPLE:
	    if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
		    &multiple) != TCL_OK) {
		goto end;
	    }
	    break;
	case OPEN_PARENT:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    tkwin = Tk_NameToWindow(interp, str, tkwin);
	    if (!tkwin) {
		goto end;
	    }
	    haveParentOption = 1;
	    break;
	case OPEN_TITLE:
	    str = Tcl_UtfToExternalDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    title = [[NSString alloc] initWithUTF8String:
		    Tcl_GetString(objv[i + 1])];
	    title = [[NSString alloc] initWithUTF8String:str];
	    Tcl_DStringFree(&ds);
	    break;
	case OPEN_TYPEVARIABLE:
	    typeVariablePtr = objv[i + 1];
	    break;
	case OPEN_COMMAND:
	    cmdObj = objv[i+1];
	    break;
899
900
901
902
903
904
905


906
907
908


909
910


911
912
913
914
915
916
917
948
949
950
951
952
953
954
955
956
957


958
959
960
961
962
963
964
965
966
967
968
969
970







+
+

-
-
+
+


+
+







		if (j == selectedFilterIndex) {
		    selectedFilter = [filterInfo.fileTypeNames objectAtIndex:selectedFilterIndex];
		} else {
		    selectedFilter = @"";
		}
	    }
	}
	Tcl_DStringFree(&ds);
	Tcl_ExternalToUtfDString(utf8, [selectedFilter UTF8String], -1, &ds);
	Tcl_ObjSetVar2(interp, typeVariablePtr, NULL,
		Tcl_NewStringObj([selectedFilter UTF8String], -1),
		TCL_GLOBAL_ONLY);
		Tcl_NewStringObj(Tcl_DStringValue(&ds),
			Tcl_DStringLength(&ds)), TCL_GLOBAL_ONLY);
    }
 end:
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_GetSaveFileObjCmd --
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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024








































































1025
1026
1027
1028
1029
1030
1031
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019






























































1020
1021
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
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098







+
+

+
+












-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    Tcl_Obj *cmdObj = NULL, *typeVariablePtr = NULL, *fileTypesPtr = NULL;
    NSString *directory = nil, *filename = nil, *defaultType = nil;
    NSString *message = nil, *title = nil;
    NSWindow *parent;
    savepanel = [NSSavePanel savePanel];
    NSInteger modalReturnCode = modalError;
    BOOL parentIsKey = NO;
    Tcl_Encoding utf8;
    Tcl_DString ds;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_DStringInit(&ds);
    for (i = 1; i < objc; i += 2) {
	if (Tcl_GetIndexFromObjStruct(interp, objv[i], saveOptionStrings,
		sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
	    goto end;
	}
	if (i + 1 == objc) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "value for \"%s\" missing", Tcl_GetString(objv[i])));
	    Tcl_SetErrorCode(interp, "TK", "FILEDIALOG", "VALUE", NULL);
	    goto end;
	}
	switch (index) {
	    case SAVE_DEFAULT:
		str = Tcl_GetStringFromObj(objv[i + 1], &len);
		while (*str && (*str == '*' || *str == '.')) {
		    str++;
		}
		if (*str) {
		    defaultType = [[[NSString alloc] initWithUTF8String:str]
			    autorelease];
		}
		break;
	    case SAVE_FILETYPES:
		fileTypesPtr = objv[i + 1];
		break;
	    case SAVE_INITDIR:
		str = Tcl_GetStringFromObj(objv[i + 1], &len);
		if (len) {
		    directory = [[[NSString alloc] initWithUTF8String:str]
			    autorelease];
		}
		break;
	    case SAVE_INITFILE:
		str = Tcl_GetStringFromObj(objv[i + 1], &len);
		if (len) {
		    filename = [[[NSString alloc] initWithUTF8String:str]
			    autorelease];
		    [savepanel setNameFieldStringValue:filename];
		}
		break;
	    case SAVE_MESSAGE:
		message = [[NSString alloc] initWithUTF8String:
			Tcl_GetString(objv[i + 1])];
		break;
	    case SAVE_PARENT:
		str = Tcl_GetStringFromObj(objv[i + 1], &len);
		tkwin = Tk_NameToWindow(interp, str, tkwin);
		if (!tkwin) {
		    goto end;
		}
		haveParentOption = 1;
		break;
	    case SAVE_TITLE:
		title = [[NSString alloc] initWithUTF8String:
			Tcl_GetString(objv[i + 1])];
		break;
	    case SAVE_TYPEVARIABLE:
		typeVariablePtr = objv[i + 1];
		break;
	    case SAVE_COMMAND:
		cmdObj = objv[i+1];
		break;
	    case SAVE_CONFIRMOW:
		if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
			&confirmOverwrite) != TCL_OK) {
		    goto end;
		}
		break;
	    case SAVE_NATIVEONLY:
		if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
			&dummy) != TCL_OK) {
		    goto end;
		}
		break;
	case SAVE_DEFAULT:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    while (*str && (*str == '*' || *str == '.')) {
		str++;
	    }
	    if (*str) {
		str = Tcl_UtfToExternalDString(utf8, str, -1, &ds);
		defaultType = [[[NSString alloc] initWithUTF8String:str]
			autorelease];
		Tcl_DStringFree(&ds);
	    }
	    break;
	case SAVE_FILETYPES:
	    fileTypesPtr = objv[i + 1];
	    break;
	case SAVE_INITDIR:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    if (len) {
		str = Tcl_UtfToExternalDString(utf8, str, -1, &ds);
		directory = [[[NSString alloc] initWithUTF8String:str]
			autorelease];
		Tcl_DStringFree(&ds);
	    }
	    break;
	case SAVE_INITFILE:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    if (len) {
		str = Tcl_UtfToExternalDString(utf8, str, -1, &ds);
		filename = [[[NSString alloc] initWithUTF8String:str]
			autorelease];
		[savepanel setNameFieldStringValue:filename];
		Tcl_DStringFree(&ds);
	    }
	    break;
	case SAVE_MESSAGE:
	    str = Tcl_UtfToExternalDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    message = [[NSString alloc] initWithUTF8String:str];
	    Tcl_DStringFree(&ds);
	    break;
	case SAVE_PARENT:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    tkwin = Tk_NameToWindow(interp, str, tkwin);
	    if (!tkwin) {
		goto end;
	    }
	    haveParentOption = 1;
	    break;
	case SAVE_TITLE:
	    str = Tcl_UtfToExternalDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    title = [[NSString alloc] initWithUTF8String:str];
	    Tcl_DStringFree(&ds);
	    break;
	case SAVE_TYPEVARIABLE:
	    typeVariablePtr = objv[i + 1];
	    break;
	case SAVE_COMMAND:
	    cmdObj = objv[i+1];
	    break;
	case SAVE_CONFIRMOW:
	    if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
		    &confirmOverwrite) != TCL_OK) {
		goto end;
	    }
	    break;
	case SAVE_NATIVEONLY:
	    if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
		    &dummy) != TCL_OK) {
		goto end;
	    }
	    break;
	}
    }

    if (title) {
	[savepanel setTitle:title];

	/*
1139
1140
1141
1142
1143
1144
1145

1146

1147

1148
1149
1150
1151


1152
1153
1154


1155
1156
1157
1158
1159
1160
1161
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215

1216

1217


1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231







+

+
-
+
-

-
-
+
+



+
+








    if (typeVariablePtr && (modalReturnCode == NSOKButton)
	    && filterInfo.doFileTypes) {
	/*
	 * The -typevariable must be set to the selected file type, if the
	 * dialog was not cancelled.
	 */
	NSString *selectedFilter = [filterInfo.fileTypeNames objectAtIndex:filterInfo.fileTypeIndex];

	Tcl_DStringFree(&ds);
	NSString *selectedFilter =
	Tcl_ExternalToUtfDString(utf8, [selectedFilter UTF8String], -1, &ds);
	    [filterInfo.fileTypeNames objectAtIndex:filterInfo.fileTypeIndex];
	Tcl_ObjSetVar2(interp, typeVariablePtr, NULL,
		Tcl_NewStringObj([selectedFilter UTF8String], -1),
		TCL_GLOBAL_ONLY);
		Tcl_NewStringObj(Tcl_DStringValue(&ds),
			Tcl_DStringLength(&ds)), TCL_GLOBAL_ONLY);
    }

  end:
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tk_ChooseDirectoryObjCmd --
1187
1188
1189
1190
1191
1192
1193


1194


1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209

1210
1211

1212
1213
1214
1215
1216



1217
1218

1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236



1237
1238

1239
1240
1241
1242
1243
1244
1245
1257
1258
1259
1260
1261
1262
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







+
+

+
+















+


+



-
-
+
+
+


+
















-
-
+
+
+


+







    Tcl_Obj *cmdObj = NULL;
    NSString *directory = nil;
    NSString *message, *title;
    NSWindow *parent;
    NSOpenPanel *panel = [NSOpenPanel openPanel];
    NSInteger modalReturnCode = modalError;
    BOOL parentIsKey = NO;
    Tcl_Encoding utf8;
    Tcl_DString ds;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_DStringInit(&ds);
    for (i = 1; i < objc; i += 2) {
	if (Tcl_GetIndexFromObjStruct(interp, objv[i], chooseOptionStrings,
		sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
	    goto end;
	}
	if (i + 1 == objc) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "value for \"%s\" missing", Tcl_GetString(objv[i])));
	    Tcl_SetErrorCode(interp, "TK", "DIRDIALOG", "VALUE", NULL);
	    goto end;
	}
	switch (index) {
	case CHOOSE_INITDIR:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    if (len) {
		str = Tcl_UtfToExternalDString(utf8, str, -1, &ds);
		directory = [[[NSString alloc] initWithUTF8String:str]
			autorelease];
		Tcl_DStringFree(&ds);
	    }
	    break;
	case CHOOSE_MESSAGE:
	    message = [[NSString alloc] initWithUTF8String:
		    Tcl_GetString(objv[i + 1])];
	    str = Tcl_UtfToExternalDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    message = [[NSString alloc] initWithUTF8String:str];
	    [panel setMessage:message];
	    [message release];
	    Tcl_DStringFree(&ds);
	    break;
	case CHOOSE_MUSTEXIST:
	    if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
		    &mustexist) != TCL_OK) {
		goto end;
	    }
	    break;
	case CHOOSE_PARENT:
	    str = Tcl_GetStringFromObj(objv[i + 1], &len);
	    tkwin = Tk_NameToWindow(interp, str, tkwin);
	    if (!tkwin) {
		goto end;
	    }
	    haveParentOption = 1;
	    break;
	case CHOOSE_TITLE:
	    title = [[NSString alloc] initWithUTF8String:
		    Tcl_GetString(objv[i + 1])];
	    str = Tcl_UtfToExternalDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    title = [[NSString alloc] initWithUTF8String:str];
	    [panel setTitle:title];
	    [title release];
	    Tcl_DStringFree(&ds);
	    break;
	case CHOOSE_COMMAND:
	    cmdObj = objv[i+1];
	    break;
	case CHOOSE_NATIVEONLY:
	    if (Tcl_GetBooleanFromObj(interp, objv[i + 1],
		    &dummy) != TCL_OK) {
1281
1282
1283
1284
1285
1286
1287


1288
1289
1290
1291
1292
1293
1294
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376







+
+







	Tcl_DecrRefCount(cmdObj);
    }
    result = (modalReturnCode != modalError) ? TCL_OK : TCL_ERROR;
    if (parentIsKey) {
	[parent makeKeyWindow];
    }
  end:
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * TkAboutDlg --
1428
1429
1430
1431
1432
1433
1434


1435
1436
1437


1438
1439
1440
1441
1442
1443
1444
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530







+
+



+
+







    AlertCallbackInfo callbackInfo;
    NSString *message, *title;
    NSWindow *parent;
    NSArray *buttons;
    NSAlert *alert = [NSAlert new];
    NSInteger modalReturnCode = 1;
    BOOL parentIsKey = NO;
    Tcl_Encoding utf8;
    Tcl_DString ds;

    iconIndex = ICON_INFO;
    typeIndex = TYPE_OK;
    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_DStringInit(&ds);
    for (i = 1; i < objc; i += 2) {
	if (Tcl_GetIndexFromObjStruct(interp, objv[i], alertOptionStrings,
		sizeof(char *), "option", TCL_EXACT, &index) != TCL_OK) {
	    goto end;
	}
	if (i + 1 == objc) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
1453
1454
1455
1456
1457
1458
1459
1460
1461



1462
1463

1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475



1476
1477

1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491



1492
1493

1494
1495
1496
1497
1498
1499
1500
1539
1540
1541
1542
1543
1544
1545


1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561


1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579


1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592







-
-
+
+
+


+










-
-
+
+
+


+












-
-
+
+
+


+







	     * know the '-type' as well.
	     */

	    indexDefaultOption = i;
	    break;

	case ALERT_DETAIL:
	    message = [[NSString alloc] initWithUTF8String:
		    Tcl_GetString(objv[i + 1])];
	    str = Tcl_ExternalToUtfDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    message = [[NSString alloc] initWithUTF8String:str];
	    [alert setInformativeText:message];
	    [message release];
	    Tcl_DStringFree(&ds);
	    break;

	case ALERT_ICON:
	    if (Tcl_GetIndexFromObjStruct(interp, objv[i + 1], alertIconStrings,
		    sizeof(char *), "-icon value", TCL_EXACT, &iconIndex) != TCL_OK) {
		goto end;
	    }
	    break;

	case ALERT_MESSAGE:
	    message = [[NSString alloc] initWithUTF8String:
		    Tcl_GetString(objv[i + 1])];
	    str = Tcl_ExternalToUtfDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    message = [[NSString alloc] initWithUTF8String:str];
	    [alert setMessageText:message];
	    [message release];
	    Tcl_DStringFree(&ds);
	    break;

	case ALERT_PARENT:
	    str = Tcl_GetString(objv[i + 1]);
	    tkwin = Tk_NameToWindow(interp, str, tkwin);
	    if (!tkwin) {
		goto end;
	    }
	    haveParentOption = 1;
	    break;

	case ALERT_TITLE:
	    title = [[NSString alloc] initWithUTF8String:
		    Tcl_GetString(objv[i + 1])];
	    str = Tcl_ExternalToUtfDString(utf8,
			Tcl_GetString(objv[i + 1]), -1, &ds);
	    title = [[NSString alloc] initWithUTF8String:str];
	    [[alert window] setTitle:title];
	    [title release];
	    Tcl_DStringFree(&ds);
	    break;

	case ALERT_TYPE:
	    if (Tcl_GetIndexFromObjStruct(interp, objv[i + 1], alertTypeStrings,
		    sizeof(char *), "-type value", TCL_EXACT, &typeIndex) != TCL_OK) {
		goto end;
	    }

Changes to jni/sdl2tk/macosx/tkMacOSXFont.c.

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







-
+
















-




-






-
+



+
+
+
+
+
+
-
+
+
+




-
-
-
-
-
-
-
-
-
-
-
-







 *	None.
 *
 *---------------------------------------------------------------------------
 */

#if TCL_UTF_MAX == 3

/* No special code for BMP needed. */
/* No special code for WTF-8 needed. */
#define NumUTF16Chars Tcl_NumUtfChars

#else

static int
NumUTF16Chars(
    const char *src,		/* The UTF-8 string to measure. */
    int length)			/* The length of the string in bytes, or -1
				 * for strlen(string). */
{
    Tcl_UniChar ch = 0;
    int i = 0;

    if (length < 0) {
	while (*src != '\0') {
	    src += Tcl_UtfToUniChar(src, &ch);
#if TCL_UTF_MAX > 4
	    if (ch > 0xFFFF) {
		/* A surrogate pair in UTF16Char representation. */
		i++;
	    }
#endif
            i++;
        }
	if (i < 0) {
	    i = INT_MAX;
	}
    } else {
	const char *endPtr = src + length - 4;
	const char *endPtr = src + length - TCL_UTF_MAX;

	while (src < endPtr) {
	    src += Tcl_UtfToUniChar(src, &ch);
	    if (ch > 0xFFFF) {
		/* A surrogate pair in UTF16Char representation. */
		i++;
	    }
	    i++;
	}
#if TCL_UTF_MAX > 4
	endPtr += TCL_UTF_MAX;
	while ((src < endPtr) && Tcl_UtfCharComplete(src, endPtr - src)) {
	    src += Tcl_UtfToUniChar(src, &ch);
	    if (ch > 0xFFFF) {
		/* A surrogate pair in UTF16Char representation. */
		i++;
	    }
#endif
	    i++;
	}
	endPtr += 4;
	while ((src < endPtr) && Tcl_UtfCharComplete(src, endPtr - src)) {
	    src += Tcl_UtfToUniChar(src, &ch);
#if TCL_UTF_MAX > 4
	    if (ch > 0xFFFF) {
		/* A surrogate pair in UTF16Char representation. */
		i++;
	    }
#endif
	    i++;
	}
	if (src < endPtr) {
	    i += endPtr - src;
	}
    }
    return i;
217
218
219
220
221
222
223
224

225
226
227
228
229
230
231
211
212
213
214
215
216
217

218
219
220
221
222
223
224
225







-
+







    while (src < end && index > 0) {
	--index;
	if (Tcl_UtfCharComplete(src, end - src)) {
	    src += Tcl_UtfToUniChar(src, &ch);
	} else {
	    ch = *src++ & 0x00FF;
	}
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
	if (ch > 0xFFFF) {
	    /* A surrogate pair in UTF16Char representation. */
	    --index;
	}
#endif
    }
    return src;
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
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







-
+










-
-

-
+

-
-
+



-
+
-
-
-
-
-
-
-
-
-
-







-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    const char *src,		/* Input UTF-8 string to be converted.
				 * Need not be '\0' terminated. */
    int numBytes,		/* Maximum number of bytes to consider from
				 * source string in all. */
    Tcl_DString *dsPtr,		/* Tcl_DString receiving the result. */
    int *lengthPtr)		/* Number of UTF16Chars in result buffer. */
{
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;
    UTF16Char utf16;
    const char *end;

    Tcl_DStringInit(dsPtr);
    if (numBytes > 0) {
	Tcl_DStringSetLength(dsPtr, numBytes * sizeof(utf16));
	Tcl_DStringSetLength(dsPtr, 0);
    }
    end = src + numBytes;
    while (src < end) {
	int len;

	if (Tcl_UtfCharComplete(src, end - src)) {
	    len = Tcl_UtfToUniChar(src, &ch);
	    src += Tcl_UtfToUniChar(src, &ch);
	} else {
	    len = 1;
	    ch = *src & 0x00FF;
	    ch = *src++ & 0x00FF;
	}

	utf16 = ch;
#if TCL_UTF_MAX < 4
#if TCL_UTF_MAX > 3
	if (ch >= 0xD800 && ch <= 0xDFFF) {
	    utf16 = 0xFFFD;
	}
#elif TCL_UTF_MAX == 4
	if (ch >= 0xD800 && ch <= 0xDFFF) {
	    if ((*src & 0xF8) != 0xF0) {
		utf16 = 0xFFFD;
	    }
	}
#else
	if (ch >= 0xD800 && ch <= 0xDFFF) {
	    utf16 = 0xFFFD;
	} else if (ch > 0xFFFF) {
	    utf16 = (((ch - 0x10000) >> 10) & 0x3FF) | 0xD800;
	    Tcl_DStringAppend(dsPtr, (char *) &utf16, sizeof(utf16));
	    utf16 = ((ch - 0x10000) & 0x3FF) | 0xDC00;
	}
#endif
	Tcl_DStringAppend(dsPtr, (char *) &utf16, sizeof(utf16));
	src += len;
#else
	if (ch >= 0xD800 && ch <= 0xDBFF) {
	    if (!Tcl_UtfCharComplete(src, end - src)) {
		utf16 = 0xFFFD;
	    } else {
		int len = Tcl_UtfToUniChar(src, &ch);

		if (ch >= 0xDC00 && ch <= 0xDFFF) {
		    Tcl_DStringAppend(dsPtr, (char *) &utf16, sizeof(utf16));
		    utf16 = ch;
		    src += len;
		} else {
		    utf16 = 0xFFFD;
		}
	    }
	} else if (ch >= 0xDC00 && ch <= 0xDFFF) {
	    utf16 = 0xFFFD;
	}
#endif
	Tcl_DStringAppend(dsPtr, (char *) &utf16, sizeof(utf16));
    }
    *lengthPtr = Tcl_DStringLength(dsPtr) / sizeof(utf16);
    return (UTF16Char *) Tcl_DStringValue(dsPtr);
}

/*
 *---------------------------------------------------------------------------
325
326
327
328
329
330
331


332


333



334
335
336
337
338
339
340
323
324
325
326
327
328
329
330
331
332
333
334

335
336
337
338
339
340
341
342
343
344







+
+

+
+
-
+
+
+







static void
GetTkFontAttributesForNSFont(
    NSFont *nsFont,
    TkFontAttributes *faPtr)
{
    NSFontTraitMask traits = [[NSFontManager sharedFontManager]
	    traitsOfFont:nsFont];
    Tcl_Encoding utf8;
    Tcl_DString ds;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_ExternalToUtfDString(utf8, [[nsFont familyName] UTF8String], -1, &ds);
    faPtr->family = Tk_GetUid([[nsFont familyName] UTF8String]);
    faPtr->family = Tk_GetUid(Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
    faPtr->size = [nsFont pointSize];
    faPtr->weight = (traits & NSBoldFontMask ? TK_FW_BOLD : TK_FW_NORMAL);
    faPtr->slant = (traits & NSItalicFontMask ? TK_FS_ITALIC : TK_FS_ROMAN);
}

/*
 *---------------------------------------------------------------------------
365
366
367
368
369
370
371





372


373
374
375
376
377
378
379
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390







+
+
+
+
+

+
+







{
    NSFontManager *fm = [NSFontManager sharedFontManager];
    NSFont *nsFont, *dflt = nil;
    #define defaultFont (dflt ? dflt : (dflt = [NSFont systemFontOfSize:0]))
    NSString *family;

    if (familyName) {
	Tcl_Encoding utf8;
	Tcl_DString ds;

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	familyName = Tcl_UtfToExternalDString(utf8, familyName, -1, &ds);
	family = [[[NSString alloc] initWithUTF8String:familyName] autorelease];
	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(utf8);
    } else {
	family = [defaultFont familyName];
    }
    if (size == 0.0) {
	size = [defaultFont pointSize];
    }
    nsFont = [fm fontWithFamily:family traits:traits weight:weight size:size];
822
823
824
825
826
827
828


829

830

831
832



833

834
835
836
837
838
839
840
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







+
+

+

+

-
+
+
+

+







void
TkpGetFontFamilies(
    Tcl_Interp *interp,		/* Interp to hold result. */
    Tk_Window tkwin)		/* For display to query. */
{
    Tcl_Obj *resultPtr = Tcl_NewListObj(0, NULL);
    NSArray *list = [[NSFontManager sharedFontManager] availableFontFamilies];
    Tcl_Encoding utf8;
    Tcl_DString ds;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    for (NSString *family in list) {
	Tcl_ExternalToUtfDString(utf8, [family UTF8String], -1, &ds);
	Tcl_ListObjAppendElement(NULL, resultPtr,
		Tcl_NewStringObj([family UTF8String], -1));
		Tcl_NewStringObj(Tcl_DStringValue(&ds),
			Tcl_DStringLength(&ds)));
	Tcl_DStringFree(&ds);
    }
    Tcl_FreeEncoding(utf8);
    Tcl_SetObjResult(interp, resultPtr);
}

/*
 *-------------------------------------------------------------------------
 *
 * TkpGetSubFonts --
859
860
861
862
863
864
865


866

867
868
869
870

871
872



873
874

875
876
877
878
879
880
881
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893

894
895
896
897
898
899
900
901
902
903
904
905
906







+
+

+




+

-
+
+
+


+







{
    MacFont *fontPtr = (MacFont *) tkfont;
    Tcl_Obj *resultPtr = Tcl_NewListObj(0, NULL);

    if (fontPtr->nsFont) {
	NSArray *list = [[fontPtr->nsFont fontDescriptor]
		objectForKey:NSFontCascadeListAttribute];
	Tcl_Encoding utf8;
	Tcl_DString ds;

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	for (NSFontDescriptor *subFontDesc in list) {
	    NSString *family = [subFontDesc objectForKey:NSFontFamilyAttribute];

	    if (family) {
		Tcl_ExternalToUtfDString(utf8, [family UTF8String], -1, &ds);
		Tcl_ListObjAppendElement(NULL, resultPtr,
			Tcl_NewStringObj([family UTF8String], -1));
			Tcl_NewStringObj(Tcl_DStringValue(&ds),
				Tcl_DStringLength(&ds)));
		Tcl_DStringFree(&ds);
	    }
	}
	Tcl_FreeEncoding(utf8);
    }
    Tcl_SetObjResult(interp, resultPtr);
}

/*
 *----------------------------------------------------------------------
 *
1481
1482
1483
1484
1485
1486
1487


1488


1489


1490
1491
1492
1493
1494
1495
1496
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527







+
+

+
+

+
+







    if (nsFont && familyName) {
	NSFontTraitMask traits = [[NSFontManager sharedFontManager]
		traitsOfFont:nsFont];
	id underline = [nsAttributes objectForKey:
		NSUnderlineStyleAttributeName];
	id strikethrough = [nsAttributes objectForKey:
		NSStrikethroughStyleAttributeName];
	Tcl_Encoding utf8;
	Tcl_DString ds;

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	familyName = Tcl_ExternalToUtfDString(utf8, familyName, -1, &ds);
	objv[i++] = Tcl_NewStringObj(familyName, -1);
	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(utf8);
	objv[i++] = Tcl_NewWideIntObj((Tcl_WideInt) floor([nsFont pointSize] + 0.5));
#define S(s)    Tcl_NewStringObj(STRINGIFY(s),(int)(sizeof(STRINGIFY(s))-1))
	objv[i++] = (traits & NSBoldFontMask)	? S(bold)   : S(normal);
	objv[i++] = (traits & NSItalicFontMask)	? S(italic) : S(roman);
	if ([underline respondsToSelector:@selector(intValue)] &&
		([underline intValue] & (NSUnderlineStyleSingle |
		NSUnderlineStyleThick | NSUnderlineStyleDouble))) {

Changes to jni/sdl2tk/macosx/tkMacOSXInit.c.

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







+
+
+
+
+

+
-
-
+
+

+








+
-
+


-
+





+











+







    }
}

- (NSString *) tkFrameworkImagePath: (NSString *) image
{
    NSString *path = nil;
    NSAutoreleasePool *pool = [NSAutoreleasePool new];
    Tcl_Encoding utf8;
    Tcl_DString ds;
    char *str;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    if (tkLibPath[0] != '\0') {
	str = Tcl_UtfToExternalDString(utf8, tkLibPath, -1, &ds);
	path = [[NSBundle bundleWithPath:[[NSString stringWithUTF8String:
		tkLibPath] stringByAppendingString:@"/../.."]]
	path = [[NSBundle bundleWithPath:[[NSString stringWithUTF8String:str]
		stringByAppendingString:@"/../.."]]
		pathForImageResource:image];
	Tcl_DStringFree(&ds);
    }
    if (!path) {
	const char *tk_library = Tcl_GetVar2(_eventInterp, "tk_library", NULL,
		TCL_GLOBAL_ONLY);

	if (tk_library) {
	    NSFileManager *fm = [NSFileManager defaultManager];

	    str = Tcl_UtfToExternalDString(utf8, tk_library, -1, &ds);
	    path = [[NSString stringWithUTF8String:tk_library]
	    path = [[NSString stringWithUTF8String:str]
		    stringByAppendingFormat:@"/%@", image];
	    if (![fm isReadableFileAtPath:path]) {
		path = [[NSString stringWithUTF8String:tk_library]
		path = [[NSString stringWithUTF8String:str]
			stringByAppendingFormat:@"/../macosx/%@", image];
		if (![fm isReadableFileAtPath:path]) {
		    path = nil;
		}
	    }
	    Tcl_DStringFree(&ds);
	}
    }
#ifdef TK_MAC_DEBUG
    if (!path && getenv("TK_SRCROOT")) {
	path = [[NSString stringWithUTF8String:getenv("TK_SRCROOT")]
		stringByAppendingFormat:@"/macosx/%@", image];
	if (![[NSFileManager defaultManager] isReadableFileAtPath:path]) {
	    path = nil;
	}
    }
#endif
    Tcl_FreeEncoding(utf8);
    [path retain];
    [pool drain];
    return path;
}
@end

#pragma mark -
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
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







+
+

+

+
-
+
+






-
-
+
+

-
-
-
+
+
+
+
+
+
+
+
+
+
+
+











MODULE_SCOPE Tcl_Obj*
TkMacOSXGetStringObjFromCFString(
    CFStringRef str)
{
    Tcl_Obj *obj = NULL;
    const char *c = CFStringGetCStringPtr(str, kCFStringEncodingUTF8);
    Tcl_Encoding utf8;
    Tcl_DString ds, buffer;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    if (c) {
	c = Tcl_ExternalToUtfDString(utf8, c, -1, &ds);
	obj = Tcl_NewStringObj(c, -1);
	obj = Tcl_NewStringObj(Tcl_DStringValue(&ds),
		    Tcl_DStringLength(&ds));
    } else {
	CFRange all = CFRangeMake(0, CFStringGetLength(str));
	CFIndex len;

	if (CFStringGetBytes(str, all, kCFStringEncodingUTF8, 0, false, NULL,
		0, &len) > 0 && len < INT_MAX) {
	    obj = Tcl_NewObj();
	    Tcl_SetObjLength(obj, len);
	    Tcl_DStringInit(&buffer);
	    Tcl_DStringSetLength(&buffer, len);
	    CFStringGetBytes(str, all, kCFStringEncodingUTF8, 0, false,
		    (UInt8*) obj->bytes, len, NULL);
	}
    }
		    (UInt8*) Tcl_DStringValue(&ds), len, NULL);
	    c = Tcl_ExternalToUtfDString(utf8, Tcl_DStringValue(&buffer),
		    Tcl_DStringLength(&buffer), &ds);
	    Tcl_DStringFree(&buffer);
	    if (Tcl_DStringLength(&ds) < INT_MAX) {
		obj = Tcl_NewStringObj(Tcl_DStringValue(&ds),
			    Tcl_DStringLength(&ds));
	    }
	}
    }
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
    return obj;
}

/*
 * Local Variables:
 * mode: objc
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */

Changes to jni/sdl2tk/macosx/tkMacOSXKeyEvent.c.

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







-
+
-
-
-
-





-



-
-
+
+

-
-
-
-
-
-







	Tk_Window focusWin = (Tk_Window) winPtr->dispPtr->focusPtr;
	TkSendVirtualEvent(focusWin, "TkAccentBackspace", NULL);
    }

    /*
     * NSString represents a non-BMP character as a string of length 2 where
     * the first character is the high surrogate and the second character is
     * the low surrogate.  We could record this in the XEvent by setting the
     * the low surrogate.
     * keycode to the unicode code point and setting the trans_chars to the
     * 4-byte UTF-8 string.  However, that will not help as long as TCL_UTF_MAX
     * is set to 3.  Until that changes, we just replace non-BMP characters by
     * the "replacement character" U+FFFD.
     */

    for (i = 0; i < len; i++) {
	UniChar nextChar = [str characterAtIndex: i];
	if (CFStringIsSurrogateHighCharacter(nextChar)) {
#if TCL_UTF_MAX > 3
	    UniChar lowChar = [str characterAtIndex: ++i];
	    xEvent.xkey.keycode = CFStringGetLongCharacterForSurrogatePair(
		nextChar, lowChar);
	    xEvent.xkey.nbytes = Tcl_UniCharToUtf(xEvent.xkey.keycode,
						  &xEvent.xkey.trans_chars);
	    xEvent.xkey.nbytes = TkUniCharToUtf(xEvent.xkey.keycode,
						&xEvent.xkey.trans_chars);
	    xEvent.xkey.trans_chars[xEvent.xkey.nbytes] = '\0';
#else
	    i++;
	    xEvent.xkey.keycode = 0xfffd;
	    strcpy(xEvent.xkey.trans_chars, "\xef\xbf\xbd");
	    xEvent.xkey.nbytes = strlen(xEvent.xkey.trans_chars);
#endif
	} else {
	    xEvent.xkey.keycode = (int) nextChar;
	    [[str substringWithRange: NSMakeRange(i,1)]
	        getCString: xEvent.xkey.trans_chars
		maxLength: XMaxTransChars-1 encoding: NSUTF8StringEncoding];
	    xEvent.xkey.nbytes = strlen(xEvent.xkey.trans_chars);
	}

Changes to jni/sdl2tk/macosx/tkMacOSXMenu.c.

147
148
149
150
151
152
153
154
155




156





157
158
159
160
161
162
163
147
148
149
150
151
152
153


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170







-
-
+
+
+
+

+
+
+
+
+







	[self setDelegate:self];
    }
    return self;
}

- (id) initWithTkMenu: (TkMenu *) tkMenu
{
    NSString *title = [[NSString alloc] initWithUTF8String:
	    Tk_PathName(tkMenu->tkwin)];
    NSString *title;
    Tcl_Encoding utf8;
    Tcl_DString ds;
    char *name;

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    name = Tcl_UtfToExternalDString(utf8, Tk_PathName(tkMenu->tkwin), -1, &ds);
    title = [[NSString alloc] initWithUTF8String:name];
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
    self = [self initWithTitle:title];
    [title release];
    if (self) {
	_tkMenu = tkMenu;
    }
    return self;
}
626
627
628
629
630
631
632






633
634


635
636
637
638
639


640
641
642
643
644
645
646
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







+
+
+
+
+
+
-
-
+
+





+
+







	    /* Use a semantic foreground color by default. */
	    [image setTemplate:YES];
	}
    }
    [menuItem setImage:image];
    if ((!image || mePtr->compound != COMPOUND_NONE) && mePtr->labelPtr &&
	    mePtr->labelLength) {
	Tcl_Encoding utf8;
	Tcl_DString ds;

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	Tcl_UtfToExternalDString(utf8, Tcl_GetString(mePtr->labelPtr),
		mePtr->labelLength, &ds);
	title = [[[NSString alloc] initWithBytes:Tcl_GetString(mePtr->labelPtr)
		length:mePtr->labelLength encoding:NSUTF8StringEncoding]
	title = [[[NSString alloc] initWithBytes:Tcl_DStringValue(&ds)
		length:Tcl_DStringLength(&ds) encoding:NSUTF8StringEncoding]
		autorelease];
	if ([title hasSuffix:@"..."]) {
	    title = [NSString stringWithFormat:@"%@%C",
		    [title substringToIndex:[title length] - 3], 0x2026];
	}
	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(utf8);
    }
    [menuItem setTitle:title];
    if (strcmp(Tcl_GetString(fontPtr), "menu") || gc->foreground != defaultFg
	    || gc->background != defaultBg) {
	attributes = TkMacOSXNSFontAttributesForFont(Tk_GetFontFromObj(
		mePtr->menuPtr->tkwin, fontPtr));
	if (gc->foreground != defaultFg || gc->background != defaultBg) {
1178
1179
1180
1181
1182
1183
1184






1185
1186





1187
1188
1189
1190
1191
1192
1193
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205


1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217







+
+
+
+
+
+
-
-
+
+
+
+
+







	    }
	    i++;
	}
    }
    if (ch) {
	return [[[NSString alloc] initWithCharacters:&ch length:1] autorelease];
    } else {
	NSString *result;
	Tcl_Encoding utf8;
	Tcl_DString ds;

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	accel = Tcl_UtfToExternalDString(utf8, accel, -1, &ds);
	return [[[[NSString alloc] initWithUTF8String:accel] autorelease]
		lowercaseString];
	result = [[[[NSString alloc] initWithUTF8String:accel] autorelease]
		    lowercaseString];
	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(utf8);
	return result;
    }
}

/*
 *--------------------------------------------------------------
 *
 * ModifierCharWidth --

Changes to jni/sdl2tk/macosx/tkMacOSXSysTray.c.

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







+
+


















+
+
-
+
+
+







	 */

	int width, height;
	Tk_Window tkwin = Tk_MainWindow(interp);
	TkWindow *winPtr = (TkWindow *)tkwin;
	Display *d = winPtr->display;
	NSImage *icon;
	Tcl_Encoding utf8;
	Tcl_DString ds;

	tk_image = Tk_GetImage(interp, tkwin, Tcl_GetString(objv[2]), NULL, NULL);
	if (tk_image == NULL) {
	    return TCL_ERROR;
	}

	Tk_SizeOfImage(tk_image, &width, &height);
	if (width != 0 && height != 0) {
	    icon = TkMacOSXGetNSImageWithTkImage(d, tk_image,
						 width, height);
	    [statusItem setImagewithImage: icon];
	    Tk_FreeImage(tk_image);
	}

	/*
	 * Set the text for the tooltip.
	 */

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	Tcl_UtfToExternalDString(utf8, Tcl_GetString(objv[3]), -1, &ds);
	NSString *tooltip = [NSString stringWithUTF8String: Tcl_GetString(objv[3])];
	NSString *tooltip = [NSString stringWithUTF8String: Tcl_DStringValue(&ds)];
	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(utf8);
	if (tooltip == nil) {
	    Tcl_AppendResult(interp, " unable to set tooltip for systray icon", NULL);
	    return TCL_ERROR;
	}

	[statusItem setTextwithString: tooltip];

638
639
640
641
642
643
644





645



646
647
648
649
650
651
652
644
645
646
647
648
649
650
651
652
653
654
655

656
657
658
659
660
661
662
663
664
665







+
+
+
+
+
-
+
+
+







	}

        /*
         * Modify the text for the tooltip.
         */

	case TRAY_TEXT: {
	    Tcl_Encoding utf8;
	    Tcl_DString ds;

	    utf8 = Tcl_GetEncoding(NULL, "utf-8");
	    Tcl_UtfToExternalDString(utf8, Tcl_GetString(objv[3]), -1, &ds);
	    NSString *tooltip = [NSString stringWithUTF8String:Tcl_GetString(objv[3])];
	    NSString *tooltip = [NSString stringWithUTF8String: Tcl_DStringValue(&ds)];
	    Tcl_DStringFree(&ds);
	    Tcl_FreeEncoding(utf8);
	    if (tooltip == nil) {
		Tcl_AppendResult(interp, "unable to set tooltip for systray icon",
				 NULL);
		return TCL_ERROR;
	    }

	    [statusItem setTextwithString: tooltip];
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
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







+
+
+












+
+
-
-
+
+
+
+
+
+







static int
SysNotifyObjCmd(
    ClientData clientData,
    Tcl_Interp * interp,
    int objc,
    Tcl_Obj *const *objv)
{
    Tcl_Encoding utf8;
    Tcl_DString ds;

    if (objc < 3) {
	Tcl_WrongNumArgs(interp, 1, objv, "title message");
	return TCL_ERROR;
    }

    if ([NSApp macOSVersion] < 101000) {
	Tcl_AppendResult(interp,
	    "Notifications not supported on macOS versions lower than 10.10",
	     NULL);
	return TCL_OK;
    }

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_UtfToExternalDString(utf8, Tcl_GetString(objv[1]), -1, &ds);
    NSString *title = [NSString stringWithUTF8String: Tcl_GetString(objv[1])];
    NSString *message = [NSString stringWithUTF8String: Tcl_GetString(objv[2])];
    NSString *title = [NSString stringWithUTF8String: Tcl_DStringValue(&ds)];
    Tcl_DStringFree(&ds);
    Tcl_UtfToExternalDString(utf8, Tcl_GetString(objv[2]), -1, &ds);
    NSString *message = [NSString stringWithUTF8String: Tcl_DStringValue(&ds)];
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);

    /*
     * Update the authorization status in case the user enabled or disabled
     * notifications after the app started up.
     */

#if BUILD_TARGET_HAS_UN_FRAMEWORK

Changes to jni/sdl2tk/macosx/tkMacOSXWm.c.

1419
1420
1421
1422
1423
1424
1425





1426


1427
1428
1429
1430
1431
1432
1433
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440







+
+
+
+
+

+
+







	}
	break;
    case WMATT_TITLEPATH: {
	const char *path = (const char *) Tcl_FSGetNativePath(value);
	NSString *filename = @"";

	if (path && *path) {
	    Tcl_Encoding utf8;
	    Tcl_DString ds;

	    utf8 = Tcl_GetEncoding(NULL, "utf-8");
	    path = Tcl_UtfToExternalDString(utf8, path, -1, &ds);
	    filename = [NSString stringWithUTF8String:path];
	    Tcl_DStringFree(&ds);
	    Tcl_FreeEncoding(utf8);
	}
	[macWindow setRepresentedFilename:filename];
	break;
    }
    case WMATT_TOPMOST:
	if (Tcl_GetBooleanFromObj(interp, value, &boolean) != TCL_OK) {
	    return TCL_ERROR;
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515











1516

1517
1518
1519
1520
1521
1522
1523
1513
1514
1515
1516
1517
1518
1519



1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539







-
-
-
+
+
+
+
+
+
+
+
+
+
+

+







	break;
    case WMATT_MODIFIED:
	result = Tcl_NewBooleanObj([macWindow isDocumentEdited]);
	break;
    case WMATT_NOTIFY:
	result = Tcl_NewBooleanObj(tkMacOSXWmAttrNotifyVal);
	break;
    case WMATT_TITLEPATH:
	result = Tcl_NewStringObj([[macWindow representedFilename] UTF8String],
		-1);
    case WMATT_TITLEPATH: {
	Tcl_Encoding utf8;
	Tcl_DString ds;

	utf8 = Tcl_GetEncoding(NULL, "utf-8");
	Tcl_ExternalToUtfDString(utf8,
		[[macWindow representedFilename] UTF8String], -1, &ds);
	result = Tcl_NewStringObj(Tcl_DStringValue(&ds),
			Tcl_DStringLength(&ds));
	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(utf8);
	break;
    }
    case WMATT_TOPMOST:
	result = Tcl_NewBooleanObj(wmPtr->flags & WM_TOPMOST);
	break;
    case WMATT_TRANSPARENT:
	result = Tcl_NewBooleanObj(wmPtr->flags & WM_TRANSPARENT);
	break;
    case WMATT_TYPE:
5440
5441
5442
5443
5444
5445
5446




5447
5448
5449
5450


5451

5452
5453


5454
5455
5456
5457
5458
5459
5460
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472

5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484







+
+
+
+




+
+
-
+


+
+







 */

void
TkSetWMName(
    TkWindow *winPtr,
    Tk_Uid titleUid)
{
    Tcl_Encoding utf8;
    Tcl_DString ds;
    char *tStr;

    if (Tk_IsEmbedded(winPtr)) {
	return;
    }

    utf8 = Tcl_GetEncoding(NULL, "utf-8");
    tStr = Tcl_UtfToExternalDString(utf8, (const char *) titleUid, -1, &ds); 
    NSString *title = [[NSString alloc] initWithUTF8String:titleUid];
    NSString *title = [[NSString alloc] initWithUTF8String:tStr];
    [TkMacOSXDrawableWindow(winPtr->window) setTitle:title];
    [title release];
    Tcl_DStringFree(&ds);
    Tcl_FreeEncoding(utf8);
}

/*
 *----------------------------------------------------------------------
 *
 * TkGetTransientMaster --
 *

Changes to jni/sdl2tk/sdl/SdlTkInt.c.

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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915

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




930

931
932
933
934
935
936
937
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
884
885
886
887
888
889
890
891

892
893
894
895
896
897
898
899







-
+

+
+
+

+
+
+
+
+
+
+
+
+
+
+

-
-
+








-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-











-
+














+
+
+
+
-
+







    return 0;
}

static int
ProcessTextInput(XEvent *event, int no_rel, int sdl_mod,
		 const char *text, int len)
{
    int i, n, n2, ulen = Tcl_NumUtfChars(text, len);
    int ret = 0, i, n, ulen;
    char buf[TCL_UTF_MAX];
#if TCL_UTF_MAX < 4
    Tcl_DString ubuf;
    Tcl_Encoding encoding = Tcl_GetEncoding(NULL, "utf-8");

    /*
     * This should make UTF-8 into WTF-8.
     */
    Tcl_DStringInit(&ubuf);
    Tcl_ExternalToUtfDString(encoding, text, len, &ubuf);
    if (encoding) {
	Tcl_FreeEncoding(encoding);
    }
    text = Tcl_DStringValue(&ubuf);
#endif
    ulen = Tcl_NumUtfChars(text, len);
    if (ulen <= 0) {
	SdlTkX.keyuc = 0;
	return 0;
	goto done;
    }
    if (sdl_mod & KMOD_RALT) {
	event->xkey.state &= ~Mod4Mask;
    }
    for (i = 0; i < ulen; i++) {
	Tcl_UniChar ch;

	n = Tcl_UtfToUniChar(text, &ch);
	n2 = 0;

	/* Deal with surrogate pairs */
#if TCL_UTF_MAX > 4
	if ((ch >= 0xd800) && (ch <= 0xdbff)) {
	    Tcl_UniChar ch2;

	    if (i + 1 < ulen) {
		n2 = Tcl_UtfToUniChar(text + n, &ch2);
		if ((ch2 >= 0xdc00) && (ch2 <= 0xdfff)) {
		    ch = ((ch & 0x3ff) << 10) | (ch2 & 0x3ff);
		    ch += 0x10000;
		    ++i;
		} else {
		    ch = 0xfffd;
		    n2 = 0;
		}
	    } else {
		SdlTkX.keyuc = ch;
		return -1;
	    }
	} else if ((ch >= 0xdc00) && (ch <= 0xdfff)) {
	    if (SdlTkX.keyuc) {
		ch = ((SdlTkX.keyuc & 0x3ff) << 10) | (ch & 0x3ff);
		ch += 0x10000;
	    } else {
		ch = 0xfffd;
	    }
	    SdlTkX.keyuc = 0;
	} else if ((ch == 0xfffe) || (ch == 0xffff)) {
	    ch = 0xfffd;
	    SdlTkX.keyuc = 0;
	} else {
	    SdlTkX.keyuc = 0;
	}
#else
	if ((ch >= 0xd800) && (ch <= 0xdbff)) {
	    Tcl_UniChar ch2;

	    if (i + 1 < ulen) {
		n2 = Tcl_UtfToUniChar(text + n, &ch2);
		if ((ch2 >= 0xdc00) && (ch2 <= 0xdfff)) {
		    ++i;
		} else {
		    n2 = 0;
		}
	    }
	    ch = 0xfffd;
	} else if ((ch >= 0xdc00) && (ch <= 0xdfff)) {
	    ch = 0xfffd;
	} else if ((ch == 0xfffe) || (ch == 0xffff)) {
	    ch = 0xfffd;
	}
	SdlTkX.keyuc = 0;
#endif
	event->xkey.nbytes = Tcl_UniCharToUtf(ch, buf);
	event->xkey.time = SdlTkX.time_count;
	if (event->xkey.nbytes > sizeof (event->xkey.trans_chars)) {
	    event->xkey.nbytes = sizeof (event->xkey.trans_chars);
	}
	memcpy(event->xkey.trans_chars, buf, event->xkey.nbytes);
	if (len == 1) {
	    event->xkey.keycode = FixKeyCode(event->xkey.trans_chars[0]);
	} else {
	    event->xkey.keycode = -1;
	}
	text += n + n2;
	text += n;

	/* Queue the KeyPress */
	EVLOG("   KEYPRESS:  CODE=0x%02X  UC=0x%X", event->xkey.keycode, ch);
	event->type = KeyPress;
	if (!no_rel || (i < ulen - 1)) {
	    SdlTkQueueEvent(event);
	    /* Queue the KeyRelease except for the last */
	    event->type = KeyRelease;
	    if (i < ulen - 1) {
		EVLOG(" KEYRELEASE:  CODE=0x%02X", event->xkey.keycode);
		SdlTkQueueEvent(event);
	    }
	}
    }
done:
#if TCL_UTF_MAX < 4
    Tcl_DStringFree(&ubuf);
#endif
    return 1;
    return ret;
}

/*
 *----------------------------------------------------------------------
 *
 * SdlTkTranslateEvent --
 *

Changes to jni/sdl2tk/sdl/SdlTkInt.h.

203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
203
204
205
206
207
208
209

210
211
212
213
214
215
216







-







    int nearby_pixels;
    TkWindow *capture_window;
    _Window *mouse_window;
    _Window *keyboard_window;
    int mouse_x, mouse_y;
    int raw_mouse_x, raw_mouse_y;
    int sdlfocus;
    int keyuc;
    int cursor_change;
#if !defined(ANDROID) || defined(__TERMUX__)
    Tcl_HashTable sdlcursors;
#endif

    /* Screen refresh, life-cycle, etc. */
    Region screen_dirty_region;

Changes to jni/sdl2tk/sdl/SdlTkUtils.c.

653
654
655
656
657
658
659

660
661
662
663
664
665
666














667
668
669
670
671
672
673
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







+






-
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    wSrcStart = (unsigned int *) src;
    wSrcEnd = (unsigned int *) (src + srcLen);

    dstStart = dst;
    dstEnd = dst + dstLen - TCL_UTF_MAX;

    for (numChars = 0; wSrc < wSrcEnd; numChars++) {
	int uch;
        Tcl_UniChar ch;

	if (dst > dstEnd) {
	    result = TCL_CONVERT_NOSPACE;
	    break;
	}
	ch = *wSrc++;
	uch = *wSrc++;
	ch = uch;
#if TCL_UTF_MAX < 4
	if (uch > 0xFFFF && uch < 0x10FFFF) {
	    uch -= 0x10000;
	    ch = (uch & 0x3FF) | 0xDC00;
	    dst += Tcl_UniCharToUtf((Tcl_UniChar)((uch >> 10) | 0xD800), dst);
	    if (dst >= dstEnd) {
		result = TCL_CONVERT_NOSPACE;
		break;
	    }
	    numChars++;
	}
#endif
	dst += Tcl_UniCharToUtf(ch, dst);
    }

    *srcReadPtr = (char *) wSrc - (char *) wSrcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
    return result;
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
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







+
-
+














-
+
+
+
+
+
+
+
+
+
+
+

-
-
-
-
+
+
+
+


-
+








    wDst = (unsigned int *) dst;
    wDstStart = (unsigned int *) dst;
    wDstEnd = (unsigned int *) (dst + dstLen - sizeof (unsigned int));

    result = TCL_OK;
    for (numChars = 0; src < srcEnd; numChars++) {
	int ch, len;
	Tcl_UniChar ucs2;
	Tcl_UniChar ucs, ucs2;

	if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) {
	    /*
	     * If there is more string to follow, this will ensure that the
	     * last UTF-8 character in the source buffer hasn't been cut off.
	     */

	    result = TCL_CONVERT_MULTIBYTE;
	    break;
	}
	if (wDst > wDstEnd) {
	    result = TCL_CONVERT_NOSPACE;
	    break;
        }
	src += Tcl_UtfToUniChar(src, &ucs2);
	src += Tcl_UtfToUniChar(src, &ucs);
	ch = ucs;
	if ((ucs & 0xFFFFFC00) == 0xD800) {
	    if (Tcl_UtfCharComplete(src, srcEnd - src)) {
		len = Tcl_UtfToUniChar(src, &ucs2);
		if ((ucs2 & 0xFFFFFC00) == 0xDC00) {
		    src += len;
		    ch = (((ucs&0x3FF)<<10) | (ucs2&0x3FF)) + 0x10000;
		}
	    }
	}
#ifdef USE_SYMBOLA_CTRL
	if (((int) ucs2 >= 0x00) && ((int) ucs2 < 0x20)) {
	    ucs2 += 0x2400;
	} else if (ucs2 == 0x7F) {
	    ucs2 = 0x2421;
	if ((ch >= 0x00) && (ch < 0x20)) {
	    ch += 0x2400;
	} else if (ch == 0x7F) {
	    ch = 0x2421;
	}
#endif
	*wDst++ = ucs2;
	*wDst++ = ch;
    }
    *srcReadPtr = src - srcStart;
    *dstWrotePtr = (char *) wDst - (char *) wDstStart;
    *dstCharsPtr = numChars;
    return result;
}

Changes to jni/sdl2tk/sdl/tkSDLFont.c.

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
29
30
31
32
33
34
35

36
37




38
39
40
41
42
43
44







-


-
-
-
-







 * character existence metrics, used to determine if a screen font can display
 * a given Unicode character.
 *
 * Under Unix, there are three attributes that uniquely identify a "font
 * family": the foundry, face name, and charset.
 */

#if TCL_UTF_MAX > 3
#define FONTMAP_SHIFT		12
#define FONTMAP_PAGES		(1 << (21 - FONTMAP_SHIFT))
#else
#define FONTMAP_SHIFT		10
#define FONTMAP_PAGES		(1 << (sizeof(Tcl_UniChar)*8 - FONTMAP_SHIFT))
#endif
#define FONTMAP_BITSPERPAGE	(1 << FONTMAP_SHIFT)

typedef struct FontFamily {
    struct FontFamily *nextPtr;	/* Next in list of all known font families. */
    int refCount;		/* How many SubFonts are referring to this
				 * FontFamily. When the refCount drops to
				 * zero, this FontFamily may be freed. */
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
83
84
85
86
87
88
89

90



91
92
93
94
95
96
97







-

-
-
-







} SubFont;

/*
 * The following structure represents Unix's implementation of a font object.
 */

#define SUBFONT_SPACE		3
#if TCL_UTF_MAX > 3
#define BASE_CHARS		4096
#else
#define BASE_CHARS		2048
#endif

typedef struct UnixFont {
    TkFont font;		/* Stuff used by generic font package. Must be
				 * first in structure. */
    SubFont staticSubFonts[SUBFONT_SPACE];
				/* Builtin space for a limited number of
				 * SubFonts. */
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
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







-
-
+












-

-
-
-







-
+









-







    int *dstCharsPtr)		/* Filled with the number of characters that
				 * correspond to the bytes stored in the
				 * output buffer. */
{
    const char *srcStart, *srcEnd;
    char *dstStart, *dstEnd;
    unsigned int *wDst;
    Tcl_UniChar ch = 0;
    int result;
    int ch, result;
    static char const hexChars[] = "0123456789abcdef";
    static char const mapChars[] = {
	0, 0, 0, 0, 0, 0, 0,
	'a', 'b', 't', 'n', 'v', 'f', 'r'
    };

    result = TCL_OK;

    srcStart = src;
    srcEnd = src + srcLen;

    dstStart = dst;
#if TCL_UTF_MAX > 3
    dstEnd = dst + dstLen - 10 * sizeof(unsigned int);
#else
    dstEnd = dst + dstLen - 6 * sizeof(unsigned int);
#endif
    wDst = (unsigned int *) dst;

    for ( ; src < srcEnd; ) {
	if ((char *) wDst > dstEnd) {
	    result = TCL_CONVERT_NOSPACE;
	    break;
	}
	src += Tcl_UtfToUniChar(src, &ch);
	src += TkUtfToUniChar(src, &ch);
	wDst[0] = '\\';
	if ((ch < sizeof(mapChars)) && (mapChars[ch] != 0)) {
	    wDst[1] = mapChars[ch];
	    wDst += 2;
	} else if (ch < 256) {
	    wDst[1] = 'x';
	    wDst[2] = hexChars[(ch >> 4) & 0xf];
	    wDst[3] = hexChars[ch & 0xf];
	    wDst += 4;
#if TCL_UTF_MAX > 3
	} else if (ch < 0x10000)  {
	    wDst[1] = 'u';
	    wDst[2] = hexChars[(ch >> 12) & 0xf];
	    wDst[3] = hexChars[(ch >> 8) & 0xf];
	    wDst[4] = hexChars[(ch >> 4) & 0xf];
	    wDst[5] = hexChars[ch & 0xf];
	    wDst += 6;
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
415
416
417
418
419
420
421









422
423
424
425
426
427
428







-
-
-
-
-
-
-
-
-







	    wDst[4] = hexChars[(ch >> 20) & 0xf];
	    wDst[5] = hexChars[(ch >> 16) & 0xf];
	    wDst[6] = hexChars[(ch >> 12) & 0xf];
	    wDst[7] = hexChars[(ch >> 8) & 0xf];
	    wDst[8] = hexChars[(ch >> 4) & 0xf];
	    wDst[9] = hexChars[ch & 0xf];
	    wDst += 10;
#else
	} else {
	    wDst[1] = 'u';
	    wDst[2] = hexChars[(ch >> 12) & 0xf];
	    wDst[3] = hexChars[(ch >> 8) & 0xf];
	    wDst[4] = hexChars[(ch >> 4) & 0xf];
	    wDst[5] = hexChars[ch & 0xf];
	    wDst += 6;
#endif
	}
    }
    *srcReadPtr = src - srcStart;
    *dstWrotePtr = (char *) wDst - dstStart;
    *dstCharsPtr = *dstWrotePtr / sizeof(unsigned int);
    return result;
}
880
881
882
883
884
885
886
887

888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903

904
905
906
907
908
909
910
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
884
885
886







-
+















-
+







    lastSubFontPtr = &fontPtr->subFontArray[0];

    if (numBytes == 0) {
	curX = 0;
	curByte = 0;
    } else if (maxLength < 0) {
	const char *p, *end, *next;
	Tcl_UniChar ch = 0;
	int ch;
	SubFont *thisSubFontPtr;
	FontFamily *familyPtr;
	Tcl_DString runString;

	/*
	 * A three step process:
	 * 1. Find a contiguous range of characters that can all be
	 *    represented by a single screen font.
	 * 2. Convert those chars to the encoding of that font.
	 * 3. Measure converted chars.
	 */

	curX = 0;
	end = source + numBytes;
	for (p = source; p < end; ) {
	    next = p + Tcl_UtfToUniChar(p, &ch);
	    next = p + TkUtfToUniChar(p, &ch);
	    thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr);
	    if (thisSubFontPtr != lastSubFontPtr) {
		familyPtr = lastSubFontPtr->familyPtr;
		Tcl_UtfToExternalDString(familyPtr->encoding, source,
			p - source, &runString);
		if (lastSubFontPtr == &fontPtr->controlSubFont) {
		    fontSize =
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
912
913
914
915
916
917
918

919

920
921
922
923
924
925
926
927
928

929
930
931
932
933
934
935
936







-
+
-









-
+







	if (lastSubFontPtr == &fontPtr->controlSubFont) {
	    SdlTkFontRestore(lastSubFontPtr->fontStructPtr, fontSize);
	}
	Tcl_DStringFree(&runString);
	curByte = numBytes;
    } else {
	const char *p, *end, *next, *term;
	int newX, termX, sawNonSpace, dstWrote;
	int newX, termX, sawNonSpace, dstWrote, ch;
	Tcl_UniChar ch = 0;
	FontFamily *familyPtr;
	char buf[64];

	/*
	 * How many chars will fit in the space allotted? This first version
	 * may be inefficient because it measures every character
	 * individually.
	 */

	next = source + Tcl_UtfToUniChar(source, &ch);
	next = source + TkUtfToUniChar(source, &ch);
	newX = curX = termX = 0;

	term = source;
	end = source + numBytes;

	sawNonSpace = (ch > 255) || !isspace(ch);
	familyPtr = lastSubFontPtr->familyPtr;
993
994
995
996
997
998
999
1000

1001
1002
1003
1004
1005
1006
1007
968
969
970
971
972
973
974

975
976
977
978
979
980
981
982







-
+







	    p = next;
	    if (p >= end) {
		term = end;
		termX = curX;
		break;
	    }

	    next += Tcl_UtfToUniChar(next, &ch);
	    next += TkUtfToUniChar(next, &ch);
	    if ((ch < 256) && isspace(ch)) {
		if (sawNonSpace) {
		    term = p;
		    termX = curX;
		    sawNonSpace = 0;
		}
	    } else {
1018
1019
1020
1021
1022
1023
1024
1025

1026
1027
1028
1029
1030
1031

1032
1033
1034
1035
1036
1037
1038
993
994
995
996
997
998
999

1000
1001
1002
1003
1004
1005

1006
1007
1008
1009
1010
1011
1012
1013







-
+





-
+







	    /*
	     * Include the first character that didn't quite fit in the
	     * desired span. The width returned will include the width of that
	     * extra character.
	     */

	    curX = newX;
	    p += Tcl_UtfToUniChar(p, &ch);
	    p += TkUtfToUniChar(p, &ch);
	}
	if ((flags & TK_AT_LEAST_ONE) && (term == source) && (p < end)) {
	    term = p;
	    termX = curX;
	    if (term == source) {
		term += Tcl_UtfToUniChar(term, &ch);
		term += TkUtfToUniChar(term, &ch);
		termX = newX;
	    }
	} else if ((p >= end) || !(flags & TK_WHOLE_WORDS)) {
	    term = p;
	    termX = curX;
	}

1136
1137
1138
1139
1140
1141
1142
1143

1144
1145
1146
1147
1148
1149
1150
1151
1111
1112
1113
1114
1115
1116
1117

1118

1119
1120
1121
1122
1123
1124
1125







-
+
-







    int x, int y)		/* Coordinates at which to place origin of
				 * string when drawing. */
{
    UnixFont *fontPtr = (UnixFont *) tkfont;
    SubFont *thisSubFontPtr, *lastSubFontPtr;
    Tcl_DString runString;
    const char *p, *end, *next;
    int xStart, window_width;
    int xStart, window_width, ch;
    Tcl_UniChar ch = 0;
    FontFamily *familyPtr;
    int rx, ry, fontSize = 0;
    unsigned width, height, border_width, depth;
    Drawable root;

    lastSubFontPtr = &fontPtr->subFontArray[0];
    xStart = x;
1160
1161
1162
1163
1164
1165
1166
1167

1168
1169
1170
1171
1172
1173
1174
1134
1135
1136
1137
1138
1139
1140

1141
1142
1143
1144
1145
1146
1147
1148







-
+







    } else {
	window_width = width;
    }

    end = source + numBytes;
    for (p = source; p <= end; ) {
	if (p < end) {
	    next = p + Tcl_UtfToUniChar(p, &ch);
	    next = p + TkUtfToUniChar(p, &ch);
	    thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr);
	} else {
	    next = p + 1;
	    thisSubFontPtr = lastSubFontPtr;
	}
	if ((thisSubFontPtr != lastSubFontPtr)
		|| (p == end) || (p-source > 200)) {
2136
2137
2138
2139
2140
2141
2142
2143

2144
2145
2146
2147
2148
2149
2150
2110
2111
2112
2113
2114
2115
2116

2117
2118
2119
2120
2121
2122
2123
2124







-
+







    }

    end = (row + 1) << FONTMAP_SHIFT;
    for (i = row << FONTMAP_SHIFT; i < end; i++) {
	if ((i >= 0xD800) && (i <= 0xDFFF)) {
	    continue;
	}
	if (Tcl_UtfToExternal(NULL, encoding, src, Tcl_UniCharToUtf(i, src),
	if (Tcl_UtfToExternal(NULL, encoding, src, TkUniCharToUtf(i, src),
		TCL_ENCODING_STOPONERROR, NULL, buf, sizeof(buf), NULL,
		NULL, NULL) != TCL_OK) {
	    continue;
	}
	if (SdlTkFontHasChar(subFontPtr->fontStructPtr, buf)) {
	    bitOffset = i & (FONTMAP_BITSPERPAGE - 1);
	    subFontPtr->fontMap[row][bitOffset >> 3] |= 1 << (bitOffset & 7);
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
2922
2869
2870
2871
2872
2873
2874
2875

2876

2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887

2888
2889
2890
2891
2892
2893
2894
2895







-
+
-











-
+







    if (angle == 0.0) {
	Tk_DrawChars(display, drawable, gc, tkfont, source, numBytes, x, y);
    } else {
	UnixFont *fontPtr = (UnixFont *) tkfont;
	SubFont *thisSubFontPtr, *lastSubFontPtr;
	Tcl_DString runString;
	const char *p, *end, *next;
	int xStart, yStart, fontSize = 0;
	int xStart, yStart, fontSize = 0, ch;
	Tcl_UniChar ch = 0;
	FontFamily *familyPtr;
        double sinA = sin(angle*PI/180), cosA = cos(angle*PI/180), dy;
        XPoint points[5];

	lastSubFontPtr = &fontPtr->subFontArray[0];
	xStart = x;
	yStart = y;

	end = source + numBytes;
	for (p = source; p <= end; ) {
	    if (p < end) {
		next = p + Tcl_UtfToUniChar(p, &ch);
		next = p + TkUtfToUniChar(p, &ch);
		thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr);
	    } else {
		next = p + 1;
		thisSubFontPtr = lastSubFontPtr;
	    }
	    if ((thisSubFontPtr != lastSubFontPtr)
		    || (p == end) || (p-source > 200)) {

Changes to jni/sdl2tk/sdl/tkSDLMenu.c.

855
856
857
858
859
860
861
862
863
864
865
866
867

868
869
870
871
872
873
874
855
856
857
858
859
860
861

862
863
864
865

866
867
868
869
870
871
872
873







-




-
+







{
    if ((mePtr->underline >= 0) && (mePtr->labelPtr != NULL)) {
	int len;

	len = Tcl_GetCharLength(mePtr->labelPtr);
	if (mePtr->underline < len) {
	    int activeBorderWidth, leftEdge;
	    Tcl_UniChar ch = 0;
	    const char *label, *start, *end;

	    label = Tcl_GetString(mePtr->labelPtr);
	    start = Tcl_UtfAtIndex(label, mePtr->underline);
	    end = start + Tcl_UtfToUniChar(start, &ch);
	    end = TkUtfNext(start);

	    Tk_GetPixelsFromObj(NULL, menuPtr->tkwin,
		    menuPtr->activeBorderWidthPtr, &activeBorderWidth);
	    leftEdge = x + mePtr->indicatorSpace + activeBorderWidth;
	    if (menuPtr->menuType == MENUBAR) {
		leftEdge += 5;
	    }

Changes to jni/sdl2tk/unix/tkUnixFont.c.

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
31
32
33
34
35
36
37

38
39




40
41
42
43
44
45
46







-


-
-
-
-







 * character existence metrics, used to determine if a screen font can display
 * a given Unicode character.
 *
 * Under Unix, there are three attributes that uniquely identify a "font
 * family": the foundry, face name, and charset.
 */

#if TCL_UTF_MAX > 3
#define FONTMAP_SHIFT		12
#define FONTMAP_PAGES		(1 << (21 - FONTMAP_SHIFT))
#else
#define FONTMAP_SHIFT		10
#define FONTMAP_PAGES		(1 << (sizeof(Tcl_UniChar)*8 - FONTMAP_SHIFT))
#endif
#define FONTMAP_BITSPERPAGE	(1 << FONTMAP_SHIFT)

typedef struct FontFamily {
    struct FontFamily *nextPtr;	/* Next in list of all known font families. */
    int refCount;		/* How many SubFonts are referring to this
				 * FontFamily. When the refCount drops to
				 * zero, this FontFamily may be freed. */
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
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







-
-
+



















-
+







				 * the conversion. */
    int *dstCharsPtr)		/* Filled with the number of characters that
				 * correspond to the bytes stored in the
				 * output buffer. */
{
    const char *srcStart, *srcEnd;
    char *dstStart, *dstEnd;
    Tcl_UniChar ch = 0;
    int result;
    int ch, result;
    static const char hexChars[] = "0123456789abcdef";
    static const char mapChars[] = {
	0, 0, 0, 0, 0, 0, 0,
	'a', 'b', 't', 'n', 'v', 'f', 'r'
    };

    result = TCL_OK;

    srcStart = src;
    srcEnd = src + srcLen;

    dstStart = dst;
    dstEnd = dst + dstLen - 6;

    for ( ; src < srcEnd; ) {
	if (dst > dstEnd) {
	    result = TCL_CONVERT_NOSPACE;
	    break;
	}
	src += Tcl_UtfToUniChar(src, &ch);
	src += TkUtfToUniChar(src, &ch);
	dst[0] = '\\';
	if ((ch < sizeof(mapChars)) && (mapChars[ch] != 0)) {
	    dst[1] = mapChars[ch];
	    dst += 2;
	} else if (ch < 256) {
	    dst[1] = 'x';
	    dst[2] = hexChars[(ch >> 4) & 0xF];
590
591
592
593
594
595
596
597

598
599
600
601
602
603
604
584
585
586
587
588
589
590

591
592
593
594
595
596
597
598







-
+







				 * the conversion. */
    int *dstCharsPtr)		/* Filled with the number of characters that
				 * correspond to the bytes stored in the
				 * output buffer. */
{
    const char *srcStart, *srcEnd, *srcClose, *dstStart, *dstEnd;
    int result, numChars;
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;

    srcStart = src;
    srcEnd = src + srcLen;
    srcClose = srcEnd;
    if (!(flags & TCL_ENCODING_END)) {
	srcClose -= TCL_UTF_MAX;
    }
1017
1018
1019
1020
1021
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
1051
1052
1053
1054
1055
1056
1057
1058

1059
1060
1061
1062
1063
1064
1065
1011
1012
1013
1014
1015
1016
1017

1018
1019
1020
1021
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

1051
1052
1053
1054
1055
1056
1057
1058







-
+

















-















-
+







				 * means return at least one character even if
				 * no characters fit. */
    int *lengthPtr)		/* Filled with x-location just after the
				 * terminating character. */
{
    UnixFont *fontPtr;
    SubFont *lastSubFontPtr;
    int curX, curByte;
    int curX, curByte, ch;

    /*
     * Unix does not use kerning or fractional character widths when
     * displaying text on the screen. So that means we can safely measure
     * individual characters or spans of characters and add up the widths w/o
     * any "off-by-one-pixel" errors.
     */

    fontPtr = (UnixFont *) tkfont;

    lastSubFontPtr = &fontPtr->subFontArray[0];

    if (numBytes == 0) {
	curX = 0;
	curByte = 0;
    } else if (maxLength < 0) {
	const char *p, *end, *next;
	Tcl_UniChar ch = 0;
	SubFont *thisSubFontPtr;
	FontFamily *familyPtr;
	Tcl_DString runString;

	/*
	 * A three step process:
	 * 1. Find a contiguous range of characters that can all be
	 *    represented by a single screen font.
	 * 2. Convert those chars to the encoding of that font.
	 * 3. Measure converted chars.
	 */

	curX = 0;
	end = source + numBytes;
	for (p = source; p < end; ) {
	    next = p + Tcl_UtfToUniChar(p, &ch);
	    next = p + TkUtfToUniChar(p, &ch);
	    thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr);
	    if (thisSubFontPtr != lastSubFontPtr) {
		familyPtr = lastSubFontPtr->familyPtr;
		Tcl_UtfToExternalDString(familyPtr->encoding, source,
			p - source, &runString);
		if (familyPtr->isTwoByteFont) {
		    curX += XTextWidth16(lastSubFontPtr->fontStructPtr,
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
1082
1083
1084
1085
1086
1087
1088

1089
1090
1091
1092
1093
1094
1095
1096
1097

1098
1099
1100
1101
1102
1103
1104
1105







-









-
+







		    Tcl_DStringLength(&runString));
	}
	Tcl_DStringFree(&runString);
	curByte = numBytes;
    } else {
	const char *p, *end, *next, *term;
	int newX, termX, sawNonSpace, dstWrote;
	Tcl_UniChar ch = 0;
	FontFamily *familyPtr;
	XChar2b buf[8];

	/*
	 * How many chars will fit in the space allotted? This first version
	 * may be inefficient because it measures every character
	 * individually.
	 */

	next = source + Tcl_UtfToUniChar(source, &ch);
	next = source + TkUtfToUniChar(source, &ch);
	newX = curX = termX = 0;

	term = source;
	end = source + numBytes;

	sawNonSpace = (ch > 255) || !isspace(ch);
	familyPtr = lastSubFontPtr->familyPtr;
1134
1135
1136
1137
1138
1139
1140
1141

1142
1143
1144
1145
1146
1147
1148
1126
1127
1128
1129
1130
1131
1132

1133
1134
1135
1136
1137
1138
1139
1140







-
+







	    p = next;
	    if (p >= end) {
		term = end;
		termX = curX;
		break;
	    }

	    next += Tcl_UtfToUniChar(next, &ch);
	    next += TkUtfToUniChar(next, &ch);
	    if ((ch < 256) && isspace(ch)) {
		if (sawNonSpace) {
		    term = p;
		    termX = curX;
		    sawNonSpace = 0;
		}
	    } else {
1159
1160
1161
1162
1163
1164
1165
1166

1167
1168
1169
1170
1171
1172

1173
1174
1175
1176
1177
1178
1179
1151
1152
1153
1154
1155
1156
1157

1158
1159
1160
1161
1162
1163

1164
1165
1166
1167
1168
1169
1170
1171







-
+





-
+







	    /*
	     * Include the first character that didn't quite fit in the
	     * desired span. The width returned will include the width of that
	     * extra character.
	     */

	    curX = newX;
	    p += Tcl_UtfToUniChar(p, &ch);
	    p += TkUtfToUniChar(p, &ch);
	}
	if ((flags & TK_AT_LEAST_ONE) && (term == source) && (p < end)) {
	    term = p;
	    termX = curX;
	    if (term == source) {
		term += Tcl_UtfToUniChar(term, &ch);
		term += TkUtfToUniChar(term, &ch);
		termX = newX;
	    }
	} else if ((p >= end) || !(flags & TK_WHOLE_WORDS)) {
	    term = p;
	    termX = curX;
	}

1276
1277
1278
1279
1280
1281
1282
1283

1284
1285
1286
1287
1288
1289
1290
1291
1268
1269
1270
1271
1272
1273
1274

1275

1276
1277
1278
1279
1280
1281
1282







-
+
-







    int x, int y)		/* Coordinates at which to place origin of
				 * string when drawing. */
{
    UnixFont *fontPtr = (UnixFont *) tkfont;
    SubFont *thisSubFontPtr, *lastSubFontPtr;
    Tcl_DString runString;
    const char *p, *end, *next;
    int xStart, needWidth, window_width, do_width;
    int xStart, needWidth, window_width, do_width, ch;
    Tcl_UniChar ch = 0;
    FontFamily *familyPtr;
#ifdef TK_DRAW_CHAR_XWINDOW_CHECK
    int rx, ry;
    unsigned width, height, border_width, depth;
    Drawable root;
#endif

1312
1313
1314
1315
1316
1317
1318
1319

1320
1321
1322
1323
1324
1325
1326
1303
1304
1305
1306
1307
1308
1309

1310
1311
1312
1313
1314
1315
1316
1317







-
+







    window_width = 32768;
#endif

    end = source + numBytes;
    needWidth = fontPtr->font.fa.underline + fontPtr->font.fa.overstrike;
    for (p = source; p <= end; ) {
	if (p < end) {
	    next = p + Tcl_UtfToUniChar(p, &ch);
	    next = p + TkUtfToUniChar(p, &ch);
	    thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr);
	} else {
	    next = p + 1;
	    thisSubFontPtr = lastSubFontPtr;
	}
	if ((thisSubFontPtr != lastSubFontPtr)
		|| (p == end) || (p-source > 200)) {
2248
2249
2250
2251
2252
2253
2254
2255

2256
2257
2258
2259
2260
2261
2262
2239
2240
2241
2242
2243
2244
2245

2246
2247
2248
2249
2250
2251
2252
2253







-
+







static void
FontMapLoadPage(
    SubFont *subFontPtr,	/* Contains font mapping cache to be
				 * updated. */
    int row)			/* Index of the page to be loaded into the
				 * cache. */
{
    char buf[16], src[TCL_UTF_MAX];
    char buf[16], src[TCL_UTF_MAX*2];
    int minHi, maxHi, minLo, maxLo, scale, checkLo;
    int i, end, bitOffset, isTwoByteFont, n;
    Tcl_Encoding encoding;
    XFontStruct *fontStructPtr;
    XCharStruct *widths;
    ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
	    Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
2286
2287
2288
2289
2290
2291
2292
2293

2294
2295
2296
2297
2298
2299
2300
2277
2278
2279
2280
2281
2282
2283

2284
2285
2286
2287
2288
2289
2290
2291







-
+







	}
    }

    end = (row + 1) << FONTMAP_SHIFT;
    for (i = row << FONTMAP_SHIFT; i < end; i++) {
	int hi, lo;

	if (Tcl_UtfToExternal(NULL, encoding, src, Tcl_UniCharToUtf(i, src),
	if (Tcl_UtfToExternal(NULL, encoding, src, TkUniCharToUtf(i, src),
		TCL_ENCODING_STOPONERROR, NULL, buf, sizeof(buf), NULL,
		NULL, NULL) != TCL_OK) {
	    continue;
	}
	if (isTwoByteFont) {
	    hi = ((unsigned char *) buf)[0];
	    lo = ((unsigned char *) buf)[1];
2452
2453
2454
2455
2456
2457
2458
2459

2460
2461
2462
2463
2464
2465
2466
2443
2444
2445
2446
2447
2448
2449

2450
2451
2452
2453
2454
2455
2456
2457







-
+







{
    int i, nameIdx, numNames, srcLen, numEncodings, bestIdx[2];
    Tk_Uid hateFoundry;
    const char *charset, *hateCharset;
    unsigned bestScore[2];
    char **nameList;
    char **nameListOrig;
    char src[TCL_UTF_MAX];
    char src[8];
    FontAttributes want, got;
    Display *display;
    SubFont subFont;
    XFontStruct *fontStructPtr;
    Tcl_DString dsEncodings;
    Tcl_Encoding *encodingCachePtr;

2482
2483
2484
2485
2486
2487
2488
2489

2490
2491
2492
2493
2494
2495
2496
2473
2474
2475
2476
2477
2478
2479

2480
2481
2482
2483
2484
2485
2486
2487







-
+







    display = fontPtr->display;
    nameList = ListFonts(display, faceName, &numNames);
    if (numNames == 0) {
	return NULL;
    }
    nameListOrig = nameList;

    srcLen = Tcl_UniCharToUtf(ch, src);
    srcLen = TkUniCharToUtf(ch, src);

    want.fa = fontPtr->font.fa;
    want.xa = fontPtr->xa;

    want.fa.family = Tk_GetUid(faceName);
    want.fa.size = (double)-fontPtr->pixelSize;

Changes to jni/sdl2tk/unix/tkUnixKey.c.

140
141
142
143
144
145
146





































147
148
149
150
151
152
153
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







#ifdef TK_USE_INPUT_METHODS
    if ((winPtr->dispPtr->flags & TK_DISPLAY_USE_IM)
	    && (winPtr->dispPtr->inputContext != NULL)
	    && (eventPtr->type == KeyPress)) {
	Status status;

#if X_HAVE_UTF8_STRING
#if TCL_UTF_MAX < 4
	/*
	 * We want WTF-8 here.
	 */
	Tcl_DStringInit(&buf);
	Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);
	len = Xutf8LookupString(winPtr->dispPtr->inputContext,
		&eventPtr->xkey,
		Tcl_DStringValue(&buf), Tcl_DStringLength(&buf),
		&kePtr->keysym, &status);

	if (status == XBufferOverflow) {
	    /*
	     * Expand buffer and try again.
	     */

	    Tcl_DStringSetLength(&buf, len);
	    len = Xutf8LookupString(winPtr->dispPtr->inputContext,
		    &eventPtr->xkey,
		    Tcl_DStringValue(&buf), Tcl_DStringLength(&buf),
		    &kePtr->keysym, &status);
	}
	Tcl_DStringSetLength(&buf, len);
	if ((status != XLookupChars) && (status != XLookupBoth)) {
	    len = 0;
	} else {
	    Tcl_Encoding encoding = Tcl_GetEncoding(NULL, "utf-8");

	    Tcl_ExternalToUtfDString(encoding, Tcl_DStringValue(&buf),
		len, dsPtr);
	    len = Tcl_DStringLength(dsPtr);
	    if (encoding) {
		Tcl_FreeEncoding(encoding);
	    }
	}
	Tcl_DStringFree(&buf);
#else
	Tcl_DStringSetLength(dsPtr, TCL_DSTRING_STATIC_SIZE-1);
	len = Xutf8LookupString(winPtr->dispPtr->inputContext,
		&eventPtr->xkey,
		Tcl_DStringValue(dsPtr), Tcl_DStringLength(dsPtr),
		&kePtr->keysym, &status);

	if (status == XBufferOverflow) {
161
162
163
164
165
166
167

168
169
170
171
172
173
174
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212







+







		    Tcl_DStringValue(dsPtr), Tcl_DStringLength(dsPtr),
		    &kePtr->keysym, &status);
	}
	if ((status != XLookupChars) && (status != XLookupBoth)) {
	    len = 0;
	}
	Tcl_DStringSetLength(dsPtr, len);
#endif
#else /* !X_HAVE_UTF8_STRING */
	/*
	 * Overallocate the dstring to the maximum stack amount.
	 */

	Tcl_DStringInit(&buf);
	Tcl_DStringSetLength(&buf, TCL_DSTRING_STATIC_SIZE-1);

Changes to jni/sdl2tk/unix/tkUnixMenu.c.

855
856
857
858
859
860
861
862
863
864
865
866
867

868
869
870
871
872
873
874
855
856
857
858
859
860
861

862
863
864
865

866
867
868
869
870
871
872
873







-




-
+







{
    if ((mePtr->underline >= 0) && (mePtr->labelPtr != NULL)) {
	int len;

	len = Tcl_GetCharLength(mePtr->labelPtr);
	if (mePtr->underline < len) {
	    int activeBorderWidth, leftEdge;
	    Tcl_UniChar ch = 0;
	    const char *label, *start, *end;

	    label = Tcl_GetString(mePtr->labelPtr);
	    start = Tcl_UtfAtIndex(label, mePtr->underline);
	    end = start + Tcl_UtfToUniChar(start, &ch);
	    end = TkUtfNext(start);

	    Tk_GetPixelsFromObj(NULL, menuPtr->tkwin,
		    menuPtr->activeBorderWidthPtr, &activeBorderWidth);
	    leftEdge = x + mePtr->indicatorSpace + activeBorderWidth;
	    if (menuPtr->menuType == MENUBAR) {
		leftEdge += 5;
	    }

Changes to jni/sdl2tk/unix/tkUnixRFont.c.

159
160
161
162
163
164
165
166

167
168
169
170
171
172
173
174
175

176
177
178
179
180
181
182
159
160
161
162
163
164
165

166
167
168
169
170
171
172
173
174

175
176
177
178
179
180
181
182







-
+








-
+







    Tcl_RegisterConfig(mainPtr->interp, "tk", cfg, "utf-8");
#endif
}

static XftFont *
GetFont(
    UnixFtFont *fontPtr,
    FcChar32 ucs4,
    int ucs4,
    double angle)
{
    int i;

    if (ucs4) {
	for (i = 0; i < fontPtr->nfaces; i++) {
	    FcCharSet *charset = fontPtr->faces[i].charset;

	    if (charset && FcCharSetHasChar(charset, ucs4)) {
	    if (charset && FcCharSetHasChar(charset, (FcChar32)ucs4)) {
		break;
	    }
	}
	if (i == fontPtr->nfaces) {
	    i = 0;
	}
    } else {
756
757
758
759
760
761
762
763
764
765

766
767
768
769
770
771
772
756
757
758
759
760
761
762



763
764
765
766
767
768
769
770







-
-
-
+







    Tk_Window tkwin,		/* Window on the font's display */
    Tk_Font tkfont,		/* Font to query */
    int c,			/* Character of interest */
    TkFontAttributes *faPtr)	/* Output: Font attributes */
{
    UnixFtFont *fontPtr = (UnixFtFont *) tkfont;
				/* Structure describing the logical font */
    FcChar32 ucs4 = (FcChar32) c;
				/* UCS-4 character to map */
    XftFont *ftFont = GetFont(fontPtr, ucs4, 0.0);
    XftFont *ftFont = GetFont(fontPtr, c, 0.0);
				/* Actual font used to render the character */

    GetTkFontAttributes(ftFont, faPtr);
    faPtr->underline = fontPtr->font.fa.underline;
    faPtr->overstrike = fontPtr->font.fa.overstrike;
}

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
805
806
807
808
809
810
811

812
813

814








815





816



817

818
819
820
821
822
823
824
825







-
+

-
+
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-

-
+








    handler = Tk_CreateErrorHandler(fontPtr->display,
	    -1, -1, -1, InitFontErrorProc, &errorFlag);
    curX = 0;
    curByte = 0;
    sawNonSpace = 0;
    while (numBytes > 0) {
	Tcl_UniChar unichar;
	int unichar;

	clen = Tcl_UtfToUniChar(source, &unichar);
	clen = TkUtfToUniChar(source, &unichar);
	c = (FcChar32) unichar;
#if TCL_UTF_MAX == 4
	if (!clen && (*source & 0xf8) == 0xf0) {
	    clen += Tcl_UtfToUniChar(source, &unichar);
	    c = (((c & 0x3ff) << 10) | (unichar & 0x3ff)) + 0x10000;
	}
#endif

	if ((unichar >= 0xD800) && (unichar <= 0xDFFF)) {
	if (clen <= 0) {
	    /*
	     * This can't happen (but see #1185640)
	     */

	    unichar = 0xFFFD;
	    Tk_DeleteErrorHandler(handler);
	    *lengthPtr = curX;
	    return curByte;
	}

	c = (FcChar32) unichar;
	source += clen;
	numBytes -= clen;
	if (c < 256 && isspace(c)) {		/* I18N: ??? */
	    if (sawNonSpace) {
		termByte = curByte;
		termX = curX;
		sawNonSpace = 0;
1043
1044
1045
1046
1047
1048
1049
1050

1051
1052
1053


1054
1055
1056
1057

1058
1059
1060
1061
1062
1063
1064
1065
1027
1028
1029
1030
1031
1032
1033

1034
1035


1036
1037




1038

1039
1040
1041
1042
1043
1044
1045







-
+

-
-
+
+
-
-
-
-
+
-







    xftcolor = LookUpColor(display, fontPtr, values.foreground);
    if (tsdPtr->clipRegion != NULL) {
	XftDrawSetClip(fontPtr->ftDraw, tsdPtr->clipRegion);
    }
    nspec = 0;
    while (numBytes > 0) {
	XftFont *ftFont;
	FcChar32 c;
	int c;

	clen = FcUtf8ToUcs4((FcChar8 *) source, &c, numBytes);
	if (clen <= 0) {
	clen = TkUtfToUniChar(source, &c);
	if ((c >= 0xD800) && (c <= 0xDFFF)) {
	    /*
	     * This should not happen, but it can.
	     */

	    c = 0xFFFD;
	    goto doUnderlineStrikeout;
	}
	source += clen;
	numBytes -= clen;

	ftFont = GetFont(fontPtr, c, 0.0);
	if (ftFont) {
	    specs[nspec].glyph = XftCharIndex(fontPtr->display, ftFont, c);
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1072
1073
1074
1075
1076
1077
1078

1079
1080
1081
1082
1083
1084
1085







-







    }
    if (nspec) {
	LOCK;
	XftDrawGlyphFontSpec(fontPtr->ftDraw, xftcolor, specs, nspec);
	UNLOCK;
    }

  doUnderlineStrikeout:
    if (tsdPtr->clipRegion != NULL) {
	XftDrawSetClip(fontPtr->ftDraw, NULL);
    }
    if (fontPtr->font.fa.underline != 0) {
	XFillRectangle(display, drawable, gc, xStart,
		y + fontPtr->font.underlinePos, (unsigned) (x - xStart),
		(unsigned) fontPtr->font.underlineHeight);
1188
1189
1190
1191
1192
1193
1194
1195

1196
1197
1198


1199
1200
1201
1202

1203
1204
1205
1206
1207
1208
1209
1210
1167
1168
1169
1170
1171
1172
1173

1174
1175


1176
1177




1178

1179
1180
1181
1182
1183
1184
1185







-
+

-
-
+
+
-
-
-
-
+
-








    nglyph = 0;
    currentFtFont = NULL;
    originX = originY = 0;

    while (numBytes > 0) {
	XftFont *ftFont;
	FcChar32 c;
	int c;

	clen = FcUtf8ToUcs4((FcChar8 *) source, &c, numBytes);
	if (clen <= 0) {
	clen = TkUtfToUniChar(source, &c);
	if ((c >= 0xD800) && (c <= 0xDFFF)) {
	    /*
	     * This should not happen, but it can.
	     */

	    c = 0xFFFD;
	    goto doUnderlineStrikeout;
	}
	source += clen;
	numBytes -= clen;

	ftFont = GetFont(fontPtr, c, angle);
	if (!ftFont) {
	    continue;
1286
1287
1288
1289
1290
1291
1292
1293

1294
1295
1296


1297
1298
1299
1300

1301
1302
1303
1304
1305
1306
1307
1308
1261
1262
1263
1264
1265
1266
1267

1268
1269


1270
1271




1272

1273
1274
1275
1276
1277
1278
1279







-
+

-
-
+
+
-
-
-
-
+
-







    xftcolor = LookUpColor(display, fontPtr, values.foreground);
    if (tsdPtr->clipRegion != NULL) {
	XftDrawSetClip(fontPtr->ftDraw, tsdPtr->clipRegion);
    }
    nspec = 0;
    while (numBytes > 0) {
	XftFont *ftFont, *ft0Font;
	FcChar32 c;
	int c;

	clen = FcUtf8ToUcs4((FcChar8 *) source, &c, numBytes);
	if (clen <= 0) {
	clen = TkUtfToUniChar(source, &c);
	if ((c >= 0xD800) && (c <= 0xDFFF)) {
	    /*
	     * This should not happen, but it can.
	     */

	    c = 0xFFFD;
	    goto doUnderlineStrikeout;
	}
	source += clen;
	numBytes -= clen;

	ftFont = GetFont(fontPtr, c, angle);
	ft0Font = GetFont(fontPtr, c, 0.0);
	if (ftFont && ft0Font) {
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1308
1309
1310
1311
1312
1313
1314

1315
1316
1317
1318
1319
1320
1321







-







    if (nspec) {
	LOCK;
	XftDrawGlyphFontSpec(fontPtr->ftDraw, xftcolor, specs, nspec);
	UNLOCK;
    }
#endif /* XFT_HAS_FIXED_ROTATED_PLACEMENT */

  doUnderlineStrikeout:
    if (tsdPtr->clipRegion != NULL) {
	XftDrawSetClip(fontPtr->ftDraw, NULL);
    }
    if (fontPtr->font.fa.underline || fontPtr->font.fa.overstrike) {
	XPoint points[5];
	double width = (x - xStart) * cosA + (yStart - y) * sinA;
	double barHeight = fontPtr->font.underlineHeight;

Changes to jni/sdl2tk/unix/tkUnixSelect.c.

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







+
+
+
















+
+
+
+
+
+
+
+
+

+
+
+
+

+
+







	    /*
	     * The X selection data is in UTF-8 format already. We can't
	     * guarantee that propInfo is NULL-terminated, so we might have to
	     * copy the string.
	     */

	    char *propData = propInfo;
#if TCL_UTF_MAX < 4
	    Tcl_Encoding encoding;
#endif

	    if (format != 8) {
		Tcl_SetObjResult(retrPtr->interp, Tcl_ObjPrintf(
			"bad format for string selection: wanted \"8\", got \"%d\"",
			format));
		Tcl_SetErrorCode(retrPtr->interp, "TK", "SELECTION", "FORMAT",
			NULL);
		retrPtr->result = TCL_ERROR;
		return;
	    }

	    if (propInfo[numItems] != '\0') {
		propData = (char *) ckalloc(numItems + 1);
		strcpy(propData, propInfo);
		propData[numItems] = '\0';
	    }

	    interp = retrPtr->interp;
	    Tcl_Preserve(interp);
#if TCL_UTF_MAX < 4
	    encoding = Tcl_GetEncoding(NULL, "utf-8");
	    Tcl_ExternalToUtfDString(encoding, propInfo, (int)numItems, &ds);
	    if (encoding) {
		Tcl_FreeEncoding(encoding);
	    }
	    retrPtr->result = retrPtr->proc(retrPtr->clientData,
		    retrPtr->interp, Tcl_DStringValue(&ds));
	    Tcl_DStringFree(&ds);
#else
	    retrPtr->result = retrPtr->proc(retrPtr->clientData,
		    retrPtr->interp, propData);
#endif
	    Tcl_Release(interp);
	    if (propData != propInfo) {
		ckfree(propData);
	    }

	} else if (type == dispPtr->incrAtom) {
	    /*
	     * It's a !?#@!?!! INCR-style reception. Arrange to receive the
967
968
969
970
971
972
973

974
975
976
977
978
979
980
981


















982
983
984
985
986
987
988
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025







+








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	    propPtr = (char *) buffer;
	    format = 32;
	    incr.converts[i].offset = 0;
	    XChangeProperty(reply.xsel.display, reply.xsel.requestor,
		    property, type, format, PropModeReplace,
		    (unsigned char *) propPtr, numItems);
	} else if (type == winPtr->dispPtr->utf8Atom) {
#if TCL_UTF_MAX > 3
	    /*
	     * This matches selection requests of type UTF8_STRING, which
	     * allows us to pass our utf-8 information untouched.
	     */

	    XChangeProperty(reply.xsel.display, reply.xsel.requestor,
		    property, type, 8, PropModeReplace,
		    (unsigned char *) buffer, numItems);
#else
	    /*
	     * Must ensure not to export WTF-8 here.
	     */
	    Tcl_DString ds;
	    Tcl_Encoding encoding;

	    encoding = Tcl_GetEncoding(NULL, "utf-8");
	    Tcl_UtfToExternalDString(encoding, (char *) buffer, -1, &ds);
	    XChangeProperty(reply.xsel.display, reply.xsel.requestor,
		    property, type, 8, PropModeReplace,
		    (unsigned char *) Tcl_DStringValue(&ds),
		    Tcl_DStringLength(&ds));
	    if (encoding) {
		Tcl_FreeEncoding(encoding);
	    }
	    Tcl_DStringFree(&ds);
#endif
	} else if ((type == XA_STRING)
		|| (type == winPtr->dispPtr->compoundTextAtom)) {
	    Tcl_DString ds;
	    Tcl_Encoding encoding;

	    /*
	     * STRING is Latin-1, COMPOUND_TEXT is an iso2022 variant. We need

Changes to jni/sdl2tk/unix/tkUnixWm.c.

5320
5321
5322
5323
5324
5325
5326

5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337


5338

5339

5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350

5351
5352







5353
5354
5355
5356
5357
5358
5359
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342

5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356

5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370







+











+
+

+
-
+











+

-
+
+
+
+
+
+
+







UpdateTitle(
    TkWindow *winPtr)
{
    WmInfo *wmPtr = winPtr->wmInfoPtr;
    Atom XA_UTF8_STRING = Tk_InternAtom((Tk_Window) winPtr, "UTF8_STRING");
    const char *string;
    Tcl_DString ds;
    Tcl_Encoding encoding;

    /*
     * Set window title:
     */

    string = (wmPtr->title != NULL) ? wmPtr->title : winPtr->nameUid;
    Tcl_UtfToExternalDString(NULL, string, -1, &ds);
    XStoreName(winPtr->display, wmPtr->wrapperPtr->window,
	    Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);

    encoding = Tcl_GetEncoding(NULL, "utf-8");
    Tcl_UtfToExternalDString(encoding, string, -1, &ds);
    SetWindowProperty(wmPtr->wrapperPtr, "_NET_WM_NAME", XA_UTF8_STRING, 8,
	    Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
	    string, strlen(string));
    Tcl_DStringFree(&ds);

    /*
     * Set icon name:
     */

    if (wmPtr->iconName != NULL) {
	Tcl_UtfToExternalDString(NULL, wmPtr->iconName, -1, &ds);
	XSetIconName(winPtr->display, wmPtr->wrapperPtr->window,
		Tcl_DStringValue(&ds));
	Tcl_DStringFree(&ds);

	Tcl_UtfToExternalDString(encoding, wmPtr->iconName, -1, &ds);
	SetWindowProperty(wmPtr->wrapperPtr, "_NET_WM_ICON_NAME",
		XA_UTF8_STRING, 8, wmPtr->iconName, strlen(wmPtr->iconName));
		XA_UTF8_STRING, 8,
		Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
	Tcl_DStringFree(&ds);
    }

    if (encoding) {
	Tcl_FreeEncoding(encoding);
    }
}

/*
 *--------------------------------------------------------------
 *
 * UpdatePhotoIcon --

Changes to jni/sdl2tk/win/tkWinFont.c.

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
22
23
24
25
26
27
28

29
30




31
32
33
34
35
36
37







-


-
-
-
-







 * instance of this structure. The most important shared property is the
 * character existence metrics, used to determine if a screen font can display
 * a given Unicode character.
 *
 * Under Windows, a "font family" is uniquely identified by its face name.
 */

#if TCL_UTF_MAX > 3
#define FONTMAP_SHIFT		12
#define FONTMAP_PAGES	    	(1 << (21 - FONTMAP_SHIFT))
#else
#define FONTMAP_SHIFT		10
#define FONTMAP_PAGES		(1 << (sizeof(Tcl_UniChar)*8 - FONTMAP_SHIFT))
#endif
#define FONTMAP_BITSPERPAGE	(1 << FONTMAP_SHIFT)

typedef struct FontFamily {
    struct FontFamily *nextPtr;	/* Next in list of all known font families. */
    size_t refCount;		/* How many SubFonts are referring to this
				 * FontFamily. When the refCount drops to
				 * zero, this FontFamily may be freed. */
819
820
821
822
823
824
825
826

827
828
829
830
831
832
833
814
815
816
817
818
819
820

821
822
823
824
825
826
827
828







-
+







    int *lengthPtr)		/* Filled with x-location just after the
				 * terminating character. */
{
    HDC hdc;
    HFONT oldFont;
    WinFont *fontPtr;
    int curX, moretomeasure;
    Tcl_UniChar ch;
    int ch;
    SIZE size;
    FontFamily *familyPtr;
    Tcl_DString runString;
    SubFont *thisSubFontPtr, *lastSubFontPtr;
    const char *p, *end, *next = NULL, *start;

    if (numBytes == 0) {
850
851
852
853
854
855
856
857

858
859
860
861
862
863
864
845
846
847
848
849
850
851

852
853
854
855
856
857
858
859







-
+







     */

    moretomeasure = 0;
    curX = 0;
    start = source;
    end = start + numBytes;
    for (p = start; p < end; ) {
	next = p + Tcl_UtfToUniChar(p, &ch);
	next = p + TkUtfToUniChar(p, &ch);
	thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr);
	if (thisSubFontPtr != lastSubFontPtr) {
	    familyPtr = lastSubFontPtr->familyPtr;
	    Tcl_UtfToExternalDString(familyPtr->encoding, start,
		    p - start, &runString);
	    size.cx = 0;
	    familyPtr->getTextExtentPoint32Proc(hdc,
912
913
914
915
916
917
918
919

920
921
922
923
924
925
926
907
908
909
910
911
912
913

914
915
916
917
918
919
920
921







-
+







	char buf[16];
	int dstWrote;
	int lastSize = 0;

	familyPtr = lastSubFontPtr->familyPtr;
	Tcl_DStringInit(&runString);
	for (p = start; p < end; ) {
	    next = p + Tcl_UtfToUniChar(p, &ch);
	    next = p + TkUtfToUniChar(p, &ch);
	    Tcl_UtfToExternal(NULL, familyPtr->encoding, p,
		    (int) (next - p), 0, NULL, buf, sizeof(buf), NULL,
		    &dstWrote, NULL);
	    Tcl_DStringAppend(&runString,buf,dstWrote);
	    size.cx = 0;
	    familyPtr->getTextExtentPoint32Proc(hdc,
		    (WCHAR *) Tcl_DStringValue(&runString),
961
962
963
964
965
966
967
968

969
970
971
972
973
974

975
976
977
978
979
980
981
956
957
958
959
960
961
962

963
964
965
966
967
968

969
970
971
972
973
974
975
976







-
+





-
+







    if ((flags & TK_WHOLE_WORDS) && (p < end)) {
	/*
	 * Scan the string for the last word break and than repeat the whole
	 * procedure without the maxLength limit or any flags.
	 */

	const char *lastWordBreak = NULL;
	Tcl_UniChar ch2;
	int ch2;

	end = p;
	p = source;
	ch = ' ';
	while (p < end) {
	    next = p + Tcl_UtfToUniChar(p, &ch2);
	    next = p + TkUtfToUniChar(p, &ch2);
	    if ((ch != ' ') && (ch2 == ' ')) {
		lastWordBreak = p;
	    }
	    p = next;
	    ch = ch2;
	}

1468
1469
1470
1471
1472
1473
1474
1475

1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491

1492
1493
1494
1495
1496
1497
1498
1463
1464
1465
1466
1467
1468
1469

1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485

1486
1487
1488
1489
1490
1491
1492
1493







-
+















-
+







				 * following string. */
    const char *source,		/* Potentially multilingual UTF-8 string. */
    int numBytes,		/* Length of string in bytes. */
    double x, double y,		/* Coordinates at which to place origin of
				 * string when drawing. */
    double angle)
{
    Tcl_UniChar ch;
    int ch;
    SIZE size;
    HFONT oldFont;
    FontFamily *familyPtr;
    Tcl_DString runString;
    const char *p, *end, *next;
    SubFont *lastSubFontPtr, *thisSubFontPtr;
    TEXTMETRICW tm;
    double sinA = sin(angle * PI/180.0), cosA = cos(angle * PI/180.0);

    lastSubFontPtr = &fontPtr->subFontArray[0];
    oldFont = SelectFont(hdc, fontPtr, lastSubFontPtr, angle);
    GetTextMetricsW(hdc, &tm);

    end = source + numBytes;
    for (p = source; p < end; ) {
	next = p + Tcl_UtfToUniChar(p, &ch);
	next = p + TkUtfToUniChar(p, &ch);
	thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr);

	/*
	 * The drawing API has a limit of 32767 pixels in one go.
	 * To avoid spending time on a rare case we do not measure each char,
	 * instead we limit to drawing chunks of 200 bytes since that works
	 * well in practice.
2223
2224
2225
2226
2227
2228
2229
2230

2231
2232
2233
2234
2235
2236
2237
2218
2219
2220
2221
2222
2223
2224

2225
2226
2227
2228
2229
2230
2231
2232







-
+







    SubFont *subFontPtr,	/* Contains font mapping cache to be
				 * updated. */
    int row)			/* Index of the page to be loaded into the
				 * cache. */
{
    FontFamily *familyPtr;
    Tcl_Encoding encoding;
    char src[TCL_UTF_MAX], buf[16];
    char src[4], buf[16];
    USHORT *startCount, *endCount;
    int i, j, bitOffset, end, segCount;

    subFontPtr->fontMap[row] = (char *) ckalloc(FONTMAP_BITSPERPAGE / 8);
    memset(subFontPtr->fontMap[row], 0, FONTMAP_BITSPERPAGE / 8);

    familyPtr = subFontPtr->familyPtr;
2276
2277
2278
2279
2280
2281
2282
2283

2284
2285
2286
2287
2288
2289
2290
2271
2272
2273
2274
2275
2276
2277

2278
2279
2280
2281
2282
2283
2284
2285







-
+







	 * we don't know the proper Unicode -> symbol font mapping, we can
	 * install the symbol font as the base font and access its glyphs.
	 */

	end = (row + 1) << FONTMAP_SHIFT;
	for (i = row << FONTMAP_SHIFT; i < end; i++) {
	    if (Tcl_UtfToExternal(NULL, encoding, src,
		    Tcl_UniCharToUtf(i, src), TCL_ENCODING_STOPONERROR, NULL,
		    TkUniCharToUtf(i, src), TCL_ENCODING_STOPONERROR, NULL,
		    buf, sizeof(buf), NULL, NULL, NULL) != TCL_OK) {
		continue;
	    }
	    bitOffset = i & (FONTMAP_BITSPERPAGE - 1);
	    subFontPtr->fontMap[row][bitOffset >> 3] |= 1 << (bitOffset & 7);
	}
    }

Changes to jni/sdl2tk/win/tkWinMenu.c.

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







-
















-
+


-






-
+







	int i;
	const char *label = (mePtr->labelPtr == NULL) ? ""
		: Tcl_GetString(mePtr->labelPtr);
	const char *accel = ((menuPtr->menuType == MENUBAR) || (mePtr->accelPtr == NULL)) ? ""
		: Tcl_GetString(mePtr->accelPtr);
	const char *p, *next;
	Tcl_DString itemString;
	Tcl_UniChar ch = 0;

	/*
	 * We have to construct the string with an ampersand preceeding the
	 * underline character, and a tab seperating the text and the accel
	 * text. We have to be careful with ampersands in the string.
	 */

	Tcl_DStringInit(&itemString);

	for (p = label, i = 0; *p != '\0'; i++, p = next) {
	    if (i == mePtr->underline) {
		Tcl_DStringAppend(&itemString, "&", 1);
	    }
	    if (*p == '&') {
		Tcl_DStringAppend(&itemString, "&", 1);
	    }
	    next = p + Tcl_UtfToUniChar(p, &ch);
	    next = TkUtfNext(p);
	    Tcl_DStringAppend(&itemString, p, (int) (next - p));
	}
	ch = 0;
	if (mePtr->accelLength > 0) {
	    Tcl_DStringAppend(&itemString, "\t", 1);
	    for (p = accel, i = 0; *p != '\0'; i++, p = next) {
		if (*p == '&') {
		    Tcl_DStringAppend(&itemString, "&", 1);
		}
		next = p + Tcl_UtfToUniChar(p, &ch);
		next = TkUtfNext(p);
		Tcl_DStringAppend(&itemString, p, (int) (next - p));
	    }
	}

	itemText = (char *)ckalloc(Tcl_DStringLength(&itemString) + 1);
	strcpy(itemText, Tcl_DStringValue(&itemString));
	Tcl_DStringFree(&itemString);
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111

2112
2113
2114
2115
2116
2117
2118
2098
2099
2100
2101
2102
2103
2104

2105
2106
2107

2108
2109
2110
2111
2112
2113
2114
2115







-



-
+







{
    if ((mePtr->underline >= 0) && (mePtr->labelPtr != NULL)) {
	int len;

	len = Tcl_GetCharLength(mePtr->labelPtr);
	if (mePtr->underline < len) {
	    const char *label, *start, *end;
	    Tcl_UniChar ch = 0;

	    label = Tcl_GetString(mePtr->labelPtr);
	    start = Tcl_UtfAtIndex(label, mePtr->underline);
	    end = start + Tcl_UtfToUniChar(start, &ch);
	    end = TkUtfNext(start);
	    Tk_UnderlineChars(menuPtr->display, d,
		    gc, tkfont, label, x + mePtr->indicatorSpace,
		    y + (height + fmPtr->ascent - fmPtr->descent) / 2,
		    (int) (start - label), (int) (end - label));
	}
    }
}

Changes to jni/sdl2tk/win/tkWinTest.c.

176
177
178
179
180
181
182

183


184
185
186
187
188
189
190
176
177
178
179
180
181
182
183

184
185
186
187
188
189
190
191
192







+
-
+
+







	msg = msgBuf;
    } else {
	Tcl_Encoding encoding;
	char *msgPtr;

	encoding = Tcl_GetEncoding(NULL, "unicode");
	Tcl_ExternalToUtfDString(encoding, (char *) wMsgPtr, -1, &ds);
	if (encoding) {
	Tcl_FreeEncoding(encoding);
	    Tcl_FreeEncoding(encoding);
	}
	LocalFree(wMsgPtr);

	msgPtr = Tcl_DStringValue(&ds);
	length = Tcl_DStringLength(&ds);

	/*
	 * Trim the trailing CR/LF from the system message.

Changes to jni/tcl/generic/regcustom.h.

85
86
87
88
89
90
91
92

93
94
95
96
97
98
99
85
86
87
88
89
90
91

92
93
94
95
96
97
98
99







-
+







typedef Tcl_UniChar chr;	/* The type itself. */
typedef int pchr;		/* What it promotes to. */
typedef unsigned uchr;		/* Unsigned type that will hold a chr. */
typedef int celt;		/* Type to hold chr, or NOCELT */
#define	NOCELT (-1)		/* Celt value which is not valid chr */
#define	CHR(c) (UCHAR(c))	/* Turn char literal into chr literal */
#define	DIGITVAL(c) ((c)-'0')	/* Turn chr digit into its value */
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
#define	CHRBITS	32		/* Bits in a chr; must not use sizeof */
#define	CHR_MIN	0x00000000	/* Smallest and largest chr; the value */
#define	CHR_MAX	0xFFFFFFFF	/* CHR_MAX-CHR_MIN+1 should fit in uchr */
#else
#define	CHRBITS	16		/* Bits in a chr; must not use sizeof */
#define	CHR_MIN	0x0000		/* Smallest and largest chr; the value */
#define	CHR_MAX	0xFFFF		/* CHR_MAX-CHR_MIN+1 should fit in uchr */

Changes to jni/tcl/generic/tcl.h.

2187
2188
2189
2190
2191
2192
2193



2194
2195
2196
2197
2198
2199
2200

2201
2202
2203
2204
2205
2206
2207
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202

2203
2204
2205
2206
2207
2208
2209
2210







+
+
+






-
+







 * is the default and recommended mode. UCS-4 is experimental and not
 * recommended. It works for the core, but most extensions expect UCS-2.
 */

#ifndef TCL_UTF_MAX
#define TCL_UTF_MAX		3
#endif
#if (TCL_UTF_MAX != 3) && (TCL_UTF_MAX != 4)
#error TCL_UTF_MAX must be 3 or 4
#endif

/*
 * This represents a Unicode character. Any changes to this should also be
 * reflected in regcustom.h.
 */

#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
    /*
     * unsigned int isn't 100% accurate as it should be a strict 4-byte value
     * (perhaps wchar_t). 64-bit systems may have troubles. The size of this
     * value must be reflected correctly in regcustom.h and
     * in tclEncoding.c.
     * XXX: Tcl is currently UCS-2 and planning UTF-16 for the Unicode
     * XXX: string rep that Tcl_UniChar represents.  Changing the size

Changes to jni/tcl/generic/tclBinary.c.

447
448
449
450
451
452
453














454
455
456
457
458
459
460
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+







	src = TclGetStringFromObj(objPtr, &length);
	srcEnd = src + length;

	byteArrayPtr = (ByteArray *) ckalloc(BYTEARRAY_SIZE(length));
	byteArrayPtr->bytes = (unsigned char *) (byteArrayPtr + 1);
	for (dst = byteArrayPtr->bytes; src < srcEnd; ) {
	    src += TclUtfToUniChar(src, &ch);
#if TCL_UTF_MAX == 3
	    if ((ch & 0xFC00) == 0xD800) {
		if (Tcl_UtfCharComplete(src, srcEnd - src)) {
		    int uch = ch;
		    int len2 = TclUtfToUniChar(src, &ch);

		    if ((ch & 0xFC00) == 0xDC00) {
			src += len2;
			uch = (((uch&0x3ff)<<10) | (ch&0x3ff)) + 0x10000;
		    }
		    ch = UCHAR(uch);
		}
	    }
#endif
	    *dst++ = UCHAR(ch);
	}

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

	TclFreeIntRep(objPtr);
1211
1212
1213
1214
1215
1216
1217
1218

1219
1220
1221
1222
1223
1224
1225
1225
1226
1227
1228
1229
1230
1231

1232
1233
1234
1235
1236
1237
1238
1239







-
+







 badIndex:
    errorString = "not enough arguments for all format specifiers";
    goto error;

 badField:
    {
	Tcl_UniChar ch = 0;
	char buf[TCL_UTF_MAX + 1] = "";
	char buf[TCL_UTF_MAX + 1];

	TclUtfToUniChar(errorString, &ch);
	buf[Tcl_UniCharToUtf(ch, buf)] = '\0';
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"bad field specifier \"%s\"", buf));
	return TCL_ERROR;
    }
1590
1591
1592
1593
1594
1595
1596
1597

1598
1599
1600
1601
1602
1603
1604
1604
1605
1606
1607
1608
1609
1610

1611
1612
1613
1614
1615
1616
1617
1618







-
+







 badIndex:
    errorString = "not enough arguments for all format specifiers";
    goto error;

 badField:
    {
	Tcl_UniChar ch = 0;
	char buf[TCL_UTF_MAX + 1] = "";
	char buf[TCL_UTF_MAX + 1];

	TclUtfToUniChar(errorString, &ch);
	buf[Tcl_UniCharToUtf(ch, buf)] = '\0';
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"bad field specifier \"%s\"", buf));
	return TCL_ERROR;
    }

Changes to jni/tcl/generic/tclCmdIL.c.

4415
4416
4417
4418
4419
4420
4421
4422
4423
4424



4425

4426
4427
4428
4429
4430








4431
4432
4433
4434
4435




4436
4437

4438
4439
4440



4441
4442
4443


4444
4445
4446
4447
4448
4449
4450
4415
4416
4417
4418
4419
4420
4421

4422

4423
4424
4425
4426
4427





4428
4429
4430
4431
4432
4433
4434
4435





4436
4437
4438
4439


4440



4441
4442
4443



4444
4445
4446
4447
4448
4449
4450
4451
4452







-

-
+
+
+

+
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
+
+
-
-
+
-
-
-
+
+
+
-
-
-
+
+







	 * Convert character to Unicode for comparison purposes. If either
	 * string is at the terminating null, do a byte-wise comparison and
	 * bail out immediately.
	 */

	if ((*left != '\0') && (*right != '\0')) {
	    Tcl_UniChar lch = 0, rch = 0;
	    int len;

	    len = TclUtfToUniChar(left, &lch);
	    left += TclUtfToUniChar(left, &lch);
	    right += TclUtfToUniChar(right, &rch);

	    uniLeft = lch;
	    uniRight = rch;
#if TCL_UTF_MAX == 4
	    if (!len) {
		len = TclUtfToUniChar(left, &lch);
		uniLeft = (((uniLeft&0x3FF)<<10) | (lch&0x3FF)) + 0x10000;
	    }
#if TCL_UTF_MAX == 3
	    if (*left && ((lch & 0xFC00) == 0xD800)) {
		int len2 = TclUtfToUniChar(left, &lch);

		if ((lch & 0xFC00) == 0xDC00) {
		    uniLeft = (((uniLeft&0x3FF)<<10) | (lch&0x3FF)) + 0x10000;
		    left += len2;
		}
#endif
	    left += len;

	    len = TclUtfToUniChar(right, &rch);
	    uniRight = rch;
	    }
	    if (*right && ((rch & 0xFC00) == 0xD800)) {
		int len2 = TclUtfToUniChar(right, &rch);

#if TCL_UTF_MAX == 4
	    if (!len) {
		if ((rch & 0xFC00) == 0xDC00) {
		len = TclUtfToUniChar(right, &rch);
		uniRight = (((uniRight&0x3FF)<<10) | (rch&0x3FF)) + 0x10000;
	    }
		    uniRight = (((uniRight&0x3FF)<<10) | (rch&0x3FF)) + 0x10000;
		    right += len2;
		}
#endif
	    right += len;

	    }
#endif
	    /*
	     * Convert both chars to lower for the comparison, because
	     * dictionary sorts are case-insensitive. Covert to lower, not
	     * upper, so chars between Z and a will sort before A (where most
	     * other interesting punctuations occur).
	     */

Changes to jni/tcl/generic/tclCmdMZ.c.

26
27
28
29
30
31
32
33

34
35
36
37
38
39
40
26
27
28
29
30
31
32

33
34
35
36
37
38
39
40







-
+







static Tcl_NRPostProc	SwitchPostProc;
static Tcl_NRPostProc	TryPostBody;
static Tcl_NRPostProc	TryPostFinal;
static Tcl_NRPostProc	TryPostHandler;
static int		UniCharIsAscii(int character);
static int		UniCharIsHexDigit(int character);

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
static int		MemCmp(const void *s1, const void *s2, size_t n,
			    int flags);
static int		NumCodePointsUtf(const char *src, int length,
			    int *flagPtr);
static int		NumCodePointsUnicode(const Tcl_UniChar *src,
			    int length, int *flagPtr);
static int		UniCharNcmp(const Tcl_UniChar *ucs,
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
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







-
+

















-
+







	"\xE2\x80\xAF" /* narrow no-break space (U+202f) */
	"\xE2\x81\x9F" /* medium mathematical space (U+205f) */
	"\xE2\x81\xA0" /* word joiner (U+2060) */
	"\xE3\x80\x80" /* ideographic space (U+3000) */
	"\xEF\xBB\xBF" /* zero width no-break space (U+feff) */
;

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
/*
 *---------------------------------------------------------------------------
 *
 * MemCmp --
 *
 *	Private wrapper for memcmp(). See C library documentation.
 *
 *---------------------------------------------------------------------------
 */

static int
MemCmp(const void *s1, const void *s2, size_t n, int flags)
{
    return memcmp(s1, s2, n);
}
#endif

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
/*
 *---------------------------------------------------------------------------
 *
 * NumCodePointsUtf --
 *
 *	Like Tcl_NumUtfChars() but returns the number of code points.
 *
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
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







-
+


+

-
-
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+




-
-
-
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+




+
+





-
+







static int
NumCodePointsUtf(
    const char *src,		/* The UTF-8 string to measure. */
    int length,			/* The length of the string in bytes. */
    int *flagPtr)		/* Location to receive end flag. */
{
    Tcl_UniChar ch = 0;
    int len, i = 0;
    int i = 0;
    const char *endPtr = src + length - TCL_UTF_MAX;

    *flagPtr = 0;
    while (src < endPtr) {
	len = TclUtfToUniChar(src, &ch);
	if (len) {
	src += TclUtfToUniChar(src, &ch);
	if ((ch & 0xFC00) == 0xD800) {
	    ch = 0;
	} else {
	    len = TclUtfToUniChar(src, &ch);
	}
	src += len;
	    if ((src < endPtr) && Tcl_UtfCharComplete(src, endPtr - src)) {
		int len = TclUtfToUniChar(src, &ch);

		if ((ch & 0xFC00) == 0xDC00) {
		    --i;
		    src += len;
		}
	    }
	}
	i++;
    }
    endPtr += TCL_UTF_MAX;
    while ((src < endPtr) && Tcl_UtfCharComplete(src, endPtr - src)) {
	len = TclUtfToUniChar(src, &ch);
	if (len) {
	    ch = 0;
	src += TclUtfToUniChar(src, &ch);
	if ((ch & 0xFC00) == 0xD800) {
	    if ((src < endPtr) && Tcl_UtfCharComplete(src, endPtr - src)) {
	} else {
	    len = TclUtfToUniChar(src, &ch);
	}
	src += len;
		int len = TclUtfToUniChar(src, &ch);

		if ((ch & 0xFC00) == 0xDC00) {
		    --i;
		    src += len;
		}
	    }
	}
	i++;
    }
    if (src < endPtr) {
	i += endPtr - src;
    } else if (i && ((ch & 0xFC00) == 0xD800)) {
	*flagPtr = 1;
    }
    return i;
}
#endif

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
/*
 *----------------------------------------------------------------------
 *
 * UtfNcmp --
 *
 *	Like Tcl_UtfNcmp() but the limit is guaranteed and specified in
 *	code points.
174
175
176
177
178
179
180








































181

182
183
184
185

186
187
188
189
190
191
192
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+



-
+







static int
UtfNcmp(
    const char *cs,		/* UTF string to compare to ct. */
    const char *ct,		/* UTF string cs is compared to. */
    size_t numCp,		/* Number of code points to compare. */
    int flags)			/* Flags describing string ends. */
{
    Tcl_UniChar ch1 = 0, ch2 = 0;
    int uch1, uch2;

    while (numCp-- > 0) {

	cs += TclUtfToUniChar(cs, &ch1);
	ct += TclUtfToUniChar(ct, &ch2);

	uch1 = ch1;
	uch2 = ch2;

	if ((ch1 & 0xFC00) == 0xD800) {
	    if ((flags & 1) && (numCp == 0)) {
		/* String ends with high surrogate. */
	    } else {
		int len = TclUtfToUniChar(cs, &ch1);

		if ((ch1 & 0xFC00) == 0xDC00) {
		    uch1 = (((uch1&0x3FF)<<10) | (ch1&0x3FF)) + 0x10000;
		    cs += len;
		}
	    }
	}
	if ((ch2 & 0xFC00) == 0xD800) {
	    if ((flags & 2) && (numCp == 0)) {
		/* String ends with high surrogate. */
	    } else {
		int len = TclUtfToUniChar(ct, &ch2);

		if ((ch2 & 0xFC00) == 0xDC00) {
		    uch2 = (((uch2&0x3FF)<<10) | (ch2&0x3FF)) + 0x10000;
		    ct += len;
		}
	    }
	}

	if (uch1 != uch2) {
	    return (uch1 - uch2);
	}
    }
    return Tcl_UtfNcmp(cs, ct, numCp);
    return 0;
}
#endif

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
/*
 *----------------------------------------------------------------------
 *
 * UtfNcasecmp --
 *
 *	Like Tcl_UtfNcasecmp() but the limit is guaranteed and specified in
 *	code points.
203
204
205
206
207
208
209












































210

211
212
213
214

215
216
217
218
219
220
221
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+



-
+







static int
UtfNcasecmp(
    const char *cs,		/* UTF string to compare to ct. */
    const char *ct,		/* UTF string cs is compared to. */
    size_t numCp,		/* Number of code points to compare. */
    int flags)			/* Flags describing string ends. */
{
    Tcl_UniChar ch1 = 0, ch2 = 0;
    int uch1, uch2;

    while (numCp-- > 0) {

	cs += TclUtfToUniChar(cs, &ch1);
	ct += TclUtfToUniChar(ct, &ch2);

	uch1 = ch1;
	uch2 = ch2;

	if ((ch1 & 0xFC00) == 0xD800) {
	    if ((flags & 1) && (numCp == 0)) {
		/* String ends with high surrogate. */
	    } else {
		int len = TclUtfToUniChar(cs, &ch1);

		if ((ch1 & 0xFC00) == 0xDC00) {
		    uch1 = (((uch1&0x3FF)<<10) | (ch1&0x3FF)) + 0x10000;
		    cs += len;
		}
	    }
	}
	if ((ch2 & 0xFC00) == 0xD800) {
	    if ((flags & 2) && (numCp == 0)) {
		/* String ends with high surrogate. */
	    } else {
		int len = TclUtfToUniChar(ct, &ch2);

		if ((ch2 & 0xFC00) == 0xDC00) {
		    uch2 = (((uch2&0x3FF)<<10) | (ch2&0x3FF)) + 0x10000;
		    ct += len;
		}
	    }
	}

	if (uch1 != uch2) {
	    uch1 = TclUCS4ToLower(uch1);
	    uch2 = TclUCS4ToLower(uch2);
	    if (uch1 != uch2) {
		return (uch1 - uch2);
	    }
	}
    }
    return Tcl_UtfNcasecmp(cs, ct, numCp);
    return 0;
}
#endif

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
/*
 *---------------------------------------------------------------------------
 *
 * NumCodePointsUnicode --
 *
 *	Returns the number of code points of a Tcl_UniChar array.
 *
247
248
249
250
251
252
253
254

255
256
257
258
259
260
261
342
343
344
345
346
347
348

349
350
351
352
353
354
355
356







-
+







	    }
	}
    }
    return n;
}
#endif

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
/*
 *----------------------------------------------------------------------
 *
 * UniCharNcmp --
 *
 *	Like Tcl_UniCharNcmp() but the limit is guaranteed and specified in
 *	code points.
301
302
303
304
305
306
307
308

309
310
311
312
313
314
315
396
397
398
399
400
401
402

403
404
405
406
407
408
409
410







-
+







	    return (lcs - lct);
	}
    }
    return 0;
}
#endif

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
/*
 *----------------------------------------------------------------------
 *
 * UniCharNcasecmp --
 *
 *	Like Tcl_UniCharNcasecmp() but the limit is guaranteed and specified in
 *	code points.
893
894
895
896
897
898
899
900

901
902
903
904
905
906
907
988
989
990
991
992
993
994

995
996
997
998
999
1000
1001
1002







-
+







	 * This is a simple one pair string map situation. We make use of a
	 * slightly modified version of the one pair STR_MAP code.
	 */

	int slen, nocase;
	int (*strCmpFn)(const Tcl_UniChar*,const Tcl_UniChar*,unsigned long);
	Tcl_UniChar *p;
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
	Tcl_UniChar wsrclc;
#endif

	numMatches = 0;
	nocase = (cflags & TCL_REG_NOCASE);
	strCmpFn = nocase ? Tcl_UniCharNcasecmp : Tcl_UniCharNcmp;

924
925
926
927
928
929
930
931

932
933
934
935
936

937
938
939
940
941
942
943
1019
1020
1021
1022
1023
1024
1025

1026
1027
1028
1029
1030

1031
1032
1033
1034
1035
1036
1037
1038







-
+




-
+







		    Tcl_AppendUnicodeToObj(resultPtr, wsubspec, wsublen);
		    Tcl_AppendUnicodeToObj(resultPtr, wstring, 1);
		    numMatches++;
		}
		wlen = 0;
	    }
	} else {
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
	    wsrclc = Tcl_UniCharToLower(*wsrc);
#endif
	    for (p = wfirstChar = wstring; wstring < wend; wstring++) {
		if (
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
		    (*wstring == *wsrc ||
			(nocase && Tcl_UniCharToLower(*wstring)==wsrclc)) &&
			(slen==1 || (strCmpFn(wstring, wsrc,
				(unsigned long) slen) == 0))
#else
			!strCmpFn(wstring, wsrc, (unsigned long) slen)
#endif
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403










1404
1405
1406
1407
1408
1409
1410
1487
1488
1489
1490
1491
1492
1493





1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510







-
-
-
-
-
+
+
+
+
+
+
+
+
+
+







	Tcl_InitHashTable(&charReuseTable, TCL_ONE_WORD_KEYS);

	for ( ; stringPtr < end; stringPtr += len) {
	    int fullchar;

	    len = TclUtfToUniChar(stringPtr, &ch);
	    fullchar = ch;

#if TCL_UTF_MAX == 4
	    if (!len) {
		len += TclUtfToUniChar(stringPtr, &ch);
		fullchar = (((fullchar & 0x3FF) << 10) | (ch & 0x3FF)) + 0x10000;
#if TCL_UTF_MAX == 3
	    if ((stringPtr + len < end) && (fullchar <= 0xFFFF)
		    && ((fullchar & 0xFC00) == 0xD800)) {
		int len2 = TclUtfToUniChar(stringPtr + len, &ch);

		if ((ch & 0xFC00) == 0xDC00) {
		    fullchar = ((fullchar & 0x3FF) << 10) | (ch & 0x3FF);
		    fullchar += 0x10000;
		    len += len2;
		}
	    }
#endif

	    /*
	     * Assume Tcl_UniChar is an integral type...
	     */

1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461










1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485










1486
1487
1488
1489
1490
1491
1492
1551
1552
1553
1554
1555
1556
1557




1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587




1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604







-
-
-
-
+
+
+
+
+
+
+
+
+
+




















-
-
-
-
+
+
+
+
+
+
+
+
+
+







	 */

	Tcl_DStringInit(&ds);
	p = splitChars;
	while (p < splitChars + splitCharLen) {
	    len = TclUtfToUniChar(p, &ch);
	    uch = ch;
#if TCL_UTF_MAX == 4
	    if (!len) {
	       	len = TclUtfToUniChar(p, &ch);
		uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
#if TCL_UTF_MAX == 3
	    if ((ch & 0xFC00) == 0xD800) {
		int len2;

		ch = 0;
	       	len2 = TclUtfToUniChar(p + len, &ch);
		if ((ch & 0xFC00) == 0xDC00) {
		    len += len2;
		    uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
		}
	    }
#endif
	    Tcl_DStringAppend(&ds, (char *) &uch, sizeof(int));
	    p += len;
	}

	/*
	 * Now have real length of split chars.
	 */

	splitCharLen = Tcl_DStringLength(&ds) / sizeof(int);

	/*
	 * Normal case: split on any of a given set of characters. Discard
	 * instances of the split characters.
	 */

	for (element = stringPtr; stringPtr < end; stringPtr += len) {
	    len = TclUtfToUniChar(stringPtr, &ch);
	    uch = ch;
#if TCL_UTF_MAX == 4
	    if (!len) {
		len = TclUtfToUniChar(stringPtr, &ch);
		uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
#if TCL_UTF_MAX == 3
	    if ((ch & 0xFC00) == 0xD800) {
		int len2;

		ch = 0;
		len2 = TclUtfToUniChar(stringPtr + len, &ch);
		if ((ch & 0xFC00) == 0xDC00) {
		    len += len2;
		    uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
		}
	    }
#endif
	    for (i = 0; i < splitCharLen; i++) {
		if (uch == ((int *)Tcl_DStringValue(&ds))[i]) {
		    TclNewStringObj(objPtr, element, stringPtr - element);
		    Tcl_ListObjAppendElement(NULL, listPtr, objPtr);
		    element = stringPtr + len;
1764
1765
1766
1767
1768
1769
1770
1771

1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1876
1877
1878
1879
1880
1881
1882

1883
1884
1885






1886
1887
1888
1889
1890
1891
1892







-
+


-
-
-
-
-
-







	 */

	if (TclIsPureByteArray(objv[1])) {
	    unsigned char uch = UCHAR(ch);

	    Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(&uch, 1));
	} else {
	    char buf[TCL_UTF_MAX] = "";
	    char buf[TCL_UTF_MAX];

	    length = Tcl_UniCharToUtf(ch, buf);
#if TCL_UTF_MAX == 4
	    if (!length) {
		/* Special case for handling high surrogates. */
		length = Tcl_UniCharToUtf(-1, buf);
	    }
#endif
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, length));
	}
    }
    return TCL_OK;
}

/*
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150









2151
2152
2153
2154
2155
2156
2157
2246
2247
2248
2249
2250
2251
2252




2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268







-
-
-
-
+
+
+
+
+
+
+
+
+







	}
	end = string1 + length1;
	for (; string1 < end; string1 += length2, failat++) {
	    int fullchar;

	    length2 = TclUtfToUniChar(string1, &ch);
	    fullchar = ch;
#if TCL_UTF_MAX == 4
	    if (!length2) {
	    	length2 = TclUtfToUniChar(string1, &ch);
	    	fullchar = (((fullchar & 0x3FF) << 10) | (ch & 0x3FF)) + 0x10000;
#if TCL_UTF_MAX == 3
	    if ((string1 + length2 < end) && ((ch & 0xFC00) == 0xD800)) {
		int length3 = TclUtfToUniChar(string1 + length2, &ch);

		if ((ch & 0xFC00) == 0xDC00) {
		    fullchar = ((fullchar & 0x3FF) << 10) | (ch & 0x3FF);
		    fullchar += 0x10000;
		    length2 += length3;
		}
	    }
#endif
	    if (!chcomp(fullchar)) {
		result = 0;
		break;
	    }
	}
2337
2338
2339
2340
2341
2342
2343
2344

2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358

2359
2360
2361
2362
2363

2364
2365
2366
2367
2368
2369
2370
2448
2449
2450
2451
2452
2453
2454

2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468

2469
2470
2471
2472
2473

2474
2475
2476
2477
2478
2479
2480
2481







-
+













-
+




-
+







	 * extra calls to get Unicode data. The algorithm is otherwise
	 * identical to the multi-pair case. This will be >30% faster on
	 * larger strings.
	 */

	int mapLen;
	Tcl_UniChar *mapString;
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
	Tcl_UniChar u2lc;
#endif

	ustring2 = Tcl_GetUnicodeFromObj(mapElemv[0], &length2);
	p = ustring1;
	if ((length2 > length1) || (length2 == 0)) {
	    /*
	     * Match string is either longer than input or empty.
	     */

	    ustring1 = end;
	} else {
	    mapString = Tcl_GetUnicodeFromObj(mapElemv[1], &mapLen);
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
	    u2lc = (nocase ? Tcl_UniCharToLower(*ustring2) : 0);
#endif
	    for (; ustring1 < end; ustring1++) {
		if (
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
		    ((*ustring1 == *ustring2) ||
			(nocase&&Tcl_UniCharToLower(*ustring1)==u2lc)) &&
			(length2==1 || strCmpFn(ustring1, ustring2,
				(unsigned long) length2) == 0)
#else
		    !strCmpFn(ustring1, ustring2, (unsigned long) length2)
#endif
2379
2380
2381
2382
2383
2384
2385
2386

2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402

2403
2404
2405
2406
2407
2408
2409
2410
2411

2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426

2427
2428
2429
2430
2431
2432
2433
2490
2491
2492
2493
2494
2495
2496

2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512

2513
2514
2515
2516
2517
2518
2519
2520
2521

2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536

2537
2538
2539
2540
2541
2542
2543
2544







-
+















-
+








-
+














-
+








		    Tcl_AppendUnicodeToObj(resultPtr, mapString, mapLen);
		}
	    }
	}
    } else {
	Tcl_UniChar **mapStrings;
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
	Tcl_UniChar *u2lc = NULL;
#endif
	int *mapLens;

	/*
	 * Precompute pointers to the Unicode string and length. This saves us
	 * repeated function calls later, significantly speeding up the
	 * algorithm. We only need the lowercase first char in the nocase
	 * case.
	 */

	mapStrings = (Tcl_UniChar **)
		TclStackAlloc(interp, mapElemc*2*sizeof(Tcl_UniChar *));
	mapLens = (int *)
		TclStackAlloc(interp, mapElemc * 2 * sizeof(int));
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
	if (nocase) {
	    u2lc = (Tcl_UniChar *)
		    TclStackAlloc(interp, mapElemc * sizeof(Tcl_UniChar));
	}
#endif
	for (index = 0; index < mapElemc; index++) {
	    mapStrings[index] = Tcl_GetUnicodeFromObj(mapElemv[index],
		    mapLens+index);
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
	    if (nocase && ((index % 2) == 0)) {
		u2lc[index/2] = Tcl_UniCharToLower(*mapStrings[index]);
	    }
#endif
	}
	for (p = ustring1; ustring1 < end; ustring1++) {
	    for (index = 0; index < mapElemc; index += 2) {
		/*
		 * Get the key string to match on.
		 */

		ustring2 = mapStrings[index];
		length2 = mapLens[index];
		if (
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
		    (length2 > 0) && ((*ustring1 == *ustring2) || (nocase &&
			(Tcl_UniCharToLower(*ustring1) == u2lc[index/2]))) &&
			/* Restrict max compare length. */
			(end-ustring1 >= length2) && ((length2 == 1) ||
			!strCmpFn(ustring2, ustring1, length2))
#else
		    (length2 > 0) && (end-ustring1 >= length2) &&
2457
2458
2459
2460
2461
2462
2463
2464

2465
2466
2467
2468
2469
2470
2471
2568
2569
2570
2571
2572
2573
2574

2575
2576
2577
2578
2579
2580
2581
2582







-
+








		    Tcl_AppendUnicodeToObj(resultPtr,
			    mapStrings[index+1], mapLens[index+1]);
		    break;
		}
	    }
	}
#if TCL_UTF_MAX != 4
#if TCL_UTF_MAX > 3
	if (nocase) {
	    TclStackFree(interp, u2lc);
	}
#endif
	TclStackFree(interp, mapLens);
	TclStackFree(interp, mapStrings);
    }
2867
2868
2869
2870
2871
2872
2873



2874
2875
2876
2877
2878


2879
2880
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
2922
2923
2978
2979
2980
2981
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
3055
3056
3057
3058
3059
3060
3061
3062







+
+
+



-
-
+
+

-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+





+












+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+



-
-
-
-
+
-
-

+







    }
    string = TclGetStringFromObj(objv[1], &length);
    if (index >= numChars) {
	index = numChars - 1;
    }
    cur = 0;
    if (index > 0) {
#if TCL_UTF_MAX == 3
	int len;
#endif

	p = Tcl_UtfAtIndex(string, index);

#if TCL_UTF_MAX == 4
	length = TclUtfToUniChar(p, &ch);
#if TCL_UTF_MAX == 3
	len = TclUtfToUniChar(p, &ch);
	uch = ch;
	if (!length) {
	    TclUtfToUniChar(p, &ch);
	    uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
	if ((p + len < string + length) && (ch & 0xFC00) == 0xD800) {
	    TclUtfToUniChar(p + len, &ch);
	    if ((ch & 0xFC00) == 0xDC00) {
		uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
	    }
	} else if ((ch & 0xFC00) == 0xDC00) {
	    const char *pp;
	    int ppInc = 0;

	    pp = TclUtfPrev(p, string);
	    do {
		pp += ppInc;
		ppInc = TclUtfToUniChar(pp, &ch);
	    } while (pp + ppInc < p);
	    if ((ch & 0xFC00) == 0xD800) {
		p = pp;
		uch = (((ch&0x3FF)<<10) | (uch&0x3FF)) + 0x10000;
		index--;
	    }
	}
#else
	TclUtfToUniChar(p, &ch);
	uch = ch;
#endif
	
	for (cur = index; cur >= 0; cur--) {
	    int delta = 0;
	    const char *next;

	    if (!Tcl_UniCharIsWordChar(uch)) {
		break;
	    }

	    next = TclUtfPrev(p, string);
	    do {
		next += delta;
		delta = TclUtfToUniChar(next, &ch);
	    } while (next + delta < p);
	    p = next;
		uch = ch;
#if TCL_UTF_MAX == 4
		if (!delta) {
		    delta = TclUtfToUniChar(next, &ch);
		    uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
	    uch = ch;

#if TCL_UTF_MAX == 3
	    if ((p > string) && (ch & 0xFC00) == 0xDC00) {
		const char *pp;
		int ppInc = 0;

		pp = TclUtfPrev(p, string);
		do {
		    pp += ppInc;
		    ppInc = TclUtfToUniChar(pp, &ch);
		} while (pp + ppInc < p);
		if ((ch & 0xFC00) == 0xD800) {
		    p = pp;
		    uch = (((ch&0x3FF)<<10) | (uch&0x3FF)) + 0x10000;
		    if (!Tcl_UniCharIsWordChar(uch)) {
			goto endForLoop;
			break;
		    }
		    cur--;
		}
#endif
	    } while (next + delta < p);
	    p = next;
	}
	    }
#if TCL_UTF_MAX == 4
endForLoop:
#endif
	}
	if (cur != index) {
	    cur += 1;
	}
    }
    Tcl_SetObjResult(interp, Tcl_NewIntObj(cur));
    return TCL_OK;
}
2965
2966
2967
2968
2969
2970
2971
2972
2973

2974
2975
2976
2977
2978
2979
2980
2981
2982
2983













2984
2985
2986
2987

2988
2989
2990
2991
2992
2993
2994
3104
3105
3106
3107
3108
3109
3110


3111
3112









3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125




3126
3127
3128
3129
3130
3131
3132
3133







-
-
+

-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+







    if (index < 0) {
	index = 0;
    }
    if (index < numChars) {
	p = Tcl_UtfAtIndex(string, index);
	end = string+length;
	for (cur = index; p < end; cur++) {
#if TCL_UTF_MAX == 4
	    length = TclUtfToUniChar(p, &ch);
	    p += TclUtfToUniChar(p, &ch);
	    uch = ch;
	    if (!length) {
		length = TclUtfToUniChar(p, &ch);
		uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
		if (Tcl_UniCharIsWordChar(uch)) {
		    cur++;
		    continue;
		}
		break;
	    }
#if TCL_UTF_MAX == 3
	    if ((p < end) && ((ch & 0xFC00) == 0xD800)) {
		int len = TclUtfToUniChar(p, &ch);

		if ((ch & 0xFC00) == 0xDC00) {
		    uch = (((uch&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
		    p += len;
		    if (Tcl_UniCharIsWordChar(uch)) {
			cur++;
			continue;
		    }
		    break;
		}
	    p += length;
#else
	    p += TclUtfToUniChar(p, &ch);
	    uch = ch;
	    }
#endif
	    if (!Tcl_UniCharIsWordChar(uch)) {
		break;
	    }
	}
	if (cur == index) {
	    cur++;
3144
3145
3146
3147
3148
3149
3150
3151

3152
3153
3154
3155
3156
3157
3158
3283
3284
3285
3286
3287
3288
3289

3290
3291
3292
3293
3294
3295
3296
3297







-
+







    int checkEq,		/* comparison is only for equality */
    int nocase,			/* comparison is not case sensitive */
    int reqlength)		/* requested length; -1 to compare whole
				 * strings */
{
    const char *s1, *s2;
    int empty, length, match, s1len, s2len;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
    int s1flag = 0, s2flag = 0;
    typedef int (*memCmpFn_t)(const void *, const void *, size_t, int);
#else
    typedef int (*memCmpFn_t)(const void *, const void *, size_t);
#endif
    memCmpFn_t memCmpFn;

3170
3171
3172
3173
3174
3175
3176
3177

3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194

3195
3196
3197
3198
3199

3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210

3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225

3226
3227
3228
3229
3230
3231
3232
3233

3234
3235
3236
3237
3238
3239
3240
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
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363

3364
3365
3366
3367
3368
3369
3370
3371

3372
3373
3374
3375
3376
3377
3378
3379







-
+
















-
+




-
+










-
+














-
+







-
+







	 * 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... :^)
	 */

	s1 = (char *) Tcl_GetByteArrayFromObj(value1Ptr, &s1len);
	s2 = (char *) Tcl_GetByteArrayFromObj(value2Ptr, &s2len);
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	memCmpFn = MemCmp;
#else
	memCmpFn = memcmp;
#endif
    } else if ((value1Ptr->typePtr == &tclStringType)
	    && (value2Ptr->typePtr == &tclStringType)) {
	/*
	 * Do a Unicode-specific comparison if both of the args are of String
	 * type. If the char length == byte length, we can do a memcmp. In
	 * benchmark testing this proved the most efficient check between the
	 * Unicode and string comparison operations.
	 */

	if (nocase) {
	    s1 = (char *) Tcl_GetUnicodeFromObj(value1Ptr, &s1len);
	    s2 = (char *) Tcl_GetUnicodeFromObj(value2Ptr, &s2len);
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    s1len = NumCodePointsUnicode((Tcl_UniChar *) s1, s1len, &s1flag);
	    s2len = NumCodePointsUnicode((Tcl_UniChar *) s2, s2len, &s2flag);
	    memCmpFn = (memCmpFn_t) UniCharNcasecmp;
#else
	    memCmpFn = (memCmpFn_t) Tcl_UniCharNcasecmp;
	    memCmpFn = (memCmpFn_t)Tcl_UniCharNcasecmp;
#endif
	} else {
	    s1len = Tcl_GetCharLength(value1Ptr);
	    s2len = Tcl_GetCharLength(value2Ptr);
	    if ((s1len == value1Ptr->length)
		    && (value1Ptr->bytes != NULL)
		    && (s2len == value2Ptr->length)
		    && (value2Ptr->bytes != NULL)) {
		s1 = value1Ptr->bytes;
		s2 = value2Ptr->bytes;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		memCmpFn = MemCmp;
#else
		memCmpFn = memcmp;
#endif
	    } else {
		s1 = (char *) Tcl_GetUnicode(value1Ptr);
		s2 = (char *) Tcl_GetUnicode(value2Ptr);
		if (
#ifdef WORDS_BIGENDIAN
			1
#else
			checkEq
#endif /* WORDS_BIGENDIAN */
		        ) {
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		    memCmpFn = MemCmp;
#else
		    memCmpFn = memcmp;
#endif
		    s1len *= sizeof(Tcl_UniChar);
		    s2len *= sizeof(Tcl_UniChar);
		} else {
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		    s1len = NumCodePointsUnicode((Tcl_UniChar *) s1,
			    s1len, &s1flag);
		    s2len = NumCodePointsUnicode((Tcl_UniChar *) s2,
			    s2len, &s2flag);
		    memCmpFn = (memCmpFn_t) UniCharNcmp;
#else
		    memCmpFn = (memCmpFn_t) Tcl_UniCharNcmp;
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
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







-
+











-
+


-
+
+








	if (!nocase && checkEq) {
	    /*
	     * When we have equal-length we can check only for (in)equality.
	     * We can use memcmp() in all (n)eq cases because we don't need to
	     * worry about lexical LE/BE variance.
	     */
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    memCmpFn = MemCmp;
#else
	    memCmpFn = memcmp;
#endif
	} else {
	    /*
	     * As a catch-all we will work with UTF-8. We cannot use memcmp()
	     * as that is unsafe with any string containing NUL (\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.
	     */
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    s1len = NumCodePointsUtf(s1, s1len, &s1flag);
	    s2len = NumCodePointsUtf(s2, s2len, &s2flag);
	    memCmpFn = (memCmpFn_t) (nocase ? UtfNcasecmp : UtfNcmp);
	    memCmpFn = (memCmpFn_t)
		    (nocase ? UtfNcasecmp : UtfNcmp);
#else
	    if ((reqlength < 0) && !nocase) {
		memCmpFn = (memCmpFn_t) TclpUtfNcmp2;
	    } else {
		s1len = Tcl_NumUtfChars(s1, s1len);
		s2len = Tcl_NumUtfChars(s2, s2len);
		memCmpFn = (memCmpFn_t)
3328
3329
3330
3331
3332
3333
3334
3335

3336
3337
3338
3339
3340
3341
3342
3468
3469
3470
3471
3472
3473
3474

3475
3476
3477
3478
3479
3480
3481
3482







-
+







    if (checkEq && (s1len != s2len)) {
	match = 1;		/* This will be reversed below. */
    } else {
	/*
	 * The comparison function should compare up to the minimum byte
	 * length only.
	 */
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	match = memCmpFn(s1, s2, (size_t) length, s1flag | (s2flag << 1));
#else
	match = memCmpFn(s1, s2, (size_t) length);
#endif
    }
    if ((match == 0) && (reqlength > length)) {
	match = s1len - s2len;

Changes to jni/tcl/generic/tclCompCmdsSZ.c.

1489
1490
1491
1492
1493
1494
1495
1496

1497
1498
1499
1500
1501
1502
1503
1489
1490
1491
1492
1493
1494
1495

1496
1497
1498
1499
1500
1501
1502
1503







-
+







	PUSH("");
	count++;
    }

    for (endTokenPtr = tokenPtr + parse.numTokens;
	    tokenPtr < endTokenPtr; tokenPtr = TokenAfter(tokenPtr)) {
	int length, literal, catchRange, breakJump;
	char buf[TCL_UTF_MAX] = "";
	char buf[TCL_UTF_MAX*2];
	JumpFixup startFixup, okFixup, returnFixup, breakFixup;
	JumpFixup continueFixup, otherFixup, endFixup;

	switch (tokenPtr->type) {
	case TCL_TOKEN_TEXT:
	    literal = TclRegisterNewLiteral(envPtr,
		    tokenPtr->start, tokenPtr->size);

Changes to jni/tcl/generic/tclCompile.c.

1719
1720
1721
1722
1723
1724
1725
1726

1727
1728
1729
1730
1731
1732
1733
1719
1720
1721
1722
1723
1724
1725

1726
1727
1728
1729
1730
1731
1732
1733







-
+







	    if (tempPtr != NULL) {
		Tcl_AppendToObj(tempPtr, tokenPtr->start, tokenPtr->size);
	    }
	    break;

	case TCL_TOKEN_BS:
	    if (tempPtr != NULL) {
		char utfBuf[TCL_UTF_MAX] = "";
		char utfBuf[TCL_UTF_MAX*2];
		int length = TclParseBackslash(tokenPtr->start,
			tokenPtr->size, NULL, utfBuf);

#if TCL_UTF_MAX > 3
		if ((length > 1) && (numComponents > 0)) {
		    /*
		     * Try to collapse a surrogate pair into a single
2386
2387
2388
2389
2390
2391
2392
2393

2394
2395
2396
2397
2398
2399
2400
2386
2387
2388
2389
2390
2391
2392

2393
2394
2395
2396
2397
2398
2399
2400







-
+







				 * compile. */
    int count,			/* Number of tokens to consider at tokenPtr.
				 * Must be at least 1. */
    CompileEnv *envPtr)		/* Holds the resulting instructions. */
{
    Tcl_DString textBuffer;	/* Holds concatenated chars from adjacent
				 * TCL_TOKEN_TEXT, TCL_TOKEN_BS tokens. */
    char buffer[TCL_UTF_MAX] = "";
    char buffer[TCL_UTF_MAX*2];
    int i, numObjsToConcat, length, adjust;
    unsigned char *entryCodeNext = envPtr->codeNext;
#define NUM_STATIC_POS 20
    int isLiteral, maxNumCL, numCL;
    int *clPosition = NULL;
    int depth = TclGetStackDepth(envPtr);

Changes to jni/tcl/generic/tclDisassemble.c.

890
891
892
893
894
895
896
897

898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
890
891
892
893
894
895
896

897
898
899
900











901
902
903
904
905
906
907







-
+



-
-
-
-
-
-
-
-
-
-
-







	    i += 2;
	    continue;
	case '\v':
	    Tcl_AppendToObj(appendObj, "\\v", -1);
	    i += 2;
	    continue;
	default:
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
	    if (ch > 0xFFFF) {
		Tcl_AppendPrintfToObj(appendObj, "\\U%08x", ch);
		i += 10;
	    } else
#elif TCL_UTF_MAX > 3
	    /* If len == 0, this means we have a char > 0xFFFF, resulting in
	     * TclUtfToUniChar producing a surrogate pair. We want to output
	     * this pair as a single Unicode character.
	     */
	    if (len == 0) {
		int upper = ((ch & 0x3FF) + 1) << 10;
		len = TclUtfToUniChar(p, &ch);
		Tcl_AppendPrintfToObj(appendObj, "\\U%08x", upper + (ch & 0x3FF));
		i += 10;
	    } else
#endif
	    if (ch < 0x20 || ch >= 0x7F) {
		Tcl_AppendPrintfToObj(appendObj, "\\u%04x", ch);
		i += 6;
	    } else {
		Tcl_AppendPrintfToObj(appendObj, "%c", ch);

Changes to jni/tcl/generic/tclEncoding.c.

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







-
+












+





+







			    int *srcReadPtr, int *dstWrotePtr,
			    int *dstCharsPtr);
static int		TableToUtfProc(ClientData clientData, const char *src,
			    int srcLen, int flags, Tcl_EncodingState *statePtr,
			    char *dst, int dstLen, int *srcReadPtr,
			    int *dstWrotePtr, int *dstCharsPtr);
static size_t		unilen(const char *src);
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
static size_t		unilen4(const char *src);
#endif
static int		UnicodeToUtfProc(ClientData clientData,
			    const char *src, int srcLen, int flags,
			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
			    int *srcReadPtr, int *dstWrotePtr,
			    int *dstCharsPtr);
static int		UtfToUnicodeProc(ClientData clientData,
			    const char *src, int srcLen, int flags,
			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
			    int *srcReadPtr, int *dstWrotePtr,
			    int *dstCharsPtr);
#if TCL_UTF_MAX > 3
static int		UtfToUtfProc(ClientData clientData,
			    const char *src, int srcLen, int flags,
			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
			    int *srcReadPtr, int *dstWrotePtr,
			    int *dstCharsPtr, int pureNullMode);
#endif
static int		UtfIntToUtfExtProc(ClientData clientData,
			    const char *src, int srcLen, int flags,
			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
			    int *srcReadPtr, int *dstWrotePtr,
			    int *dstCharsPtr);
static int		UtfExtToUtfIntProc(ClientData clientData,
			    const char *src, int srcLen, int flags,
268
269
270
271
272
273
274
275

276
277
278
279
280
281
282
270
271
272
273
274
275
276

277
278
279
280
281
282
283
284







-
+







			    int *srcReadPtr, int *dstWrotePtr,
			    int *dstCharsPtr);
static int		UtfToMcesu8Proc(ClientData clientData,
			    const char *src, int srcLen, int flags,
			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
			    int *srcReadPtr, int *dstWrotePtr,
			    int *dstCharsPtr);
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
static int		Utf16ToUtfProc(ClientData clientData,
			    const char *src, int srcLen, int flags,
			    Tcl_EncodingState *statePtr, char *dst, int dstLen,
			    int *srcReadPtr, int *dstWrotePtr,
			    int *dstCharsPtr);
static int		UtfToUtf16Proc(ClientData clientData,
			    const char *src, int srcLen, int flags,
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
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







-
+







-
+







-
+







    type.toUtfProc	= UtfExtToUtfIntProc;
    type.fromUtfProc	= UtfIntToUtfExtProc;
    type.freeProc	= NULL;
    type.nullSize	= 1;
    type.clientData	= NULL;
    Tcl_CreateEncoding(&type);

#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
    type.encodingName   = "utf-32";
#else
    type.encodingName   = "unicode";
#endif
    type.toUtfProc	= UnicodeToUtfProc;
    type.fromUtfProc    = UtfToUnicodeProc;
    type.freeProc	= NULL;
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
    type.nullSize	= 4;
#else
    type.nullSize	= 2;
#endif
    type.clientData	= NULL;
    Tcl_CreateEncoding(&type);

#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
    type.encodingName   = "unicode";
    type.toUtfProc	= Utf16ToUtfProc;
    type.fromUtfProc    = UtfToUtf16Proc;
    type.freeProc	= NULL;
    type.nullSize	= 2;
    type.clientData	= NULL;
    Tcl_CreateEncoding(&type);
1105
1106
1107
1108
1109
1110
1111
1112

1113
1114
1115
1116
1117
1118
1119
1107
1108
1109
1110
1111
1112
1113

1114
1115
1116
1117
1118
1119
1120
1121







-
+







    encodingPtr->toUtfProc	= typePtr->toUtfProc;
    encodingPtr->fromUtfProc	= typePtr->fromUtfProc;
    encodingPtr->freeProc	= typePtr->freeProc;
    encodingPtr->nullSize	= typePtr->nullSize;
    encodingPtr->clientData	= typePtr->clientData;
    if (typePtr->nullSize == 1) {
	encodingPtr->lengthProc = (LengthProc *) strlen;
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
    } else if (typePtr->nullSize == 4) {
	encodingPtr->lengthProc = (LengthProc *) unilen4;
#endif
    } else {
	encodingPtr->lengthProc = (LengthProc *) unilen;
    }
    encodingPtr->refCount	= 1;
2275
2276
2277
2278
2279
2280
2281
2282
2283


2284
2285
2286
2287
2288
2289
2290
2277
2278
2279
2280
2281
2282
2283


2284
2285
2286
2287
2288
2289
2290
2291
2292







-
-
+
+







}

/*
 *-------------------------------------------------------------------------
 *
 * UtfIntToUtfExtProc --
 *
 *	Convert from UTF-8 to UTF-8. While converting null-bytes from the
 *	Tcl's internal representation (0xC0, 0x80) to the official
 *	Convert from (U|W)TF-8 to UTF-8. While converting null-bytes
 *	from Tcl's internal representation (0xC0, 0x80) to the official
 *	representation (0x00). See UtfToUtfProc for details.
 *
 * Results:
 *	Returns TCL_OK if conversion was successful.
 *
 * Side effects:
 *	None.
2315
2316
2317
2318
2319
2320
2321



2322
2323


2324
2325






























































2326
2327
2328
2329
2330
2331

2332
2333
2334
2335
2336
2337
2338
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330


2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397

2398
2399
2400
2401
2402
2403
2404
2405







+
+
+


+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+





-
+







    int *dstWrotePtr,		/* Filled with the number of bytes that were
				 * stored in the output buffer as a result of
				 * the conversion. */
    int *dstCharsPtr)		/* Filled with the number of characters that
				 * correspond to the bytes stored in the
				 * output buffer. */
{
#if TCL_UTF_MAX > 3
    /* UTF-8 to UTF-8 */

    return UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
	    srcReadPtr, dstWrotePtr, dstCharsPtr, 1);
#else
    /* WTF-8 to UTF-8 */
}


    const char *srcStart, *srcEnd, *srcClose;
    const char *dstStart, *dstEnd;
    int result, numChars, charLimit = INT_MAX;
    Tcl_UniChar ch;

    result = TCL_OK;

    srcStart = src;
    srcEnd = src + srcLen;
    srcClose = srcEnd;
    if ((flags & TCL_ENCODING_END) == 0) {
	srcClose -= 3;
    }
    if (flags & TCL_ENCODING_CHAR_LIMIT) {
	charLimit = *dstCharsPtr;
    }

    dstStart = dst;
    dstEnd = dst + dstLen - 4;

    for (numChars = 0; src < srcEnd && numChars <= charLimit; numChars++) {
	if ((src > srcClose) && (!Tcl_UtfCharComplete(src, srcEnd - src))) {
	    result = TCL_CONVERT_MULTIBYTE;
	    break;
	}
	if (dst > dstEnd) {
	    result = TCL_CONVERT_NOSPACE;
	    break;
	}
	if (UCHAR(*src) == 0xc0 && (src + 1 < srcEnd) &&
		UCHAR(*(src+1)) == 0x80) {
	    *dst++ = 0;
	    src += 2;
	} else if (!Tcl_UtfCharComplete(src, srcEnd - src)) {
	    ch = (unsigned char) *src;
	    src += 1;
	    dst += Tcl_UniCharToUtf(ch, dst);
	} else {
	    int ch2, len;
	    
	    src += Tcl_UtfToUniChar(src, &ch);
	    ch2 = ch;
	    if ((ch & 0xFC00) == 0xD800) {
		if (Tcl_UtfCharComplete(src, srcEnd - src)) {
		    len = Tcl_UtfToUniChar(src, &ch);
		    if ((ch & 0xFC00) == 0xDC00) {
			src += len;
			ch2 = (((ch2&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
		    }
		}
	    }
	    dst += TclUniCharToUtfExt(ch2, dst);
	}
    }
    *srcReadPtr = src - srcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
    return result;
#endif
}

/*
 *-------------------------------------------------------------------------
 *
 * UtfExtToUtfIntProc --
 *
 *	Convert from UTF-8 to UTF-8 while converting null-bytes from the
 *	Convert from UTF-8 to (U|W)TF-8 while converting null-bytes from the
 *	official representation (0x00) to Tcl's internal representation (0xC0,
 *	0x80). See UtfToUtfProc for details.
 *
 * Results:
 *	Returns TCL_OK if conversion was successful.
 *
 * Side effects:
2364
2365
2366
2367
2368
2369
2370



2371
2372


2373
2374
































































2375
2376
2377
2378
2379
2380
2381
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444


2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515







+
+
+


+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    int *dstWrotePtr,		/* Filled with the number of bytes that were
				 * stored in the output buffer as a result of
				 * the conversion. */
    int *dstCharsPtr)		/* Filled with the number of characters that
				 * correspond to the bytes stored in the
				 * output buffer. */
{
#if TCL_UTF_MAX > 3
    /* UTF-8 to UTF-8 */

    return UtfToUtfProc(clientData, src, srcLen, flags, statePtr, dst, dstLen,
	    srcReadPtr, dstWrotePtr, dstCharsPtr, 0);
#else
    /* UTF-8 to WTF-8 */
}


    const char *srcStart, *srcEnd, *srcClose;
    const char *dstStart, *dstEnd;
    int ch, result, numChars, charLimit = INT_MAX;

    result = TCL_OK;

    srcStart = src;
    srcEnd = src + srcLen;
    srcClose = srcEnd;
    if ((flags & TCL_ENCODING_END) == 0) {
	srcClose -= 4;
    }
    if (flags & TCL_ENCODING_CHAR_LIMIT) {
	charLimit = *dstCharsPtr;
    }

    dstStart = dst;
    dstEnd = dst + dstLen - TCL_UTF_MAX;

    for (numChars = 0; src < srcEnd && numChars <= charLimit; numChars++) {
	if ((src > srcClose) && (!TclUtfCharCompleteExt(src, srcEnd - src))) {
	    result = TCL_CONVERT_MULTIBYTE;
	    break;
	}
	if (dst > dstEnd) {
	    result = TCL_CONVERT_NOSPACE;
	    break;
	}
	if (UCHAR(*src) < 0x80 && UCHAR(*src) != 0) {
	    *dst++ = *src++;
	} else if (!TclUtfCharCompleteExt(src, srcEnd - src)) {
	    ch = (unsigned char) *src;
	    src += 1;
	    dst += Tcl_UniCharToUtf(ch, dst);
	} else {
	    int len = TclUtfToUniCharExt(src, &ch);

	    if (ch > 0xFFFF && ch <= 0x10FFFF) {
		int dstLen;

		ch -= 0x10000;
		dstLen = Tcl_UniCharToUtf((ch >> 10) | 0xD800, dst);
		ch = (ch & 0x3FF) | 0xDC00;
		if (dst + dstLen > dstEnd) {
		    result = TCL_CONVERT_NOSPACE;
		    break;
		}
		dst += dstLen;
		numChars++;
	    }
	    src += len;
	    dst += Tcl_UniCharToUtf(ch, dst);
	}
    }

    *srcReadPtr = src - srcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
    return result;
#endif
}

#if TCL_UTF_MAX > 3
/*
 *-------------------------------------------------------------------------
 *
 * UtfToUtfProc --
 *
 *	Convert from UTF-8 to UTF-8. Note that the UTF-8 to UTF-8 translation
 *	is not a no-op, because it will turn a stream of improperly formed
2419
2420
2421
2422
2423
2424
2425
2426

2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2553
2554
2555
2556
2557
2558
2559

2560
2561



2562
2563
2564
2565
2566
2567
2568







-
+

-
-
-







    int pureNullMode)		/* Convert embedded nulls from internal
				 * representation to real null-bytes or vice
				 * versa. */
{
    const char *srcStart, *srcEnd, *srcClose;
    const char *dstStart, *dstEnd;
    int result, numChars, charLimit = INT_MAX;
    Tcl_UniChar *chPtr = (Tcl_UniChar *) statePtr;
    Tcl_UniChar ch = 0;

    if (flags & TCL_ENCODING_START) {
    	*statePtr = 0;
    }
    result = TCL_OK;

    srcStart = src;
    srcEnd = src + srcLen;
    srcClose = srcEnd;
    if ((flags & TCL_ENCODING_END) == 0) {
	srcClose -= TCL_UTF_MAX;
2475
2476
2477
2478
2479
2480
2481
2482

2483
2484

2485
2486

2487
2488
2489
2490
2491
2492
2493
















2494

2495
2496
2497
2498
2499
2500
2501
2502

2503
2504
2505
2506
2507
2508
2509
2606
2607
2608
2609
2610
2611
2612

2613
2614

2615
2616

2617







2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651







-
+

-
+

-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+








+







	} else if (!Tcl_UtfCharComplete(src, srcEnd - src)) {
	    /*
	     * Always check before using TclUtfToUniChar. Not doing can so
	     * cause it run beyond the end of the buffer! If we happen such an
	     * incomplete char its bytes are made to represent themselves.
	     */

	    *chPtr = (unsigned char) *src;
	    ch = (unsigned char) *src;
	    src += 1;
	    dst += Tcl_UniCharToUtf(*chPtr, dst);
	    dst += Tcl_UniCharToUtf(ch, dst);
	} else {
	    int len = TclUtfToUniChar(src, chPtr);
	    src += TclUtfToUniChar(src, &ch);
	    src += len;
	    dst += Tcl_UniCharToUtf(*chPtr, dst);
#if TCL_UTF_MAX == 4
	    if (!len) {
		src += TclUtfToUniChar(src, chPtr);
		dst += Tcl_UniCharToUtf(*chPtr, dst);
	    }
#if TCL_UTF_MAX > 3
	    /*
	     * Collapse surrogate pairs in both directions.
	     */

	    if ((ch & 0xFFFFC00) == 0xD800 &&
		Tcl_UtfCharComplete(src, srcEnd - src)) {
		Tcl_UniChar ch2;
		int len;

		len = Tcl_UtfToUniChar(src, &ch2);
		if ((ch2 & 0xFFFFFC00) == 0xDC00) {
		    src += len;
		    ch = (((ch & 0x3FF) << 10) | (ch2 & 0x3FF)) + 0x10000;
		}
	    }
#endif
	    dst += Tcl_UniCharToUtf(ch, dst);
	}
    }

    *srcReadPtr = src - srcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
    return result;
}
#endif

/*
 *-------------------------------------------------------------------------
 *
 * UnicodeToUtfProc --
 *
 *	Convert from Unicode to UTF-8.
2543
2544
2545
2546
2547
2548
2549
2550

2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2685
2686
2687
2688
2689
2690
2691

2692
2693



2694
2695
2696
2697
2698
2699
2700







-
+

-
-
-







    int *dstCharsPtr)		/* Filled with the number of characters that
				 * correspond to the bytes stored in the
				 * output buffer. */
{
    const char *srcStart, *srcEnd;
    const char *dstEnd, *dstStart;
    int result, numChars, charLimit = INT_MAX;
    Tcl_UniChar *chPtr = (Tcl_UniChar *) statePtr;
    Tcl_UniChar ch = 0;

    if (flags & TCL_ENCODING_START) {
    	*statePtr = 0;
    }
    if (flags & TCL_ENCODING_CHAR_LIMIT) {
	charLimit = *dstCharsPtr;
    }
    result = TCL_OK;
    if ((srcLen % sizeof(Tcl_UniChar)) != 0) {
	result = TCL_CONVERT_MULTIBYTE;
	srcLen /= sizeof(Tcl_UniChar);
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584



2585
2586

2587
2588
2589
2590
2591
2592
2593
2714
2715
2716
2717
2718
2719
2720



2721
2722
2723
2724

2725
2726
2727
2728
2729
2730
2731
2732







-
-
-
+
+
+

-
+







	}

	/*
	 * Special case for 1-byte utf chars for speed. Make sure we work with
	 * Tcl_UniChar-size data.
	 */

	*chPtr = *(Tcl_UniChar *)src;
	if (*chPtr && *chPtr < 0x80) {
	    *dst++ = (*chPtr & 0xFF);
	ch = *(Tcl_UniChar *)src;
	if (ch && ch < 0x80) {
	    *dst++ = (ch & 0xFF);
	} else {
	    dst += Tcl_UniCharToUtf(*chPtr, dst);
	    dst += Tcl_UniCharToUtf(ch, dst);
	}
	src += sizeof(Tcl_UniChar);
    }

    *srcReadPtr = src - srcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
2636
2637
2638
2639
2640
2641
2642
2643

2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2775
2776
2777
2778
2779
2780
2781

2782
2783



2784
2785
2786
2787
2788
2789
2790







-
+

-
-
-







				 * the conversion. */
    int *dstCharsPtr)		/* Filled with the number of characters that
				 * correspond to the bytes stored in the
				 * output buffer. */
{
    const char *srcStart, *srcEnd, *srcClose, *dstStart, *dstEnd;
    int result, numChars;
    Tcl_UniChar *chPtr = (Tcl_UniChar *) statePtr;
    Tcl_UniChar ch = 0;

    if (flags & TCL_ENCODING_START) {
    	*statePtr = 0;
    }
    srcStart = src;
    srcEnd = src + srcLen;
    srcClose = srcEnd;
    if ((flags & TCL_ENCODING_END) == 0) {
	srcClose -= TCL_UTF_MAX;
    }

2666
2667
2668
2669
2670
2671
2672
2673

2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685





2686
2687
2688


2689
2690
2691
2692
2693
2694
2695





2696
2697
2698


2699
2700
2701
2702
2703
2704
2705
2802
2803
2804
2805
2806
2807
2808

2809
2810
2811
2812
2813
2814
2815
2816





2817
2818
2819
2820
2821
2822


2823
2824
2825
2826





2827
2828
2829
2830
2831
2832


2833
2834
2835
2836
2837
2838
2839
2840
2841







-
+







-
-
-
-
-
+
+
+
+
+

-
-
+
+


-
-
-
-
-
+
+
+
+
+

-
-
+
+







	    result = TCL_CONVERT_MULTIBYTE;
	    break;
	}
	if (dst > dstEnd) {
	    result = TCL_CONVERT_NOSPACE;
	    break;
	}
	src += TclUtfToUniChar(src, chPtr);
	src += TclUtfToUniChar(src, &ch);

	/*
	 * Need to handle this in a way that won't cause misalignment by
	 * casting dst to a Tcl_UniChar. [Bug 1122671]
	 */

#ifdef WORDS_BIGENDIAN
#if TCL_UTF_MAX > 4
	*dst++ = (*chPtr >> 24);
	*dst++ = ((*chPtr >> 16) & 0xFF);
	*dst++ = ((*chPtr >> 8) & 0xFF);
	*dst++ = (*chPtr & 0xFF);
#if TCL_UTF_MAX > 3
	*dst++ = (ch >> 24);
	*dst++ = ((ch >> 16) & 0xFF);
	*dst++ = ((ch >> 8) & 0xFF);
	*dst++ = (ch & 0xFF);
#else
	*dst++ = (*chPtr >> 8);
	*dst++ = (*chPtr & 0xFF);
	*dst++ = (ch >> 8);
	*dst++ = (ch & 0xFF);
#endif
#else
#if TCL_UTF_MAX > 4
	*dst++ = (*chPtr & 0xFF);
	*dst++ = ((*chPtr >> 8) & 0xFF);
	*dst++ = ((*chPtr >> 16) & 0xFF);
	*dst++ = (*chPtr >> 24);
#if TCL_UTF_MAX > 3
	*dst++ = (ch & 0xFF);
	*dst++ = ((ch >> 8) & 0xFF);
	*dst++ = ((ch >> 16) & 0xFF);
	*dst++ = (ch >> 24);
#else
	*dst++ = (*chPtr & 0xFF);
	*dst++ = (*chPtr >> 8);
	*dst++ = (ch & 0xFF);
	*dst++ = (ch >> 8);
#endif
#endif
    }
    *srcReadPtr = src - srcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
    return result;
2902
2903
2904
2905
2906
2907
2908
2909

2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
3038
3039
3040
3041
3042
3043
3044

3045
3046
3047
3048
3049
3050
3051




3052
3053
3054
3055
3056
3057
3058







-
+






-
-
-
-







	     */

	    result = TCL_CONVERT_MULTIBYTE;
	    break;
	}
	len = TclUtfToUniChar(src, &ch);

#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
	/*
	 * This prevents a crash condition. More evaluation is required for
	 * full support of int Tcl_UniChar. [Bug 1004065]
	 */

	if (ch & 0xFFFF0000) {
	    word = 0;
	} else
#elif TCL_UTF_MAX == 4
	if (!len) {
	    word = 0;
	} else
#endif
	    word = fromUnicode[(ch >> 8)][ch & 0xFF];

	if ((word == 0) && (ch != 0)) {
	    if (flags & TCL_ENCODING_STOPONERROR) {
3112
3113
3114
3115
3116
3117
3118
3119

3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3244
3245
3246
3247
3248
3249
3250

3251




3252
3253
3254
3255



3256
3257
3258
3259
3260
3261
3262







-
+
-
-
-
-




-
-
-







	}
	len = TclUtfToUniChar(src, &ch);

	/*
	 * Check for illegal characters.
	 */

	if (ch > 0xFF
	if (ch > 0xFF) {
#if TCL_UTF_MAX == 4
		|| !len
#endif
		) {
	    if (flags & TCL_ENCODING_STOPONERROR) {
		result = TCL_CONVERT_UNKNOWN;
		break;
	    }
#if TCL_UTF_MAX == 4
	    if (!len) len = 4;
#endif

	    /*
	     * Plunge on, using '?' as a fallback character.
	     */

	    ch = (Tcl_UniChar) '?';
	}
3146
3147
3148
3149
3150
3151
3152
3153

3154
3155
3156
3157
3158
3159
3160
3271
3272
3273
3274
3275
3276
3277

3278
3279
3280
3281
3282
3283
3284
3285







-
+








    *srcReadPtr = src - srcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
    return result;
}

#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
/*
 *-------------------------------------------------------------------------
 *
 * Utf16ToUtfProc --
 *
 *	Convert from UTF-16 to UTF-8.
 *
3240
3241
3242
3243
3244
3245
3246
3247

3248
3249
3250
3251
3252
3253
3254
3365
3366
3367
3368
3369
3370
3371

3372
3373
3374
3375
3376
3377
3378
3379







-
+







    *srcReadPtr = src - srcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
    return result;
}
#endif

#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
/*
 *-------------------------------------------------------------------------
 *
 * UtfToUtf16Proc --
 *
 *	Convert from UTF-8 to UTF-16.
 *
3453
3454
3455
3456
3457
3458
3459
3460

3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
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







-
+










-
-
-
-
-
-
-
-
-
-
-
-
-







	    *chPtr = (unsigned char) *src;
	    src += 1;
	    dst += Tcl_UniCharToUtf(*chPtr, dst);
	} else {
	    int len = TclUtfToUniChar(src, chPtr);

	    src += len;
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
	    if (*chPtr > 0xFFFF) {
		int ch;

		ch = (((*chPtr - 0x10000) >> 10) & 0x3FF) | 0xD800;
		dst += Tcl_UniCharToUtf(ch, dst);
		ch = ((*chPtr - 0x10000) & 0x3FF) | 0xDC00;
		dst += Tcl_UniCharToUtf(ch, dst);
		continue;
	    }
#endif
#if TCL_UTF_MAX == 4
	    if (!len) {
		dst += Tcl_UniCharToUtf(*chPtr, dst);
		src += TclUtfToUniChar(src, chPtr);
	    }
#endif
	    if ((*chPtr & 0xF800) == 0xD800) {
		/*
		 * Don't let stray surrogates pass.
		 */

		*chPtr = 0xFFFD;
	    }
	    dst += Tcl_UniCharToUtf(*chPtr, dst);
	}
    }

    *srcReadPtr = src - srcStart;
    *dstWrotePtr = dst - dstStart;
    *dstCharsPtr = numChars;
3588
3589
3590
3591
3592
3593
3594
3595

3596
3597
3598
3599
3600
3601
3602
3700
3701
3702
3703
3704
3705
3706

3707
3708
3709
3710
3711
3712
3713
3714







-
+







	    *chPtr = (unsigned char) *src;
	    src += 1;
	    dst += Tcl_UniCharToUtf(*chPtr, dst);
	} else {
	    int len = TclUtfToUniChar(src, chPtr);

	    src += len;
#if TCL_UTF_MAX >= 4
#if TCL_UTF_MAX > 3
	    if ((*chPtr & 0xFC00) == 0xD800) {
		int len2 = 0;
		Tcl_UniChar ch2 = 0;

		if (Tcl_UtfCharComplete(src, srcEnd - src)) {
		    len2 = TclUtfToUniChar(src, &ch2);
		}
4201
4202
4203
4204
4205
4206
4207
4208

4209
4210
4211
4212
4213
4214
4215
4313
4314
4315
4316
4317
4318
4319

4320
4321
4322
4323
4324
4325
4326
4327







-
+







    p = (unsigned short *) src;
    while (*p != 0x0000) {
	p++;
    }
    return (char *) p - src;
}

#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
static size_t
unilen4(
    const char *src)
{
    unsigned int *p;

    p = (unsigned int *) src;

Changes to jni/tcl/generic/tclExecute.c.

5585
5586
5587
5588
5589
5590
5591
5592

5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5585
5586
5587
5588
5589
5590
5591

5592
5593
5594
5595
5596
5597
5598
5599
5600
5601






5602
5603
5604
5605
5606
5607
5608







-
+









-
-
-
-
-
-







	} else if (TclIsPureByteArray(valuePtr)) {
	    objResultPtr = Tcl_NewByteArrayObj(
		    Tcl_GetByteArrayFromObj(valuePtr, NULL)+index, 1);
	} else if (valuePtr->bytes && length == valuePtr->length) {
	    objResultPtr = Tcl_NewStringObj((const char *)
		    valuePtr->bytes+index, 1);
	} else {
	    char buf[TCL_UTF_MAX] = "";
	    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);
#if TCL_UTF_MAX == 4
	    /* Special case for handling high surrogates. */
	    if (!length) {
		length = Tcl_UniCharToUtf(-1, buf);
	    }
#endif
	    objResultPtr = Tcl_NewStringObj(buf, length);
	}

	TRACE_APPEND(("\"%s\"\n", O2S(objResultPtr)));
	NEXT_INST_F(1, 2, 1);

    case INST_STR_RANGE:
5947
5948
5949
5950
5951
5952
5953
5954

5955
5956
5957
5958
5959
5960
5961
5941
5942
5943
5944
5945
5946
5947

5948
5949
5950
5951
5952
5953
5954
5955







-
+







	ustring1 = Tcl_GetUnicodeFromObj(valuePtr, &length);
	match = 1;
	if (length > 0) {
	    end = ustring1 + length;
	    for (p=ustring1 ; p<end ; p++) {
		int ch = *p;

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		if (((ch&0xFC00)==0xD800) && (p+1<end)) {
		    if ((p[1]&0xFC00)==0xDC00) {
			ch = ((ch&0x3FF) << 10) + 0x10000 + (p[1]&0x3FF);
			p++;
		    }
		}
#endif

Changes to jni/tcl/generic/tclInt.h.

3249
3250
3251
3252
3253
3254
3255



3256

3257



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

3259

3260
3261
3262
3263
3264
3265




3266
3267
3268
3269
3270
3271
3272







+
+
+
-
+
-
+
+
+



-
-
-
-







MODULE_SCOPE void *	TclpThreadGetGlobalTSD(void *tsdKeyPtr);

MODULE_SCOPE void	TclErrorStackResetIf(Tcl_Interp *interp, const char *msg, int length);

#if TCL_UTF_MAX > 3
MODULE_SCOPE int	TclCollapseSurrogatePair(Tcl_Token *tokenPtr,
			    int *numReadPtr, char *buffer);
#define	TclUCS4ToUpper(ch) Tcl_UniCharToUpper((ch))
#define	TclUCS4ToLower(ch) Tcl_UniCharToLower((ch))
#define	TclUCS4ToTitle(ch) Tcl_UniCharToTitle((ch))
#endif
#else
#if TCL_UTF_MAX == 4
MODULE_SCOPE int	TclUniCharToUtfExt(int ch, char *buf);
MODULE_SCOPE int	TclUtfToUniCharExt(const char *src, int *chPtr);
MODULE_SCOPE int	TclUtfCharCompleteExt(const char *src, int len);
MODULE_SCOPE int	TclUCS4ToUpper(int ch);
MODULE_SCOPE int	TclUCS4ToLower(int ch);
MODULE_SCOPE int	TclUCS4ToTitle(int ch);
#else
#define	TclUCS4ToUpper(ch) Tcl_UniCharToUpper((ch))
#define	TclUCS4ToLower(ch) Tcl_UniCharToLower((ch))
#define	TclUCS4ToTitle(ch) Tcl_UniCharToTitle((ch))
#endif

/*
 * Many parsing tasks need a common definition of whitespace.
 * Use this routine and macro to achieve that and place
 * optimization (fragile on changes) in one place.
 */

Changes to jni/tcl/generic/tclMain.c.

65
66
67
68
69
70
71
72

73
74

75
76
77
78
79
80
81
65
66
67
68
69
70
71

72
73

74
75
76
77
78
79
80
81







-
+

-
+








/*
 * Further on, in UNICODE mode we just use Tcl_NewUnicodeObj, otherwise
 * NewNativeObj is needed (which provides proper conversion from native
 * encoding to UTF-8).
 */

#if defined(UNICODE) && (TCL_UTF_MAX <= 4)
#if defined(UNICODE) && (TCL_UTF_MAX == 3)
#   define NewNativeObj Tcl_NewUnicodeObj
#else /* !UNICODE || (TCL_UTF_MAX > 4) */
#else /* !UNICODE || (TCL_UTF_MAX > 3) */
static inline Tcl_Obj *
NewNativeObj(
    TCHAR *string,
    int length)
{
    Tcl_DString ds;

Changes to jni/tcl/generic/tclParse.c.

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







-
+

















-
+






-
+







 *	Scans up to numBytes bytes starting at src, consuming a backslash
 *	sequence as defined by Tcl's parsing rules.
 *
 * Results:
 *	Records at readPtr the number of bytes making up the backslash
 *	sequence. Records at dst the UTF-8 encoded equivalent of that
 *	backslash sequence. Returns the number of bytes written to dst, at
 *	most TCL_UTF_MAX. Either readPtr or dst may be NULL, if the results
 *	most TCL_UTF_MAX*2. Either readPtr or dst may be NULL, if the results
 *	are not needed, but the return value is the same either way.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

int
TclParseBackslash(
    const char *src,		/* Points to the backslash character of a a
				 * backslash sequence. */
    int numBytes,		/* Max number of bytes to scan. */
    int *readPtr,		/* NULL, or points to storage where the number
				 * of bytes scanned should be written. */
    char *dst)			/* NULL, or points to buffer where the UTF-8
				 * encoding of the backslash sequence is to be
				 * written. At most TCL_UTF_MAX bytes will be
				 * written. At most TCL_UTF_MAX*2 bytes will be
				 * written there. */
{
    const char *p = src+1;
    Tcl_UniChar unichar = 0;
    int result;
    int count;
    char buf[TCL_UTF_MAX] = "";
    char buf[TCL_UTF_MAX];

    if (numBytes == 0) {
	if (readPtr != NULL) {
	    *readPtr = 0;
	}
	return 0;
    }
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
929
930
931
932
933
934
935

936
937
938





939
940
941
942
943
944
945







-



-
-
-
-
-







	     * No hexdigits -> This is just "U".
	     */
	    result = 'U';
	} else {
	    /*
	     * Check Unicode range.
	     */
#if TCL_UTF_MAX > 3
	    if ((result < 0) || (result > 0x10FFFF)) {
		result = 0xFFFD;
	    }
#else
	    if ((result < 0) || (result > 0xFFFF)) {
		result = 0xFFFD;
	    }
#endif
	}
	break;
    case '\n':
	count--;
	do {
	    p++;
	    count++;
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011






1012
1013

1014
1015
1016
1017
1018
1019
1020
994
995
996
997
998
999
1000





1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016







-
-
-
-
-
+
+
+
+
+
+


+







	break;
    }

  done:
    if (readPtr != NULL) {
	*readPtr = count;
    }
    count = Tcl_UniCharToUtf(result, dst);
#if TCL_UTF_MAX == 4
    if (!count) {
	/* Special case for handling high surrogates. */
	count = Tcl_UniCharToUtf(-1, dst);
    count = 0;
#if TCL_UTF_MAX == 3
    if ((result > 0xFFFF) && (result <= 0x10FFFF)) {
	result -= 0x10000;
	count += Tcl_UniCharToUtf((result >> 10) | 0xD800, dst);
	result = (result & 0x3FF) | 0xDC00;
    }
#endif
    count += Tcl_UniCharToUtf(result, dst + count);
    return count;
}

/*
 *----------------------------------------------------------------------
 *
 * ParseComment --
2222
2223
2224
2225
2226
2227
2228
2229

2230
2231
2232
2233
2234
2235
2236
2218
2219
2220
2221
2222
2223
2224

2225
2226
2227
2228
2229
2230
2231
2232







-
+








    adjust = 0;
    result = NULL;
    for (; count>0 && code==TCL_OK ; count--, tokenPtr++) {
	Tcl_Obj *appendObj = NULL;
	const char *append = NULL;
	int appendByteLength = 0;
	char utfCharBytes[TCL_UTF_MAX] = "";
	char utfCharBytes[TCL_UTF_MAX*2];

	switch (tokenPtr->type) {
	case TCL_TOKEN_TEXT:
	    append = tokenPtr->start;
	    appendByteLength = tokenPtr->size;
	    break;

2587
2588
2589
2590
2591
2592
2593
2594

2595
2596

2597
2598
2599
2600
2601
2602
2603
2604

2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619

2620
2621
2622
2623
2624
2625
2626
2583
2584
2585
2586
2587
2588
2589

2590
2591

2592
2593
2594
2595
2596
2597
2598
2599

2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614

2615
2616
2617
2618
2619
2620
2621
2622







-
+

-
+







-
+














-
+







TclCollapseSurrogatePair(
    Tcl_Token *tokenPtr,	/* Pointer to token to be checked. */
    int *numReadPtr,		/* Pointer to number of consumed input chars. */
    char *buffer)		/* Buffer holding UTF data of previous token. */
{
    int count, numRead;
    Tcl_UniChar ch = 0, ch2 = 0;
    char buffer2[TCL_UTF_MAX] = "";
    char buffer2[TCL_UTF_MAX];

    Tcl_UtfToUniChar(buffer, &ch);
    TclUtfToUniChar(buffer, &ch);
    if ((ch <= 0xFFFF) && ((ch & 0xFC00) == 0xD800)) {
	if (tokenPtr->type == TCL_TOKEN_BS) {
	    count = TclParseBackslash(tokenPtr->start, tokenPtr->size,
			    &numRead, buffer2);
	    if (count <= 0) {
		return 0;
	    }
	    Tcl_UtfToUniChar(buffer2, &ch2);
	    TclUtfToUniChar(buffer2, &ch2);
	    if ((ch2 <= 0xFFFF) && ((ch2 & 0xFC00) == 0xDC00)) {
		unsigned int uch = ((ch & 0x3FF) << 10) + (ch2 & 0x3FF);

		uch += 0x10000;
		if (numReadPtr != NULL) {
		    *numReadPtr = numRead;
		}
		return Tcl_UniCharToUtf(uch, buffer);
	    }
	}
    }
    return 0;
}
#endif


/*
 * Local Variables:
 * mode: c
 * c-basic-offset: 4
 * fill-column: 78
 * End:
 */

Changes to jni/tcl/generic/tclScan.c.

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







-
+













-
-
-




-
+




-
-
-
+
+
+

+
-
+
+
+








/*
 *----------------------------------------------------------------------
 *
 * UtfToUniChar --
 *
 *	Wrapper to Tcl_UtfToUniChar() capable of dealing with
 *	UCS4 when compiled with TCL_UTF_MAX == 4.
 *	surrogate pairs when compiled with TCL_UTF_MAX == 3.
 *
 * Results:
 *	*chPtr is filled with the full unicode character, and the
 *	return value is the number of bytes from the UTF-8 string that
 *	were consumed.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static int
#if TCL_UTF_MAX != 4
inline
#endif
UtfToUniChar(
    const char *src,
    int *chPtr)
{
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;
    int uch, len;

    len = TclUtfToUniChar(src, &ch);
    uch = ch;
#if TCL_UTF_MAX == 4
    if (!len) {
	len = TclUtfToUniChar(src + len, &ch);
#if TCL_UTF_MAX == 3
    if ((ch & 0xFC00) == 0xD800) {
	int len2 = TclUtfToUniChar(src + len, &ch);

	if (len2 && ((ch & 0xFC00) == 0xDC00)) {
	uch = ((uch & 0x3FF) << 10) + 0x10000 + (ch & 0x3FF);
	    uch = ((uch & 0x3FF) << 10) + 0x10000 + (ch & 0x3FF);
	    len += len2;
	}
    }
#endif
    *chPtr = uch;
    return len;
}

/*

Changes to jni/tcl/generic/tclStringObj.c.

755
756
757
758
759
760
761
762

763
764
765
766
767
768
769
755
756
757
758
759
760
761

762
763
764
765
766
767
768
769







-
+







    if (last < 0 || last >= stringPtr->numChars) {
	last = stringPtr->numChars - 1;
    }
    if (last < first) {
	TclNewObj(newObjPtr);
	return newObjPtr;
    }
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
    /* See: bug [11ae2be95dac9417] */
    if ((first > 0) && ((stringPtr->unicode[first] & 0xFC00) == 0xDC00)
	    && ((stringPtr->unicode[first-1] & 0xFC00) == 0xD800)) {
	++first;
    }
    if ((last + 1 < stringPtr->numChars)
	    && ((stringPtr->unicode[last+1] & 0xFC00) == 0xDC00)
2093
2094
2095
2096
2097
2098
2099
2100

2101
2102
2103
2104
2105
2106
2107
2108
2109
2110






2111
2112

2113
2114
2115
2116
2117
2118
2119
2093
2094
2095
2096
2097
2098
2099

2100
2101
2102
2103
2104
2105





2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121







-
+





-
-
-
-
-
+
+
+
+
+
+


+







		    numChars = precision;
		    Tcl_IncrRefCount(segment);
		    allocSegment = 1;
		}
	    }
	    break;
	case 'c': {
	    char buf[TCL_UTF_MAX] = "";
	    char buf[TCL_UTF_MAX*2];
	    int code, length;

	    if (TclGetIntFromObj(interp, segment, &code) != TCL_OK) {
		goto error;
	    }
	    length = Tcl_UniCharToUtf(code, buf);
#if TCL_UTF_MAX == 4
	    if (!length) {
		/* Special case for handling high surrogates. */
		length = Tcl_UniCharToUtf(-1, buf);
	    length = 0;
#if TCL_UTF_MAX == 3
	    if ((code > 0xFFFF) && (code <= 0x10FFFF)) {
		code -= 0x10000;
		length = Tcl_UniCharToUtf((code >> 10) | 0xD800, buf);
		code = (code & 0x3FF) | 0xDC00;
	    }
#endif
	    length += Tcl_UniCharToUtf(code, buf + length);
	    segment = Tcl_NewStringObj(buf, length);
	    Tcl_IncrRefCount(segment);
	    allocSegment = 1;
	    break;
	}

	case 'u':
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
2922
2923
2924
2925
2926
2927

2928
2929
2930
2931
2932
2933
2934
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
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
2922
2923
2924
2925
2926
2927
2928

2929
2930
2931
2932
2933
2934
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







+
+
+



















-
-
-











-
+














-
+




-
+








-
+








Tcl_Obj *
TclStringReverse(
    Tcl_Obj *objPtr)
{
    String *stringPtr;
    Tcl_UniChar ch = 0;
#if TCL_UTF_MAX == 3
    int needFlip = 0;
#endif

    if (TclIsPureByteArray(objPtr)) {
	int numBytes;
	unsigned char *from = Tcl_GetByteArrayFromObj(objPtr, &numBytes);

	if (Tcl_IsShared(objPtr)) {
	    objPtr = Tcl_NewByteArrayObj(NULL, numBytes);
	}
	ReverseBytes(Tcl_GetByteArrayFromObj(objPtr, NULL), from, numBytes);
	return objPtr;
    }

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

    if (stringPtr->hasUnicode) {
	Tcl_UniChar *from = Tcl_GetUnicode(objPtr);
	Tcl_UniChar *src = from + stringPtr->numChars;
	Tcl_UniChar *to;
#if TCL_UTF_MAX == 4
	int needFlip = 0;
#endif

	if (Tcl_IsShared(objPtr)) {
	    /*
	     * Create a non-empty, pure unicode value, so we can coax
	     * Tcl_SetObjLength into growing the unicode rep buffer.
	     */

	    objPtr = Tcl_NewUnicodeObj(&ch, 1);
	    Tcl_SetObjLength(objPtr, stringPtr->numChars);
	    to = Tcl_GetUnicode(objPtr);
	    while (--src >= from) {
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		ch = *src;
		if ((ch & 0xF800) == 0xD800) {
		    needFlip = 1;
		}
		*to++ = ch;
#else
		*to++ = *src;
#endif
	    }
	} else {
	    /*
	     * Reversing in place.
	     */

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    to = src;
#endif
	    while (--src > from) {
		ch = *src;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		if ((ch & 0xF800) == 0xD800) {
		    needFlip = 1;
		}
#endif
		*src = *from;
		*from++ = ch;
	    }
	}
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	if (needFlip) {
	    /*
	     * Flip back surrogate pairs. There might be a better way.
	     */

	    from = to - stringPtr->numChars;
	    while (--to >= from) {
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
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
3055
3056
3057
3058
3059
3060
3061







-
-
-
+
+
+















+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







		 * NOTE: We know that the from buffer is NUL-terminated. It's
		 * part of the contract for objPtr->bytes values. Thus, we can
		 * skip calling Tcl_UtfCharComplete() here.
		 */

		int bytesInChar = TclUtfToUniChar(from, &ch);

#if TCL_UTF_MAX == 4
		if (bytesInChar == 0) {
		    bytesInChar += TclUtfToUniChar(from, &ch);
#if TCL_UTF_MAX == 3
		if ((ch & 0xF800) == 0xD800) {
		    needFlip = 1;
		}
#endif
		ReverseBytes((unsigned char *)to, (unsigned char *)from,
			bytesInChar);
		to += bytesInChar;
		from += bytesInChar;
		bytesLeft -= bytesInChar;
		charCount++;
	    }

	    from = to = objPtr->bytes;
	    stringPtr->numChars = charCount;
	}
	/* Pass 2. Reverse all the bytes. */
	ReverseBytes((unsigned char *)to, (unsigned char *)from, numBytes);

#if TCL_UTF_MAX == 3
	if (needFlip) {
	    /* Pass 3. Flip back surrogate pairs. Might be a better way. */
	    numBytes = objPtr->length;
	    from = to = objPtr->bytes;
	    from += numBytes;	/* This is the end (The Doors, Jim Morrison) */
	    while (to < from) {
		Tcl_UniChar ch1, ch2;
		int len, len2;

		len = TclUtfToUniChar(to, &ch1);
	 	if (((ch1 & 0xFC00) == 0xDC00) &&
			(to + len + TCL_UTF_MAX <= from)) {
		    len2 = TclUtfToUniChar(to + len, &ch2);
		    if ((ch2 & 0xFC00) == 0xD800) {
			Tcl_UniCharToUtf(ch2, to);
			Tcl_UniCharToUtf(ch1, to + len);
			len += len2;
		    }
		}
		to += len;
	     }
	}
#endif
    }

    return objPtr;
}

/*
 *---------------------------------------------------------------------------
3063
3064
3065
3066
3067
3068
3069
3070

3071
3072
3073
3074
3075
3076
3077
3090
3091
3092
3093
3094
3095
3096

3097
3098
3099
3100
3101
3102
3103
3104







-
+







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

    if (stringPtr->hasUnicode) {
	numOrigChars = stringPtr->numChars;
    }
    if (numAppendChars == -1) {
	TclNumUtfChars(numAppendChars, bytes, numBytes);
    }
3086
3087
3088
3089
3090
3091
3092
3093

3094
3095
3096
3097
3098
3099
3100
3101
3113
3114
3115
3116
3117
3118
3119

3120

3121
3122
3123
3124
3125
3126
3127







-
+
-







    stringPtr->hasUnicode = 1;
    if (bytes) {
	stringPtr->numChars = needed;
    } else {
	numAppendChars = 0;
    }
    for (dst=stringPtr->unicode + numOrigChars; numAppendChars-- > 0; dst++) {
	bytes += TclUtfToUniChar(bytes, &unichar);
	bytes += TclUtfToUniChar(bytes, dst);
	*dst = unichar;
    }
    *dst = 0;
}

/*
 *----------------------------------------------------------------------
 *
3308
3309
3310
3311
3312
3313
3314
3315

3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3334
3335
3336
3337
3338
3339
3340

3341
3342



3343
3344
3345
3346
3347
3348
3349







-
+

-
-
-







    int numChars)
{
    /*
     * Precondition: this is the "string" Tcl_ObjType.
     */

    int i, origLength, size = 0;
    char *dst, buf[TCL_UTF_MAX] = "";
    char *dst, buf[TCL_UTF_MAX];
    String *stringPtr = GET_STRING(objPtr);
#if TCL_UTF_MAX == 4
    int length;
#endif

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

    if (numChars == 0) {
	return 0;
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3359
3360
3361
3362
3363
3364
3365











3366
3367
3368

3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382











3383
3384
3385

3386
3387
3388
3389
3390
3391
3392







-
-
-
-
-
-
-
-
-
-
-



-














-
-
-
-
-
-
-
-
-
-
-



-







     */

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

#if TCL_UTF_MAX == 4
    length = -1;
    for (i = 0; i < numChars && size >= 0; i++) {
	length = Tcl_UniCharToUtf((int) unicode[i], buf);
	size += (unsigned int) length;
    }
    if (!length) {
	/* Special case for handling high surrogates. */
	size += (unsigned int) Tcl_UniCharToUtf(-1, buf);
    }
#else
    for (i = 0; i < numChars && size >= 0; i++) {
	size += (unsigned int) Tcl_UniCharToUtf((int) unicode[i], buf);
    }
#endif
    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;
#if TCL_UTF_MAX == 4
    length = -1;
    for (i = 0; i < numChars; i++) {
	length = Tcl_UniCharToUtf(unicode[i], dst);
	dst += length;
    }
    if (!length) {
	/* Special case for handling high surrogates. */
	dst += Tcl_UniCharToUtf(-1, dst);
    }
#else
    for (i = 0; i < numChars; i++) {
	dst += Tcl_UniCharToUtf(unicode[i], dst);
    }
#endif
    *dst = '\0';
    objPtr->length = dst - objPtr->bytes;
    return numChars;
}

/*
 *----------------------------------------------------------------------

Changes to jni/tcl/generic/tclUniData.c.

190
191
192
193
194
195
196
197

198
199

200
201
202
203
204
205
206
190
191
192
193
194
195
196

197


198
199
200
201
202
203
204
205







-
+
-
-
+







    9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856,
    9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856, 9856,
    9856, 9856, 9856, 9856, 9856, 1344, 1344, 1344, 1344, 1344, 1344, 1344,
    1344, 1344, 1344, 1344, 9888, 1344, 1344, 9920, 3296, 9952, 9984, 10016,
    1344, 1344, 10048, 10080, 1344, 1344, 1344, 1344, 1344, 1344, 1344,
    1344, 1344, 1344, 10112, 10144, 1344, 10176, 1344, 10208, 10240, 10272,
    10304, 10336, 10368, 1344, 1344, 1344, 10400, 10432, 64, 10464, 10496,
    10528, 4736, 10560, 10592
    10528, 4736, 10560, 10592,
#if TCL_UTF_MAX > 3 || TCL_MAJOR_VERSION > 8 || TCL_MINOR_VERSION > 6
    ,10624, 10656, 10688, 3296, 1344, 1344, 1344, 10720, 10752, 10784,
    10624, 10656, 10688, 3296, 1344, 1344, 1344, 10720, 10752, 10784,
    10816, 10848, 10880, 10912, 8032, 10944, 3296, 3296, 3296, 3296, 9216,
    1344, 10976, 11008, 1344, 11040, 11072, 11104, 11136, 1344, 11168,
    3296, 11200, 11232, 11264, 1344, 11296, 11328, 11360, 11392, 1344,
    11424, 1344, 11456, 11488, 11520, 3296, 3296, 1344, 1344, 1344, 1344,
    1344, 1344, 1344, 1344, 1344, 7776, 4704, 11552, 11584, 11616, 3296,
    3296, 11648, 11680, 11712, 11744, 4736, 11776, 3296, 11808, 11840,
    11872, 3296, 3296, 1344, 11904, 11936, 6880, 11968, 12000, 12032, 12064,
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
564
565
566
567
568
569
570

571
572
573
574
575
576
577







-







    1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344,
    1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344,
    1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344,
    1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344,
    1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344,
    1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344, 1344,
    15488
#endif /* TCL_UTF_MAX > 3 */
};

/*
 * The groupMap is indexed by combining the alternate page number with
 * the page offset and returns a group number that identifies a unique
 * set of character attributes.
 */
1175
1176
1177
1178
1179
1180
1181
1182

1183
1184

1185
1186
1187
1188
1189
1190
1191
1173
1174
1175
1176
1177
1178
1179

1180


1181
1182
1183
1184
1185
1186
1187
1188







-
+
-
-
+







    13, 13, 13, 13, 13, 13, 13, 13, 13, 5, 7, 6, 7, 5, 6, 3, 5, 6, 3, 3,
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 92, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 92, 92, 0, 0, 15, 15, 15, 15, 15, 15, 0, 0, 15,
    15, 15, 15, 15, 15, 0, 0, 15, 15, 15, 15, 15, 15, 0, 0, 15, 15, 15,
    0, 0, 0, 4, 4, 7, 11, 14, 4, 4, 0, 14, 7, 7, 7, 7, 14, 14, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 17, 17, 17, 14, 14, 0, 0
    0, 0, 0, 0, 0, 0, 0, 17, 17, 17, 14, 14, 0, 0,
#if TCL_UTF_MAX > 3 || TCL_MAJOR_VERSION > 8 || TCL_MINOR_VERSION > 6
    ,15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 15, 0, 15, 15, 0, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 0, 0, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    15, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, 18, 18, 18, 18, 18, 18, 18,
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1645
1646
1647
1648
1649
1650
1651

1652
1653
1654
1655
1656
1657
1658







-







    14, 14, 0, 0, 0, 0, 0, 0, 0, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
    14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 14, 14, 14, 14, 14, 14, 14,
    14, 14, 14, 14, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 0, 0, 0, 0, 0, 0, 15, 15, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
    15, 15, 15, 15, 15, 15, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15
#endif /* TCL_UTF_MAX > 3 */
};

/*
 * Each group represents a unique set of character attributes.  The attributes
 * are encoded into a 32-bit value as follows:
 *
 * Bits 0-4	Character category: see the constants listed below.
1696
1697
1698
1699
1700
1701
1702
1703
1704

1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1692
1693
1694
1695
1696
1697
1698


1699



1700
1701
1702
1703
1704
1705
1706







-
-
+
-
-
-







    -2750143, -976319, -2746047, 2763650, 2762882, -2759615, -2751679,
    -2760383, -2760127, -2768575, 1859714, -9044927, -10823615, -12158,
    -10830783, -10833599, -10832575, -10830015, -10817983, -10824127,
    -10818751, 237633, -12223, -10830527, -9058239, 237698, 9949314,
    18, 17, 10305, 10370, 10049, 10114, 8769, 8834
};

#if TCL_UTF_MAX > 3 || TCL_MAJOR_VERSION > 8 || TCL_MINOR_VERSION > 6
#   define UNICODE_OUT_OF_RANGE(ch) (((ch) & 0x1FFFFF) >= 0x323C0)
#define UNICODE_OUT_OF_RANGE(ch) (((ch) & 0x1FFFFF) >= 0x323C0)
#else
#   define UNICODE_OUT_OF_RANGE(ch) (((ch) & 0x1F0000) != 0)
#endif

/*
 * The following constants are used to determine the category of a
 * Unicode character.
 */

enum {
1755
1756
1757
1758
1759
1760
1761
1762
1763

1764
1765
1766
1747
1748
1749
1750
1751
1752
1753


1754










-
-
+
-
-
-
#define GetDelta(info) ((info) >> 8)

/*
 * This macro extracts the information about a character from the
 * Unicode character tables.
 */

#if TCL_UTF_MAX > 3 || TCL_MAJOR_VERSION > 8 || TCL_MINOR_VERSION > 6
#   define GetUniCharInfo(ch) (groups[groupMap[pageMap[((ch) & 0x1FFFFF) >> OFFSET_BITS] | ((ch) & ((1 << OFFSET_BITS)-1))]])
#define GetUniCharInfo(ch) (groups[groupMap[pageMap[((ch) & 0x1FFFFF) >> OFFSET_BITS] | ((ch) & ((1 << OFFSET_BITS)-1))]])
#else
#   define GetUniCharInfo(ch) (groups[groupMap[pageMap[((ch) & 0xFFFF) >> OFFSET_BITS] | ((ch) & ((1 << OFFSET_BITS)-1))]])
#endif

Changes to jni/tcl/generic/tclUtf.c.

72
73
74
75
76
77
78

















79
80
81
82
83
84
85
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    4,4,4,4,4,
#else
    1,1,1,1,1,
#endif
    1,1,1,1,1,1,1,1,1,1,1
};

#if TCL_UTF_MAX == 3

static const unsigned char totalBytesExt[256] = {
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
    2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
    3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
    4,4,4,4,4,
    1,1,1,1,1,1,1,1,1,1,1
};

#endif

/*
 * Functions used only in this module.
 */

static int		UtfCount(int ch);
static int		Invalid(unsigned char *src);

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
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







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-














+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
+
+
+
+
-
+
-
+








+
+







    if (ch >= 0) {
	if (ch <= 0x7FF) {
	    buf[1] = (char) ((ch | 0x80) & 0xBF);
	    buf[0] = (char) ((ch >> 6) | 0xC0);
	    return 2;
	}
	if (ch <= 0xFFFF) {
#if TCL_UTF_MAX == 4
	    if ((ch & 0xF800) == 0xD800) {
		if (ch & 0x0400) {
		    /* Low surrogate */
		    if (((buf[0] & 0xF8) == 0xF0) && ((buf[1] & 0xC0) == 0x80)
			    && ((buf[2] & 0xCF) == 0)) {
			/* Previous Tcl_UniChar was a High surrogate, so combine */
			buf[3] = (char) ((ch & 0x3F) | 0x80);
			buf[2] |= (char) (((ch >> 6) & 0x0F) | 0x80);
			return 4;
		    }
		    /* Previous Tcl_UniChar was not a high surrogate, so just output */
		} else {
		    /* High surrogate */
		    ch += 0x40;
		    /* Fill buffer with specific 3-byte (invalid) byte combination,
		       so following low surrogate can recognize it and combine */
		    buf[2] = (char) ((ch << 4) & 0x30);
		    buf[1] = (char) (((ch >> 2) & 0x3F) | 0x80);
		    buf[0] = (char) (((ch >> 8) & 0x07) | 0xF0);
		    return 0;
		}
	    }
#endif
	    goto three;
	}

#if TCL_UTF_MAX > 3
	if (ch <= 0x10FFFF) {
	    buf[3] = (char) ((ch | 0x80) & 0xBF);
	    buf[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
	    buf[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
	    buf[0] = (char) ((ch >> 18) | 0xF0);
	    return 4;
	}
#endif
    }

    ch = 0xFFFD;
three:
    buf[2] = (char) ((ch | 0x80) & 0xBF);
    buf[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
    buf[0] = (char) ((ch >> 12) | 0xE0);
    return 3;
}

#if TCL_UTF_MAX == 4
    else if (ch == -1) {
	if (((buf[0] & 0xF8) == 0xF0) && ((buf[1] & 0xC0) == 0x80)
		&& ((buf[2] & 0xCF) == 0)) {
	    ch = 0xD7C0 + ((buf[0] & 0x07) << 8) + ((buf[1] & 0x3F) << 2)
		    + ((buf[2] & 0x30) >> 4);
#if TCL_UTF_MAX == 3

int
TclUniCharToUtfExt(
    int ch,			/* The Tcl_UniChar to be stored in the
				 * buffer. */
    char *buf)			/* Buffer in which the UTF-8 representation of
				 * the Tcl_UniChar is stored. Buffer must be
				 * large enough to hold the UTF-8 character
				 * (at most 4 bytes). */
{
    if ((unsigned)(ch - 1) < (UNICODE_SELF - 1)) {
	buf[0] = (char) ch;
	return 1;
    }
    if (ch >= 0) {
	if (ch <= 0x7FF) {
	    buf[1] = (char) ((ch | 0x80) & 0xBF);
	    buf[0] = (char) ((ch >> 6) | 0xC0);
	    return 2;
	}
	if (ch <= 0xFFFF) {
	    goto three;
	}
	if (ch <= 0x10FFFF) {
	    buf[3] = (char) ((ch | 0x80) & 0xBF);
	    buf[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
	    buf[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
	    buf[0] = (char) ((ch >> 18) | 0xF0);
	    return 4;
    }
	}
#endif
    }

    ch = 0xFFFD;
three:
    buf[2] = (char) ((ch | 0x80) & 0xBF);
    buf[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
    buf[0] = (char) ((ch >> 12) | 0xE0);
    return 3;
}

#endif

/*
 *---------------------------------------------------------------------------
 *
 * Tcl_UniCharToUtfDString --
 *
 *	Convert the given Unicode string to UTF-8.
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
353
354
355
356
357
358
359









360
361
362
363
364
365
366







-
-
-
-
-
-
-
-
-







 *	The caller must ensure that the source buffer is long enough that this
 *	routine does not run off the end and dereference non-existent memory
 *	looking for trail bytes. If the source buffer is known to be '\0'
 *	terminated, this cannot happen. Otherwise, the caller should call
 *	Tcl_UtfCharComplete() before calling this routine to ensure that
 *	enough bytes remain in the string.
 *
 *	If TCL_UTF_MAX == 4, special handling of Surrogate pairs is done:
 *	For any UTF-8 string containing a character outside of the BMP, the
 *	first call to this function will fill *chPtr with the high surrogate
 *	and generate a return value of 0. Calling Tcl_UtfToUniChar again
 *	will produce the low surrogate and a return value of 4. Because *chPtr
 *	is used to remember whether the high surrogate is already produced, it
 *	is recommended to initialize the variable it points to as 0 before
 *	the first call to Tcl_UtfToUniChar is done.
 *
 * Results:
 *	*chPtr is filled with the Tcl_UniChar, and the return value is the
 *	number of bytes from the UTF-8 string that were consumed.
 *
 * Side effects:
 *	None.
 *
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
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







-
-

-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
+
+



-












+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    }
#if TCL_UTF_MAX > 3
    else if (byte < 0xF8) {
	if (((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80) && ((src[3] & 0xC0) == 0x80)) {
	    /*
	     * Four-byte-character lead byte followed by three trail bytes.
	     */
#if TCL_UTF_MAX == 4
	    Tcl_UniChar surrogate;

	    byte = (((byte & 0x07) << 18) | ((src[1] & 0x3F) << 12)
		    | ((src[2] & 0x3F) << 6) | (src[3] & 0x3F)) - 0x10000;
	    surrogate = (Tcl_UniChar) (0xD800 + (byte >> 10));
	    if (byte & 0x100000) {
		/* out of range, < 0x10000 or > 0x10ffff */
	    } else if (*chPtr != surrogate) {
		/* produce high surrogate, but don't advance source pointer */
		*chPtr = surrogate;
		return 0;
	    } else {
		/* produce low surrogate, and advance source pointer */
		*chPtr = (Tcl_UniChar) (0xDC00 | (byte & 0x3FF));
	    *chPtr = (Tcl_UniChar) (((byte & 0x07) << 18)
		return 4;
	    }
#else
	    *chPtr = (Tcl_UniChar) (((byte & 0x07) << 18) | ((src[1] & 0x3F) << 12)
		    | ((src[2] & 0x3F) << 6) | (src[3] & 0x3F));
		    | ((src[1] & 0x3F) << 12)
		    | ((src[2] & 0x3F) << 6)
		    | (src[3] & 0x3F));
	    if ((unsigned)(*chPtr - 0x10000) <= 0xFFFFF) {
		return 4;
	    }
#endif
	}

	/*
	 * A four-byte-character lead-byte not followed by two trail-bytes
	 * represents itself.
	 */
    }
#endif

    *chPtr = (Tcl_UniChar) byte;
    return 1;
}

#if TCL_UTF_MAX == 3

int
TclUtfToUniCharExt(
    const char *src,	/* The UTF-8 string. */
    int *chPtr)		/* Filled with the Unicode represented by
			 * the UTF-8 string. */
{
    int byte;

    byte = *((unsigned char *) src);
    if (byte < 0xC0) {
	*chPtr = byte;
	return 1;
    } else if (byte < 0xE0) {
	if ((src[1] & 0xC0) == 0x80) {
	    *chPtr = ((byte & 0x1F) << 6) | (src[1] & 0x3F);
	    if ((unsigned)(*chPtr - 1) >= (UNICODE_SELF - 1)) {
		return 2;
	    }
	}
    } else if (byte < 0xF0) {
	if (((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80)) {
	    *chPtr = ((byte & 0x0F) << 12) | ((src[1] & 0x3F) << 6) |
		(src[2] & 0x3F);
	    if (*chPtr > 0x7FF) {
		return 3;
	    }
	}
    } else if (byte < 0xF8) {
	if (((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80) && ((src[3] & 0xC0) == 0x80)) {
	    *chPtr = ((byte & 0x0E) << 18) | ((src[1] & 0x3F) << 12)
		    | ((src[2] & 0x3F) << 6) | (src[3] & 0x3F);
	    if ((unsigned)(*chPtr - 0x10000) <= 0xFFFFF) {
		return 4;
	    }
	}
    }
    *chPtr =  byte;
    return 1;
}

#endif

/*
 *---------------------------------------------------------------------------
 *
 * Tcl_UtfToUniCharDString --
 *
 *	Convert the UTF-8 string to Unicode.
542
543
544
545
546
547
548
















549
550
551
552
553
554
555
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







Tcl_UtfCharComplete(
    const char *src,		/* String to check if first few bytes contain
				 * a complete UTF-8 character. */
    int length)			/* Length of above string in bytes. */
{
    return length >= totalBytes[UCHAR(*src)];
}

#if TCL_UTF_MAX == 3

int
TclUtfCharCompleteExt(
    const char *src,		/* String to check if first few bytes contain
				 * a complete UTF-8 character. */
    int length)			/* Length of above string in bytes. */
{
    int ch;

    ch = *((unsigned char *) src);
    return length >= totalBytesExt[ch];
}

#endif

/*
 *---------------------------------------------------------------------------
 *
 * Tcl_NumUtfChars --
 *
 *	Returns the number of characters (not bytes) in the UTF-8 string, not
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
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







-
-
-










-
-
-
-
-
-
-

-







-
-
-
-
-
-
-

-




-
-
-
-
-
-
-

-







Tcl_NumUtfChars(
    const char *src,		/* The UTF-8 string to measure. */
    int length)			/* The length of the string in bytes, or -1
				 * for strlen(string). */
{
    Tcl_UniChar ch = 0;
    int i = 0;
#if TCL_UTF_MAX == 4
    int ulen;
#endif

    /*
     * The separate implementations are faster.
     *
     * Since this is a time-sensitive function, we also do the check for the
     * single-byte char case specially.
     */

    if (length < 0) {
	while (*src != '\0') {
#if TCL_UTF_MAX == 4
	    ulen = TclUtfToUniChar(src, &ch);
	    src += ulen;
	    if (ulen) {
		ch = 0;
	    }
#else
	    src += TclUtfToUniChar(src, &ch);
#endif
	    i++;
	}
	if (i < 0) i = INT_MAX; /* Bug [2738427] */
    } else {
	const char *endPtr = src + length - TCL_UTF_MAX;

	while (src < endPtr) {
#if TCL_UTF_MAX == 4
	    ulen = TclUtfToUniChar(src, &ch);
	    src += ulen;
	    if (ulen) {
		ch = 0;
	    }
#else
	    src += TclUtfToUniChar(src, &ch);
#endif
	    i++;
	}
	endPtr += TCL_UTF_MAX;
	while ((src < endPtr) && Tcl_UtfCharComplete(src, endPtr - src)) {
#if TCL_UTF_MAX == 4
	    ulen = TclUtfToUniChar(src, &ch);
	    src += ulen;
	    if (ulen) {
		ch = 0;
	    }
#else
	    src += TclUtfToUniChar(src, &ch);
#endif
	    i++;
	}
	if (src < endPtr) {
	    i += endPtr - src;
	}
    }
    return i;
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
686
687
688
689
690
691
692


693
694
695
696
697








698
699
700
701
702
703
704
705







-
-
+
+



-
-
-
-
-
-
-
-
+







 */

const char *
Tcl_UtfFindFirst(
    const char *src,		/* The UTF-8 string to be searched. */
    int ch)			/* The Unicode character to search for. */
{
    int len, fullchar;
    Tcl_UniChar find = 0;
    int len;
    Tcl_UniChar find;

    while (1) {
	len = TclUtfToUniChar(src, &find);
	fullchar = find;
#if TCL_UTF_MAX == 4
	if (!len) {
	    len += TclUtfToUniChar(src, &find);
	    fullchar = (((fullchar & 0x3FF) << 10) | (find & 0x3FF)) + 0x10000;
	}
#endif
	if (fullchar == ch) {
	if (find == ch) {
	    return src;
	}
	if (*src == '\0') {
	    return NULL;
	}
	src += len;
    }
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
725
726
727
728
729
730
731


732
733
734
735
736
737
738








739
740
741
742
743
744
745
746







-
-
+
+





-
-
-
-
-
-
-
-
+







 */

const char *
Tcl_UtfFindLast(
    const char *src,		/* The UTF-8 string to be searched. */
    int ch)			/* The Unicode character to search for. */
{
    int len, fullchar;
    Tcl_UniChar find = 0;
    int len;
    Tcl_UniChar find;
    const char *last;

    last = NULL;
    while (1) {
	len = TclUtfToUniChar(src, &find);
	fullchar = find;
#if TCL_UTF_MAX == 4
	if (!len) {
	    len += TclUtfToUniChar(src, &find);
	    fullchar = (((fullchar & 0x3FF) << 10) | (find & 0x3FF)) + 0x10000;
	}
#endif
	if (fullchar == ch) {
	if (find == ch) {
	    last = src;
	}
	if (*src == '\0') {
	    break;
	}
	src += len;
    }
943
944
945
946
947
948
949

950
951
952
953
954
955
956
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976







+







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

    while (index-- > 0) {
	int len = TclUtfToUniChar(src, &ch);

	src += len;
    }
    return src;
}

/*
 *---------------------------------------------------------------------------
986
987
988
989
990
991
992

993
994

995
996
997
998
999
1000

1001
1002
1003
1004




1005
1006
1007
1008
1009
1010
1011
1006
1007
1008
1009
1010
1011
1012
1013
1014

1015
1016
1017
1018
1019
1020

1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036







+

-
+





-
+




+
+
+
+







				 * src, unless NULL. */
    char *dst)			/* Filled with the bytes represented by the
				 * backslash sequence. */
{
#define LINE_LENGTH 128
    int numRead;
    int result;
    char buffer[TCL_UTF_MAX*2];

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

	result = TclParseBackslash(src, strlen(src), &numRead, dst);
	result = TclParseBackslash(src, strlen(src), &numRead, buffer);
    }
    if (readPtr != NULL) {
	*readPtr = numRead;
    }
    if (result > TCL_UTF_MAX) {
	result = TCL_UTF_MAX;
    }
    memcpy(dst, buffer, result);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UtfToUpper --
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
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066



1067
1068



1069

1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1048
1049
1050
1051
1052
1053
1054

1055
1056
1057



1058
1059
1060
1061
1062
1063
1064
1065

1066
1067
1068
1069



1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089



1090
1091
1092


1093
1094
1095
1096
1097
1098
1099
1100





1101
1102
1103
1104
1105
1106
1107







-
+


-
-
-








-
+

+
+
-
-
-
+
+
+
+
+















-
-
-
+
+
+
-
-
+
+
+

+



-
-
-
-
-







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

int
Tcl_UtfToUpper(
    char *str)			/* String to convert in place. */
{
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;
    char *src, *dst;
    int len, upChar;
#if TCL_UTF_MAX == 4
    int ulen = -1;
#endif

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

    src = dst = str;
    while (*src) {
	len = TclUtfToUniChar(src, &ch);
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	upChar = ch;
	if ((ch & 0xFC00) == 0xD800 && src[len] != '\0') {
	    int len2 = TclUtfToUniChar(src + len, &ch);
	if (!len) {
	    len += TclUtfToUniChar(src, &ch);
	    upChar = (((upChar & 0x3FF) << 10) | (ch & 0x3FF)) + 0x10000;

	    if ((ch & 0xFC00) == 0xDC00) {
		len += len2;
		upChar = (((upChar&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
	    }
	}
	upChar = TclUCS4ToUpper(upChar);
#else
	upChar = Tcl_UniCharToUpper(ch);
#endif
	/*
	 * To keep badly formed Utf strings from getting inflated by the
	 * conversion (thereby causing a segfault), only copy the upper case
	 * char to dst if its size is <= the original char.
	 */

	if (len < UtfCount(upChar)) {
	    memmove(dst, src, len);
	    dst += len;
	} else {
#if TCL_UTF_MAX == 4
	    ulen = Tcl_UniCharToUtf(upChar, dst);
	    dst += ulen;
#if TCL_UTF_MAX == 3
	    if (upChar > 0xFFFF) {
		upChar -= 0x10000;
#else
	    dst += Tcl_UniCharToUtf(upChar, dst);
		dst += Tcl_UniCharToUtf((upChar >> 10) | 0xD800, dst);
		upChar = (upChar & 0x3FF) | 0xDC00;
	    }
#endif
	    dst += Tcl_UniCharToUtf(upChar, dst);
	}
	src += len;
    }
#if TCL_UTF_MAX == 4
    if (!ulen) {
	dst += Tcl_UniCharToUtf(-1, dst);
    }
#endif
    *dst = '\0';
    return (dst - str);
}

/*
 *----------------------------------------------------------------------
 *
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
1136
1137
1138
1139
1140
1141



1142
1143



1144

1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1120
1121
1122
1123
1124
1125
1126

1127
1128
1129



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
1158
1159
1160
1161
1162



1163
1164
1165


1166
1167
1168
1169
1170
1171
1172
1173





1174
1175
1176
1177
1178
1179
1180







-
+


-
-
-








-
+

+
+
-
-
-
+
+
+
+
+
















-
-
-
+
+
+
-
-
+
+
+

+



-
-
-
-
-







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

int
Tcl_UtfToLower(
    char *str)			/* String to convert in place. */
{
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;
    char *src, *dst;
    int len, lowChar;
#if TCL_UTF_MAX == 4
    int ulen = -1;
#endif

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

    src = dst = str;
    while (*src) {
	len = TclUtfToUniChar(src, &ch);
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	lowChar = ch;
	if ((ch & 0xFC00) == 0xD800 && src[len] != '\0') {
	    int len2 = TclUtfToUniChar(src + len, &ch);
	if (!len) {
	    len += TclUtfToUniChar(src, &ch);
	    lowChar = (((lowChar & 0x3FF) << 10) | (ch & 0x3FF)) + 0x10000;

	    if ((ch & 0xFC00) == 0xDC00) {
		len += len2;
		lowChar = (((lowChar&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
	    }
	}
	lowChar = TclUCS4ToLower(lowChar);
#else
	lowChar = Tcl_UniCharToLower(ch);
#endif

	/*
	 * To keep badly formed Utf strings from getting inflated by the
	 * conversion (thereby causing a segfault), only copy the lower case
	 * char to dst if its size is <= the original char.
	 */

	if (len < UtfCount(lowChar)) {
	    memmove(dst, src, len);
	    dst += len;
	} else {
#if TCL_UTF_MAX == 4
	    ulen = Tcl_UniCharToUtf(lowChar, dst);
	    dst += ulen;
#if TCL_UTF_MAX == 3
	    if (lowChar > 0xFFFF) {
		lowChar -= 0x10000;
#else
	    dst += Tcl_UniCharToUtf(lowChar, dst);
		dst += Tcl_UniCharToUtf((lowChar >> 10) | 0xD800, dst);
		lowChar = (lowChar & 0x3FF) | 0xDC00;
	    }
#endif
	    dst += Tcl_UniCharToUtf(lowChar, dst);
	}
	src += len;
    }
#if TCL_UTF_MAX == 4
    if (!ulen) {
	dst += Tcl_UniCharToUtf(-1, dst);
    }
#endif
    *dst = '\0';
    return (dst - str);
}

/*
 *----------------------------------------------------------------------
 *
1173
1174
1175
1176
1177
1178
1179
1180

1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196

1197


1198
1199
1200





1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213



1214
1215



1216

1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
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
1194
1195
1196
1197
1198
1199
1200

1201
1202
1203



1204
1205
1206
1207
1208
1209
1210
1211
1212
1213

1214
1215
1216
1217



1218
1219
1220
1221
1222
1223
1224
1225
1226
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
1275
1276
1277
1278
1279
1280
1281





1282
1283
1284
1285
1286
1287
1288







-
+


-
-
-










-
+

+
+
-
-
-
+
+
+
+
+










-
-
-
+
+
+
-
-
+
+
+

+



-
-
-
-
-
-



-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+




+









-
-
-
+
+
+
-
-
+
+
+

+



-
-
-
-
-







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

int
Tcl_UtfToTitle(
    char *str)			/* String to convert in place. */
{
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;
    char *src, *dst;
    int len, titleChar, lowChar;
#if TCL_UTF_MAX == 4
    int ulen = -1;
#endif

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

    src = dst = str;

    if (*src) {
	len = TclUtfToUniChar(src, &ch);
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	titleChar = ch;
	if ((ch & 0xFC00) == 0xD800 && src[len] != '\0') {
	    int len2 = TclUtfToUniChar(src + len, &ch);
	if (!len) {
	    len += TclUtfToUniChar(src, &ch);
	    titleChar = (((titleChar & 0x3FF) << 10) | (ch & 0x3FF)) + 0x10000;

	    if ((ch & 0xFC00) == 0xDC00) {
		len += len2;
		titleChar = (((titleChar&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
	    }
	}
	titleChar = TclUCS4ToTitle(titleChar);
#else
	titleChar = Tcl_UniCharToTitle(ch);
#endif

	if (len < UtfCount(titleChar)) {
	    memmove(dst, src, len);
	    dst += len;
	} else {
#if TCL_UTF_MAX == 4
	    ulen = Tcl_UniCharToUtf(titleChar, dst);
	    dst += ulen;
#if TCL_UTF_MAX == 3
	    if (titleChar > 0xFFFF) {
		titleChar -= 0x10000;
#else
	    dst += Tcl_UniCharToUtf(titleChar, dst);
		dst += Tcl_UniCharToUtf((titleChar >> 10) | 0xD800, dst);
		titleChar = (titleChar & 0x3FF) | 0xDC00;
	    }
#endif
	    dst += Tcl_UniCharToUtf(titleChar, dst);
	}
	src += len;
    }
#if TCL_UTF_MAX == 4
    if (!ulen) {
	dst += Tcl_UniCharToUtf(-1, dst);
	ulen = -1;
    }
#endif
    while (*src) {
	len = TclUtfToUniChar(src, &ch);
	lowChar = ch;
	/* Special exception for Georgian Asomtavruli chars, no titlecase. */
#if TCL_UTF_MAX == 4
	if (!len) {
	    len += TclUtfToUniChar(src, &ch);
	    lowChar = (((lowChar & 0x3FF) << 10) | (ch & 0x3FF)) + 0x10000;
	}
#if TCL_UTF_MAX == 3
	if ((ch & 0xFC00) == 0xD800 && src[len] != '\0') {
	    int len2 = TclUtfToUniChar(src + len, &ch);

	    if ((ch & 0xFC00) == 0xDC00) {
		len += len2;
		lowChar = (((lowChar&0x3FF)<<10) | (ch&0x3FF)) + 0x10000;
	    }
	}
	/* Special exception for Georgian Asomtavruli chars, no titlecase. */
	if ((unsigned)(lowChar - 0x1C90) >= 0x30) {
	    lowChar = TclUCS4ToLower(lowChar);
	}
#else
	/* Special exception for Georgian Asomtavruli chars, no titlecase. */
	if ((unsigned)(lowChar - 0x1C90) >= 0x30) {
	    lowChar = Tcl_UniCharToLower(lowChar);
	}
#endif

	if (len < UtfCount(lowChar)) {
	    memmove(dst, src, len);
	    dst += len;
	} else {
#if TCL_UTF_MAX == 4
	    ulen = Tcl_UniCharToUtf(lowChar, dst);
	    dst += ulen;
#if TCL_UTF_MAX == 3
	    if (lowChar > 0xFFFF) {
		lowChar -= 0x10000;
#else
	    dst += Tcl_UniCharToUtf(lowChar, dst);
		dst += Tcl_UniCharToUtf((lowChar >> 10) | 0xD800, dst);
		lowChar = (lowChar & 0x3FF) | 0xDC00;
	    }
#endif
	    dst += Tcl_UniCharToUtf(lowChar, dst);
	}
	src += len;
    }
#if TCL_UTF_MAX == 4
    if (!ulen) {
	dst += Tcl_UniCharToUtf(-1, dst);
    }
#endif
    *dst = '\0';
    return (dst - str);
}

/*
 *----------------------------------------------------------------------
 *
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
1376
1377
1378
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
1376
1377
1378
1379
1380

1381
1382
1383
1384
1385
1386



1387
1388
1389





1390
1391
1392
1393
1394
1395


1396
1397



1398
1399
1400


1401
1402
1403
1404
1405
1406

1407
1408
1409
1410
1411
1412
1413







-
-
+
+
+
+
+







+
+
+
+
-
+
+
+






-
+
+
+

+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
-
-
-
+

+
-
-
+
+
+
+
+

-








int
Tcl_UtfNcmp(
    const char *cs,		/* UTF string to compare to ct. */
    const char *ct,		/* UTF string cs is compared to. */
    unsigned long numChars)	/* Number of UTF chars to compare. */
{
    Tcl_UniChar ch1 = 0, ch2 = 0;
    int uch1, uch2, len;
    Tcl_UniChar ch1, ch2;
    int uch1, uch2;
#if TCL_UTF_MAX == 3
    int num1 = numChars, num2 = numChars;
#endif

    /*
     * Cannot use 'memcmp(cs, ct, n);' as byte representation of \u0000 (the
     * pair of bytes 0xC0,0x80) is larger than byte representation of \u0001
     * (the byte 0x01.)
     */

    while (
#if TCL_UTF_MAX == 3
	(num1-- > 0) && (num2 > 0)
#else
    while (numChars-- > 0) {
	numChars-- > 0
#endif
    ) {
	/*
	 * n must be interpreted as chars, not bytes. This should be called
	 * only when both strings are of at least n chars long (no need for \0
	 * check)
	 */

	len = TclUtfToUniChar(cs, &ch1);
	cs += TclUtfToUniChar(cs, &ch1);
	ct += TclUtfToUniChar(ct, &ch2);

	uch1 = ch1;
	uch2 = ch2;

#if TCL_UTF_MAX == 4
	if (!len) {
	    len = TclUtfToUniChar(cs, &ch1);
#if TCL_UTF_MAX == 3
	if ((num1 > 0) && ((ch1 & 0xFC00) == 0xD800)) {
	    int len = TclUtfToUniChar(cs, &ch1);
	    uch1 = (((uch1&0x3FF)<<10) | (ch1&0x3FF)) + 0x10000;
	}
#endif
	cs += len;


	    if ((ch1 & 0xFC00) == 0xDC00) {
		uch1 = (((uch1&0x3FF)<<10) | (ch1&0x3FF)) + 0x10000;
		cs += len;
		num1--;
	    }
	len = TclUtfToUniChar(ct, &ch2);
	uch2 = ch2;
	}
	if ((num2 > 0) && ((ch2 & 0xFC00) == 0xD800)) {
#if TCL_UTF_MAX == 4
	if (!len) {
	    len = TclUtfToUniChar(ct, &ch2);
	    int len = TclUtfToUniChar(ct, &ch2);

	    if ((ch2 & 0xFC00) == 0xDC00) {
	    uch2 = (((uch2&0x3FF)<<10) | (ch2&0x3FF)) + 0x10000;
	}
		uch2 = (((uch2&0x3FF)<<10) | (ch2&0x3FF)) + 0x10000;
		ct += len;
		num2--;
	    }
	}
#endif
	ct += len;

	if (uch1 != uch2) {
	    return (uch1 - uch2);
	}

    }
    return 0;
1398
1399
1400
1401
1402
1403
1404
1405
1406





1407
1408
1409
1410
1411
1412
1413




1414




1415
1416
1417
1418



1419


1420
1421
1422


1423
1424
1425



1426
1427
1428
1429
1430






1431
1432


1433
1434
1435

1436

1437
1438





1439
1440
1441
1442
1443
1444
1445
1446
1447
1433
1434
1435
1436
1437
1438
1439


1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455

1456
1457
1458
1459
1460



1461
1462
1463
1464
1465
1466
1467

1468
1469
1470



1471
1472
1473





1474
1475
1476
1477
1478
1479


1480
1481



1482
1483
1484


1485
1486
1487
1488
1489
1490

1491
1492
1493
1494
1495
1496
1497







-
-
+
+
+
+
+







+
+
+
+
-
+
+
+
+

-
-
-
+
+
+

+
+

-

+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
-
-
-
+

+
-
-
+
+
+
+
+

-








int
Tcl_UtfNcasecmp(
    const char *cs,		/* UTF string to compare to ct. */
    const char *ct,		/* UTF string cs is compared to. */
    unsigned long numChars)	/* Number of UTF chars to compare. */
{
    Tcl_UniChar ch1 = 0, ch2 = 0;
    int uch1, uch2, len;
    Tcl_UniChar ch1, ch2;
    int uch1, uch2;
#if TCL_UTF_MAX == 3
    int num1 = numChars, num2 = numChars;
#endif

    /*
     * Cannot use 'memcmp(cs, ct, n);' as byte representation of \u0000 (the
     * pair of bytes 0xC0,0x80) is larger than byte representation of \u0001
     * (the byte 0x01.)
     */

    while (
#if TCL_UTF_MAX == 3
	(num1-- > 0) && (num2-- > 0)
#else
    while (numChars-- > 0) {
	numChars-- > 0
#endif
    ) {
	
	/*
	 * n must be interpreted as chars, not bytes. This should be called
	 * only when both strings are of at least n chars long (no need for \0
	 * check)
	 * n must be interpreted as chars, not bytes.
	 * This should be called only when both strings are of
	 * at least n chars long (no need for \0 check)
	 */
	cs += TclUtfToUniChar(cs, &ch1);
	ct += TclUtfToUniChar(ct, &ch2);

	len = TclUtfToUniChar(cs, &ch1);
	uch1 = ch1;
	uch2 = ch2;

#if TCL_UTF_MAX == 4
	if (!len) {
	    len = TclUtfToUniChar(cs, &ch1);
#if TCL_UTF_MAX == 3
	if ((num1 > 0) && ((ch1 & 0xFC00) == 0xD800)) {
	    int len = TclUtfToUniChar(cs, &ch1);
	    uch1 = (((uch1&0x3FF)<<10) | (ch1&0x3FF)) + 0x10000;
	}
#endif
	cs += len;


	    if ((ch1 & 0xFC00) == 0xDC00) {
		uch1 = (((uch1&0x3FF)<<10) | (ch1&0x3FF)) + 0x10000;
		cs += len;
		num1--;
	    }
	len = TclUtfToUniChar(ct, &ch2);
	uch2 = ch2;
	}
	if ((num2 > 0) && ((ch2 & 0xFC00) == 0xD800)) {
#if TCL_UTF_MAX == 4
	if (!len) {
	    len = TclUtfToUniChar(ct, &ch2);
	    int len = TclUtfToUniChar(ct, &ch2);

	    if ((ch2 & 0xFC00) == 0xDC00) {
	    uch2 = (((uch2&0x3FF)<<10) | (ch2&0x3FF)) + 0x10000;
	}
		uch2 = (((uch2&0x3FF)<<10) | (ch2&0x3FF)) + 0x10000;
		ct += len;
		num2--;
	    }
	}
#endif
	ct += len;

	if (uch1 != uch2) {
	    uch1 = TclUCS4ToLower(uch1);
	    uch2 = TclUCS4ToLower(uch2);
	    if (uch1 != uch2) {
		return (uch1 - uch2);
	    }
1469
1470
1471
1472
1473
1474
1475
1476
1477


1478

1479
1480



1481


1482
1483
1484



1485
1486
1487
1488
1489





1490
1491


1492
1493
1494

1495

1496
1497




1498
1499
1500
1501

1502
1503




1504
1505

1506
1507
1508
1509
1510
1511
1512
1519
1520
1521
1522
1523
1524
1525


1526
1527
1528
1529
1530

1531
1532
1533
1534
1535
1536



1537
1538
1539





1540
1541
1542
1543
1544


1545
1546



1547
1548
1549


1550
1551
1552
1553
1554

1555
1556
1557
1558
1559
1560
1561
1562
1563
1564

1565
1566
1567
1568
1569
1570
1571
1572







-
-
+
+

+

-
+
+
+

+
+
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
+
+
-
-
-
+

+
-
-
+
+
+
+

-


+


+
+
+
+

-
+







 */

int
TclUtfCasecmp(
    const char *cs,		/* UTF string to compare to ct. */
    const char *ct)		/* UTF string cs is compared to. */
{
    Tcl_UniChar ch1 = 0, ch2 = 0;
    int uch1, uch2, len;
    Tcl_UniChar ch1, ch2;
    int uch1, uch2;


    while (*cs && *ct) {
	len = TclUtfToUniChar(cs, &ch1);
	cs += TclUtfToUniChar(cs, &ch1);
	ct += TclUtfToUniChar(ct, &ch2);

	uch1 = ch1;
	uch2 = ch2;

#if TCL_UTF_MAX == 4
	if (!len) {
	    len = TclUtfToUniChar(cs, &ch1);
#if TCL_UTF_MAX == 3
	if (*cs && ((ch1 & 0xFC00) == 0xD800)) {
	    int len = TclUtfToUniChar(cs, &ch1);
	    uch1 = (((uch1&0x3FF)<<10) | (ch1&0x3FF)) + 0x10000;
	}
#endif
	cs += len;


	    if ((ch1 & 0xFC00) == 0xDC00) {
		uch1 = (((uch1&0x3FF)<<10) | (ch1&0x3FF)) + 0x10000;
		cs += len;
	    }
	len = TclUtfToUniChar(ct, &ch2);
	uch2 = ch2;
	}
	if (*ct && ((ch2 & 0xFC00) == 0xD800)) {
#if TCL_UTF_MAX == 4
	if (!len) {
	    len = TclUtfToUniChar(ct, &ch2);
	    int len = TclUtfToUniChar(ct, &ch2);

	    if ((ch2 & 0xFC00) == 0xDC00) {
	    uch2 = (((uch2&0x3FF)<<10) | (ch2&0x3FF)) + 0x10000;
	}
		uch2 = (((uch2&0x3FF)<<10) | (ch2&0x3FF)) + 0x10000;
		ct += len;
	    }
	}
#endif
	ct += len;

	if (uch1 != uch2) {
#if TCL_UTF_MAX == 3
	    uch1 = TclUCS4ToLower(uch1);
	    uch2 = TclUCS4ToLower(uch2);
#else
	    uch1 = Tcl_UniCharToLower(uch1);
	    uch2 = Tcl_UniCharToLower(uch2);
#endif
	    if (uch1 != uch2) {
		return (uch1 - uch2);
		return uch1 - uch2;
	    }
	}
    }
    return UCHAR(*cs) - UCHAR(*ct);
}

/*
1521
1522
1523
1524
1525
1526
1527
1528

1529
1530
1531
1532
1533
1534
1535
1581
1582
1583
1584
1585
1586
1587

1588
1589
1590
1591
1592
1593
1594
1595







-
+







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

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
int
TclUCS4ToUpper(
    int ch)			/* Unicode character to convert. */
{
    if (!UNICODE_OUT_OF_RANGE(ch)) {
	int info = GetUniCharInfo(ch);

1568
1569
1570
1571
1572
1573
1574
1575

1576
1577
1578
1579
1580
1581
1582
1628
1629
1630
1631
1632
1633
1634

1635
1636
1637
1638
1639
1640
1641
1642







-
+







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

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
int
TclUCS4ToLower(
    int ch)			/* Unicode character to convert. */
{
    if (!UNICODE_OUT_OF_RANGE(ch)) {
	int info = GetUniCharInfo(ch);
	int mode = GetCaseType(info);
1617
1618
1619
1620
1621
1622
1623
1624

1625
1626
1627
1628
1629
1630
1631
1677
1678
1679
1680
1681
1682
1683

1684
1685
1686
1687
1688
1689
1690
1691







-
+







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

#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
int
TclUCS4ToTitle(
    int ch)			/* Unicode character to convert. */
{
    if (!UNICODE_OUT_OF_RANGE(ch)) {
	int info = GetUniCharInfo(ch);
	int mode = GetCaseType(info);
1719
1720
1721
1722
1723
1724
1725
1726

1727
1728
1729
1730
1731
1732
1733
1779
1780
1781
1782
1783
1784
1785

1786
1787
1788
1789
1790
1791
1792
1793







-
+








int
Tcl_UniCharNcmp(
    const Tcl_UniChar *ucs,	/* Unicode string to compare to uct. */
    const Tcl_UniChar *uct,	/* Unicode string ucs is compared to. */
    unsigned long numChars)	/* Number of unichars to compare. */
{
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
    int lcs, lct, nums = numChars, numt = numChars;

    for ( ; nums != 0 && numt != 0; nums--, numt--, ucs++, uct++) {
	lcs = *ucs;
	lct = *uct;
	if ((nums > 1) && ((lcs & 0xFC00) == 0xD800)) {
	    if ((ucs[1] & 0xFC00) == 0xDC00) {
1764
1765
1766
1767
1768
1769
1770
1771

1772
1773
1774
1775
1776
1777
1778
1824
1825
1826
1827
1828
1829
1830

1831
1832
1833
1834
1835
1836
1837
1838







-
+







    for ( ; numChars != 0; ucs++, uct++, numChars--) {
	if (*ucs != *uct) {
	    return (*ucs - *uct);
	}
    }
    return 0;
#endif /* WORDS_BIGENDIAN */
#endif /* TCL_UTF_MAX == 4 */
#endif /* TCL_UTF_MAX == 3 */
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharNcasecmp --
 *
1791
1792
1793
1794
1795
1796
1797
1798

1799
1800
1801
1802
1803
1804
1805
1851
1852
1853
1854
1855
1856
1857

1858
1859
1860
1861
1862
1863
1864
1865







-
+








int
Tcl_UniCharNcasecmp(
    const Tcl_UniChar *ucs,	/* Unicode string to compare to uct. */
    const Tcl_UniChar *uct,	/* Unicode string ucs is compared to. */
    unsigned long numChars)	/* Number of unichars to compare. */
{
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
    int lcs, lct, nums = numChars, numt = numChars;

    for ( ; nums != 0 && numt != 0; nums--, numt--, ucs++, uct++) {
	lcs = *ucs;
	lct = *uct;
	if ((nums > 1) && ((lcs & 0xFC00) == 0xD800)) {
	    if ((ucs[1] & 0xFC00) == 0xDC00) {
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1914
1915
1916
1917
1918
1919
1920

1921
1922
1923

1924
1925
1926
1927
1928
1929
1930







-



-







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

int
Tcl_UniCharIsAlnum(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return 0;
    }
#endif
    return (((ALPHA_BITS | DIGIT_BITS) >> GetCategory(ch)) & 1);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsAlpha --
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1940
1941
1942
1943
1944
1945
1946

1947
1948
1949

1950
1951
1952
1953
1954
1955
1956







-



-







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

int
Tcl_UniCharIsAlpha(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return 0;
    }
#endif
    return ((ALPHA_BITS >> GetCategory(ch)) & 1);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsControl --
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1966
1967
1968
1969
1970
1971
1972

1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983

1984
1985
1986
1987
1988
1989
1990







-











-







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

int
Tcl_UniCharIsControl(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	/* Clear away extension bits, if any */
	ch &= 0x1FFFFF;
	if ((ch == 0xE0001) || ((ch >= 0xE0020) && (ch <= 0xE007F))) {
	    return 1;
	}
	if ((ch >= 0xF0000) && ((ch & 0xFFFF) <= 0xFFFD)) {
	    return 1;
	}
	return 0;
    }
#endif
    return ((CONTROL_BITS >> GetCategory(ch)) & 1);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsDigit --
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
2000
2001
2002
2003
2004
2005
2006

2007
2008
2009

2010
2011
2012
2013
2014
2015
2016







-



-







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

int
Tcl_UniCharIsDigit(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return 0;
    }
#endif
    return (GetCategory(ch) == DECIMAL_DIGIT_NUMBER);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsGraph --
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
2026
2027
2028
2029
2030
2031
2032

2033
2034
2035

2036
2037
2038
2039
2040
2041
2042







-



-







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

int
Tcl_UniCharIsGraph(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return ((unsigned)((ch & 0x1FFFFF) - 0xE0100) <= 0xEF);
    }
#endif
    return ((GRAPH_BITS >> GetCategory(ch)) & 1);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsLower --
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2052
2053
2054
2055
2056
2057
2058

2059
2060
2061

2062
2063
2064
2065
2066
2067
2068







-



-







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

int
Tcl_UniCharIsLower(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return 0;
    }
#endif
    return (GetCategory(ch) == LOWERCASE_LETTER);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsPrint --
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2078
2079
2080
2081
2082
2083
2084

2085
2086
2087

2088
2089
2090
2091
2092
2093
2094







-



-







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

int
Tcl_UniCharIsPrint(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return ((unsigned)((ch & 0x1FFFFF) - 0xE0100) <= 0xEF);
    }
#endif
    return (((GRAPH_BITS|SPACE_BITS) >> GetCategory(ch)) & 1);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsPunct --
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2104
2105
2106
2107
2108
2109
2110

2111
2112
2113

2114
2115
2116
2117
2118
2119
2120







-



-







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

int
Tcl_UniCharIsPunct(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return 0;
    }
#endif
    return ((PUNCT_BITS >> GetCategory(ch)) & 1);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsSpace --
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2130
2131
2132
2133
2134
2135
2136

2137
2138




2139
2140
2141
2142
2143
2144
2145
2146

2147
2148

2149
2150
2151
2152
2153
2154
2155







-


-
-
-
-








-


-







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

int
Tcl_UniCharIsSpace(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    /* Ignore upper 11 bits. */
    ch &= 0x1FFFFF;
#else
    /* Ignore upper 16 bits. */
    ch &= 0xFFFF;
#endif

    /*
     * If the character is within the first 127 characters, just use the
     * standard C function, otherwise consult the Unicode table.
     */

    if (ch < 0x80) {
	return TclIsSpaceProcM((char) ch);
#if TCL_UTF_MAX > 3
    } else if (UNICODE_OUT_OF_RANGE(ch)) {
	return 0;
#endif
    } else if (ch == 0x0085 || ch == 0x180E || ch == 0x200B
	    || ch == 0x202F || ch == 0x2060 || ch == 0xFEFF) {
	return 1;
    } else {
	return ((SPACE_BITS >> GetCategory(ch)) & 1);
    }
}
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2170
2171
2172
2173
2174
2175
2176

2177
2178
2179

2180
2181
2182
2183
2184
2185
2186







-



-







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

int
Tcl_UniCharIsUpper(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return 0;
    }
#endif
    return (GetCategory(ch) == UPPERCASE_LETTER);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharIsWordChar --
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2196
2197
2198
2199
2200
2201
2202

2203
2204
2205

2206
2207
2208
2209
2210
2211
2212







-



-







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

int
Tcl_UniCharIsWordChar(
    int ch)			/* Unicode character to test. */
{
#if TCL_UTF_MAX > 3
    if (UNICODE_OUT_OF_RANGE(ch)) {
	return 0;
    }
#endif
    return ((WORD_BITS >> GetCategory(ch)) & 1);
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_UniCharCaseMatch --
2200
2201
2202
2203
2204
2205
2206
2207

2208
2209
2210
2211
2212
2213
2214
2233
2234
2235
2236
2237
2238
2239

2240
2241
2242
2243
2244
2245
2246
2247







-
+







Tcl_UniCharCaseMatch(
    const Tcl_UniChar *uniStr,	/* Unicode String. */
    const Tcl_UniChar *uniPattern,
				/* Pattern, which may contain special
				 * characters. */
    int nocase)			/* 0 for case sensitive, 1 for insensitive */
{
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
    int strLen = 0, ptnLen = 0;

    while (uniStr[strLen] != 0) {
	strLen++;
    }
    while (uniPattern[ptnLen] != 0) {
	ptnLen++;
2403
2404
2405
2406
2407
2408
2409
2410

2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428

2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442

2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460

2461
2462
2463
2464
2465
2466
2467
2468







-
+

















-
+







    const Tcl_UniChar *pattern,	/* Pattern, which may contain special
				 * characters. */
    int ptnLen,			/* Length of Pattern */
    int nocase)			/* 0 for case sensitive, 1 for insensitive */
{
    const Tcl_UniChar *stringEnd, *patternEnd;
    int p;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
    int q;
#endif

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

    while (1) {
	/*
	 * See if we're at the end of both the pattern and the string. If so,
	 * we succeeded. If we're at the end of the pattern but not at the end
	 * of the string, we failed.
	 */

	if (pattern == patternEnd) {
	    return (string == stringEnd);
	}
	p = *pattern;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	if ((p & 0xFC00) == 0xD800) {
	    if ((pattern + 1 < patternEnd) &&
		    ((pattern[1] & 0xFC00) == 0xDC00)) {
		p = (((p&0x3FF)<<10) | (pattern[1]&0x3FF)) + 0x10000;
		++pattern;
	    }
	}
2454
2455
2456
2457
2458
2459
2460
2461

2462
2463
2464
2465
2466
2467
2468
2487
2488
2489
2490
2491
2492
2493

2494
2495
2496
2497
2498
2499
2500
2501







-
+







	    while (*(++pattern) == '*') {
		/* empty body */
	    }
	    if (pattern == patternEnd) {
		return 1;
	    }
	    p = *pattern;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    if ((p & 0xFC00) == 0xD800) {
		if ((pattern + 1 < patternEnd) &&
			((pattern[1] & 0xFC00) == 0xDC00)) {
		    p = (((p&0x3FF)<<10) | (pattern[1]&0x3FF)) + 0x10000;
		    ++pattern;
		}
	    }
2476
2477
2478
2479
2480
2481
2482
2483

2484
2485
2486
2487
2488
2489
2490
2509
2510
2511
2512
2513
2514
2515

2516
2517
2518
2519
2520
2521
2522
2523







-
+







		/*
		 * Optimization for matching - cruise through the string
		 * quickly if the next char in the pattern isn't a special
		 * character.
		 */

		if ((p != '[') && (p != '?') && (p != '\\')) {
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		    while (string < stringEnd) {
			q = *string;
			if ((q & 0xFC00) == 0xD800) {
			    if ((string + 1 < stringEnd) &&
				    ((string[1] & 0xFC00) == 0xDC00)) {
				q = (((q&0x3FF)<<10) | (string[1]&0x3FF))
					+ 0x10000;
2515
2516
2517
2518
2519
2520
2521
2522

2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541

2542
2543
2544
2545
2546
2547
2548
2548
2549
2550
2551
2552
2553
2554

2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573

2574
2575
2576
2577
2578
2579
2580
2581







-
+


















-
+







		if (TclUniCharMatch(string, stringEnd - string,
			pattern, patternEnd - pattern, nocase)) {
		    return 1;
		}
		if (string == stringEnd) {
		    return 0;
		}
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		if ((string[0] & 0xFC00) == 0xD800) {
		    if ((string + 1 < stringEnd) &&
			    ((string[1] & 0xFC00) == 0xDC00)) {
			string++;
		    }
		}
#endif
		string++;
	    }
	}

	/*
	 * Check for a "?" as the next pattern character. It matches any
	 * single character.
	 */

	if (p == '?') {
	    pattern++;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    if ((string[0] & 0xFC00) == 0xD800) {
		if ((string + 1 < stringEnd) &&
			((string[1] & 0xFC00) == 0xDC00)) {
		    string++;
		}
	    }
#endif
2556
2557
2558
2559
2560
2561
2562
2563

2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583

2584
2585
2586
2587
2588
2589
2590
2589
2590
2591
2592
2593
2594
2595

2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615

2616
2617
2618
2619
2620
2621
2622
2623







-
+



















-
+







	 * characters separated by "-").
	 */

	if (p == '[') {
	    int ch1, startChar, endChar;

	    pattern++;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    ch1 = *string;
	    if ((ch1 & 0xFC00) == 0xD800) {
		if ((string + 1 < stringEnd) &&
			((string[1] & 0xFC00) == 0xDC00)) {
		    ch1 = (((ch1&0x3FF)<<10) | (string[1]&0x3FF)) + 0x10000;
		    string++;
		}
	    }
	    if (nocase) {
		ch1 = TclUCS4ToLower(ch1);
	    }
#else
	    ch1 = (nocase ? Tcl_UniCharToLower(*string) : *string);
#endif
	    string++;
	    while (1) {
		if ((*pattern == ']') || (pattern == patternEnd)) {
		    return 0;
		}
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		startChar = *pattern;
		if ((startChar & 0xFC00) == 0xD800) {
		    if ((pattern + 1 < patternEnd) &&
			    ((pattern[1] & 0xFC00) == 0xDC00)) {
			startChar = (((startChar&0x3FF)<<10) |
					(pattern[1]&0x3FF)) + 0x10000;
			pattern++;
2598
2599
2600
2601
2602
2603
2604
2605

2606
2607
2608
2609
2610
2611
2612
2631
2632
2633
2634
2635
2636
2637

2638
2639
2640
2641
2642
2643
2644
2645







-
+







#endif
		pattern++;
		if (*pattern == '-') {
		    pattern++;
		    if (pattern == patternEnd) {
			return 0;
		    }
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
		    endChar = *pattern;
		    if ((endChar & 0xFC00) == 0xD800) {
			if ((pattern + 1 < patternEnd) &&
				((pattern[1] & 0xFC00) == 0xDC00)) {
			    endChar = (((endChar&0x3FF)<<10) |
					    (pattern[1]&0x3FF)) + 0x10000;
			    pattern++;
2634
2635
2636
2637
2638
2639
2640
2641

2642
2643
2644
2645
2646
2647
2648
2667
2668
2669
2670
2671
2672
2673

2674
2675
2676
2677
2678
2679
2680
2681







-
+







	    while (*pattern != ']') {
		if (pattern == patternEnd) {
		    pattern--;
		    break;
		}
		pattern++;
	    }
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    if ((pattern[0] & 0xFC00) == 0xD800) {
		if ((pattern + 1 < patternEnd) &&
			((pattern[1] & 0xFC00) == 0xDC00)) {
		    pattern++;
		}
	    }
#endif
2661
2662
2663
2664
2665
2666
2667
2668

2669
2670
2671
2672
2673
2674
2675
2694
2695
2696
2697
2698
2699
2700

2701
2702
2703
2704
2705
2706
2707
2708







-
+







	    }
	}

	/*
	 * There's no special character. Just make sure that the next bytes of
	 * each string match.
	 */
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	p = *pattern;
	if ((p & 0xFC00) == 0xD800) {
	    if ((pattern + 1 < patternEnd) &&
		    ((pattern[1] & 0xFC00) == 0xDC00)) {
		p = (((p&0x3FF)<<10) | (pattern[1]&0x3FF)) + 0x10000;
		pattern++;
	    }

Changes to jni/tcl/generic/tclUtil.c.

100
101
102
103
104
105
106


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







+
+








static Tcl_ThreadDataKey precisionKey;

/*
 * Prototypes for functions defined later in this file.
 */

#undef UtfToUniChar
static int		UtfToUniChar(const char *string, int *chPtr);
static void		ClearHash(Tcl_HashTable *tablePtr);
static void		FreeProcessGlobalValue(ClientData clientData);
static void		FreeThreadHash(ClientData clientData);
static int		GetEndOffsetFromObj(Tcl_Obj *objPtr, int endValue,
			    int *indexPtr);
static Tcl_HashTable *	GetThreadHash(Tcl_ThreadDataKey *keyPtr);
static int		SetEndOffsetFromAny(Tcl_Interp *interp,
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
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







-
+













-
-
-




-
+




-
-
-
-
+
+
+
+
+
+
+
+








/*
 *----------------------------------------------------------------------
 *
 * UtfToUniChar --
 *
 *	Wrapper to Tcl_UtfToUniChar() capable of dealing with
 *	UCS4 when compiled with TCL_UTF_MAX > 3.
 *	surrogate pairs when compiled with TCL_UTF_MAX == 3.
 *
 * Results:
 *	*chPtr is filled with the full unicode character, and the
 *	return value is the number of bytes from the UTF-8 string that
 *	were consumed.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static int
#if TCL_UTF_MAX != 4
inline
#endif
UtfToUniChar(
    const char *src,
    int *chPtr)
{
    Tcl_UniChar ch = 0;
    Tcl_UniChar ch;
    int uch, len;

    len = TclUtfToUniChar(src, &ch);
    uch = ch;
#if TCL_UTF_MAX == 4
    if (!len) {
	len = TclUtfToUniChar(src, &ch);
	uch = ((uch & 0x3FF) << 10) + 0x10000 + (ch & 0x3FF);
#if TCL_UTF_MAX == 3
    if ((ch & 0xFC00) == 0xD800) {
	int len2 = TclUtfToUniChar(src + len, &ch);

	if (len2 && ((ch & 0xFC00) == 0xDC00)) {
	    uch = ((uch & 0x3FF) << 10) + 0x10000 + (ch & 0x3FF);
	    len += len2;
	}
    }
#endif
    *chPtr = uch;
    return len;
}

/*
1753
1754
1755
1756
1757
1758
1759

1760
1761
1762
1763
1764
1765
1766
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770







+







			 * rely on (bytes[numBytes] == '\0'). */
    const char *trim,	/* String of trim characters... */
    int numTrim)	/* ...and its length in bytes */
			/* Calls to UtfToUniChar() in this routine
			 * rely on (trim[numTrim] == '\0'). */
{
    const char *pp, *p = bytes + numBytes, *q;
    Tcl_UniChar ch1 = 0;
    int i;
    Tcl_DString ds;

    /* Empty strings -> nothing to do */
    if ((numBytes == 0) || (numTrim == 0)) {
	return 0;
    }
1814
1815
1816
1817
1818
1819
1820
1821

1822
1823

















1824
1825
1826
1827
1828
1829
1830
1818
1819
1820
1821
1822
1823
1824

1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851







-
+


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+








    do {
	int uch, pInc = 0;

	pp = TclUtfPrev(p, bytes);
	do {
	    pp += pInc;
 	    pInc = UtfToUniChar(pp, &uch);
 	    pInc = TclUtfToUniChar(pp, &ch1);
	} while (pp + pInc < p);

	uch = ch1;
#if TCL_UTF_MAX == 3
	if (((ch1 & 0xFC00) == 0xDC00) && (pp > bytes)) {
	    const char *ppp;
	    int ppInc = 0;

	    ppp = TclUtfPrev(pp, bytes);
	    do {
		ppp += ppInc;
		ppInc = TclUtfToUniChar(ppp, &ch1);
	    } while (ppp + ppInc < pp);
	    if ((ch1 & 0xFC00) == 0xD800) {
		pp = ppp;
		uch = (((ch1&0x3FF)<<10) | (uch&0x3FF)) + 0x10000;
	    }
	}
#endif

	/*
	 * Inner loop: scan trim string for match to current character.
	 */

	for (i = 0; i < numTrim; i++) {
	    if (uch == ((int *)Tcl_DStringValue(&ds))[i]) {

Changes to jni/tcl/generic/zipfs.c.

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
1136
1137
1138









1139


1140
1141
1142
1143
1144
1145
1146
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
1136


1137




1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156

1157
1158
1159
1160
1161
1162
1163
1164
1165







-
+
-
-
+

-
+



-
+
+
+

+
+
+
-
-
-
+
+
+
+
+
+
+
-
-
+
-
-
-
-
+
+
+
+
+
+




+
+
+
+
+
+
+
+
+
-
+
+







	0x00b0, 0x2219, 0x00b7, 0x221a, 0x207f, 0x00b2, 0x25a0, 0x00a0
    };
    char *q, buffer[TCL_UTF_MAX * 2];
    int i, n;

    if (utf) {
	const char *p = path, *end = p + length;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	Tcl_UniChar ch = 0;
	int uch;
	int ch;
#else
	Tcl_UniChar uch;
	Tcl_UniChar ch;
#endif

	while (p < end) {
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	    n = TclUtfToUniCharExt(p, &ch);
#else
	    n = Tcl_UtfToUniChar(p, &ch);
#endif
	    if (p + n > end) {
		while (p < end) {
	    uch = ch;
	    if (n == 0) {
		n = Tcl_UtfToUniChar(p, &ch);
		    ch = UCHAR(*p);
		    if (ch == 0) {
			buffer[0] = 0xc0;
			buffer[1] = 0x80;
			i = 2;
		    } else {
			i = Tcl_UniCharToUtf(ch, buffer);
		uch = (((uch & 0x3ff) << 10) | (ch & 0x3ff)) + 0x10000;
	    }
		    }
#else
	    n = Tcl_UtfToUniChar(p, &uch);
#endif
	    if (uch == 0) {
		    Tcl_DStringAppend(dsPtr, buffer, i);
		    p++;
		}
		break;
	    }
	    if (ch == 0) {
		buffer[0] = 0xc0;
		buffer[1] = 0x80;
		i = 2;
	    } else {
#if TCL_UTF_MAX == 3
		i = 0;
		if (ch > 0xFFFF) {
		    ch -= 0x10000;
		    i = Tcl_UniCharToUtf((ch >> 10) | 0xd800, buffer);
		    ch = (ch & 0x3ff) | 0xdc00;
		}
		i += Tcl_UniCharToUtf(ch, buffer + i);
#else
		i = Tcl_UniCharToUtf(uch, buffer);
		i = Tcl_UniCharToUtf(ch, buffer);
#endif
	    }
	    Tcl_DStringAppend(dsPtr, buffer, i);
	    p += n;
	}
    } else {
	for (i = 0; i < length; i++) {
	    int ch = UCHAR(path[i]);
1204
1205
1206
1207
1208
1209
1210

1211

1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227

1228


1229
1230
1231
1232








1233
1234

1235
1236
1237
1238
1239
1240
1241
1242
1243
1223
1224
1225
1226
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







+
-
+
-

-
-










-

+

+
+
-
-
-
-
+
+
+
+
+
+
+
+

-
+

-







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

static char *
EncodePathname(const char *path, int length, Tcl_DString *dsPtr)
{
    const char *p, *end;
    Tcl_UniChar ch;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
    Tcl_UniChar ch = 0;
    int uch;
#else
    Tcl_UniChar uch;
#endif
    int i, n;
    char buffer[TCL_UTF_MAX * 2];

    if (length < 0) {
	length = strlen(path);
    }
    p = path;
    end = p + length;
    while (p < end) {
#if TCL_UTF_MAX == 4
	n = Tcl_UtfToUniChar(p, &ch);
#if TCL_UTF_MAX == 3
	uch = ch;
	if (((ch & 0xfc00) == 0xd800) && (p + n < end - 3)) {
	    int nn;
	if (n == 0) {
	    n = Tcl_UtfToUniChar(p, &ch);
	    uch = (((uch & 0x3ff) << 10) | (ch & 0x3ff)) + 0x10000;
	}

	    nn = Tcl_UtfToUniChar(p + n, &ch);
	    if ((ch & 0xfc00) == 0xdc00) {
		n += nn;
		uch = (((uch & 0x3ff) << 10) | (ch & 0x3ff)) + 0x10000;
	    }
	}
	i = TclUniCharToUtfExt(uch, buffer);
#else
	n = Tcl_UtfToUniChar(p, &uch);
	i = Tcl_UniCharToUtf(ch, buffer);
#endif
	i = Tcl_UniCharToUtf(uch, buffer);
	Tcl_DStringAppend(dsPtr, buffer, i);
	p += n;
    }
    return Tcl_DStringValue(dsPtr);
}

/*

Changes to jni/tcl/pkgs/sqlite3.45.3/generic/tclsqlite3.c.

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







+











+
+
+
+
+
+
+
+
+














+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







  int nVMStep;               /* Another statistic for most recent operation */
  int nTransaction;          /* Number of nested [transaction] methods */
  int openFlags;             /* Flags used to open.  (SQLITE_OPEN_URI) */
  int nRef;                  /* Delete object when this reaches 0 */
#ifdef SQLITE_TEST
  int bLegacyPrepare;        /* True to use sqlite3_prepare() */
#endif
  Tcl_DString dsErr;         /* Used for error messages (utf8Encoding) */
};

struct IncrblobChannel {
  sqlite3_blob *pBlob;      /* sqlite3 blob handle */
  SqliteDb *pDb;            /* Associated database connection */
  Tcl_WideInt iSeek;        /* Current seek offset */
  Tcl_Channel channel;      /* Channel identifier */
  IncrblobChannel *pNext;   /* Linked list of all open incrblob channels */
  IncrblobChannel *pPrev;   /* Linked list of all open incrblob channels */
};

/*
 ** Tcl UTF-8 encoding; loaded and tested on module init. Depending
 ** on unicode support detected, it is reset to NULL or kept until
 ** module gets unloaded.
 */
static Tcl_Encoding utf8Encoding = NULL;
static int utf8EncInit = 0;
TCL_DECLARE_MUTEX(utf8EncMutex);

/*
** Compute a string length that is limited to what can be stored in
** lower 30 bits of a 32-bit signed integer.
*/
static int strlen30(const char *z){
  const char *z2 = z;
  while( *z2 ){ z2++; }
  return 0x3fffffff & (int)(z2 - z);
}

#ifdef USE_TCL_STUBS
# define tclStubsPtr staticTclStubsPtr
static const TclStubs *tclStubsPtr = 0;
#endif

/*
 ** Error message converter.
 */
static const char *SQLITEDB_ERRMSG(SqliteDb *pDb){
  if( pDb!=NULL ){
    if( pDb->db!=NULL ){
      if( utf8Encoding!=NULL ){
        Tcl_DStringFree(&pDb->dsErr);
        Tcl_ExternalToUtfDString(utf8Encoding, sqlite3_errmsg(pDb->db),
            -1, &pDb->dsErr);
        return Tcl_DStringValue(&pDb->dsErr);
      }
      return sqlite3_errmsg(pDb->db);
    }
    return "not a valid database";
  }
  return "unknown error";
}

#ifndef SQLITE_OMIT_INCRBLOB
/*
** Close all incrblob channels opened using database connection pDb.
** This is called when shutting down the database connection.
*/
static void closeIncrblobChannels(SqliteDb *pDb){
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
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







-
+
















-
+







*/
static int SQLITE_TCLAPI incrblobClose(
  ClientData instanceData,
  Tcl_Interp *interp
){
  IncrblobChannel *p = (IncrblobChannel *)instanceData;
  int rc = sqlite3_blob_close(p->pBlob);
  sqlite3 *db = p->pDb->db;
  SqliteDb *pDb = p->pDb;

  /* Remove the channel from the SqliteDb.pIncrblob list. */
  if( p->pNext ){
    p->pNext->pPrev = p->pPrev;
  }
  if( p->pPrev ){
    p->pPrev->pNext = p->pNext;
  }
  if( p->pDb->pIncrblob==p ){
    p->pDb->pIncrblob = p->pNext;
  }

  /* Free the IncrblobChannel structure */
  Tcl_Free((char *)p);

  if( rc!=SQLITE_OK ){
    Tcl_SetResult(interp, (char *)sqlite3_errmsg(db), TCL_VOLATILE);
    Tcl_SetResult(interp, (char*)SQLITEDB_ERRMSG(pDb), TCL_VOLATILE);
    return TCL_ERROR;
  }
  return TCL_OK;
}

static int SQLITE_TCLAPI incrblobClose2(
  ClientData instanceData,
501
502
503
504
505
506
507
508

509
510
511
512
513
514
515
530
531
532
533
534
535
536

537
538
539
540
541
542
543
544







-
+








  /* This variable is used to name the channels: "incrblob_[incr count]" */
  static int count = 0;
  char zChannel[64];

  rc = sqlite3_blob_open(db, zDb, zTable, zColumn, iRow, !isReadonly, &pBlob);
  if( rc!=SQLITE_OK ){
    Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
    Tcl_SetResult(interp, (char*)SQLITEDB_ERRMSG(pDb), TCL_VOLATILE);
    return TCL_ERROR;
  }

  p = (IncrblobChannel *)Tcl_Alloc(sizeof(IncrblobChannel));
  p->iSeek = 0;
  p->pBlob = pBlob;

677
678
679
680
681
682
683

684
685
686
687
688
689
690
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720







+







    }
    if( pDb->pWalHook ){
      Tcl_DecrRefCount(pDb->pWalHook);
    }
    if( pDb->pCollateNeeded ){
      Tcl_DecrRefCount(pDb->pCollateNeeded);
    }
    Tcl_DStringFree(&pDb->dsErr);
    Tcl_Free((char*)pDb);
  }
}

/*
** TCL calls this procedure when an sqlite3 database command is
** deleted.
746
747
748
749
750
751
752






753


754
755
756
757
758
759
760
776
777
778
779
780
781
782
783
784
785
786
787
788

789
790
791
792
793
794
795
796
797







+
+
+
+
+
+
-
+
+







  void *xd       /* Extra event data, depends on event type. */
){
  SqliteDb *pDb = (SqliteDb*)cd;
  Tcl_DString str;

  Tcl_DStringInit(&str);
  Tcl_DStringAppend(&str, pDb->zTrace, -1);
  if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, (char *)xd, -1, &ds);
    Tcl_DStringAppendElement(&str, Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);
  }else{
  Tcl_DStringAppendElement(&str, (char *)xd);
    Tcl_DStringAppendElement(&str, (char *)xd);
  }
  Tcl_EvalEx(pDb->interp, Tcl_DStringValue(&str), -1, 0);
  Tcl_DStringFree(&str);
  Tcl_ResetResult(pDb->interp);
  return TCL_OK;
}
#endif

779
780
781
782
783
784
785



786
787








788
789
790
791
792
793
794
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







+
+
+
-
-
+
+
+
+
+
+
+
+







      sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
      char *zSql = (char *)xd;

      pCmd = Tcl_NewStringObj(pDb->zTraceV2, -1);
      Tcl_IncrRefCount(pCmd);
      Tcl_ListObjAppendElement(pDb->interp, pCmd,
                               Tcl_NewWideIntObj((Tcl_WideInt)(uptr)pStmt));
      if( utf8Encoding!=NULL ){
        Tcl_DString ds;
        Tcl_ExternalToUtfDString(utf8Encoding, zSql, -1, &ds);
      Tcl_ListObjAppendElement(pDb->interp, pCmd,
                               Tcl_NewStringObj(zSql, -1));
        Tcl_ListObjAppendElement(pDb->interp, pCmd,
                                 Tcl_NewStringObj(Tcl_DStringValue(&ds),
                                                  Tcl_DStringLength(&ds)));
        Tcl_DStringFree(&ds);
      }else{
        Tcl_ListObjAppendElement(pDb->interp, pCmd,
                                 Tcl_NewStringObj(zSql, -1));
      }
      Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
      Tcl_DecrRefCount(pCmd);
      Tcl_ResetResult(pDb->interp);
      break;
    }
    case SQLITE_TRACE_PROFILE: {
      sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;
849
850
851
852
853
854
855






856


857
858
859
860
861
862
863
895
896
897
898
899
900
901
902
903
904
905
906
907

908
909
910
911
912
913
914
915
916







+
+
+
+
+
+
-
+
+







  Tcl_DString str;
  char zTm[100];
  sqlite3_stmt *pStmt = (sqlite3_stmt *)pd;

  sqlite3_snprintf(sizeof(zTm)-1, zTm, "%lld", (Tcl_WideInt)(uptr)xd);
  Tcl_DStringInit(&str);
  Tcl_DStringAppend(&str, pDb->zProfile, -1);
  if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, sqlite3_sql(pStmt), -1, &ds);
    Tcl_DStringAppendElement(&str, Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);
  }else{
  Tcl_DStringAppendElement(&str, sqlite3_sql(pStmt));
    Tcl_DStringAppendElement(&str, sqlite3_sql(pStmt));
  }
  Tcl_DStringAppendElement(&str, zTm);
  Tcl_EvalEx(pDb->interp, Tcl_DStringValue(&str), -1, 0);
  Tcl_DStringFree(&str);
  Tcl_ResetResult(pDb->interp);
  return SQLITE_OK;
}
#endif
902
903
904
905
906
907
908







909


910
911
912
913
914
915
916
955
956
957
958
959
960
961
962
963
964
965
966
967
968

969
970
971
972
973
974
975
976
977







+
+
+
+
+
+
+
-
+
+







  SqliteDb *pDb = (SqliteDb*)clientData;
  Tcl_Interp *interp = pDb->interp;
  assert(pDb->pWalHook);

  assert( db==pDb->db );
  p = Tcl_DuplicateObj(pDb->pWalHook);
  Tcl_IncrRefCount(p);
  if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zDb, -1, &ds);
    Tcl_ListObjAppendElement(interp, p,
        Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)));
    Tcl_DStringFree(&ds);
  }else{
  Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1));
    Tcl_ListObjAppendElement(interp, p, Tcl_NewStringObj(zDb, -1));
  }
  Tcl_ListObjAppendElement(interp, p, Tcl_NewWideIntObj(nEntry));
  if( TCL_OK!=(rc=Tcl_EvalObjEx(interp, p, 0))
   || TCL_OK!=(rc=Tcl_GetIntFromObj(interp, Tcl_GetObjResult(interp), &ret))
  ){
    Tcl_BackgroundException(interp, rc);
  }
  Tcl_DecrRefCount(p);
968
969
970
971
972
973
974











975
976



977
978
979
980
981
982
983
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
1055
1056







+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+







  assert( pDb->pPreUpdateHook );
  assert( db==pDb->db );
  assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );

  pCmd = Tcl_DuplicateObj(pDb->pPreUpdateHook);
  Tcl_IncrRefCount(pCmd);
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1));
  if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zDb, -1, &ds);
    Tcl_ListObjAppendElement(0, pCmd,
        Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)));
    Tcl_DStringFree(&ds);
    Tcl_ExternalToUtfDString(utf8Encoding, zTbl, -1, &ds);
    Tcl_ListObjAppendElement(0, pCmd,
        Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)));
    Tcl_DStringFree(&ds);
  }else{
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
    Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
    Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
  }
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey1));
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(iKey2));
  Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
  Tcl_DecrRefCount(pCmd);
}
#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */

998
999
1000
1001
1002
1003
1004











1005
1006



1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020







1021


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
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145


1146
1147
1148
1149
1150
1151
1152
1153
1154
1155







+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+














+
+
+
+
+
+
+
-
+
+




















+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+








  assert( pDb->pUpdateHook );
  assert( op==SQLITE_INSERT || op==SQLITE_UPDATE || op==SQLITE_DELETE );

  pCmd = Tcl_DuplicateObj(pDb->pUpdateHook);
  Tcl_IncrRefCount(pCmd);
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(azStr[(op-1)/9], -1));
  if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zDb, -1, &ds);
    Tcl_ListObjAppendElement(0, pCmd,
        Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)));
    Tcl_DStringFree(&ds);
    Tcl_ExternalToUtfDString(utf8Encoding, zTbl, -1, &ds);
    Tcl_ListObjAppendElement(0, pCmd,
        Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)));
    Tcl_DStringFree(&ds);
  }else{
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
    Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zDb, -1));
    Tcl_ListObjAppendElement(0, pCmd, Tcl_NewStringObj(zTbl, -1));
  }
  Tcl_ListObjAppendElement(0, pCmd, Tcl_NewWideIntObj(rowid));
  Tcl_EvalObjEx(pDb->interp, pCmd, TCL_EVAL_DIRECT);
  Tcl_DecrRefCount(pCmd);
}

static void tclCollateNeeded(
  void *pCtx,
  sqlite3 *db,
  int enc,
  const char *zName
){
  SqliteDb *pDb = (SqliteDb *)pCtx;
  Tcl_Obj *pScript = Tcl_DuplicateObj(pDb->pCollateNeeded);
  Tcl_IncrRefCount(pScript);
  if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zName, -1, &ds);
    Tcl_ListObjAppendElement(0, pScript,
        Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds)));
    Tcl_DStringFree(&ds);
  }else{
  Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
    Tcl_ListObjAppendElement(0, pScript, Tcl_NewStringObj(zName, -1));
  }
  Tcl_EvalObjEx(pDb->interp, pScript, 0);
  Tcl_DecrRefCount(pScript);
}

/*
** This routine is called to evaluate an SQL collation function implemented
** using TCL script.
*/
static int tclSqlCollate(
  void *pCtx,
  int nA,
  const void *zA,
  int nB,
  const void *zB
){
  SqlCollate *p = (SqlCollate *)pCtx;
  Tcl_Obj *pCmd;

  pCmd = Tcl_NewStringObj(p->zScript, -1);
  Tcl_IncrRefCount(pCmd);
  if( utf8Encoding!=NULL ){
    Tcl_DString dsA, dsB;
    Tcl_ExternalToUtfDString(utf8Encoding, (char*)zA, nA, &dsA);
    Tcl_ExternalToUtfDString(utf8Encoding, (char*)zB, nB, &dsB);
    Tcl_ListObjAppendElement(p->interp, pCmd,
        Tcl_NewStringObj(Tcl_DStringValue(&dsA), Tcl_DStringLength(&dsA)));
    Tcl_ListObjAppendElement(p->interp, pCmd,
        Tcl_NewStringObj(Tcl_DStringValue(&dsB), Tcl_DStringLength(&dsB)));
    Tcl_DStringFree(&dsA);
    Tcl_DStringFree(&dsB);
  }else{
  Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj((const char *)zA, nA));
  Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj((const char *)zB, nB));
    Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj((const char *)zA, nA));
    Tcl_ListObjAppendElement(p->interp, pCmd, Tcl_NewStringObj((const char *)zB, nB));
  }
  Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
  Tcl_DecrRefCount(pCmd);
  return (atoi(Tcl_GetStringResult(p->interp)));
}

/*
** This routine is called to evaluate an SQL function implemented
1074
1075
1076
1077
1078
1079
1080









1081


1082
1083
1084
1085
1086
1087
1088
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194

1195
1196
1197
1198
1199
1200
1201
1202
1203







+
+
+
+
+
+
+
+
+
-
+
+







    ** That way, when Tcl_EvalObjv() is run and shimmers the first element
    ** of the list to tclCmdNameType, that alternate representation will
    ** be preserved and reused on the next invocation.
    */
    Tcl_Obj **aArg;
    int nArg;
    if( Tcl_ListObjGetElements(p->interp, p->pScript, &nArg, &aArg) ){
resultError:
      if( utf8Encoding!=NULL ){
        Tcl_DString ds;
        Tcl_UtfToExternalDString(utf8Encoding, Tcl_GetStringResult(p->interp),
            -1, &ds);
        sqlite3_result_error(context, Tcl_DStringValue(&ds),
            Tcl_DStringLength(&ds));
        Tcl_DStringFree(&ds);
      }else{
      sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
        sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
      }
      return;
    }
    pCmd = Tcl_NewListObj(nArg, aArg);
    Tcl_IncrRefCount(pCmd);
    for(i=0; i<argc; i++){
      sqlite3_value *pIn = argv[i];
      Tcl_Obj *pVal;
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
1136
1137

1138
1139
1140
1141
1142
1143
1144
1221
1222
1223
1224
1225
1226
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







+
+
+
+
+
+
+
+
-
+
+






-
-
+















-
+







        }
        case SQLITE_NULL: {
          pVal = Tcl_NewStringObj(p->pDb->zNull, -1);
          break;
        }
        default: {
          int bytes = sqlite3_value_bytes(pIn);
          if( utf8Encoding!=NULL ){
            Tcl_DString ds;
            Tcl_ExternalToUtfDString(utf8Encoding,
                (char *)sqlite3_value_text(pIn), bytes, &ds);
            pVal = Tcl_NewStringObj(Tcl_DStringValue(&ds),
                       Tcl_DStringLength(&ds));
            Tcl_DStringFree(&ds);
          }else{
          pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes);
            pVal = Tcl_NewStringObj((char *)sqlite3_value_text(pIn), bytes);
          }
          break;
        }
      }
      rc = Tcl_ListObjAppendElement(p->interp, pCmd, pVal);
      if( rc ){
        Tcl_DecrRefCount(pCmd);
        sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
        return;
        goto resultError;
      }
    }
    if( !p->useEvalObjv ){
      /* Tcl_EvalObjEx() will automatically call Tcl_EvalObjv() if pCmd
      ** is a list without a string representation.  To prevent this from
      ** happening, make sure pCmd has a valid string representation */
      Tcl_GetString(pCmd);
    }
    rc = Tcl_EvalObjEx(p->interp, pCmd, TCL_EVAL_DIRECT);
    Tcl_DecrRefCount(pCmd);
  }

  if( rc==TCL_BREAK ){
    sqlite3_result_null(context);
  }else if( rc && rc!=TCL_RETURN ){
    sqlite3_result_error(context, Tcl_GetStringResult(p->interp), -1);
    goto resultError;
  }else{
    Tcl_Obj *pVar = Tcl_GetObjResult(p->interp);
    int n;
    u8 *data;
    const char *zType = (pVar->typePtr ? pVar->typePtr->name : "");
    char c = zType[0];
    int eType = p->eType;
1261
1262
1263
1264
1265
1266
1267

1268
1269
1270
1271







































1272

1273









1274
1275
1276
1277
1278
1279
1280
1384
1385
1386
1387
1388
1389
1390
1391




1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432

1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448







+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+
-
+
+
+
+
+
+
+
+
+







    case SQLITE_SAVEPOINT         : zCode="SQLITE_SAVEPOINT"; break;
    case SQLITE_RECURSIVE         : zCode="SQLITE_RECURSIVE"; break;
    default                       : zCode="????"; break;
  }
  Tcl_DStringInit(&str);
  Tcl_DStringAppend(&str, pDb->zAuth, -1);
  Tcl_DStringAppendElement(&str, zCode);
  if( zArg1==NULL ){
  Tcl_DStringAppendElement(&str, zArg1 ? zArg1 : "");
  Tcl_DStringAppendElement(&str, zArg2 ? zArg2 : "");
  Tcl_DStringAppendElement(&str, zArg3 ? zArg3 : "");
  Tcl_DStringAppendElement(&str, zArg4 ? zArg4 : "");
    Tcl_DStringAppendElement(&str, "");
  }else if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zArg1, -1, &ds);
    Tcl_DStringAppendElement(&str, Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);
  }else{
    Tcl_DStringAppendElement(&str, zArg1);
  }
  if( zArg2==NULL ){
    Tcl_DStringAppendElement(&str, "");
  }else if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zArg2, -1, &ds);
    Tcl_DStringAppendElement(&str, Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);
  }else{
    Tcl_DStringAppendElement(&str, zArg2);
  }
  if( zArg3==NULL ){
    Tcl_DStringAppendElement(&str, "");
  }else if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zArg3, -1, &ds);
    Tcl_DStringAppendElement(&str, Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);
  }else{
    Tcl_DStringAppendElement(&str, zArg3);
  }
  if( zArg4==NULL ){
    Tcl_DStringAppendElement(&str, "");
  }else if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zArg4, -1, &ds);
    Tcl_DStringAppendElement(&str, Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);
  }else{
    Tcl_DStringAppendElement(&str, zArg4);
  }
#ifdef SQLITE_USER_AUTHENTICATION
  if( zArg5==NULL ){
  Tcl_DStringAppendElement(&str, zArg5 ? zArg5 : "");
    Tcl_DStringAppendElement(&str, "");
  }else if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_ExternalToUtfDString(utf8Encoding, zArg5, -1, &ds);
    Tcl_DStringAppendElement(&str, Tcl_DStringValue(&ds));
    Tcl_DStringFree(&ds);
  }else{
    Tcl_DStringAppendElement(&str, zArg5);
  }
#endif
  rc = Tcl_EvalEx(pDb->interp, Tcl_DStringValue(&str), -1, TCL_EVAL_GLOBAL);
  Tcl_DStringFree(&str);
  zReply = rc==TCL_OK ? Tcl_GetStringResult(pDb->interp) : "SQLITE_DENY";
  if( strcmp(zReply,"SQLITE_OK")==0 ){
    rc = SQLITE_OK;
  }else if( strcmp(zReply,"SQLITE_DENY")==0 ){
1371
1372
1373
1374
1375
1376
1377
1378

1379
1380
1381
1382
1383
1384
1385
1539
1540
1541
1542
1543
1544
1545

1546
1547
1548
1549
1550
1551
1552
1553







-
+







      ** and try to rollback the transaction.
      **
      ** But it could also be that the user executed one or more BEGIN,
      ** COMMIT, SAVEPOINT, RELEASE or ROLLBACK commands that are confusing
      ** this method's logic. Not clear how this would be best handled.
      */
    if( rc!=TCL_ERROR ){
      Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
      Tcl_AppendResult(interp, (char*)SQLITEDB_ERRMSG(pDb), (char*)0);
      rc = TCL_ERROR;
    }
    sqlite3_exec(pDb->db, "ROLLBACK", 0, 0, 0);
  }
  pDb->disableAuth--;

  delDatabaseRef(pDb);
1441
1442
1443
1444
1445
1446
1447

1448
1449
1450
1451
1452
1453
1454
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623







+







  int nVar = 0;                   /* Number of variables in statement */
  int iParm = 0;                  /* Next free entry in apParm */
  char c;
  int i;
  int needResultReset = 0;        /* Need to invoke Tcl_ResetResult() */
  int rc = SQLITE_OK;             /* Value to return */
  Tcl_Interp *interp = pDb->interp;
  Tcl_DString ds;

  *ppPreStmt = 0;

  /* Trim spaces from the start of zSql and calculate the remaining length. */
  while( (c = zSql[0])==' ' || c=='\t' || c=='\r' || c=='\n' ){ zSql++; }
  nSql = strlen30(zSql);

1483
1484
1485
1486
1487
1488
1489
1490

1491
1492
1493
1494
1495
1496

1497
1498
1499
1500
1501
1502
1503
1652
1653
1654
1655
1656
1657
1658

1659
1660
1661
1662
1663
1664

1665
1666
1667
1668
1669
1670
1671
1672







-
+





-
+








  /* If no prepared statement was found. Compile the SQL text. Also allocate
  ** a new SqlPreparedStmt structure.  */
  if( pPreStmt==0 ){
    int nByte;

    if( SQLITE_OK!=dbPrepare(pDb, zSql, &pStmt, pzOut) ){
      Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
      Tcl_SetObjResult(interp, Tcl_NewStringObj(SQLITEDB_ERRMSG(pDb), -1));
      return TCL_ERROR;
    }
    if( pStmt==0 ){
      if( SQLITE_OK!=sqlite3_errcode(pDb->db) ){
        /* A compile-time error in the statement. */
        Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
        Tcl_SetObjResult(interp, Tcl_NewStringObj(SQLITEDB_ERRMSG(pDb), -1));
        return TCL_ERROR;
      }else{
        /* The statement was a no-op.  Continue to the next statement
        ** in the SQL string.
        */
        return TCL_OK;
      }
1523
1524
1525
1526
1527
1528
1529

1530
1531





1532
1533
1534
1535
1536
1537
1538
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713







+


+
+
+
+
+







#endif
  }
  assert( pPreStmt );
  assert( strlen30(pPreStmt->zSql)==pPreStmt->nSql );
  assert( 0==memcmp(pPreStmt->zSql, zSql, pPreStmt->nSql) );

  /* Bind values to parameters that begin with $ or : */
  Tcl_DStringInit(&ds);
  for(i=1; i<=nVar; i++){
    const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
    if( utf8Encoding!= NULL ){
      Tcl_DStringFree(&ds);
      Tcl_ExternalToUtfDString(utf8Encoding, zVar, -1, &ds);
      zVar = Tcl_DStringValue(&ds);
    }
    if( zVar!=0 && (zVar[0]=='$' || zVar[0]==':' || zVar[0]=='@') ){
      Tcl_Obj *pVar = Tcl_GetVar2Ex(interp, &zVar[1], 0, 0);
      if( pVar==0 && pDb->zBindFallback!=0 ){
        Tcl_Obj *pCmd;
        int rx;
        pCmd = Tcl_NewStringObj(pDb->zBindFallback, -1);
        Tcl_IncrRefCount(pCmd);
1573
1574
1575
1576
1577
1578
1579










1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591

1592
1593
1594
1595
1596
1597
1598
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784







+
+
+
+
+
+
+
+
+
+












+







          Tcl_GetDoubleFromObj(interp, pVar, &r);
          sqlite3_bind_double(pStmt, i, r);
        }else if( (c=='w' && strcmp(zType,"wideInt")==0) ||
              (c=='i' && strcmp(zType,"int")==0) ){
          Tcl_WideInt v;
          Tcl_GetWideIntFromObj(interp, pVar, &v);
          sqlite3_bind_int64(pStmt, i, v);
        }else if( utf8Encoding!=NULL ){
          const char *pStr;
          int len;
          Tcl_DStringFree(&ds);
          pStr = Tcl_GetStringFromObj(pVar, &len);
          Tcl_UtfToExternalDString(utf8Encoding, pStr, len, &ds);
          sqlite3_bind_text(pStmt, i, Tcl_DStringValue(&ds),
              Tcl_DStringLength(&ds), SQLITE_TRANSIENT);
          Tcl_IncrRefCount(pVar);
          pPreStmt->apParm[iParm++] = pVar;
        }else{
          data = (unsigned char *)Tcl_GetString(pVar);
          sqlite3_bind_text(pStmt, i, (char *)data, pVar->length, SQLITE_STATIC);
          Tcl_IncrRefCount(pVar);
          pPreStmt->apParm[iParm++] = pVar;
        }
      }else{
        sqlite3_bind_null(pStmt, i);
      }
      if( needResultReset ) Tcl_ResetResult(pDb->interp);
    }
  }
  Tcl_DStringFree(&ds);
  pPreStmt->nParm = iParm;
  *ppPreStmt = pPreStmt;
  if( needResultReset && rc==TCL_OK ) Tcl_ResetResult(pDb->interp);

  return rc;
}

1658
1659
1660
1661
1662
1663
1664

1665
1666
1667
1668
1669
1670
1671
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858







+







**   dbEvalRowInfo()
**   dbEvalColumnValue()
*/
typedef struct DbEvalContext DbEvalContext;
struct DbEvalContext {
  SqliteDb *pDb;                  /* Database handle */
  Tcl_Obj *pSql;                  /* Object holding string zSql */
  Tcl_DString dsSql;              /* Encoded SQL text */
  const char *zSql;               /* Remaining SQL to execute */
  SqlPreparedStmt *pPreStmt;      /* Current statement */
  int nCol;                       /* Number of columns returned by pStmt */
  int evalFlags;                  /* Flags used */
  Tcl_Obj *pArray;                /* Name of array variable */
  Tcl_Obj **apColName;            /* Array of column names */
};
1706
1707
1708
1709
1710
1711
1712







1713


1714
1715
1716
1717
1718
1719
1720
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906

1907
1908
1909
1910
1911
1912
1913
1914
1915







+
+
+
+
+
+
+
-
+
+







  SqliteDb *pDb,                  /* Database handle */
  Tcl_Obj *pSql,                  /* Object containing SQL script */
  Tcl_Obj *pArray,                /* Name of Tcl array to set (*) element of */
  int evalFlags                   /* Flags controlling evaluation */
){
  memset(p, 0, sizeof(DbEvalContext));
  p->pDb = pDb;
  if( utf8Encoding!=NULL ){
    const char *pStr;
    int len;
    pStr = Tcl_GetStringFromObj(pSql, &len);
    p->zSql = Tcl_UtfToExternalDString(utf8Encoding, pStr, len, &p->dsSql);
  }else{
    Tcl_DStringInit(&p->dsSql);
  p->zSql = Tcl_GetString(pSql);
    p->zSql = Tcl_GetString(pSql);
  }
  p->pSql = pSql;
  Tcl_IncrRefCount(pSql);
  if( pArray ){
    p->pArray = pArray;
    Tcl_IncrRefCount(pArray);
  }
  p->evalFlags = evalFlags;
1737
1738
1739
1740
1741
1742
1743








1744


1745
1746
1747
1748
1749
1750
1751
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946

1947
1948
1949
1950
1951
1952
1953
1954
1955







+
+
+
+
+
+
+
+
-
+
+







    int nCol;                     /* Number of columns returned by pStmt */
    Tcl_Obj **apColName = 0;      /* Array of column names */

    p->nCol = nCol = sqlite3_column_count(pStmt);
    if( nCol>0 && (papColName || p->pArray) ){
      apColName = (Tcl_Obj**)Tcl_Alloc( sizeof(Tcl_Obj*)*nCol );
      for(i=0; i<nCol; i++){
        if( utf8Encoding!=NULL ){
          Tcl_DString ds;
          Tcl_ExternalToUtfDString(utf8Encoding, sqlite3_column_name(pStmt,i),
              -1, &ds);
          apColName[i] = Tcl_NewStringObj(Tcl_DStringValue(&ds),
                             Tcl_DStringLength(&ds));
          Tcl_DStringFree(&ds);
        }else{
        apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1);
          apColName[i] = Tcl_NewStringObj(sqlite3_column_name(pStmt,i), -1);
        }
        Tcl_IncrRefCount(apColName[i]);
      }
      p->apColName = apColName;
    }

    /* If results are being stored in an array variable, then create
    ** the array(*) entry for that array
1848
1849
1850
1851
1852
1853
1854
1855

1856
1857
1858
1859
1860
1861
1862
2052
2053
2054
2055
2056
2057
2058

2059
2060
2061
2062
2063
2064
2065
2066







-
+







          ** This only happens once. If there is a second SQLITE_SCHEMA
          ** error, the error will be returned to the caller. */
          p->zSql = zPrevSql;
          continue;
        }
#endif
        Tcl_SetObjResult(pDb->interp,
                         Tcl_NewStringObj(sqlite3_errmsg(pDb->db), -1));
            Tcl_NewStringObj(SQLITEDB_ERRMSG(pDb), -1));
        return TCL_ERROR;
      }else{
        dbReleaseStmt(pDb, pPreStmt, 0);
      }
    }
  }

1875
1876
1877
1878
1879
1880
1881

1882
1883
1884
1885
1886
1887
1888
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093







+







    dbReleaseStmt(p->pDb, p->pPreStmt, 0);
    p->pPreStmt = 0;
  }
  if( p->pArray ){
    Tcl_DecrRefCount(p->pArray);
    p->pArray = 0;
  }
  Tcl_DStringFree(&p->dsSql);
  Tcl_DecrRefCount(p->pSql);
  dbReleaseColumnNames(p);
  delDatabaseRef(p->pDb);
}

/*
** Return a pointer to a Tcl_Obj structure with ref-count 0 that contains
1905
1906
1907
1908
1909
1910
1911








1912

1913
1914
1915
1916
1917
1918
1919
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124

2125
2126
2127
2128
2129
2130
2131
2132







+
+
+
+
+
+
+
+
-
+







    case SQLITE_FLOAT: {
      return Tcl_NewDoubleObj(sqlite3_column_double(pStmt, iCol));
    }
    case SQLITE_NULL: {
      return Tcl_NewStringObj(p->pDb->zNull, -1);
    }
  }
  if( utf8Encoding!=NULL ){
    Tcl_DString ds;
    Tcl_Obj *obj;
    Tcl_ExternalToUtfDString(utf8Encoding,
        (char*)sqlite3_column_text(pStmt, iCol), -1, &ds);
    obj = Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
    Tcl_DStringFree(&ds);
    return obj;

  }
  return Tcl_NewStringObj((char*)sqlite3_column_text(pStmt, iCol), -1);
}

/*
** If using Tcl version 8.6 or greater, use the NR functions to avoid
** recursive evaluation of scripts by the [db eval] and [db trans]
** commands. Even if the headers used while compiling the extension
2122
2123
2124
2125
2126
2127
2128
2129

2130
2131
2132
2133
2134
2135
2136
2335
2336
2337
2338
2339
2340
2341

2342
2343
2344
2345
2346
2347
2348
2349







-
+







    "interrupt",              "last_insert_rowid",     "nullvalue",
    "onecolumn",              "preupdate",             "profile",
    "progress",               "rekey",                 "restore",
    "rollback_hook",          "serialize",             "status",
    "timeout",                "total_changes",         "trace",
    "trace_v2",               "transaction",           "unlock_notify",
    "update_hook",            "version",               "wal_hook",
    0                        
    0
  };
  enum DB_enum {
    DB_AUTHORIZER,            DB_BACKUP,               DB_BIND_FALLBACK,
    DB_BUSY,                  DB_CACHE,                DB_CHANGES,
    DB_CLOSE,                 DB_COLLATE,              DB_COLLATION_NEEDED,
    DB_COMMIT_HOOK,           DB_COMPLETE,             DB_CONFIG,
    DB_COPY,                  DB_DESERIALIZE,          DB_ENABLE_LOAD_EXTENSION,
2232
2233
2234
2235
2236
2237
2238







2239

2240

2241



2242
2243







2244
2245
2246







2247


2248



2249
2250







2251
2252
2253
2254
2255
2256
2257
2258



2259
2260







2261
2262
2263
2264
2265
2266
2267
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458

2459
2460
2461
2462
2463
2464
2465


2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482

2483
2484
2485
2486
2487
2488


2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506


2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520







+
+
+
+
+
+
+
-
+

+

+
+
+
-
-
+
+
+
+
+
+
+



+
+
+
+
+
+
+
-
+
+

+
+
+
-
-
+
+
+
+
+
+
+








+
+
+
-
-
+
+
+
+
+
+
+







    }else if( objc==4 ){
      zSrcDb = Tcl_GetString(objv[2]);
      zDestFile = Tcl_GetString(objv[3]);
    }else{
      Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME");
      return TCL_ERROR;
    }
    if( utf8Encoding!=NULL ){
      Tcl_DString ds;
      Tcl_UtfToExternalDString(utf8Encoding, zDestFile, -1, &ds);
      rc = sqlite3_open_v2(Tcl_DStringValue(&ds), &pDest,
               SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0);
      Tcl_DStringFree(&ds);
    }else{
    rc = sqlite3_open_v2(zDestFile, &pDest,
      rc = sqlite3_open_v2(zDestFile, &pDest,
               SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE| pDb->openFlags, 0);
    }
    if( rc!=SQLITE_OK ){
      if( utf8Encoding!=NULL ){
        Tcl_DString ds;
        Tcl_ExternalToUtfDString(utf8Encoding, sqlite3_errmsg(pDest), -1, &ds);
      Tcl_AppendResult(interp, "cannot open target database: ",
           sqlite3_errmsg(pDest), (char*)0);
        Tcl_AppendResult(interp, "cannot open target database: ",
             Tcl_DStringValue(&ds), (char*)0);
        Tcl_DStringFree(&ds);
      }else{
        Tcl_AppendResult(interp, "cannot open target database: ",
             sqlite3_errmsg(pDest), (char*)0);
      }
      sqlite3_close(pDest);
      return TCL_ERROR;
    }
    if( utf8Encoding!=NULL ){
      Tcl_DString ds;
      Tcl_UtfToExternalDString(utf8Encoding, zSrcDb, -1, &ds);
      pBackup = sqlite3_backup_init(pDest, "main", pDb->db,
                    Tcl_DStringValue(&ds));
      Tcl_DStringFree(&ds);
    }else{
    pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb);
      pBackup = sqlite3_backup_init(pDest, "main", pDb->db, zSrcDb);
    }
    if( pBackup==0 ){
      if( utf8Encoding!=NULL ){
        Tcl_DString ds;
        Tcl_ExternalToUtfDString(utf8Encoding, sqlite3_errmsg(pDest), -1, &ds);
      Tcl_AppendResult(interp, "backup failed: ",
           sqlite3_errmsg(pDest), (char*)0);
        Tcl_AppendResult(interp, "backup failed: ",
             Tcl_DStringValue(&ds), (char*)0);
        Tcl_DStringFree(&ds);
      }else{
        Tcl_AppendResult(interp, "backup failed: ",
             sqlite3_errmsg(pDest), (char*)0);
      }
      sqlite3_close(pDest);
      return TCL_ERROR;
    }
    while(  (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
    sqlite3_backup_finish(pBackup);
    if( rc==SQLITE_DONE ){
      rc = TCL_OK;
    }else{
      if( utf8Encoding!=NULL ){
        Tcl_DString ds;
        Tcl_ExternalToUtfDString(utf8Encoding, sqlite3_errmsg(pDest), -1, &ds);
      Tcl_AppendResult(interp, "backup failed: ",
           sqlite3_errmsg(pDest), (char*)0);
        Tcl_AppendResult(interp, "backup failed: ",
             Tcl_DStringValue(&ds), (char*)0);
        Tcl_DStringFree(&ds);
      }else{
        Tcl_AppendResult(interp, "backup failed: ",
             sqlite3_errmsg(pDest), (char*)0);
      }
      rc = TCL_ERROR;
    }
    sqlite3_close(pDest);
    break;
  }

  /*    $db bind_fallback ?CALLBACK?
2426
2427
2428
2429
2430
2431
2432

2433
2434
2435
2436
2437






2438
2439
2440
2441
2442
2443
2444
2445
2446
2447

2448

2449
2450

2451
2452
2453
2454
2455
2456
2457
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708

2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719







+





+
+
+
+
+
+










+
-
+


+







  ** that function is called, invoke SCRIPT to evaluate the function.
  */
  case DB_COLLATE: {
    SqlCollate *pCollate;
    char *zName;
    char *zScript;
    int nScript;
    Tcl_DString ds;
    if( objc!=4 ){
      Tcl_WrongNumArgs(interp, 2, objv, "NAME SCRIPT");
      return TCL_ERROR;
    }
    zName = Tcl_GetString(objv[2]);
    if( utf8Encoding!=NULL ){
      Tcl_UtfToExternalDString(utf8Encoding, zName, -1, &ds);
      zName = Tcl_DStringValue(&ds);
    }else{
      Tcl_DStringInit(&ds);
    }
    zScript = Tcl_GetStringFromObj(objv[3], &nScript);
    pCollate = (SqlCollate*)Tcl_Alloc( sizeof(*pCollate) + nScript + 1 );
    if( pCollate==0 ) return TCL_ERROR;
    pCollate->interp = interp;
    pCollate->pNext = pDb->pCollate;
    pCollate->zScript = (char*)&pCollate[1];
    pDb->pCollate = pCollate;
    memcpy(pCollate->zScript, zScript, nScript+1);
    if( sqlite3_create_collation_v2(pDb->db, zName, SQLITE_UTF8,
        pCollate, tclSqlCollate, 0) ){
      Tcl_DStringFree(&ds);
      Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
      Tcl_SetResult(interp, (char*)SQLITEDB_ERRMSG(pDb), TCL_VOLATILE);
      return TCL_ERROR;
    }
    Tcl_DStringFree(&ds);
    break;
  }

  /*
  **     $db collation_needed SCRIPT
  **
  ** Create a new SQL collation function called NAME.  Whenever
2519
2520
2521
2522
2523
2524
2525









2526


2527
2528
2529
2530
2531
2532
2533
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







+
+
+
+
+
+
+
+
+
-
+
+







#ifndef SQLITE_OMIT_COMPLETE
    Tcl_Obj *pResult;
    int isComplete;
    if( objc!=3 ){
      Tcl_WrongNumArgs(interp, 2, objv, "SQL");
      return TCL_ERROR;
    }
    if( utf8Encoding!=NULL ){
      const char *pStr;
      int len;
      Tcl_DString ds;
      pStr = Tcl_GetStringFromObj(objv[2], &len);
      Tcl_UtfToExternalDString(utf8Encoding, pStr, len, &ds);
      isComplete = sqlite3_complete( Tcl_DStringValue(&ds) );
      Tcl_DStringFree(&ds);
    }else{
    isComplete = sqlite3_complete( Tcl_GetString(objv[2]) );
      isComplete = sqlite3_complete( Tcl_GetString(objv[2]) );
    }
    pResult = Tcl_GetObjResult(interp);
    Tcl_SetBooleanObj(pResult, isComplete);
#endif
    break;
  }

  /*    $db config ?OPTION? ?BOOLEAN?
2670
2671
2672
2673
2674
2675
2676






2677


2678
2679
2680
2681
2682
2683
2684
2685
2686


2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699



2700
2701







2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712


2713
2714
2715
2716
2717
2718
2719
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
2973
2974
2975
2976
2977
2978
2979
2980
2981
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







+
+
+
+
+
+
-
+
+








-
+
+













+
+
+
-
-
+
+
+
+
+
+
+










-
+
+







       strcmp(zConflict, "ignore"  ) != 0 &&
       strcmp(zConflict, "replace" ) != 0 ) {
      Tcl_AppendResult(interp, "Error: \"", zConflict,
            "\", conflict-algorithm must be one of: rollback, "
            "abort, fail, ignore, or replace", (char*)0);
      return TCL_ERROR;
    }
    if( utf8Encoding!=NULL ){
      Tcl_DString ds;
      Tcl_UtfToExternalDString(utf8Encoding, zTable, -1, &ds);
      zSql = sqlite3_mprintf("SELECT * FROM '%q'", Tcl_DStringValue(&ds));
      Tcl_DStringFree(&ds);
    }else{
    zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
      zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
    }
    if( zSql==0 ){
      Tcl_AppendResult(interp, "Error: no such table: ", zTable, (char*)0);
      return TCL_ERROR;
    }
    nByte = strlen30(zSql);
    rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
    sqlite3_free(zSql);
    if( rc ){
      Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0);
      Tcl_AppendResult(interp, "Error: ", (char*)SQLITEDB_ERRMSG(pDb),
          (char*)0);
      nCol = 0;
    }else{
      nCol = sqlite3_column_count(pStmt);
    }
    sqlite3_finalize(pStmt);
    if( nCol==0 ) {
      return TCL_ERROR;
    }
    zSql = (char *)sqlite3_malloc( nByte + 50 + nCol*2 );
    if( zSql==0 ) {
      Tcl_AppendResult(interp, "Error: can't malloc()", (char*)0);
      return TCL_ERROR;
    }
    if( utf8Encoding!=NULL ){
      Tcl_DString ds;
      Tcl_UtfToExternalDString(utf8Encoding, zTable, -1, &ds);
    sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
         zConflict, zTable);
      sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
                       zConflict, Tcl_DStringValue(&ds));
      Tcl_DStringFree(&ds);
    }else{
      sqlite3_snprintf(nByte+50, zSql, "INSERT OR %q INTO '%q' VALUES(?",
                       zConflict, zTable);
    }
    j = strlen30(zSql);
    for(i=1; i<nCol; i++){
      zSql[j++] = ',';
      zSql[j++] = '?';
    }
    zSql[j++] = ')';
    zSql[j] = 0;
    rc = sqlite3_prepare(pDb->db, zSql, -1, &pStmt, 0);
    sqlite3_free(zSql);
    if( rc ){
      Tcl_AppendResult(interp, "Error: ", sqlite3_errmsg(pDb->db), (char*)0);
      Tcl_AppendResult(interp, "Error: ", (char*)SQLITEDB_ERRMSG(pDb),
          (char*)0);
      sqlite3_finalize(pStmt);
      return TCL_ERROR;
    }
    in = Tcl_OpenFileChannel(interp, zFile, "r", 0666);
    if( in==0 ){
      sqlite3_finalize(pStmt);
      return TCL_ERROR;
2757
2758
2759
2760
2761
2762
2763






2764
2765


2766
2767
2768
2769
2770
2771
2772


2773
2774
2775
2776
2777
2778
2779
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







+
+
+
+
+
+

-
+
+






-
+
+







        break;
      }
      for(i=0; i<nCol; i++){
        /* check for null data, if so, bind as null */
        if( (azCol[i][0]==0) || (nNull>0 && strcmp(azCol[i], zNull)==0)
        ){
          sqlite3_bind_null(pStmt, i+1);
        }else if( utf8Encoding!=NULL ){
          Tcl_DString ds;
          Tcl_UtfToExternalDString(utf8Encoding, azCol[i], -1, &ds);
          sqlite3_bind_text(pStmt, i+1, Tcl_DStringValue(&ds),
              Tcl_DStringLength(&ds), SQLITE_TRANSIENT);
          Tcl_DStringFree(&ds);
        }else{
          sqlite3_bind_text(pStmt, i+1, azCol[i], strlen30(azCol[i]), SQLITE_STATIC);
          sqlite3_bind_text(pStmt, i+1, azCol[i], strlen30(azCol[i]),
              SQLITE_STATIC);
        }
      }
      sqlite3_step(pStmt);
      rc = sqlite3_reset(pStmt);
      Tcl_DStringSetLength(&str, 0);
      if( rc!=SQLITE_OK ){
        Tcl_AppendResult(interp,"Error: ", sqlite3_errmsg(pDb->db), (char*)0);
        Tcl_AppendResult(interp,"Error: ", (char*)SQLITEDB_ERRMSG(pDb),
            (char*)0);
        zCommit = "ROLLBACK";
        break;
      }
    }
    Tcl_DStringFree(&str);
    sqlite3_free(azCol);
    Tcl_Close(interp, in);
2853
2854
2855
2856
2857
2858
2859







2860


2861
2862
2863
2864
2865
2866
2867
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163

3164
3165
3166
3167
3168
3169
3170
3171
3172







+
+
+
+
+
+
+
-
+
+







      int flags;
      if( len>0 ) memcpy(pData, pBA, len);
      if( isReadonly ){
        flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_READONLY;
      }else{
        flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
      }
      if( utf8Encoding!=NULL ){
        Tcl_DString ds;
        Tcl_ExternalToUtfDString(utf8Encoding, (char*)zSchema, -1, &ds);
        xrc = sqlite3_deserialize(pDb->db, Tcl_DStringValue(&ds),
                  pData, len, len, flags);
        Tcl_DStringFree(&ds);
      } else {
      xrc = sqlite3_deserialize(pDb->db, zSchema, pData, len, len, flags);
        xrc = sqlite3_deserialize(pDb->db, zSchema, pData, len, len, flags);
      }
      if( xrc ){
        Tcl_AppendResult(interp, "unable to set MEMDB content", (char*)0);
        rc = TCL_ERROR;
      }
      if( mxSize>0 ){
        sqlite3_file_control(pDb->db, zSchema,SQLITE_FCNTL_SIZE_LIMIT,&mxSize);
      }
3044
3045
3046
3047
3048
3049
3050

3051
3052
3053
3054
3055
3056
3057
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363







+







    int flags = SQLITE_UTF8;
    SqlFunc *pFunc;
    Tcl_Obj *pScript;
    char *zName;
    int nArg = -1;
    int i;
    int eType = SQLITE_NULL;
    Tcl_DString ds;
    if( objc<4 ){
      Tcl_WrongNumArgs(interp, 2, objv, "NAME ?SWITCHES? SCRIPT");
      return TCL_ERROR;
    }
    for(i=3; i<(objc-1); i++){
      const char *z = Tcl_GetString(objv[i]);
      int n = strlen30(z);
3097
3098
3099
3100
3101
3102
3103






3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114

3115
3116
3117

3118
3119
3120
3121
3122
3123
3124
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429

3430
3431
3432
3433
3434
3435
3436
3437







+
+
+
+
+
+











+


-
+







        );
        return TCL_ERROR;
      }
    }

    pScript = objv[objc-1];
    zName = Tcl_GetString(objv[2]);
    if( utf8Encoding!=NULL ){
      Tcl_UtfToExternalDString(utf8Encoding, zName, -1, &ds);
      zName = Tcl_DStringValue(&ds);
    }else{
      Tcl_DStringInit(&ds);
    }
    pFunc = findSqlFunc(pDb, zName);
    if( pFunc==0 ) return TCL_ERROR;
    if( pFunc->pScript ){
      Tcl_DecrRefCount(pFunc->pScript);
    }
    pFunc->pScript = pScript;
    Tcl_IncrRefCount(pScript);
    pFunc->useEvalObjv = safeToUseEvalObjv(interp, pScript);
    pFunc->eType = eType;
    rc = sqlite3_create_function_v2(pDb->db, zName, nArg, flags,
        pFunc, tclSqlFunc, 0, 0, 0);
    Tcl_DStringFree(&ds);
    if( rc!=SQLITE_OK ){
      rc = TCL_ERROR;
      Tcl_SetResult(interp, (char *)sqlite3_errmsg(pDb->db), TCL_VOLATILE);
      Tcl_SetResult(interp, (char*)SQLITEDB_ERRMSG(pDb), TCL_VOLATILE);
    }
    break;
  }

  /*
  **     $db incrblob ?-readonly? ?DB? TABLE COLUMN ROWID
  */
3147
3148
3149
3150
3151
3152
3153





3154
3155
3156












3157
3158
3159
3160
3161
3162
3163
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471



3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490







+
+
+
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+







      zDb = Tcl_GetString(objv[2+isReadonly]);
    }
    zTable = Tcl_GetString(objv[objc-3]);
    zColumn = Tcl_GetString(objv[objc-2]);
    rc = Tcl_GetWideIntFromObj(interp, objv[objc-1], &iRow);

    if( rc==TCL_OK ){
      if( utf8Encoding!=NULL ){
        Tcl_DString ds1, ds2, ds3;
        Tcl_UtfToExternalDString(utf8Encoding, zDb, -1, &ds1);
        Tcl_UtfToExternalDString(utf8Encoding, zTable, -1, &ds2);
        Tcl_UtfToExternalDString(utf8Encoding, zColumn, -1, &ds3);
      rc = createIncrblobChannel(
          interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly
      );
        rc = createIncrblobChannel(
            interp, pDb, Tcl_DStringValue(&ds1), Tcl_DStringValue(&ds2),
            Tcl_DStringValue(&ds3), (sqlite3_int64)iRow, isReadonly
        );
        Tcl_DStringFree(&ds1);
        Tcl_DStringFree(&ds2);
        Tcl_DStringFree(&ds3);
      }else{
        rc = createIncrblobChannel(
            interp, pDb, zDb, zTable, zColumn, (sqlite3_int64)iRow, isReadonly
        );
      }
    }
#endif
    break;
  }

  /*
  **     $db interrupt
3338
3339
3340
3341
3342
3343
3344







3345
3346



3347



3348
3349







3350
3351
3352







3353


3354
3355
3356

3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376

3377
3378
3379
3380
3381
3382
3383
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
3723
3724
3725
3726

3727
3728
3729
3730
3731
3732
3733
3734







+
+
+
+
+
+
+
-
-
+
+
+

+
+
+
-
-
+
+
+
+
+
+
+



+
+
+
+
+
+
+
-
+
+


-
+



















-
+







    }else if( objc==4 ){
      zDestDb = Tcl_GetString(objv[2]);
      zSrcFile = Tcl_GetString(objv[3]);
    }else{
      Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE? FILENAME");
      return TCL_ERROR;
    }
    if( utf8Encoding!=NULL ){
      Tcl_DString ds;
      Tcl_UtfToExternalDString(utf8Encoding, zSrcFile, -1, &ds);
      rc = sqlite3_open_v2(Tcl_DStringValue(&ds), &pSrc,
               SQLITE_OPEN_READONLY | pDb->openFlags, 0);
      Tcl_DStringFree(&ds);
    }else{
    rc = sqlite3_open_v2(zSrcFile, &pSrc,
                         SQLITE_OPEN_READONLY | pDb->openFlags, 0);
      rc = sqlite3_open_v2(zSrcFile, &pSrc,
                           SQLITE_OPEN_READONLY | pDb->openFlags, 0);
    }
    if( rc!=SQLITE_OK ){
      if( utf8Encoding!=NULL ){
        Tcl_DString ds;
        Tcl_ExternalToUtfDString(utf8Encoding, sqlite3_errmsg(pSrc), -1, &ds);
      Tcl_AppendResult(interp, "cannot open source database: ",
           sqlite3_errmsg(pSrc), (char*)0);
        Tcl_AppendResult(interp, "cannot open source database: ",
             Tcl_DStringValue(&ds), (char*)0);
        Tcl_DStringFree(&ds);
      }else{
        Tcl_AppendResult(interp, "cannot open source database: ",
             sqlite3_errmsg(pSrc), (char*)0);
      }
      sqlite3_close(pSrc);
      return TCL_ERROR;
    }
    if( utf8Encoding!=NULL ){
      Tcl_DString ds;
      Tcl_UtfToExternalDString(utf8Encoding, zDestDb, -1, &ds);
      pBackup = sqlite3_backup_init(pDb->db, Tcl_DStringValue(&ds), pSrc,
                    "main");
      Tcl_DStringFree(&ds);
    }else{
    pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main");
      pBackup = sqlite3_backup_init(pDb->db, zDestDb, pSrc, "main");
    }
    if( pBackup==0 ){
      Tcl_AppendResult(interp, "restore failed: ",
           sqlite3_errmsg(pDb->db), (char*)0);
          (char*)SQLITEDB_ERRMSG(pDb), (char*)0);
      sqlite3_close(pSrc);
      return TCL_ERROR;
    }
    while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
              || rc==SQLITE_BUSY ){
      if( rc==SQLITE_BUSY ){
        if( nTimeout++ >= 3 ) break;
        sqlite3_sleep(100);
      }
    }
    sqlite3_backup_finish(pBackup);
    if( rc==SQLITE_DONE ){
      rc = TCL_OK;
    }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
      Tcl_AppendResult(interp, "restore failed: source database busy",
                       (char*)0);
      rc = TCL_ERROR;
    }else{
      Tcl_AppendResult(interp, "restore failed: ",
           sqlite3_errmsg(pDb->db), (char*)0);
          (char*)SQLITEDB_ERRMSG(pDb), (char*)0);
      rc = TCL_ERROR;
    }
    sqlite3_close(pSrc);
    break;
  }

  /*
3395
3396
3397
3398
3399
3400
3401







3402



3403
3404
3405
3406
3407
3408
3409
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759

3760
3761
3762
3763
3764
3765
3766
3767
3768
3769







+
+
+
+
+
+
+
-
+
+
+







    sqlite3_int64 sz = 0;
    unsigned char *pData;
    if( objc!=2 && objc!=3 ){
      Tcl_WrongNumArgs(interp, 2, objv, "?DATABASE?");
      rc = TCL_ERROR;
    }else{
      int needFree;
      if( utf8Encoding!=NULL ){
        Tcl_DString ds;
        Tcl_ExternalToUtfDString(utf8Encoding, (char*)zSchema, -1, &ds);
        pData = sqlite3_serialize(pDb->db, Tcl_DStringValue(&ds), &sz,
                    SQLITE_SERIALIZE_NOCOPY);
        Tcl_DStringFree(&ds);
      } else {
      pData = sqlite3_serialize(pDb->db, zSchema, &sz, SQLITE_SERIALIZE_NOCOPY);
        pData = sqlite3_serialize(pDb->db, zSchema, &sz,
                    SQLITE_SERIALIZE_NOCOPY);
      }
      if( pData ){
        needFree = 0;
      }else{
        pData = sqlite3_serialize(pDb->db, zSchema, &sz, 0);
        needFree = 1;
      }
      Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(pData,sz));
3639
3640
3641
3642
3643
3644
3645
3646

3647
3648
3649
3650
3651
3652
3653
3999
4000
4001
4002
4003
4004
4005

4006
4007
4008
4009
4010
4011
4012
4013







-
+







    pScript = objv[objc-1];

    /* Run the SQLite BEGIN command to open a transaction or savepoint. */
    pDb->disableAuth++;
    rc = sqlite3_exec(pDb->db, zBegin, 0, 0, 0);
    pDb->disableAuth--;
    if( rc!=SQLITE_OK ){
      Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
      Tcl_AppendResult(interp, (char*)SQLITEDB_ERRMSG(pDb), (char*)0);
      return TCL_ERROR;
    }
    pDb->nTransaction++;

    /* If using NRE, schedule a callback to invoke the script pScript, then
    ** a second callback to commit (or rollback) the transaction or savepoint
    ** opened above. If not using NRE, evaluate the script directly, then
3688
3689
3690
3691
3692
3693
3694
3695

3696
3697
3698
3699
3700
3701
3702
4048
4049
4050
4051
4052
4053
4054

4055
4056
4057
4058
4059
4060
4061
4062







-
+







        xNotify = DbUnlockNotify;
        pNotifyArg = (void *)pDb;
        pDb->pUnlockNotify = objv[2];
        Tcl_IncrRefCount(pDb->pUnlockNotify);
      }

      if( sqlite3_unlock_notify(pDb->db, xNotify, pNotifyArg) ){
        Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
        Tcl_AppendResult(interp, (char*)SQLITEDB_ERRMSG(pDb), (char*)0);
        rc = TCL_ERROR;
      }
    }
#endif
    break;
  }

3772
3773
3774
3775
3776
3777
3778
3779

3780
3781
3782
3783
3784
3785
3786
4132
4133
4134
4135
4136
4137
4138

4139
4140
4141
4142
4143
4144
4145
4146







-
+







        }

        if( rc==SQLITE_OK ){
          Tcl_Obj *pObj;
          pObj = Tcl_NewStringObj((char*)sqlite3_value_text(pValue), -1);
          Tcl_SetObjResult(interp, pObj);
        }else{
          Tcl_AppendResult(interp, sqlite3_errmsg(pDb->db), (char*)0);
          Tcl_AppendResult(interp, (char*)SQLITEDB_ERRMSG(pDb), (char*)0);
          return TCL_ERROR;
        }
      }
    }
#endif /* SQLITE_ENABLE_PREUPDATE_HOOK */
    break;
  }
4028
4029
4030
4031
4032
4033
4034

4035
4036
4037
4038













4039


4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052






4053


4054
4055
4056
4057
4058
4059
4060
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412

4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433

4434
4435
4436
4437
4438
4439
4440
4441
4442







+




+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+













+
+
+
+
+
+
-
+
+







      Tcl_AppendResult(interp, "unknown option: ", zArg, (char*)0);
      return TCL_ERROR;
    }
  }
  zErrMsg = 0;
  p = (SqliteDb*)Tcl_Alloc( sizeof(*p) );
  memset(p, 0, sizeof(*p));
  Tcl_DStringInit(&p->dsErr);
  if( zFile==0 ) zFile = "";
  if( bTranslateFileName ){
    zFile = Tcl_TranslateFileName(interp, zFile, &translatedFilename);
  }
  if( utf8Encoding!=NULL ){
    Tcl_DString ds1, ds2;
    Tcl_UtfToExternalDString(utf8Encoding, zFile, -1, &ds1);
    if ( zVfs!=NULL ){
      Tcl_UtfToExternalDString(utf8Encoding, zVfs, -1, &ds2);
      zVfs = Tcl_DStringValue(&ds2);
    }
    rc = sqlite3_open_v2(Tcl_DStringValue(&ds1), &p->db, flags, zVfs);
    Tcl_DStringFree(&ds1);
    if( zVfs!=NULL ){
      Tcl_DStringFree(&ds2);
    }
  }else{
  rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs);
    rc = sqlite3_open_v2(zFile, &p->db, flags, zVfs);
  }
  if( bTranslateFileName ){
    Tcl_DStringFree(&translatedFilename);
  }
  if( p->db ){
    if( SQLITE_OK!=sqlite3_errcode(p->db) ){
      zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(p->db));
      sqlite3_close(p->db);
      p->db = 0;
    }
  }else{
    zErrMsg = sqlite3_mprintf("%s", sqlite3_errstr(rc));
  }
  if( p->db==0 ){
    if( utf8Encoding!=NULL && zErrMsg ){
      Tcl_DString ds;
      Tcl_ExternalToUtfDString(utf8Encoding, zErrMsg, -1, &ds);
      Tcl_SetResult(interp, Tcl_DStringValue(&ds), TCL_VOLATILE);
      Tcl_DStringFree(&ds);
    }else{
    Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
      Tcl_SetResult(interp, zErrMsg, TCL_VOLATILE);
    }
    Tcl_Free((char*)p);
    sqlite3_free(zErrMsg);
    return TCL_ERROR;
  }
  p->maxStmt = NUM_PREPARED_STMTS;
  p->openFlags = flags & SQLITE_OPEN_URI;
  p->interp = interp;
4126
4127
4128
4129
4130
4131
4132







































4133
4134
4135

4136
4137
4138






















4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157






4158
4159
4160
4161
4162
4163
4164
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
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
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596


4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
+

-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

















-
-
+
+
+
+
+
+







    /* The "sqlite" alias is undocumented.  It is here only to support
    ** legacy scripts.  All new scripts should use only the "sqlite3"
    ** command. */
    Tcl_CreateObjCommand(interp, "sqlite", (Tcl_ObjCmdProc*)DbMain, 0, 0);
#endif
    rc = Tcl_PkgProvideEx(interp, "sqlite3", PACKAGE_VERSION, NULL);
  }
  if( !utf8EncInit ){
    Tcl_Encoding enc;
    Tcl_MutexLock(&utf8EncMutex);
    if( ++utf8EncInit>1 ){
      goto utf8EncInitDone;
    }
    enc = Tcl_GetEncoding(NULL, "utf-8");
    /*
     ** Try to detect the level of UTF-8 support in the Tcl core.
     */
    if( enc!=NULL ){
      const char probe1[] = { 0xF0, 0x9F, 0x98, 0x82 };
      const char probe1sp[] = { 0xED, 0xA0, 0xBD, 0xED, 0xB8, 0x82 };
      Tcl_DString ds;
      Tcl_ExternalToUtfDString(enc, probe1, sizeof(probe1), &ds);
      if( Tcl_DStringLength(&ds)==sizeof(probe1) &&
          memcmp(probe1, Tcl_DStringValue(&ds), sizeof(probe1))==0 ){
        /*
         ** Tcl core supports full Unicode.
         */
        Tcl_FreeEncoding(enc);
      }else if( Tcl_DStringLength(&ds)==sizeof(probe1sp) &&
          memcmp(probe1sp, Tcl_DStringValue(&ds), sizeof(probe1sp))==0 ){
        /*
         ** Tcl core supports full Unicode using surrogate pairs,
         ** keep utf8Encoding for conversion to/from 4 byte UTF-8 sequences.
         */
        utf8Encoding = enc;
      }else{
        /*
         ** Tcl core supports BMP only, let SQLite handle corner cases.
         */
        Tcl_FreeEncoding(enc);
      }
      Tcl_DStringFree(&ds);
    }
utf8EncInitDone:
    Tcl_MutexUnlock(&utf8EncMutex);
  }
  return rc;
}
DLLEXPORT int Tclsqlite3_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }

#ifdef ENABLE_UNLOAD
DLLEXPORT int Sqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
DLLEXPORT int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
DLLEXPORT int Sqlite3_Unload(Tcl_Interp *interp, int flags){
  Tcl_MutexLock(&utf8EncMutex);
  if( --utf8EncInit<1 ){
    if( utf8Encoding!=NULL ){
      Tcl_FreeEncoding(utf8Encoding);
      utf8Encoding = NULL;
    }
    utf8EncInit = 0;
  }
  Tcl_MutexUnlock(&utf8EncMutex);
  return TCL_OK;
}
#endif

DLLEXPORT int Tclsqlite3_Init(Tcl_Interp *interp){
  return Sqlite3_Init(interp);
}

#ifdef ENABLE_UNLOAD
DLLEXPORT int Tclsqlite3_Unload(Tcl_Interp *interp, int flags){
  return Sqlite3_Unload(interp, flags);
}
#endif

/* Because it accesses the file-system and uses persistent state, SQLite
** is not considered appropriate for safe interpreters.  Hence, we cause
** the _SafeInit() interfaces return TCL_ERROR.
*/
DLLEXPORT int Sqlite3_SafeInit(Tcl_Interp *interp){ return TCL_ERROR; }
#ifdef ENABLE_UNLOAD
DLLEXPORT int Sqlite3_SafeUnload(Tcl_Interp *interp, int flags){return TCL_ERROR;}
#endif



#ifndef SQLITE_3_SUFFIX_ONLY
int Sqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
int Tclsqlite_Init(Tcl_Interp *interp){ return Sqlite3_Init(interp); }
#ifdef ENABLE_UNLOAD
int Sqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
int Tclsqlite_Unload(Tcl_Interp *interp, int flags){ return TCL_OK; }
int Sqlite_Unload(Tcl_Interp *interp, int flags){
  return SqLite3_Unload(interp, flags);
}
int Tclsqlite_Unload(Tcl_Interp *interp, int flags){
  return Sqlite3_Unload(interp, flags);
}
#endif
#endif

/*
** If the TCLSH macro is defined, add code to make a stand-alone program.
*/
#if defined(TCLSH)

Changes to jni/tcl/pkgs/tdbcmysql1.1.5/generic/tdbcmysql.c.

114
115
116
117
118
119
120

121
122
123
124
125
126
127
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128







+







 *	referring to it, which avoids taking down the hDBC until the
 *	other objects that refer to it vanish.
 */

typedef struct ConnectionData {
    int refCount;		/* Reference count. */
    PerInterpData* pidata;	/* Per-interpreter data */
    Tcl_Encoding enc;		/* "utf-8" encoding */
    MYSQL* mysqlPtr;		/* MySql connection handle */
    unsigned int nCollations;	/* Number of collations defined */
    int* collationSizes;	/* Character lengths indexed by collation ID */
    int flags;
} ConnectionData;

/*
153
154
155
156
157
158
159
160

161
162
163
164
165
166
167
154
155
156
157
158
159
160

161
162
163
164
165
166
167
168







-
+







 *	structure is reference counted as well.
 */

typedef struct StatementData {
    int refCount;		/* Reference count */
    ConnectionData* cdata;	/* Data for the connection to which this
				 * statement pertains. */
    Tcl_Obj* subVars;	        /* List of variables to be substituted, in the
    Tcl_Obj* subVars;		/* List of variables to be substituted, in the
				 * order in which they appear in the
				 * statement */
    struct ParamData* params;	/* Data types and attributes of parameters */
    Tcl_Obj* nativeSql;		/* Native SQL statement to pass into
				 * MySQL */
    MYSQL_STMT* stmtPtr;	/* MySQL statement handle */
    MYSQL_RES* metadataPtr;	/* MySQL result set metadata */
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
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







-
+










-
+


















-
+







    const char* query;	/* How to determine the option value? */
} ConnOptions [] = {
    { "-compress",    TYPE_FLAG,      CLIENT_COMPRESS,	  0,
      "SELECT '', @@SLAVE_COMPRESSED_PROTOCOL" },
    { "-database",    TYPE_STRING,    INDX_DB,		  CONN_OPT_FLAG_MOD,
      "SELECT '', DATABASE();"},
    { "-db",	      TYPE_STRING,    INDX_DB, 		  CONN_OPT_FLAG_MOD
                                                        | CONN_OPT_FLAG_ALIAS,
							| CONN_OPT_FLAG_ALIAS,
      "SELECT '', DATABASE()" },
    { "-encoding",    TYPE_ENCODING,  0,		  0,
      "SELECT '', 'utf-8'" },
    { "-host",	      TYPE_STRING,    INDX_HOST,	  0,
      "SHOW SESSION VARIABLES WHERE VARIABLE_NAME = 'hostname'" },
    { "-interactive", TYPE_FLAG,      CLIENT_INTERACTIVE, 0,
      "SELECT '', 0" },
    { "-isolation",   TYPE_ISOLATION, 0,		  CONN_OPT_FLAG_MOD,
      "SELECT '', LCASE(REPLACE(@@TX_ISOLATION, '-', ''))" },
    { "-passwd",      TYPE_STRING,    INDX_PASSWD,	  CONN_OPT_FLAG_MOD
                                                        | CONN_OPT_FLAG_ALIAS,
							| CONN_OPT_FLAG_ALIAS,
      "SELECT '', ''" },
    { "-password",    TYPE_STRING,    INDX_PASSWD,	  CONN_OPT_FLAG_MOD,
      "SELECT '', ''" },
    { "-port",	      TYPE_PORT,      0,		  0,
      "SHOW SESSION VARIABLES WHERE VARIABLE_NAME = 'port'" },
    { "-readonly",    TYPE_READONLY,  0,		  0,
      "SELECT '', 0" },
    { "-socket",      TYPE_STRING,    INDX_SOCKET,	  0,
      "SHOW SESSION VARIABLES WHERE VARIABLE_NAME = 'socket'" },
    { "-ssl_ca",      TYPE_STRING,    INDX_SSLCA,	  CONN_OPT_FLAG_SSL,
      "SELECT '', @@SSL_CA"},
    { "-ssl_capath",  TYPE_STRING,    INDX_SSLCAPATH,	  CONN_OPT_FLAG_SSL,
      "SELECT '', @@SSL_CAPATH" },
    { "-ssl_cert",    TYPE_STRING,    INDX_SSLCERT,	  CONN_OPT_FLAG_SSL,
      "SELECT '', @@SSL_CERT" },
    { "-ssl_cipher",  TYPE_STRING,    INDX_SSLCIPHER,	  CONN_OPT_FLAG_SSL,
      "SELECT '', @@SSL_CIPHER" },
    { "-ssl_cypher",  TYPE_STRING,    INDX_SSLCIPHER,	  CONN_OPT_FLAG_SSL
                                                        | CONN_OPT_FLAG_ALIAS,
							| CONN_OPT_FLAG_ALIAS,
      "SELECT '', @@SSL_CIPHER" },
    { "-ssl_key",     TYPE_STRING,    INDX_SSLKEY,	  CONN_OPT_FLAG_SSL,
      "SELECT '', @@SSL_KEY" },
    { "-timeout",     TYPE_TIMEOUT,   0,		  CONN_OPT_FLAG_MOD,
      "SELECT '', @@WAIT_TIMEOUT" },
    { "-user",	      TYPE_STRING,    INDX_USER,	  CONN_OPT_FLAG_MOD,
      "SELECT '', USER()" },
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
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







+
+
+
+














-
-
+
+
+







    ISOL_REPEATABLE_READ,
    ISOL_SERIALIZABLE,
    ISOL_NONE = -1
};

/* Declarations of static functions appearing in this file */

static Tcl_Obj* StringObjFromExternal(ConnectionData* cdata,
				      const char* string, int length);
static char*  ExternalFromStringObj(ConnectionData* cdata, Tcl_Obj* strObj,
				    Tcl_DString* dsPtr);
static MYSQL_BIND* MysqlBindAlloc(int nBindings);
static MYSQL_BIND* MysqlBindIndex(MYSQL_BIND* b, int i);
static void* MysqlBindAllocBuffer(MYSQL_BIND* b, int i, unsigned long len);
static void MysqlBindFreeBuffer(MYSQL_BIND* b, int i);
static void MysqlBindSetBufferType(MYSQL_BIND* b, int i,
				   enum enum_field_types t);
static void* MysqlBindGetBuffer(MYSQL_BIND* b, int i);
static unsigned long MysqlBindGetBufferLength(MYSQL_BIND* b, int i);
static void MysqlBindSetLength(MYSQL_BIND* b, int i, unsigned long* p);
static void MysqlBindSetIsNull(MYSQL_BIND* b, int i, my_bool* p);
static void MysqlBindSetError(MYSQL_BIND* b, int i, my_bool* p);

static MYSQL_FIELD* MysqlFieldIndex(MYSQL_FIELD* fields, int i);

static void TransferMysqlError(Tcl_Interp* interp, MYSQL* mysqlPtr);
static void TransferMysqlStmtError(Tcl_Interp* interp, MYSQL_STMT* mysqlPtr);
static void TransferMysqlError(ConnectionData* cdata, Tcl_Interp* interp);
static void TransferMysqlStmtError(ConnectionData* cdata, Tcl_Interp* interp,
				   MYSQL_STMT* mysqlPtr);

static Tcl_Obj* QueryConnectionOption(ConnectionData* cdata, Tcl_Interp* interp,
				      int optionNum);
static int ConfigureConnection(ConnectionData* cdata, Tcl_Interp* interp,
			       int objc, Tcl_Obj *const objv[], int skip);
static int ConnectionConstructor(ClientData clientData, Tcl_Interp* interp,
				 Tcl_ObjectContext context,
455
456
457
458
459
460
461
462


463
464
465
466
467
468
469
461
462
463
464
465
466
467

468
469
470
471
472
473
474
475
476







-
+
+







static void DeleteConnection(ConnectionData* cdata);
static int CloneConnection(Tcl_Interp* interp, ClientData oldClientData,
			   ClientData* newClientData);

static StatementData* NewStatement(ConnectionData* cdata);
static MYSQL_STMT* AllocAndPrepareStatement(Tcl_Interp* interp,
					    StatementData* sdata);
static Tcl_Obj* ResultDescToTcl(MYSQL_RES* resultDesc, int flags);
static Tcl_Obj* ResultDescToTcl(ConnectionData* cdata,
				MYSQL_RES* resultDesc, int flags);

static int StatementConstructor(ClientData clientData, Tcl_Interp* interp,
				Tcl_ObjectContext context,
				int objc, Tcl_Obj *const objv[]);
static int StatementParamtypeMethod(ClientData clientData, Tcl_Interp* interp,
				    Tcl_ObjectContext context,
				    int objc, Tcl_Obj *const objv[]);
706
707
708
709
710
711
712
713























































714
715
716
717
718
719
720
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







-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







/* Methods to create on the result set class */

const static Tcl_MethodType* ResultSetMethods[] = {
    &ResultSetColumnsMethodType,
    &ResultSetRowcountMethodType,
    NULL
};


/*
 *-----------------------------------------------------------------------------
 *
 * StringObjFromExternal --
 *
 *	Return a new Tcl string object from a string with conversion from
 *	external to internal utf-8 encoding.
 *
 * Results:
 *	New string object.
 *
 *-----------------------------------------------------------------------------
 */

static Tcl_Obj*
StringObjFromExternal(
    ConnectionData* cdata,	/* Connection data */
    const char* string,		/* String to convert */
    int length			/* Length of string */
) {
    Tcl_DString ds;
    Tcl_Obj* obj;

    Tcl_ExternalToUtfDString(cdata->enc, string, length, &ds);
    obj = Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
    Tcl_DStringFree(&ds);
    return obj;
}

/*
 *-----------------------------------------------------------------------------
 *
 * ExternalFromStringObj --
 *
 *	Return a string from Tcl_Obj* in an uninitialized Tcl_DString
 *	which the caller must free. The resulting string is converted
 *	to external utf-8 encoding.
 *
 * Results:
 *	New string in provided Tcl_DString.
 *
 *-----------------------------------------------------------------------------
 */

static char*
ExternalFromStringObj(
    ConnectionData* cdata,	/* Connection data */
    Tcl_Obj* strObj,		/* Tcl_Obj to convert */
    Tcl_DString *dsPtr		/* Result area */
) {
    Tcl_UtfToExternalDString(cdata->enc, Tcl_GetString(strObj), -1, dsPtr);
    return Tcl_DStringValue(dsPtr);
}

/*
 *-----------------------------------------------------------------------------
 *
 * MysqlBindAlloc --
 *
 *	Allocate a number of MYSQL_BIND structures.
 *
978
979
980
981
982
983
984

985

986
987

988
989
990
991
992
993
994
995
996
997
998

999

1000
1001
1002
1003
1004
1005
1006
1039
1040
1041
1042
1043
1044
1045
1046

1047

1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061

1062
1063
1064
1065
1066
1067
1068
1069







+
-
+
-

+











+
-
+







 *	Sets the interpreter result and error code to describe the SQL error
 *
 *-----------------------------------------------------------------------------
 */

static void
TransferMysqlError(
    ConnectionData* cdata,	/* Connection data */
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_Interp* interp		/* Tcl interpreter */
    MYSQL* mysqlPtr		/* MySQL connection handle */
) {
    MYSQL* mysqlPtr = cdata->mysqlPtr;
    const char* sqlstate = mysql_sqlstate(mysqlPtr);
    Tcl_Obj* errorCode = Tcl_NewObj();
    Tcl_ListObjAppendElement(NULL, errorCode, Tcl_NewStringObj("TDBC", -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewStringObj(Tdbc_MapSqlState(sqlstate), -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewStringObj(sqlstate, -1));
    Tcl_ListObjAppendElement(NULL, errorCode, Tcl_NewStringObj("MYSQL", -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewWideIntObj(mysql_errno(mysqlPtr)));
    Tcl_SetObjErrorCode(interp, errorCode);
    Tcl_SetObjResult(interp, StringObjFromExternal(cdata,
    Tcl_SetObjResult(interp, Tcl_NewStringObj(mysql_error(mysqlPtr), -1));
						   mysql_error(mysqlPtr), -1));
}

/*
 *-----------------------------------------------------------------------------
 *
 * TransferMysqlStmtError --
 *
1014
1015
1016
1017
1018
1019
1020

1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035



1036
1037
1038
1039
1040
1041
1042
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098

1099
1100
1101
1102
1103
1104
1105
1106
1107
1108







+














-
+
+
+







 *	Sets the interpreter result and error code to describe the SQL error
 *
 *-----------------------------------------------------------------------------
 */

static void
TransferMysqlStmtError(
    ConnectionData* cdata,	/* Connection data */
    Tcl_Interp* interp,		/* Tcl interpreter */
    MYSQL_STMT* stmtPtr		/* MySQL statment handle */
) {
    const char* sqlstate = mysql_stmt_sqlstate(stmtPtr);
    Tcl_Obj* errorCode = Tcl_NewObj();
    Tcl_ListObjAppendElement(NULL, errorCode, Tcl_NewStringObj("TDBC", -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewStringObj(Tdbc_MapSqlState(sqlstate), -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewStringObj(sqlstate, -1));
    Tcl_ListObjAppendElement(NULL, errorCode, Tcl_NewStringObj("MYSQL", -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewWideIntObj(mysql_stmt_errno(stmtPtr)));
    Tcl_SetObjErrorCode(interp, errorCode);
    Tcl_SetObjResult(interp, Tcl_NewStringObj(mysql_stmt_error(stmtPtr), -1));
    Tcl_SetObjResult(interp,
		     StringObjFromExternal(cdata,
					   mysql_stmt_error(stmtPtr), -1));
}

/*
 *-----------------------------------------------------------------------------
 *
 * QueryConnectionOption --
 *
1059
1060
1061
1062
1063
1064
1065
1066

1067
1068
1069
1070
1071

1072
1073
1074
1075
1076
1077
1078
1079
1080

1081
1082
1083
1084
1085
1086
1087
1088

1089
1090
1091
1092
1093
1094
1095
1125
1126
1127
1128
1129
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
1158
1159
1160
1161







-
+




-
+








-
+







-
+







    MYSQL_RES* result;		/* Result of the MySQL query for the option */
    MYSQL_ROW row;		/* Row of the result set */
    int fieldCount;		/* Number of fields in a row */
    unsigned long* lengths;	/* Character lengths of the fields */
    Tcl_Obj* retval;		/* Return value */

    if (mysql_query(cdata->mysqlPtr, ConnOptions[optionNum].query)) {
	TransferMysqlError(interp, cdata->mysqlPtr);
	TransferMysqlError(cdata, interp);
	return NULL;
    }
    result = mysql_store_result(cdata->mysqlPtr);
    if (result == NULL) {
	TransferMysqlError(interp, cdata->mysqlPtr);
	TransferMysqlError(cdata, interp);
	return NULL;
    }
    fieldCount = mysql_num_fields(result);
    if (fieldCount < 2) {
	retval = cdata->pidata->literals[LIT_EMPTY];
    } else {
	if ((row = mysql_fetch_row(result)) == NULL) {
	    if (mysql_errno(cdata->mysqlPtr)) {
		TransferMysqlError(interp, cdata->mysqlPtr);
		TransferMysqlError(cdata, interp);
		mysql_free_result(result);
		return NULL;
	    } else {
		retval = cdata->pidata->literals[LIT_EMPTY];
	    }
	} else {
	    lengths = mysql_fetch_lengths(result);
	    retval = Tcl_NewStringObj(row[1], lengths[1]);
	    retval = StringObjFromExternal(cdata, row[1], lengths[1]);
	}
    }
    mysql_free_result(result);
    return retval;
}

/*
1117
1118
1119
1120
1121
1122
1123

1124
1125
1126
1127
1128
1129
1130
1131
1132
1133

1134
1135
1136
1137
1138
1139
1140
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208







+










+







    int objc,			/* Parameter count */
    Tcl_Obj* const objv[],	/* Parameter data */
    int skip			/* Number of parameters to skip */
) {

    const char* stringOpts[INDX_MAX];
				/* String-valued options */
    Tcl_DString stringBufs[INDX_MAX];
    unsigned long mysqlFlags=0;	/* Connection flags */
    int sslFlag = 0;		/* Flag==1 if SSL configuration is needed */
    int optionIndex;		/* Index of the current option in ConnOptions */
    int optionValue;		/* Integer value of the current option */
    unsigned short port = 0;	/* Server port number */
    int isolation = ISOL_NONE;	/* Isolation level */
    int timeout = 0;		/* Timeout value */
    int i;
    Tcl_Obj* retval;
    Tcl_Obj* optval;
    int result = TCL_ERROR;

    if (cdata->mysqlPtr != NULL) {

	/* Query configuration options on an existing connection */

	if (objc == skip) {
	    retval = Tcl_NewObj();
1173
1174
1175
1176
1177
1178
1179

1180
1181
1182
1183
1184
1185
1186
1187
1188

1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201

1202
1203
1204
1205
1206
1207
1208
1209

1210
1211
1212
1213
1214

1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
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
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287

1288
1289
1290
1291
1292
1293
1294
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
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

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







+








-
+












-
+







-
+




-
+













-
+






-
+




-
+







-
+






-
+







-
+




-
+


















-
+







	return TCL_ERROR;
    }

    /* Extract options from the command line */

    for (i = 0; i < INDX_MAX; ++i) {
	stringOpts[i] = NULL;
	Tcl_DStringInit(&stringBufs[i]);
    }
    for (i = skip; i < objc; i += 2) {

	/* Unknown option */

	if (Tcl_GetIndexFromObjStruct(interp, objv[i], (void*) ConnOptions,
				      sizeof(ConnOptions[0]), "option",
				      0, &optionIndex) != TCL_OK) {
	    return TCL_ERROR;
	    goto error;
	}

	/* Unmodifiable option */

	if (cdata->mysqlPtr != NULL && !(ConnOptions[optionIndex].flags
					 & CONN_OPT_FLAG_MOD)) {
	    Tcl_Obj* msg = Tcl_NewStringObj("\"", -1);
	    Tcl_AppendObjToObj(msg, objv[i]);
	    Tcl_AppendToObj(msg, "\" option cannot be changed dynamically", -1);
	    Tcl_SetObjResult(interp, msg);
	    Tcl_SetErrorCode(interp, "TDBC", "GENERAL_ERROR", "HY000",
			     "MYSQL", "-1", NULL);
	    return TCL_ERROR;
	    goto error;
	}

	/* Record option value */

	switch (ConnOptions[optionIndex].type) {
	case TYPE_STRING:
	    stringOpts[ConnOptions[optionIndex].info] =
		Tcl_GetString(objv[i+1]);
		ExternalFromStringObj(cdata, objv[i+1], &stringBufs[i]);
	    break;
	case TYPE_FLAG:
	    if (Tcl_GetBooleanFromObj(interp, objv[i+1], &optionValue)
		!= TCL_OK) {
		return TCL_ERROR;
		goto error;
	    }
	    if (optionValue) {
		mysqlFlags |= ConnOptions[optionIndex].info;
	    }
	    break;
	case TYPE_ENCODING:
	    if (strcmp(Tcl_GetString(objv[i+1]), "utf-8")) {
		Tcl_SetObjResult(interp,
				 Tcl_NewStringObj("Only UTF-8 transfer "
						  "encoding is supported.\n",
						  -1));
		Tcl_SetErrorCode(interp, "TDBC", "GENERAL_ERROR", "HY000",
				 "MYSQL", "-1", NULL);
		return TCL_ERROR;
		goto error;
	    }
	    break;
	case TYPE_ISOLATION:
	    if (Tcl_GetIndexFromObjStruct(interp, objv[i+1], TclIsolationLevels,
				    sizeof(char *), "isolation level", TCL_EXACT, &isolation)
		!= TCL_OK) {
		return TCL_ERROR;
		goto error;
	    }
	    break;
	case TYPE_PORT:
	    if (Tcl_GetIntFromObj(interp, objv[i+1], &optionValue) != TCL_OK) {
		return TCL_ERROR;
		goto error;
	    }
	    if (optionValue < 0 || optionValue > 0xffff) {
		Tcl_SetObjResult(interp, Tcl_NewStringObj("port number must "
							  "be in range "
							  "[0..65535]", -1));
		Tcl_SetErrorCode(interp, "TDBC", "GENERAL_ERROR", "HY000",
				 "MYSQL", "-1", NULL);
		return TCL_ERROR;
		goto error;
	    }
	    port = optionValue;
	    break;
	case TYPE_READONLY:
	    if (Tcl_GetBooleanFromObj(interp, objv[i+1], &optionValue)
		!= TCL_OK) {
		return TCL_ERROR;
		goto error;
	    }
	    if (optionValue != 0) {
		Tcl_SetObjResult(interp,
				 Tcl_NewStringObj("MySQL does not support "
						  "readonly connections", -1));
		Tcl_SetErrorCode(interp, "TDBC", "GENERAL_ERROR", "HY000",
				 "MYSQL", "-1", NULL);
		return TCL_ERROR;
		goto error;
	    }
	    break;
	case TYPE_TIMEOUT:
	    if (Tcl_GetIntFromObj(interp, objv[i+1], &timeout) != TCL_OK) {
		return TCL_ERROR;
		goto error;
	    }
	    break;
	}
	if (ConnOptions[optionIndex].flags & CONN_OPT_FLAG_SSL) {
	    sslFlag = 1;
	}
    }

    if (cdata->mysqlPtr == NULL) {

	/* Configuring a new connection. Open the database */

	cdata->mysqlPtr = mysql_init(NULL);
	if (cdata->mysqlPtr == NULL) {
	    Tcl_SetObjResult(interp,
			     Tcl_NewStringObj("mysql_init() failed.", -1));
	    Tcl_SetErrorCode(interp, "TDBC", "GENERAL_ERROR", "HY001",
			     "MYSQL", "NULL", NULL);
	    return TCL_ERROR;
	    goto error;
	}

	/* Set character set for the connection */

	mysql_options(cdata->mysqlPtr, MYSQL_SET_CHARSET_NAME, "utf8");

	    /* Set SSL options if needed */
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
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
1376
1375
1376
1377
1378
1379
1380
1381


1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399


1400
1401
1402
1403
1404
1405
1406
1407


1408
1409
1410
1411
1412
1413
1414
1415
1416
1417


1418
1419
1420
1421
1422
1423
1424
1425

1426
1427
1428
1429

1430
1431




1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443

1444
1445
1446
1447
1448
1449
1450
1451







-
-
+
+
















-
-
+
+






-
-
+
+








-
-
+
+






-
+



-
+

-
-
-
-
+
+
+
+
+
+
+
+
+
+


-
+







	 * TODO - mutex around this unless linked to libmysqlclient_r ?
	 */

	if (mysql_real_connect(cdata->mysqlPtr, stringOpts[INDX_HOST],
			       stringOpts[INDX_USER], stringOpts[INDX_PASSWD],
			       stringOpts[INDX_DB], port,
			       stringOpts[INDX_SOCKET], mysqlFlags) == NULL) {
	    TransferMysqlError(interp, cdata->mysqlPtr);
	    return TCL_ERROR;
	    TransferMysqlError(cdata, interp);
	    goto error;
	}

	cdata->flags |= CONN_FLAG_AUTOCOMMIT;

    } else {

	/* Already open connection */

	if (stringOpts[INDX_USER] != NULL) {

	    /* User name changed - log in again */

	    if (mysql_change_user(cdata->mysqlPtr,
				  stringOpts[INDX_USER],
				  stringOpts[INDX_PASSWD],
				  stringOpts[INDX_DB])) {
		TransferMysqlError(interp, cdata->mysqlPtr);
		return TCL_ERROR;
		TransferMysqlError(cdata, interp);
		goto error;
	    }
	} else if (stringOpts[INDX_DB] != NULL) {

	    /* Database name changed - use the new database */

	    if (mysql_select_db(cdata->mysqlPtr, stringOpts[INDX_DB])) {
		TransferMysqlError(interp, cdata->mysqlPtr);
		return TCL_ERROR;
		TransferMysqlError(cdata, interp);
		goto error;
	    }
	}
    }

    /* Transaction isolation level */

    if (isolation != ISOL_NONE) {
	if (mysql_query(cdata->mysqlPtr, SqlIsolationLevels[isolation])) {
	    TransferMysqlError(interp, cdata->mysqlPtr);
	    return TCL_ERROR;
	    TransferMysqlError(cdata, interp);
	    goto error;
	}
    }

    /* Timeout */

    if (timeout != 0) {
        int result;
	int sqlresult;
	Tcl_Obj* query = Tcl_ObjPrintf("SET SESSION WAIT_TIMEOUT = %d\n",
				       timeout);
	Tcl_IncrRefCount(query);
	result = mysql_query(cdata->mysqlPtr, Tcl_GetString(query));
	sqlresult = mysql_query(cdata->mysqlPtr, Tcl_GetString(query));
	Tcl_DecrRefCount(query);
	if (result) {
	    TransferMysqlError(interp, cdata->mysqlPtr);
	    return TCL_ERROR;
	}
	if (sqlresult) {
	    TransferMysqlError(cdata, interp);
	    goto error;
	}
    }
    result = TCL_OK;

error:
    for (i = 0; i < INDX_MAX; ++i) {
	Tcl_DStringFree(&stringBufs[i]);
    }

    return TCL_OK;
    return result;
}

/*
 *-----------------------------------------------------------------------------
 *
 * ConnectionConstructor --
 *
1404
1405
1406
1407
1408
1409
1410

1411
1412
1413
1414
1415
1416
1417
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493







+







    ConnectionData* cdata;	/* Per-connection data */

    /* Hang client data on this connection */

    cdata = (ConnectionData*) ckalloc(sizeof(ConnectionData));
    cdata->refCount = 1;
    cdata->pidata = pidata;
    cdata->enc = Tcl_GetEncoding(NULL, "utf-8");
    cdata->mysqlPtr = NULL;
    cdata->nCollations = 0;
    cdata->collationSizes = NULL;
    cdata->flags = 0;
    IncrPerInterpRefCount(pidata);
    Tcl_ObjectSetMetadata(thisObject, &connectionDataType, (ClientData) cdata);

1476
1477
1478
1479
1480
1481
1482
1483

1484
1485
1486
1487
1488
1489
1490
1552
1553
1554
1555
1556
1557
1558

1559
1560
1561
1562
1563
1564
1565
1566







-
+







    }
    cdata->flags |= CONN_FLAG_IN_XCN;

    /* Turn off autocommit for the duration of the transaction */

    if (cdata->flags & CONN_FLAG_AUTOCOMMIT) {
	if (mysql_autocommit(cdata->mysqlPtr, 0)) {
	    TransferMysqlError(interp, cdata->mysqlPtr);
	    TransferMysqlError(cdata, interp);
	    return TCL_ERROR;
	}
	cdata->flags &= ~CONN_FLAG_AUTOCOMMIT;
    }

    return TCL_OK;
}
1543
1544
1545
1546
1547
1548
1549
1550

1551
1552
1553
1554
1555
1556
1557
1558
1559
1560

1561

1562
1563
1564
1565
1566
1567
1568
1619
1620
1621
1622
1623
1624
1625

1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637

1638
1639
1640
1641
1642
1643
1644
1645







-
+










+
-
+







	Tcl_WrongNumArgs(interp, 2, objv, "table ?pattern?");
	return TCL_ERROR;
    }

    results = mysql_list_fields(cdata->mysqlPtr, Tcl_GetString(objv[2]),
				patternStr);
    if (results == NULL) {
	TransferMysqlError(interp, cdata->mysqlPtr);
	TransferMysqlError(cdata, interp);
	return TCL_ERROR;
    } else {
	unsigned int fieldCount = mysql_num_fields(results);
	MYSQL_FIELD* fields = mysql_fetch_fields(results);
	unsigned int i;
	retval = Tcl_NewObj();
	Tcl_IncrRefCount(retval);
	for (i = 0; i < fieldCount; ++i) {
	    MYSQL_FIELD* field = MysqlFieldIndex(fields, i);
	    attrs = Tcl_NewObj();
	    name = StringObjFromExternal(cdata,
	    name = Tcl_NewStringObj(field->name, field->name_length);
					 field->name, field->name_length);

	    Tcl_DictObjPut(NULL, attrs, literals[LIT_NAME], name);
	    /* TODO - Distinguish CHAR and BINARY */
	    entry = Tcl_FindHashEntry(&(pidata->typeNumHash),
				      (char*) field->type);
	    if (entry != NULL) {
		Tcl_DictObjPut(NULL, attrs, literals[LIT_TYPE],
1644
1645
1646
1647
1648
1649
1650
1651

1652
1653
1654
1655
1656
1657
1658
1721
1722
1723
1724
1725
1726
1727

1728
1729
1730
1731
1732
1733
1734
1735







-
+







    }

    /* End transaction, turn off "transaction in progress", and report status */

    rc = mysql_commit(cdata->mysqlPtr);
    cdata->flags &= ~ CONN_FLAG_IN_XCN;
    if (rc) {
	TransferMysqlError(interp, cdata->mysqlPtr);
	TransferMysqlError(cdata, interp);
	return TCL_ERROR;
    }
    return TCL_OK;
}

/*
 *-----------------------------------------------------------------------------
1735
1736
1737
1738
1739
1740
1741
1742

1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755

1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768




1769
1770

1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787

1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800

1801
1802
1803
1804
1805
1806
1807
1812
1813
1814
1815
1816
1817
1818

1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844


1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867

1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880

1881
1882
1883
1884
1885
1886
1887
1888







-
+













+











-
-
+
+
+
+


+
















-
+












-
+








static int
ConnectionEvaldirectMethod(
    ClientData clientData,	     /* Unused */
    Tcl_Interp* interp,		     /* Tcl interpreter */
    Tcl_ObjectContext objectContext, /* Object context */
    int objc,			     /* Parameter count */
    Tcl_Obj *const objv[])           /* Parameter vector */
    Tcl_Obj *const objv[])	     /* Parameter vector */
{
    Tcl_Object thisObject = Tcl_ObjectContextObject(objectContext);
				/* Current connection object */
    ConnectionData* cdata = (ConnectionData*)
	Tcl_ObjectGetMetadata(thisObject, &connectionDataType);
				/* Instance data */
    int nColumns;		/* Number of columns in the result set */
    MYSQL_RES* resultPtr;	/* MySQL result set */
    MYSQL_ROW rowPtr;		/* One row of the result set */
    unsigned long* lengths;	/* Lengths of the fields in a row */
    Tcl_Obj* retObj;		/* Result set as a Tcl list */
    Tcl_Obj* rowObj;		/* One row of the result set as a Tcl list */
    Tcl_Obj* fieldObj;		/* One field of the row */
    Tcl_DString ds;
    int i;

    /* Check parameters */

    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 2, objv, "");
	return TCL_ERROR;
    }

    /* Execute the given statement */

    if (mysql_query(cdata->mysqlPtr, Tcl_GetString(objv[2]))) {
	TransferMysqlError(interp, cdata->mysqlPtr);
    if (mysql_query(cdata->mysqlPtr,
	    ExternalFromStringObj(cdata, objv[2], &ds))) {
	Tcl_DStringFree(&ds);
	TransferMysqlError(cdata, interp);
	return TCL_ERROR;
    }
    Tcl_DStringFree(&ds);

    /* Retrieve the result set */

    resultPtr = mysql_store_result(cdata->mysqlPtr);
    nColumns = mysql_field_count(cdata->mysqlPtr);
    if (resultPtr == NULL) {
	/*
	 * Can't retrieve result set. Distinguish result-less statements
	 * from MySQL errors.
	 */
	if (nColumns == 0) {
	    Tcl_SetObjResult
		(interp,
		 Tcl_NewWideIntObj(mysql_affected_rows(cdata->mysqlPtr)));
	    return TCL_OK;
	} else {
	    TransferMysqlError(interp, cdata->mysqlPtr);
	    TransferMysqlError(cdata, interp);
	    return TCL_ERROR;
	}
    }

    /* Make a list-of-lists of the result */

    retObj = Tcl_NewObj();
    while ((rowPtr = mysql_fetch_row(resultPtr)) != NULL) {
	rowObj = Tcl_NewObj();
	lengths = mysql_fetch_lengths(resultPtr);
	for (i = 0; i < nColumns; ++i) {
	    if (rowPtr[i] != NULL) {
		fieldObj = Tcl_NewStringObj(rowPtr[i], lengths[i]);
		fieldObj = StringObjFromExternal(cdata, rowPtr[i], lengths[i]);
	    } else {
		fieldObj = cdata->pidata->literals[LIT_EMPTY];
	    }
	    Tcl_ListObjAppendElement(NULL, rowObj, fieldObj);
	}
	Tcl_ListObjAppendElement(NULL, retObj, rowObj);
    }
1913
1914
1915
1916
1917
1918
1919
1920

1921
1922
1923
1924
1925
1926
1927
1994
1995
1996
1997
1998
1999
2000

2001
2002
2003
2004
2005
2006
2007
2008







-
+







    }

    /* End transaction, turn off "transaction in progress", and report status */

    rc = mysql_rollback(cdata->mysqlPtr);
    cdata->flags &= ~CONN_FLAG_IN_XCN;
    if (rc) {
	TransferMysqlError(interp, cdata->mysqlPtr);
	TransferMysqlError(cdata, interp);
	return TCL_ERROR;
    }
    return TCL_OK;
}

/*
 *-----------------------------------------------------------------------------
2065
2066
2067
2068
2069
2070
2071
2072

2073
2074
2075
2076
2077
2078
2079
2080
2081
2082

2083
2084
2085
2086
2087

2088
2089
2090
2091
2092
2093
2094
2146
2147
2148
2149
2150
2151
2152

2153
2154
2155
2156
2157
2158
2159
2160
2161


2162
2163
2164
2165
2166

2167
2168
2169
2170
2171
2172
2173
2174







-
+








-
-
+




-
+







    } else {
	Tcl_WrongNumArgs(interp, 2, objv, "");
	return TCL_ERROR;
    }

    results = mysql_list_tables(cdata->mysqlPtr, patternStr);
    if (results == NULL) {
	TransferMysqlError(interp, cdata->mysqlPtr);
	TransferMysqlError(cdata, interp);
	return TCL_ERROR;
    } else {
	retval = Tcl_NewObj();
	Tcl_IncrRefCount(retval);
	while ((row = mysql_fetch_row(results)) != NULL) {
	    unsigned long* lengths = mysql_fetch_lengths(results);
	    if (row[0]) {
		Tcl_ListObjAppendElement(NULL, retval,
					 Tcl_NewStringObj(row[0],
							  (int)lengths[0]));
			StringObjFromExternal(cdata, row[0], (int)lengths[0]));
		Tcl_ListObjAppendElement(NULL, retval, literals[LIT_EMPTY]);
	    }
	}
	if (mysql_errno(cdata->mysqlPtr)) {
	    TransferMysqlError(interp, cdata->mysqlPtr);
	    TransferMysqlError(cdata, interp);
	    status = TCL_ERROR;
	}
	if (status == TCL_OK) {
	    Tcl_SetObjResult(interp, retval);
	}
	Tcl_DecrRefCount(retval);
	mysql_free_result(results);
2177
2178
2179
2180
2181
2182
2183

2184
2185
2186
2187
2188
2189
2190
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271







+







    if (cdata->collationSizes != NULL) {
	ckfree((char*) cdata->collationSizes);
    }
    if (cdata->mysqlPtr != NULL) {
	mysql_close(cdata->mysqlPtr);
    }
    DecrPerInterpRefCount(cdata->pidata);
    Tcl_FreeEncoding(cdata->enc);
    ckfree((char*) cdata);
}

/*
 *-----------------------------------------------------------------------------
 *
 * CloneConnection --
2281
2282
2283
2284
2285
2286
2287
2288

2289

2290
2291
2292
2293


2294

2295

2296
2297
2298

2299
2300
2301
2302
2303
2304
2305
2362
2363
2364
2365
2366
2367
2368

2369
2370
2371
2372
2373
2374

2375
2376
2377
2378

2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390







-
+

+



-
+
+

+
-
+



+







    /*
     * MySQL allows only one writable cursor open at a time, and
     * the default cursor type is writable. Make all our cursors
     * read-only to avoid 'Commands out of sync' errors.
     */

    if (stmtPtr == NULL) {
	TransferMysqlError(interp, cdata->mysqlPtr);
	TransferMysqlError(cdata, interp);
    } else {
	Tcl_DString ds;

	/* Prepare the statement */

	nativeSqlStr = Tcl_GetStringFromObj(sdata->nativeSql, &nativeSqlLen);
	nativeSqlStr = ExternalFromStringObj(cdata, sdata->nativeSql, &ds);
	nativeSqlLen = Tcl_DStringLength(&ds);
	if (mysql_stmt_prepare(stmtPtr, nativeSqlStr, nativeSqlLen)) {
	    Tcl_DStringFree(&ds);
	    TransferMysqlStmtError(interp, stmtPtr);
	    TransferMysqlStmtError(cdata, interp, stmtPtr);
	    mysql_stmt_close(stmtPtr);
	    stmtPtr = NULL;
	}
	Tcl_DStringFree(&ds);
    }
    return stmtPtr;
}

/*
 *-----------------------------------------------------------------------------
 *
2315
2316
2317
2318
2319
2320
2321

2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340

2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358

2359

2360
2361
2362
2363
2364
2365
2366
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425

2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443

2444
2445
2446
2447
2448
2449
2450
2451
2452
2453







+


















-
+

















-
+

+







 * column name.
 *
 *-----------------------------------------------------------------------------
 */

static Tcl_Obj*
ResultDescToTcl(
    ConnectionData* cdata,	/* Connection data */
    MYSQL_RES* result,		/* Result set description */
    int flags			/* Flags governing the conversion */
) {
    Tcl_Obj* retval = Tcl_NewObj();
    Tcl_HashTable names;	/* Hash table to resolve name collisions */
    Tcl_Obj* nameObj;		/* Name of a result column */
    int isNew;			/* Flag == 1 if a result column is unique */
    Tcl_HashEntry* entry;	/* Hash table entry for a column name */
    Tcl_HashEntry* countEntry;	/* Ditto */
    int count;			/* Number used to disambiguate a column name */

    Tcl_InitHashTable(&names, TCL_STRING_KEYS);
    if (result != NULL) {
	unsigned int fieldCount = mysql_num_fields(result);
	MYSQL_FIELD* fields = mysql_fetch_fields(result);
	unsigned int i;
	for (i = 0; i < fieldCount; ++i) {
	    MYSQL_FIELD* field = MysqlFieldIndex(fields, i);
	    nameObj = Tcl_NewStringObj(field->name, -1);
	    nameObj = StringObjFromExternal(cdata, field->name, -1);
	    Tcl_IncrRefCount(nameObj);

	    countEntry = NULL;
	    for (;;) {
		entry = Tcl_CreateHashEntry(&names, Tcl_GetString(nameObj),
					    &isNew);
		if (isNew) {
		    Tcl_SetHashValue(entry, (ClientData) 1);
		    break;
		}
		if (countEntry == NULL) {
		    countEntry = entry;
		}
		count = PTR2INT(Tcl_GetHashValue(countEntry));
		++count;
		Tcl_SetHashValue(countEntry, INT2PTR(count));
		Tcl_DecrRefCount(nameObj);
		nameObj = Tcl_ObjPrintf("%s#%d", field->name, count);
		nameObj = StringObjFromExternal(cdata, field->name, -1);
		Tcl_IncrRefCount(nameObj);
		Tcl_AppendPrintfToObj(nameObj, "#%d", count);
	    }

	    Tcl_ListObjAppendElement(NULL, retval, nameObj);
	    Tcl_DecrRefCount(nameObj);
	}
    }
    Tcl_DeleteHashTable(&names);
2499
2500
2501
2502
2503
2504
2505
2506

2507
2508
2509

2510
2511
2512
2513
2514
2515
2516
2586
2587
2588
2589
2590
2591
2592

2593
2594
2595

2596
2597
2598
2599
2600
2601
2602
2603







-
+


-
+







	goto freeSData;
    }

    /* Get result set metadata */

    sdata->metadataPtr = mysql_stmt_result_metadata(sdata->stmtPtr);
    if (mysql_stmt_errno(sdata->stmtPtr)) {
	TransferMysqlStmtError(interp, sdata->stmtPtr);
	TransferMysqlStmtError(cdata, interp, sdata->stmtPtr);
	goto freeSData;
    }
    sdata->columnNames = ResultDescToTcl(sdata->metadataPtr, 0);
    sdata->columnNames = ResultDescToTcl(cdata, sdata->metadataPtr, 0);
    Tcl_IncrRefCount(sdata->columnNames);

    Tcl_ListObjLength(NULL, sdata->subVars, &nParams);
    sdata->params = (ParamData*) ckalloc(nParams * sizeof(ParamData));
    for (i = 0; i < nParams; ++i) {
	sdata->params[i].flags = PARAM_IN;
	sdata->params[i].dataType = MYSQL_TYPE_VARCHAR;
2917
2918
2919
2920
2921
2922
2923
2924

2925
2926
2927
2928
2929
2930
2931
3004
3005
3006
3007
3008
3009
3010

3011
3012
3013
3014
3015
3016
3017
3018







-
+







    /*
     * If there is no transaction in progress, turn on auto-commit so that
     * this statement will execute directly.
     */

    if ((cdata->flags & (CONN_FLAG_IN_XCN | CONN_FLAG_AUTOCOMMIT)) == 0) {
	if (mysql_autocommit(cdata->mysqlPtr, 1)) {
	    TransferMysqlError(interp, cdata->mysqlPtr);
	    TransferMysqlError(cdata, interp);
	    return TCL_ERROR;
	}
	cdata->flags |= CONN_FLAG_AUTOCOMMIT;
    }

    /* Allocate an object to hold data about this result set */

3049
3050
3051
3052
3053
3054
3055


3056
3057
3058
3059
3060
3061
3062
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151







+
+







	/*
	 * At this point, paramValObj contains the parameter to bind.
	 * Convert the parameters to the appropriate data types for
	 * MySQL's prepared statement interface, and bind them.
	 */

	if (paramValObj != NULL) {
	    Tcl_DString ds;

	    switch (sdata->params[nBound].dataType & 0xffff) {

	    case MYSQL_TYPE_NEWDECIMAL:
	    case MYSQL_TYPE_DECIMAL:
		if (sdata->params[nBound].scale == 0) {
		    if (sdata->params[nBound].precision < 10) {
			goto smallinteger;
3128
3129
3130
3131
3132
3133
3134
3135



3136
3137
3138
3139
3140



3141
3142
3143
3144
3145
3146
3147
3217
3218
3219
3220
3221
3222
3223

3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241







-
+
+
+





+
+
+







		    MysqlBindSetBufferType(rdata->paramBindings, nBound,
					   MYSQL_TYPE_BLOB);
		    paramValStr = (char*)
			Tcl_GetByteArrayFromObj(paramValObj, &len);
		} else {
		    MysqlBindSetBufferType(rdata->paramBindings, nBound,
					   MYSQL_TYPE_STRING);
		    paramValStr = Tcl_GetStringFromObj(paramValObj, &len);
		    paramValStr =
			ExternalFromStringObj(cdata, paramValObj, &ds);
		    len = Tcl_DStringLength(&ds);
		}
		bufPtr = (char *)MysqlBindAllocBuffer(rdata->paramBindings, nBound,
					      len+1);
		memcpy(bufPtr, paramValStr, len);
		rdata->paramLengths[nBound] = len;
		if (!(sdata->params[nBound].dataType & IS_BINARY)) {
		    Tcl_DStringFree(&ds);
		}
		MysqlBindSetLength(rdata->paramBindings, nBound,
				   &(rdata->paramLengths[nBound]));
		break;

	    }
	} else {
	    MysqlBindSetBufferType(rdata->paramBindings, nBound,
3163
3164
3165
3166
3167
3168
3169
3170

3171
3172
3173
3174
3175
3176
3177
3257
3258
3259
3260
3261
3262
3263

3264
3265
3266
3267
3268
3269
3270
3271







-
+







     */

    if (mysql_stmt_bind_param(rdata->stmtPtr, rdata->paramBindings)
	|| ((nColumns > 0) && mysql_stmt_bind_result(rdata->stmtPtr,
						     resultBindings))
	|| mysql_stmt_execute(rdata->stmtPtr)
	|| mysql_stmt_store_result(rdata->stmtPtr) ) {
	TransferMysqlStmtError(interp, sdata->stmtPtr);
	TransferMysqlStmtError(cdata, interp, sdata->stmtPtr);
	return TCL_ERROR;
    }

    /* Determine and store the row count */

    rdata->rowCount = mysql_stmt_affected_rows(sdata->stmtPtr);
    return TCL_OK;
3371
3372
3373
3374
3375
3376
3377
3378
3379


3380
3381
3382
3383
3384
3385
3386
3465
3466
3467
3468
3469
3470
3471


3472
3473
3474
3475
3476
3477
3478
3479
3480







-
-
+
+







		break;

	    default:
		if (field->charsetnr == 63) {
		    colObj = Tcl_NewByteArrayObj((unsigned char*) bufPtr,
						 resultLengths[i]);
		} else {
		    colObj = Tcl_NewStringObj((char*) bufPtr,
					      resultLengths[i]);
		    colObj = StringObjFromExternal(cdata, (char*) bufPtr,
						   resultLengths[i]);
		}
		break;
	    }
	}

	if (lists) {
	    if (colObj == NULL) {
3403
3404
3405
3406
3407
3408
3409
3410

3411
3412
3413
3414
3415
3416
3417
3497
3498
3499
3500
3501
3502
3503

3504
3505
3506
3507
3508
3509
3510
3511







-
+







    }

    Tcl_SetObjResult(interp, literals[LIT_1]);
    status = TCL_OK;

 cleanup:
    if (status != TCL_OK) {
	TransferMysqlStmtError(interp, rdata->stmtPtr);
	TransferMysqlStmtError(cdata, interp, rdata->stmtPtr);
    }
    Tcl_DecrRefCount(resultRow);
    return status;

}

/*

Changes to jni/tcl/pkgs/tdbcodbc1.1.1/generic/tdbcodbc.c.

818
819
820
821
822
823
824
825

826
827
828
829
830
831
832
818
819
820
821
822
823
824

825
826
827
828
829
830
831
832







-
+







	    unsigned int ch;
	    int bytes;

	    ch = ptr16[i];
	    if (ch > 0x10ffff) {
		ch = 0xfffd;
	    }
#if TCL_UTF_MAX >= 4
#if TCL_UTF_MAX > 3
	    /* Collapse a surrogate pair, if any. */
	    if (ch >= 0xd800 && ch <= 0xdbff) {
		if (i + 1 < len) {
		    unsigned int ch2 = ptr16[i+1];

		    if (ch2 >= 0xdc00 && ch2 <= 0xdfff) {
			ch = ((ch & 0x3ff) << 10) + 0x10000 + (ch2 & 0x3ff);
845
846
847
848
849
850
851
852

853
854
855
856
857
858
859
845
846
847
848
849
850
851

852
853
854
855
856
857
858
859







-
+







	    unsigned int ch;
	    int bytes;

	    ch = ptr32[i];
	    if (ch > 0x10ffff) {
		ch = 0xfffd;
	    }
#if TCL_UTF_MAX >= 4
#if TCL_UTF_MAX > 3
	    /* Collapse a surrogate pair, if any. */
	    if (ch >= 0xd800 && ch <= 0xdbff) {
		if (i + 1 < len) {
		    unsigned int ch2 = ptr32[i+1];

		    if (ch2 >= 0xdc00 && ch2 <= 0xdfff) {
			ch = ((ch & 0x3ff) << 10) + 0x10000 + (ch2 & 0x3ff);
915
916
917
918
919
920
921
922

923
924
925
926
927
928
929
915
916
917
918
919
920
921

922
923
924
925
926
927
928
929







-
+








	    if (Tcl_UtfCharComplete(bytes, end - bytes)) {
		bytes += Tcl_UtfToUniChar(bytes, &ch);
	    } else {
		ch = *bytes++ & 0x00ff;
	    }
	    uch = ch;
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
	    if (uch > 0xffff) {
		*ptr16++ = (((uch - 0x10000) >> 10) & 0x3ff) | 0xd800;
		uch = ((uch - 0x10000) & 0x3ff) | 0xdc00;
	    }
#endif
	    if (uch > 0x7f) {
		shrink = 1;
941
942
943
944
945
946
947
948

949
950
951
952
953
954
955
941
942
943
944
945
946
947

948
949
950
951
952
953
954
955







-
+








	    if (Tcl_UtfCharComplete(bytes, end - bytes)) {
		bytes += Tcl_UtfToUniChar(bytes, &ch);
	    } else {
		ch = *bytes++ & 0x00ff;
	    }
	    uch = ch;
#if TCL_UTF_MAX <= 4
#if TCL_UTF_MAX == 3
	    if ((uch & 0xfc00) == 0xd800) {
		if (Tcl_UtfCharComplete(bytes, end - bytes)) {
		    len = Tcl_UtfToUniChar(bytes, &ch);
		    if ((ch & 0xfc00) == 0xdc00) {
			bytes += len;
			uch = (((uch & 0x3ff) << 10) | (ch & 0x3ff)) + 0x10000;
		    }

Changes to jni/tcl/pkgs/tdbcpostgres1.1.1/generic/tdbcpostgres.c.

190
191
192
193
194
195
196
197

198
199
200
201
202
203
204
190
191
192
193
194
195
196

197
198
199
200
201
202
203
204







-
+







static char* _PQport(const PGconn* conn) { return PQport(conn); }
static char* _PQuser(const PGconn* conn) { return PQuser(conn); }
static char* _PQtty(const PGconn* conn) { return PQtty(conn); }

/* Table of configuration options */

static const struct {
    const char * name;		    /* Option name */
    const char *name;		    /* Option name */
    enum OptType type;		    /* Option data type */
    int info;			    /* Option index or flag value */
    int flags;			    /* Flags - modifiable; SSL related;
				     * is an alias */
    char *(*queryF)(const PGconn*); /* Function used to determine the
				     * option value */
} ConnOptions [] = {
254
255
256
257
258
259
260

261

262
263
264
265
266

267
268
269
270
271
272
273
254
255
256
257
258
259
260
261

262
263
264
265
266

267
268
269
270
271
272
273
274







+
-
+




-
+







 *	destruction until the last statement or result set that refers
 *	to it is destroyed.
 */

typedef struct ConnectionData {
    int refCount;		/* Reference count. */
    PerInterpData* pidata;	/* Per-interpreter data */
    Tcl_Encoding enc;		/* "utf-8" encoding */
    PGconn* pgPtr;		/* Postgres  connection handle */
    PGconn* pgPtr;		/* Postgres connection handle */
    int stmtCounter;		/* Counter for naming statements */
    int flags;
    int isolation;		/* Current isolation level */
    int readOnly;		/* Read only connection indicator */
    char * savedOpts[INDX_MAX];  /* Saved configuration options */
    char* savedOpts[INDX_MAX];	/* Saved configuration options */
} ConnectionData;

/*
 * Flags for the state of an POSTGRES connection
 */

#define CONN_FLAG_IN_XCN	0x1	/* Transaction is in progress */
396
397
398
399
400
401
402




403
404
405
406
407
408
409
410





411
412
413
414
415
416
417
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







+
+
+
+




-
-
-
-
+
+
+
+
+







    ISOL_REPEATABLE_READ,
    ISOL_SERIALIZABLE,
    ISOL_NONE = -1
};

/* Static functions defined within this file */

static Tcl_Obj* StringObjFromExternal(ConnectionData* cdata,
				      const char* string, int length);
static char*  ExternalFromStringObj(ConnectionData* cdata, Tcl_Obj* strObj,
				    Tcl_DString* dsPtr);
static int DeterminePostgresMajorVersion(Tcl_Interp* interp,
					 ConnectionData* cdata,
					 int* versionPtr);
static void DummyNoticeProcessor(void*, const PGresult*);
static int ExecSimpleQuery(Tcl_Interp* interp, PGconn * pgPtr,
			   const char * query, PGresult** resOut);
static void TransferPostgresError(Tcl_Interp* interp, PGconn * pgPtr);
static int TransferResultError(Tcl_Interp* interp, PGresult * res);
static int ExecSimpleQuery(ConnectionData* cdata, Tcl_Interp* interp,
			   const char* query, PGresult** resOut);
static void TransferPostgresError(ConnectionData* cdata, Tcl_Interp* interp);
static int TransferResultError(ConnectionData* cdata, Tcl_Interp* interp,
			       PGresult *res);

static Tcl_Obj* QueryConnectionOption(ConnectionData* cdata,
				      Tcl_Interp* interp,
				      int optionNum);
static int ConfigureConnection(ConnectionData* cdata, Tcl_Interp* interp,
			       int objc, Tcl_Obj *const objv[], int skip);
static int ConnectionConstructor(ClientData clientData, Tcl_Interp* interp,
442
443
444
445
446
447
448
449


450
451
452
453
454
455
456
448
449
450
451
452
453
454

455
456
457
458
459
460
461
462
463







-
+
+







			   ClientData* newClientData);

static char* GenStatementName(ConnectionData* cdata);
static void UnallocateStatement(PGconn* pgPtr, char* stmtName);
static StatementData* NewStatement(ConnectionData* cdata);
static PGresult* PrepareStatement(Tcl_Interp* interp,
				  StatementData* sdata, char* stmtName);
static Tcl_Obj* ResultDescToTcl(PGresult* resultDesc, int flags);
static Tcl_Obj* ResultDescToTcl(ConnectionData* cdata, PGresult* resultDesc,
				int flags);
static int StatementConstructor(ClientData clientData, Tcl_Interp* interp,
				Tcl_ObjectContext context,
				int objc, Tcl_Obj *const objv[]);
static int StatementParamtypeMethod(ClientData clientData, Tcl_Interp* interp,
				    Tcl_ObjectContext context,
				    int objc, Tcl_Obj *const objv[]);
static int StatementParamsMethod(ClientData clientData, Tcl_Interp* interp,
675
676
677
678
679
680
681






















































682
683
684
685
686
687
688
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    &StatementParamtypeMethodType,
    NULL
};

/*
 *-----------------------------------------------------------------------------
 *
 * StringObjFromExternal --
 *
 *	Return a new Tcl string object from a string with conversion from
 *	external to internal utf-8 encoding.
 *
 * Results:
 *	New string object.
 *
 *-----------------------------------------------------------------------------
 */

static Tcl_Obj*
StringObjFromExternal(
    ConnectionData* cdata,	/* Connection data */
    const char* string,		/* String to convert */
    int length			/* Length of string */
) {
    Tcl_DString ds;
    Tcl_Obj* obj;

    Tcl_ExternalToUtfDString(cdata->enc, string, length, &ds);
    obj = Tcl_NewStringObj(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds));
    Tcl_DStringFree(&ds);
    return obj;
}

/*
 *-----------------------------------------------------------------------------
 *
 * ExternalFromStringObj --
 *
 *	Return a string from Tcl_Obj* in an uninitialized Tcl_DString
 *	which the caller must free. The resulting string is converted
 *	to external utf-8 encoding.
 *
 * Results:
 *	New string in provided Tcl_DString.
 *
 *-----------------------------------------------------------------------------
 */

static char*
ExternalFromStringObj(
    ConnectionData* cdata,	/* Connection data */
    Tcl_Obj* strObj,		/* Tcl_Obj to convert */
    Tcl_DString *dsPtr		/* Result area */
) {
    Tcl_UtfToExternalDString(cdata->enc, Tcl_GetString(strObj), -1, dsPtr);
    return Tcl_DStringValue(dsPtr);
}

/*
 *-----------------------------------------------------------------------------
 *
 * DummyNoticeReceiver --
 *
 *	Ignores warnings and notices from the PostgreSQL client library
 *
 * Results:
 *	None.
 *
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
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







-
+






+

-
-
+


-
+
+
+








-
+


-
+







 * Results:
 *	TCL_OK on success or the error was non fatal otherwise TCL_ERROR .
 *
 * Side effects:
 *	Sets the interpreter result and error code appropiately to
 *	query execution process. Optionally, when res parameter is
 *	not NULL and the execution is successful, it returns the
 *	PGResult * struct by this parameter. This struct should be
 *	PGResult* struct by this parameter. This struct should be
 *	freed with PQclear() when no longer needed.
 *
 *-----------------------------------------------------------------------------
 */

static int ExecSimpleQuery(
    ConnectionData* cdata,	/* Connection data */
    Tcl_Interp* interp,		/* Tcl interpreter */
    PGconn * pgPtr,		/* Connection handle */
    const char * query,		/* Query to execute */
    const char* query,		/* Query to execute */
    PGresult** resOut		/* Optional handle to result struct */
) {
    PGresult * res;		/* Query result */
    PGresult* res;		/* Query result */
    PGconn *pgPtr = cdata->pgPtr;
				/* Connection handle */

    /* Execute the query */

    res = PQexec(pgPtr, query);

    /* Return error if the query was unsuccessful */

    if (res == NULL) {
	TransferPostgresError(interp, pgPtr);
	TransferPostgresError(cdata, interp);
	return TCL_ERROR;
    }
    if (TransferResultError(interp, res) != TCL_OK) {
    if (TransferResultError(cdata, interp, res) != TCL_OK) {
	PQclear(res);
	return TCL_ERROR;
    }

    /* Transfer query result to the caller */

    if (resOut != NULL) {
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
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
884
885

886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903

904
905
906
907
908
909
910
911
912

913
914
915
916
917
918
919
920







+
-
+
-

+

+









-
+
+





-
+



















+

-
+

















-
+








-
+







 *	connection error.
 *
 *-----------------------------------------------------------------------------
 */

static void
TransferPostgresError(
    ConnectionData* cdata,	/* Connection data */
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_Interp* interp		/* Tcl interpreter */
    PGconn* pgPtr		/* Postgres connection handle */
) {
    PGconn *pgPtr = cdata->pgPtr;	/* Connection handle */
    Tcl_Obj* errorCode = Tcl_NewObj();

    Tcl_ListObjAppendElement(NULL, errorCode, Tcl_NewStringObj("TDBC", -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewStringObj("GENERAL_ERROR", -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewStringObj("HY000", -1));
    Tcl_ListObjAppendElement(NULL, errorCode, Tcl_NewStringObj("POSTGRES", -1));
    Tcl_ListObjAppendElement(NULL, errorCode,
			     Tcl_NewWideIntObj(-1));
    Tcl_SetObjErrorCode(interp, errorCode);
    Tcl_SetObjResult(interp, Tcl_NewStringObj(PQerrorMessage(pgPtr), -1));
    Tcl_SetObjResult(interp,
		     StringObjFromExternal(cdata, PQerrorMessage(pgPtr), -1));
}

/*
 *-----------------------------------------------------------------------------
 *
 * TransferPostgresError --
 * TransferResultError --
 *
 *	Check if there is any error related to given PGresult object.
 *	If there was an error, it obtains error message, SQL state
 *	and error number from the Postgres client library and transfers
 *	thenm into the Tcl interpreter.
 *
 * Results:
 *	TCL_OK if no error exists or the error was non fatal,
 *	otherwise TCL_ERROR is returned
 *
 * Side effects:
 *
 *	Sets the interpreter result and error code to describe the SQL
 *	connection error.
 *
 *-----------------------------------------------------------------------------
 */

static int TransferResultError(
    ConnectionData* cdata,
    Tcl_Interp* interp,
    PGresult * res
    PGresult* res
) {
    ExecStatusType error = PQresultStatus(res);
    const char* sqlstate;
    if (error == PGRES_BAD_RESPONSE
	|| error == PGRES_EMPTY_QUERY
	|| error == PGRES_NONFATAL_ERROR
	|| error == PGRES_FATAL_ERROR) {
	Tcl_Obj* errorCode = Tcl_NewObj();
	Tcl_ListObjAppendElement(NULL, errorCode, Tcl_NewStringObj("TDBC", -1));

	sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
	if (sqlstate == NULL) {
	    sqlstate = "HY000";
	}
	Tcl_ListObjAppendElement(NULL, errorCode,
		Tcl_NewStringObj(Tdbc_MapSqlState(sqlstate), -1));
	Tcl_ListObjAppendElement(NULL, errorCode,
		Tcl_NewStringObj(sqlstate, -1));
		StringObjFromExternal(cdata, sqlstate, -1));
	Tcl_ListObjAppendElement(NULL, errorCode,
				 Tcl_NewStringObj("POSTGRES", -1));
	Tcl_ListObjAppendElement(NULL, errorCode,
		Tcl_NewWideIntObj(error));
	Tcl_SetObjErrorCode(interp, errorCode);
	if (error == PGRES_EMPTY_QUERY) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj("empty query", -1));
	} else {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
	    Tcl_SetObjResult(interp, StringObjFromExternal(cdata,
		PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY), -1));
	}
    }
    if (error == PGRES_BAD_RESPONSE
	|| error == PGRES_EMPTY_QUERY
	|| error == PGRES_FATAL_ERROR) {
	return TCL_ERROR;
870
871
872
873
874
875
876
877
878
879



880
881
882

883
884
885
886
887
888

889
890
891
892
893
894
895
937
938
939
940
941
942
943



944
945
946



947
948
949
950
951
952

953
954
955
956
957
958
959
960







-
-
-
+
+
+
-
-
-
+





-
+







 * Side effects:
 *	Stores the version number in '*versionPtr' if successful.
 *
 *-----------------------------------------------------------------------------
 */

static int
DeterminePostgresMajorVersion(Tcl_Interp* interp,
				/* Tcl interpreter */
			      ConnectionData* cdata,
DeterminePostgresMajorVersion(
    Tcl_Interp* interp,		/* Tcl interpreter */
    ConnectionData* cdata,	/* Connection data */
				/* Connection data */
			      int* versionPtr)
				/* OUTPUT: PostgreSQL server version */
    int* versionPtr)		/* OUTPUT: PostgreSQL server version */
{
    PGresult* res;		/* Result of a Postgres query */
    int status = TCL_ERROR;	/* Status return */
    char* versionStr;		/* Version information from server */

    if (ExecSimpleQuery(interp, cdata->pgPtr,
    if (ExecSimpleQuery(cdata, interp,
			"SELECT version()", &res) == TCL_OK) {
	versionStr = PQgetvalue(res, 0, 0);
	if (sscanf(versionStr, " PostgreSQL %d", versionPtr) == 1) {
	    status = TCL_OK;
	} else {
	    Tcl_Obj* result = Tcl_NewStringObj("unable to parse PostgreSQL "
					       "version: \"", -1);
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
952
953
954
955
987
988
989
990
991
992
993

994
995
996
997
998
999
1000
1001

1002
1003
1004
1005
1006


1007
1008
1009
1010
1011
1012

1013
1014
1015
1016
1017
1018
1019
1020







-
+







-
+




-
-
+
+




-
+







QueryConnectionOption (
    ConnectionData* cdata,	/* Connection data */
    Tcl_Interp* interp,		/* Tcl interpreter */
    int optionNum		/* Position of the option in the table */
) {
    PerInterpData* pidata = cdata->pidata; /* Per-interpreter data */
    Tcl_Obj** literals = pidata->literals;
    char * value;		/* Return value as C string */
    char* value;		/* Return value as C string */

    /* Suppress attempts to query the password */
    if (ConnOptions[optionNum].info == INDX_PASS) {
	return Tcl_NewObj();
    }
    if (ConnOptions[optionNum].type == TYPE_ENCODING) {
	value = (char* )pg_encoding_to_char(PQclientEncoding(cdata->pgPtr));
	return Tcl_NewStringObj(value, -1);
	return StringObjFromExternal(cdata, value, -1);
    }

    if (ConnOptions[optionNum].type == TYPE_ISOLATION) {
	if (cdata->isolation == ISOL_NONE) {
	    PGresult * res;
	    char * isoName;
	    PGresult* res;
	    char* isoName;
	    int i = 0;

	    /* The isolation level wasn't set - get default value */

	    if (ExecSimpleQuery(interp, cdata->pgPtr,
	    if (ExecSimpleQuery(cdata, interp,
		    "SHOW default_transaction_isolation", &res) != TCL_OK) {
		return NULL;
	    }
	    value = PQgetvalue(res, 0, 0);
	    isoName = (char*) ckalloc(strlen(value) + 1);
	    strcpy(isoName, value);
	    PQclear(res);
991
992
993
994
995
996
997
998

999
1000
1001
1002
1003
1004
1005
1056
1057
1058
1059
1060
1061
1062

1063
1064
1065
1066
1067
1068
1069
1070







-
+







	    return literals[LIT_1];
	}
    }

    if (ConnOptions[optionNum].queryF != NULL) {
	value = ConnOptions[optionNum].queryF(cdata->pgPtr);
	if (value != NULL) {
	    return Tcl_NewStringObj(value, -1);
	    return StringObjFromExternal(cdata, value, -1);
	}
    }
    if (ConnOptions[optionNum].type == TYPE_STRING &&
	    ConnOptions[optionNum].info != -1) {
	/* Fallback: get value saved ealier */
	value = cdata->savedOpts[ConnOptions[optionNum].info];
	if (value != NULL) {
1037
1038
1039
1040
1041
1042
1043
1044

1045
1046
1047
1048
1049
1050
1051

1052
1053
1054
1055
1056
1057
1058
1102
1103
1104
1105
1106
1107
1108

1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124







-
+







+







) {
    int optionIndex;		/* Index of the current option in
				 * ConnOptions */
    int optionValue;		/* Integer value of the current option */
    int i;
    size_t j;
    char portval[16];		/* String representation of port number */
    char * encoding = NULL;	/* Selected encoding name */
    char* encoding = NULL;	/* Selected encoding name */
    int isolation = ISOL_NONE;	/* Isolation level */
    int readOnly = -1;		/* Read only indicator */
#define CONNINFO_LEN 1000
    char connInfo[CONNINFO_LEN]; /* Configuration string for PQconnectdb() */

    Tcl_Obj* retval;
    Tcl_Obj* optval;
    char* valPtr;
    int vers;			/* PostgreSQL major version */

    if (cdata->pgPtr != NULL) {

	/* Query configuration options on an existing connection */

	if (objc == skip) {
1095
1096
1097
1098
1099
1100
1101


1102


1103
1104
1105
1106
1107
1108
1109
1161
1162
1163
1164
1165
1166
1167
1168
1169

1170
1171
1172
1173
1174
1175
1176
1177
1178







+
+
-
+
+







	Tcl_WrongNumArgs(interp, skip, objv, "?-option value?...");
	return TCL_ERROR;
    }

    /* Extract options from the command line */

    for (i = 0; i < INDX_MAX; ++i) {
	if (cdata->savedOpts[i] != NULL) {
	    ckfree(cdata->savedOpts[i]);
	cdata->savedOpts[i] = NULL;
	    cdata->savedOpts[i] = NULL;
	}
    }
    for (i = skip; i < objc; i += 2) {

	/* Unknown option */

	if (Tcl_GetIndexFromObjStruct(interp, objv[i], (void*) ConnOptions,
				      sizeof(ConnOptions[0]), "option",
1125
1126
1127
1128
1129
1130
1131

1132
1133


1134
1135
1136
1137
1138
1139
1140
1194
1195
1196
1197
1198
1199
1200
1201
1202

1203
1204
1205
1206
1207
1208
1209
1210
1211







+

-
+
+







	    return TCL_ERROR;
	}

	/* Record option value */

	switch (ConnOptions[optionIndex].type) {
	case TYPE_STRING:
	    valPtr = Tcl_GetString(objv[i+1]);
	    cdata->savedOpts[ConnOptions[optionIndex].info] =
		Tcl_GetString(objv[i+1]);
		ckalloc(strlen(valPtr) + 1);
	    strcpy(cdata->savedOpts[ConnOptions[optionIndex].info], valPtr);
	    break;
	case TYPE_ENCODING:
	    encoding = Tcl_GetString(objv[i+1]);
	    break;
	case TYPE_ISOLATION:
	    if (Tcl_GetIndexFromObjStruct(interp, objv[i+1], TclIsolationLevels,
				    sizeof(char *), "isolation level", TCL_EXACT, &isolation)
1151
1152
1153
1154
1155
1156
1157
1158


1159
1160
1161
1162
1163
1164
1165
1222
1223
1224
1225
1226
1227
1228

1229
1230
1231
1232
1233
1234
1235
1236
1237







-
+
+







							  "be in range "
							  "[0..65535]", -1));
		Tcl_SetErrorCode(interp, "TDBC", "GENERAL_ERROR", "HY000",
				 "POSTGRES", "-1", NULL);
		return TCL_ERROR;
	    }
	    snprintf(portval, 16, "%d", optionValue);
	    cdata->savedOpts[INDX_PORT] = portval;
	    cdata->savedOpts[INDX_PORT] = ckalloc(strlen(portval) + 1);
	    strcpy(cdata->savedOpts[INDX_PORT], portval);
	    break;
	case TYPE_READONLY:
	    if (Tcl_GetBooleanFromObj(interp, objv[i+1], &readOnly)
		!= TCL_OK) {
		return TCL_ERROR;
	    }
	    break;
1189
1190
1191
1192
1193
1194
1195
1196

1197
1198
1199
1200
1201
1202
1203
1204
1205
1206

1207
1208
1209
1210
1211
1212
1213
1214

1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225

1226
1227
1228
1229
1230

1231
1232
1233
1234
1235
1236
1237
1261
1262
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







-
+









-
+







-
+










-
+




-
+







					      "propably out of memory.", -1));
	    Tcl_SetErrorCode(interp, "TDBC", "GENERAL_ERROR", "HY001",
			     "POSTGRES", "NULL", NULL);
	    return TCL_ERROR;
	}

	if (PQstatus(cdata->pgPtr) != CONNECTION_OK) {
	    TransferPostgresError(interp, cdata->pgPtr);
	    TransferPostgresError(cdata, interp);
	    return TCL_ERROR;
	}
	PQsetNoticeProcessor(cdata->pgPtr, DummyNoticeProcessor, NULL);
    }

    /* Character encoding */

    if (encoding != NULL ) {
	if (PQsetClientEncoding(cdata->pgPtr, encoding) != 0) {
	    TransferPostgresError(interp, cdata->pgPtr);
	    TransferPostgresError(cdata, interp);
	    return TCL_ERROR;
	}
    }

    /* Transaction isolation level */

    if (isolation != ISOL_NONE) {
	if (ExecSimpleQuery(interp, cdata->pgPtr,
	if (ExecSimpleQuery(cdata, interp,
		    SqlIsolationLevels[isolation], NULL) != TCL_OK) {
	    return TCL_ERROR;
	}
	cdata->isolation = isolation;
    }

    /* Readonly indicator */

    if (readOnly != -1) {
	if (readOnly == 0) {
	    if (ExecSimpleQuery(interp, cdata->pgPtr,
	    if (ExecSimpleQuery(cdata, interp,
			"SET TRANSACTION READ WRITE", NULL) != TCL_OK) {
		return TCL_ERROR;
	    }
	} else {
	    if (ExecSimpleQuery(interp, cdata->pgPtr,
	    if (ExecSimpleQuery(cdata, interp,
			"SET TRANSACTION READ ONLY", NULL) != TCL_OK) {
		return TCL_ERROR;
	    }

	}
	cdata->readOnly = readOnly;
    }
1245
1246
1247
1248
1249
1250
1251
1252

1253
1254
1255
1256
1257
1258
1259
1317
1318
1319
1320
1321
1322
1323

1324
1325
1326
1327
1328
1329
1330
1331







-
+







    /*
     * On PostgreSQL 9.0 and later, change 'bytea_output' to the
     * backward-compatible 'escape' setting, so that the code in
     * ResultSetNextrowMethod will retrieve byte array values correctly
     * on either 8.x or 9.x servers.
     */
    if (vers >= 9) {
	if (ExecSimpleQuery(interp, cdata->pgPtr,
	if (ExecSimpleQuery(cdata, interp,
			    "SET bytea_output = 'escape'", NULL) != TCL_OK) {
	    return TCL_ERROR;
	}
    }
    return TCL_OK;
}

1293
1294
1295
1296
1297
1298
1299

1300
1301
1302
1303
1304
1305
1306
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379







+








    /* Hang client data on this connection */

    cdata = (ConnectionData*) ckalloc(sizeof(ConnectionData));
    memset(cdata, 0, sizeof(ConnectionData));
    cdata->refCount = 1;
    cdata->pidata = pidata;
    cdata->enc = Tcl_GetEncoding(NULL, "utf-8");
    cdata->pgPtr = NULL;
    cdata->stmtCounter = 0;
    cdata->flags = 0;
    cdata->isolation = ISOL_NONE;
    cdata->readOnly = 0;
    IncrPerInterpRefCount(pidata);
    Tcl_ObjectSetMetadata(thisObject, &connectionDataType, (ClientData) cdata);
1364
1365
1366
1367
1368
1369
1370
1371

1372
1373
1374
1375
1376
1377
1378
1437
1438
1439
1440
1441
1442
1443

1444
1445
1446
1447
1448
1449
1450
1451







-
+







			 "POSTGRES", "-1", NULL);
	return TCL_ERROR;
    }
   cdata->flags |= CONN_FLAG_IN_XCN;

   /* Execute begin trasnaction block command */

   return ExecSimpleQuery(interp, cdata->pgPtr, "BEGIN", NULL);
   return ExecSimpleQuery(cdata, interp, "BEGIN", NULL);
}

/*
 *-----------------------------------------------------------------------------
 *
 * ConnectionCommitMethod --
 *
1422
1423
1424
1425
1426
1427
1428
1429

1430
1431
1432
1433
1434
1435
1436
1495
1496
1497
1498
1499
1500
1501

1502
1503
1504
1505
1506
1507
1508
1509







-
+







			 "POSTGRES", "-1", NULL);
	return TCL_ERROR;
    }

    cdata->flags &= ~ CONN_FLAG_IN_XCN;

    /* Execute commit SQL command */
    return ExecSimpleQuery(interp, cdata->pgPtr, "COMMIT", NULL);
    return ExecSimpleQuery(cdata, interp, "COMMIT", NULL);
}

/*
 *-----------------------------------------------------------------------------
 *
 * ConnectionColumnsMethod --
 *
1470
1471
1472
1473
1474
1475
1476

1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492


1493

1494
1495
1496

1497
1498
1499
1500
1501
1502
1503
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565

1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580







+















-
+
+

+



+







    char* columnName;		/* Name of the column */
    Oid typeOid;		/* Oid of column type */
    Tcl_Obj* retval;		/* List of table names */
    Tcl_Obj* attrs;		/* Attributes of the column */
    Tcl_Obj* name;		/* Name of a column */
    Tcl_Obj* sqlQuery = Tcl_NewStringObj("SELECT * FROM \"", -1);
				/* Query used */
    Tcl_DString ds;

    Tcl_IncrRefCount(sqlQuery);

    /* Check parameters */

    if (objc < 3 || objc > 4) {
	Tcl_WrongNumArgs(interp, 2, objv, "table ?pattern?");
	return TCL_ERROR;
    }

    /* Check if table exists by retreiving one row.
     * The result will be later used to determine column types (oids) */
    Tcl_AppendObjToObj(sqlQuery, objv[2]);
    Tcl_AppendToObj(sqlQuery, "\" LIMIT 1", -1);

    if (ExecSimpleQuery(interp, cdata->pgPtr, Tcl_GetString(sqlQuery),
    if (ExecSimpleQuery(cdata, interp,
			ExternalFromStringObj(cdata, sqlQuery, &ds),
			&resType) != TCL_OK) {
	Tcl_DStringFree(&ds);
	Tcl_DecrRefCount(sqlQuery);
	return TCL_ERROR;
    }
    Tcl_DStringFree(&ds);

    Tcl_DecrRefCount(sqlQuery);

    /* Retreive column attributes */

    sqlQuery = Tcl_NewStringObj("SELECT "
	"  column_name,"
1512
1513
1514
1515
1516
1517
1518
1519
1520




1521
1522
1523
1524
1525


1526
1527
1528
1529
1530
1531
1532
1533

1534
1535
1536
1537
1538
1539
1540
1589
1590
1591
1592
1593
1594
1595


1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613

1614
1615
1616
1617
1618
1619
1620
1621







-
-
+
+
+
+





+
+







-
+








    if (objc == 4) {
	Tcl_AppendToObj(sqlQuery, "' AND column_name LIKE '", -1);
	Tcl_AppendObjToObj(sqlQuery, objv[3]);
    }
    Tcl_AppendToObj(sqlQuery, "'", -1);

    if (ExecSimpleQuery(interp, cdata->pgPtr,
			Tcl_GetString(sqlQuery), &res) != TCL_OK) {
    if (ExecSimpleQuery(cdata, interp,
			ExternalFromStringObj(cdata, sqlQuery, &ds),
			&res) != TCL_OK) {
	Tcl_DStringFree(&ds);
	Tcl_DecrRefCount(sqlQuery);
	PQclear(resType);
	return TCL_ERROR;
    } else {
	int i, j;

	Tcl_DStringFree(&ds);
	retval = Tcl_NewObj();
	Tcl_IncrRefCount(retval);
	for (i = 0; i < PQntuples(res); i += 1) {
	    attrs = Tcl_NewObj();

	    /* 0 is column_name column number */
	    columnName = PQgetvalue(res, i, 0);
	    name = Tcl_NewStringObj(columnName, -1);
	    name = StringObjFromExternal(cdata, columnName, -1);

	    Tcl_DictObjPut(NULL, attrs, literals[LIT_NAME], name);

	    /* Get the type name, by retrieving type oid */

	    j = PQfnumber(resType, columnName);
	    if (j >= 0) {
1552
1553
1554
1555
1556
1557
1558
1559

1560
1561
1562
1563
1564
1565
1566

1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577

1578
1579
1580
1581
1582
1583
1584
1633
1634
1635
1636
1637
1638
1639

1640
1641
1642
1643
1644
1645
1646

1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657

1658
1659
1660
1661
1662
1663
1664
1665







-
+






-
+










-
+







		}
	    }

	    /* 1 is numeric_precision column number */

	    if (!PQgetisnull(res, i, 1)) {
		Tcl_DictObjPut(NULL, attrs, literals[LIT_PRECISION],
			Tcl_NewStringObj(PQgetvalue(res, i, 1), -1));
		    StringObjFromExternal(cdata, PQgetvalue(res, i, 1), -1));
	    } else {

		/* 2 is character_maximum_length column number */

		if (!PQgetisnull(res, i, 2)) {
		    Tcl_DictObjPut(NULL, attrs, literals[LIT_PRECISION],
			    Tcl_NewStringObj(PQgetvalue(res, i, 2), -1));
			StringObjFromExternal(cdata, PQgetvalue(res, i, 2), -1));
		}
	    }

	    /* 3 is character_maximum_length column number */

	    if (!PQgetisnull(res, i, 3)) {

		/* This is for numbers */

		Tcl_DictObjPut(NULL, attrs, literals[LIT_SCALE],
			Tcl_NewStringObj(PQgetvalue(res, i, 3), -1));
			StringObjFromExternal(cdata, PQgetvalue(res, i, 3), -1));
	    }

	    /* 4 is is_nullable column number */

	    Tcl_DictObjPut(NULL, attrs, literals[LIT_NULLABLE],
		    Tcl_NewWideIntObj(strcmp("YES",
			    PQgetvalue(res, i, 4)) == 0));
1695
1696
1697
1698
1699
1700
1701
1702

1703
1704
1705
1706
1707
1708
1709
1776
1777
1778
1779
1780
1781
1782

1783
1784
1785
1786
1787
1788
1789
1790







-
+







			 "POSTGRES", "-1", NULL);
	return TCL_ERROR;
    }

    cdata->flags &= ~CONN_FLAG_IN_XCN;

    /* Send end transaction SQL command */
    return ExecSimpleQuery(interp, cdata->pgPtr, "ROLLBACK", NULL);
    return ExecSimpleQuery(cdata, interp, "ROLLBACK", NULL);
}

/*
 *-----------------------------------------------------------------------------
 *
 * ConnectionTablesMethod --
 *
1734
1735
1736
1737
1738
1739
1740
1741

1742
1743
1744
1745
1746
1747
1748


1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762

1763
1764
1765
1766
1767
1768
1769


1770

1771
1772
1773

1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784

1785
1786
1787
1788
1789
1790
1791
1815
1816
1817
1818
1819
1820
1821

1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844

1845
1846
1847
1848
1849
1850
1851

1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869

1870
1871
1872
1873
1874
1875
1876
1877







-
+







+
+













-
+






-
+
+

+



+










-
+







				/* The current connection object */
    ConnectionData* cdata = (ConnectionData*)
	Tcl_ObjectGetMetadata(thisObject, &connectionDataType);
				/* Instance data */
    Tcl_Obj** literals = cdata->pidata->literals;
				/* Literal pool */
    PGresult* res;		/* Result of libpq call */
    char * field;		/* Field value from SQL result */
    char* field;		/* Field value from SQL result */
    Tcl_Obj* retval;		/* List of table names */
    Tcl_Obj* sqlQuery = Tcl_NewStringObj("SELECT tablename"
					 " FROM pg_tables"
					 " WHERE  schemaname = 'public'",
					 -1);
				/* SQL query for table list */
    int i;
    Tcl_DString ds;

    Tcl_IncrRefCount(sqlQuery);

    /* Check parameters */

    if (objc < 2 || objc > 3) {
	Tcl_WrongNumArgs(interp, 2, objv, "");
	return TCL_ERROR;
    }

    if (objc == 3) {

	/* Pattern string is given */

	Tcl_AppendToObj(sqlQuery, " AND  tablename LIKE '", -1);
	Tcl_AppendToObj(sqlQuery, " AND tablename LIKE '", -1);
	Tcl_AppendObjToObj(sqlQuery, objv[2]);
	Tcl_AppendToObj(sqlQuery, "'", -1);
    }

    /* Retrieve the table list */

    if (ExecSimpleQuery(interp, cdata ->pgPtr, Tcl_GetString(sqlQuery),
    if (ExecSimpleQuery(cdata, interp,
			ExternalFromStringObj(cdata, sqlQuery, &ds),
			&res) != TCL_OK) {
	Tcl_DStringFree(&ds);
	Tcl_DecrRefCount(sqlQuery);
	return TCL_ERROR;
    }
    Tcl_DStringFree(&ds);
    Tcl_DecrRefCount(sqlQuery);

    /* Iterate through the tuples and make the Tcl result */

    retval = Tcl_NewObj();
    for (i = 0; i < PQntuples(res); i+=1) {
	if (!PQgetisnull(res, i, 0)) {
	    field = PQgetvalue(res, i, 0);
	    if (field) {
		Tcl_ListObjAppendElement(NULL, retval,
			Tcl_NewStringObj(field, -1));
			StringObjFromExternal(cdata, field, -1));
		Tcl_ListObjAppendElement(NULL, retval, literals[LIT_EMPTY]);
	    }
	}
    }
    PQclear(res);

    Tcl_SetObjResult(interp, retval);
1816
1817
1818
1819
1820
1821
1822


1823
1824
1825
1826







1827
1828
1829
1830
1831
1832
1833
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928







+
+




+
+
+
+
+
+
+







    DecrConnectionRefCount((ConnectionData*)clientData);
}

static void
DeleteConnection(
    ConnectionData* cdata	/* Instance data for the connection */
) {
    int i;

    if (cdata->pgPtr != NULL) {
	PQfinish(cdata->pgPtr);
    }
    DecrPerInterpRefCount(cdata->pidata);
    for (i = 0; i < INDX_MAX; ++i) {
	if (cdata->savedOpts[i] != NULL) {
	    ckfree(cdata->savedOpts[i]);
	    cdata->savedOpts[i] = NULL;
	}
    }
    Tcl_FreeEncoding(cdata->enc);
    ckfree(cdata);
}

/*
 *-----------------------------------------------------------------------------
 *
 * CloneConnection --
1946
1947
1948
1949
1950
1951
1952
1953

1954
1955
1956

1957
1958
1959
1960
1961
1962
1963
2041
2042
2043
2044
2045
2046
2047

2048
2049
2050

2051
2052
2053
2054
2055
2056
2057
2058







-
+


-
+







 *	Nothing.
 *
 *-----------------------------------------------------------------------------
 */

static void
UnallocateStatement(
	PGconn * pgPtr,	    /* Connection handle */
	PGconn* pgPtr,	    /* Connection handle */
	char* stmtName	    /* Statement name */
) {
    Tcl_Obj * sqlQuery = Tcl_NewStringObj("DEALLOCATE ", -1);
    Tcl_Obj* sqlQuery = Tcl_NewStringObj("DEALLOCATE ", -1);
    Tcl_IncrRefCount(sqlQuery);
    Tcl_AppendToObj(sqlQuery, stmtName, -1);
    PQclear(PQexec(pgPtr, Tcl_GetString(sqlQuery)));
    Tcl_DecrRefCount(sqlQuery);
}

/*
2013
2014
2015
2016
2017
2018
2019
2020

2021
2022
2023
2024
2025
2026
2027
2028

2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039

2040

2041
2042

2043
2044
2045
2046
2047
2048
2049
2050
2051
2052

2053
2054
2055
2056
2057
2058
2059
2108
2109
2110
2111
2112
2113
2114

2115
2116
2117
2118
2119

2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133

2134
2135
2136
2137

2138
2139
2140
2141
2142
2143
2144
2145
2146
2147

2148
2149
2150
2151
2152
2153
2154
2155







-
+




-



+










-
+

+

-
+









-
+







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

static PGresult*
PrepareStatement(
    Tcl_Interp* interp,		/* Tcl interpreter for error reporting */
    StatementData* sdata,	/* Statement data */
    char * stmtName		/* Overriding name of the statement */
    char* stmtName		/* Overriding name of the statement */
) {
    ConnectionData* cdata = sdata->cdata;
				/* Connection data */
    const char* nativeSqlStr;	/* Native SQL statement to prepare */
    int nativeSqlLen;		/* Length of the statement */
    PGresult* res;		/* result of statement preparing*/
    PGresult* res2;
    int i;
    Tcl_DString ds;

    if (stmtName == NULL) {
	stmtName = sdata->stmtName;
    }

    /*
     * Prepare the statement. Rather than giving parameter types, try
     * to let PostgreSQL infer all of them.
     */

    nativeSqlStr = Tcl_GetStringFromObj(sdata->nativeSql, &nativeSqlLen);
    nativeSqlStr = ExternalFromStringObj(cdata, sdata->nativeSql, &ds);
    res = PQprepare(cdata->pgPtr, stmtName, nativeSqlStr, 0, NULL);
    Tcl_DStringFree(&ds);
    if (res == NULL) {
	TransferPostgresError(interp, cdata->pgPtr);
	TransferPostgresError(cdata, interp);
	return NULL;
    }

    /*
     * Report on what parameter types were inferred.
     */

    res2 = PQdescribePrepared(cdata->pgPtr, stmtName);
    if (res2 == NULL) {
	TransferPostgresError(interp, cdata->pgPtr);
	TransferPostgresError(cdata, interp);
	PQclear(res);
	return NULL;
    }
    for (i = 0; i < PQnparams(res2); ++i) {
	sdata->paramDataTypes[i] = PQparamtype(res2, i);
	sdata->params[i].precision = 0;
	sdata->params[i].scale = 0;
2078
2079
2080
2081
2082
2083
2084

2085
2086
2087
2088
2089
2090

2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102

2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120

2121

2122
2123
2124
2125
2126
2127
2128
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186

2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198

2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216

2217
2218
2219
2220
2221
2222
2223
2224
2225
2226







+





-
+











-
+

















-
+

+







 * column name.
 *
 *-----------------------------------------------------------------------------
 */

static Tcl_Obj*
ResultDescToTcl(
    ConnectionData* cdata,	/* Instance data for the connection */
    PGresult* result,		/* Result set description */
    int flags			/* Flags governing the conversion */
) {
    Tcl_Obj* retval = Tcl_NewObj();
    Tcl_HashTable names;	/* Hash table to resolve name collisions */
    char * fieldName;
    char* fieldName;
    Tcl_InitHashTable(&names, TCL_STRING_KEYS);
    if (result != NULL) {
	unsigned int fieldCount = PQnfields(result);
	unsigned int i;
	for (i = 0; i < fieldCount; ++i) {
	    int isNew;
	    int count = 1;
	    Tcl_Obj* nameObj;
	    Tcl_HashEntry* entry;
	    Tcl_HashEntry* countEntry;
	    fieldName = PQfname(result, i);
	    nameObj = Tcl_NewStringObj(fieldName, -1);
	    nameObj = StringObjFromExternal(cdata, fieldName, -1);
	    Tcl_IncrRefCount(nameObj);

	    countEntry = NULL;
	    for (;;) {
		entry = Tcl_CreateHashEntry(&names, Tcl_GetString(nameObj),
					    &isNew);
		if (isNew) {
		    Tcl_SetHashValue(entry, (ClientData) 1);
		    break;
		}
		if (countEntry == NULL) {
		    countEntry = entry;
		}
		count = PTR2INT(Tcl_GetHashValue(countEntry));
		++count;
		Tcl_SetHashValue(countEntry, INT2PTR(count));
		Tcl_DecrRefCount(nameObj);
		nameObj = Tcl_ObjPrintf("%s#%d", fieldName, count);
		nameObj = StringObjFromExternal(cdata, fieldName, -1);
		Tcl_IncrRefCount(nameObj);
		Tcl_AppendPrintfToObj(nameObj, "#%d", count);
	    }

	    Tcl_ListObjAppendElement(NULL, retval, nameObj);
	    Tcl_DecrRefCount(nameObj);
	}
    }
    Tcl_DeleteHashTable(&names);
2281
2282
2283
2284
2285
2286
2287
2288

2289
2290
2291
2292
2293
2294
2295
2379
2380
2381
2382
2383
2384
2385

2386
2387
2388
2389
2390
2391
2392
2393







-
+








    /* Prepare the statement */

    res = PrepareStatement(interp, sdata, NULL);
    if (res == NULL) {
	goto freeSData;
    }
    if (TransferResultError(interp, res) != TCL_OK) {
    if (TransferResultError(cdata, interp, res) != TCL_OK) {
	PQclear(res);
	goto freeSData;
    }

    PQclear(res);

    /* Attach the current statement data as metadata to the current object */
2656
2657
2658
2659
2660
2661
2662
2663

2664
2665
2666
2667
2668
2669
2670
2754
2755
2756
2757
2758
2759
2760

2761
2762
2763
2764
2765
2766
2767
2768







-
+







    int* paramLengths;		/* Table of parameter lengths */
    int* paramFormats;		/* Table of parameter formats
				 * (binary or string) */
    char* paramNeedsFreeing;	/* Flags for whether a parameter needs
				 * its memory released */
    Tcl_Obj** paramTempObjs;	/* Temporary parameter objects allocated
				 * to canonicalize numeric parameter values */

    Tcl_DString* paramTempDs;	/* Temporary string buffers */
    PGresult* res;		/* Temporary result */
    int i;
    int status = TCL_ERROR;	/* Return status */

    /* Check parameter count */

    if (objc != skip+1 && objc != skip+2) {
2710
2711
2712
2713
2714
2715
2716
2717

2718
2719
2720
2721
2722
2723
2724
2808
2809
2810
2811
2812
2813
2814

2815
2816
2817
2818
2819
2820
2821
2822







-
+








    if (sdata->flags & STMT_FLAG_BUSY) {
	rdata->stmtName = GenStatementName(cdata);
	res = PrepareStatement(interp, sdata, rdata->stmtName);
	if (res == NULL) {
	    return TCL_ERROR;
	}
	if (TransferResultError(interp, res) != TCL_OK) {
	if (TransferResultError(cdata, interp, res) != TCL_OK) {
	    PQclear(res);
	    return TCL_ERROR;
	}
	PQclear(res);
    } else {
	rdata->stmtName = sdata->stmtName;
	sdata->flags |= STMT_FLAG_BUSY;
2732
2733
2734
2735
2736
2737
2738
2739

2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752

2753
2754
2755
2756

2757
2758
2759
2760
2761
2762
2763
2830
2831
2832
2833
2834
2835
2836

2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863







-
+













+




+







	    ckfree(sdata->stmtName);
	    sdata->stmtName = GenStatementName(cdata);
	    rdata->stmtName = sdata->stmtName;
	    res = PrepareStatement(interp, sdata, NULL);
	    if (res == NULL) {
		return TCL_ERROR;
	    }
	    if (TransferResultError(interp, res) != TCL_OK) {
	    if (TransferResultError(cdata, interp, res) != TCL_OK) {
		PQclear(res);
		return TCL_ERROR;
	    }
	    PQclear(res);
	    sdata->paramTypesChanged = 0;
	}
    }

    paramValues = (const char**) ckalloc(sdata->nParams * sizeof(char* ));
    paramLengths = (int*) ckalloc(sdata->nParams * sizeof(int*));
    paramFormats = (int*) ckalloc(sdata->nParams * sizeof(int*));
    paramNeedsFreeing = (char *)ckalloc(sdata->nParams);
    paramTempObjs = (Tcl_Obj**) ckalloc(sdata->nParams * sizeof(Tcl_Obj*));
    paramTempDs = (Tcl_DString*) ckalloc(sdata->nParams * sizeof(Tcl_DString));

    memset(paramNeedsFreeing, 0, sdata->nParams);
    for (i = 0; i < sdata->nParams; i++) {
	paramTempObjs[i] = NULL;
	Tcl_DStringInit(&paramTempDs[i]);
    }

    for (i=0; i<sdata->nParams; i++) {
	Tcl_ListObjIndex(NULL, sdata->subVars, i, &paramNameObj);
	paramName = Tcl_GetString(paramNameObj);
	if (objc == skip+2) {
	    /* Param from a dictionary */
2771
2772
2773
2774
2775
2776
2777
2778

2779
2780
2781
2782
2783
2784
2785
2871
2872
2873
2874
2875
2876
2877

2878
2879
2880
2881
2882
2883
2884
2885







-
+








	    paramValObj = Tcl_GetVar2Ex(interp, paramName, NULL,
					TCL_LEAVE_ERR_MSG);

	}
	/* At this point, paramValObj contains the parameter value */
	if (paramValObj != NULL) {
	    char * bufPtr;
	    char* bufPtr;
	    int32_t tmp32;
	    int16_t tmp16;

	    switch (sdata->paramDataTypes[i]) {
	    case INT2OID:
		bufPtr = (char *)ckalloc(sizeof(int));
		if (Tcl_GetIntFromObj(interp, paramValObj,
2826
2827
2828
2829
2830
2831
2832
2833
2834



2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853



2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872




2873
2874
2875
2876
2877
2878
2879
2880
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
2926
2927
2928
2929
2930
2931
2932


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


2973
2974
2975
2976
2977
2978
2979
2980
2981
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







-
-
+
+
+

















-
-
+
+
+

















-
-
+
+
+
+












-
+



-
+













+







+







		    Tcl_WideInt val;
		    if (Tcl_GetWideIntFromObj(NULL, paramValObj, &val)
			== TCL_OK) {
			paramTempObjs[i] = Tcl_NewWideIntObj(val);
			Tcl_IncrRefCount(paramTempObjs[i]);
			paramFormats[i] = 0;
			paramValues[i] =
			    Tcl_GetStringFromObj(paramTempObjs[i],
						 &paramLengths[i]);
			    ExternalFromStringObj(cdata, paramTempObjs[i],
						  &paramTempDs[i]);
			paramLengths[i] = Tcl_DStringLength(&paramTempDs[i]);
		    } else {
			goto convertString;
				/* If Tcl can't parse it, let SQL try */
		    }
		}
		break;

	    case FLOAT4OID:
	    case FLOAT8OID:
		{
		    double val;
		    if (Tcl_GetDoubleFromObj(NULL, paramValObj, &val)
			== TCL_OK) {
			paramTempObjs[i] = Tcl_NewDoubleObj(val);
			Tcl_IncrRefCount(paramTempObjs[i]);
			paramFormats[i] = 0;
			paramValues[i] =
			    Tcl_GetStringFromObj(paramTempObjs[i],
						 &paramLengths[i]);
			    ExternalFromStringObj(cdata, paramTempObjs[i],
						  &paramTempDs[i]);
			paramLengths[i] = Tcl_DStringLength(&paramTempDs[i]);
		    } else {
			goto convertString;
				/* If Tcl can't parse it, let SQL try */
		    }
		}
		break;

	    case BYTEAOID:
		paramFormats[i] = 1;
		paramValues[i] =
		    (char*)Tcl_GetByteArrayFromObj(paramValObj,
						   &paramLengths[i]);
		break;

	    default:
	    convertString:
		paramFormats[i] = 0;
		paramValues[i] = Tcl_GetStringFromObj(paramValObj,
						      &paramLengths[i]);
		paramValues[i] =
		    ExternalFromStringObj(cdata, paramValObj,
					  &paramTempDs[i]);
		paramLengths[i] = Tcl_DStringLength(&paramTempDs[i]);
		break;
	    }
	} else {
	    paramValues[i] = NULL;
	    paramFormats[i] = 0;
	}
    }

    /* Execute the statement */
    rdata->execResult = PQexecPrepared(cdata->pgPtr, rdata->stmtName,
				       sdata->nParams, paramValues,
				       paramLengths, paramFormats, 0);
    if (TransferResultError(interp, rdata->execResult) != TCL_OK) {
    if (TransferResultError(cdata, interp, rdata->execResult) != TCL_OK) {
	goto freeParamTables;
    }

    sdata->columnNames = ResultDescToTcl(rdata->execResult, 0);
    sdata->columnNames = ResultDescToTcl(cdata, rdata->execResult, 0);
    Tcl_IncrRefCount(sdata->columnNames);
    status = TCL_OK;

    /* Clean up allocated memory */

 freeParamTables:
    for (i = 0; i < sdata->nParams; ++i) {
	if (paramNeedsFreeing[i]) {
	    ckfree(paramValues[i]);
	}
	if (paramTempObjs[i] != NULL) {
	    Tcl_DecrRefCount(paramTempObjs[i]);
	}
	Tcl_DStringFree(&paramTempDs[i]);
    }

    ckfree(paramValues);
    ckfree(paramLengths);
    ckfree(paramFormats);
    ckfree(paramNeedsFreeing);
    ckfree(paramTempObjs);
    ckfree(paramTempDs);

    return status;

}

/*
 *-----------------------------------------------------------------------------
3005
3006
3007
3008
3009
3010
3011
3012

3013
3014
3015
3016
3017
3018
3019
3111
3112
3113
3114
3115
3116
3117

3118
3119
3120
3121
3122
3123
3124
3125







-
+







    int nColumns = 0;		/* Number of columns in the result set */
    Tcl_Obj* colObj;		/* Column obtained from the row */
    Tcl_Obj* colName;		/* Name of the current column */
    Tcl_Obj* resultRow;		/* Row of the result set under construction */

    int status = TCL_ERROR;	/* Status return from this command */

    char * buffer;		/* buffer containing field value */
    char* buffer;		/* buffer containing field value */
    int buffSize;		/* size of buffer containing field value */
    int i;

    if (objc != 3) {
	Tcl_WrongNumArgs(interp, 2, objv, "varName");
	return TCL_ERROR;
    }
3043
3044
3045
3046
3047
3048
3049
3050

3051
3052
3053
3054
3055

3056
3057
3058
3059
3060
3061
3062
3149
3150
3151
3152
3153
3154
3155

3156
3157
3158
3159
3160

3161
3162
3163
3164
3165
3166
3167
3168







-
+




-
+







	    buffer = PQgetvalue(rdata->execResult, rdata->rowCount, i);
	    if (PQftype(rdata->execResult, i) == BYTEAOID) {
		/*
		 * Postgres returns backslash-escape sequences for
		 * binary data. Substitute them away.
		 */
		Tcl_Obj* toSubst;
		toSubst = Tcl_NewStringObj(buffer, buffSize);
		toSubst = StringObjFromExternal(cdata, buffer, buffSize);
		Tcl_IncrRefCount(toSubst);
		colObj = Tcl_SubstObj(interp, toSubst, TCL_SUBST_BACKSLASHES);
		Tcl_DecrRefCount(toSubst);
	    } else {
		colObj = Tcl_NewStringObj((char*)buffer, buffSize);
		colObj = StringObjFromExternal(cdata, (char*)buffer, buffSize);
	    }
	}

	if (lists) {
	    if (colObj == NULL) {
		colObj = Tcl_NewObj();
	    }
3176
3177
3178
3179
3180
3181
3182
3183

3184
3185
3186
3187
3188
3189
3190
3282
3283
3284
3285
3286
3287
3288

3289
3290
3291
3292
3293
3294
3295
3296







-
+







ResultSetRowcountMethod(
    ClientData clientData,	/* Not used */
    Tcl_Interp* interp,		/* Tcl interpreter */
    Tcl_ObjectContext context,	/* Object context  */
    int objc,			/* Parameter count */
    Tcl_Obj *const objv[]	/* Parameter vector */
) {
    char * nTuples;
    char* nTuples;
    Tcl_Object thisObject = Tcl_ObjectContextObject(context);
				/* The current result set object */
    ResultSetData* rdata = (ResultSetData*)
	Tcl_ObjectGetMetadata(thisObject, &resultSetDataType);
				/* Data pertaining to the current result set */
    StatementData* sdata = rdata->sdata;
				/* The current statement */
3198
3199
3200
3201
3202
3203
3204
3205

3206
3207
3208
3209
3210
3211
3212
3304
3305
3306
3307
3308
3309
3310

3311
3312
3313
3314
3315
3316
3317
3318







-
+







    }

    nTuples = PQcmdTuples(rdata->execResult);
    if (strlen(nTuples) == 0) {
	Tcl_SetObjResult(interp, literals[LIT_0]);
    } else {
	Tcl_SetObjResult(interp,
	    Tcl_NewStringObj(nTuples, -1));
		StringObjFromExternal(cdata, nTuples, -1));
    }
    return TCL_OK;
}

/*
 *-----------------------------------------------------------------------------
 *

Changes to jni/tcl/tcl-config.mk.

49
50
51
52
53
54
55
56

57
58
59
60
61
62
63
49
50
51
52
53
54
55

56
57
58
59
60
61
62
63







-
+







	-DHAVE_ZLIB=1 \
	-DMP_PREC=4 \
	-DMP_FIXED_CUTOFFS=1 \
	-DMP_NO_STDINT=1 \
	-DTCL_TOMMATH=1 \
	-D_REENTRANT=1 \
	-D_THREADSAFE=1 \
	-DTCL_UTF_MAX=6 \
	-DTCL_UTF_MAX=4 \
	-DTCL_THREADS=1 \
	-DUSE_THREAD_ALLOC=1 \
	-DTCL_CFGVAL_ENCODING="\"utf-8\"" \
	-DTCL_UNLOAD_DLLS=1 \
	-DTCL_CFG_OPTIMIZED=1 \
	-DZIPFS_IN_TCL=1 \
	-DTCL_PACKAGE_PATH="\"/assets\"" \

Changes to jni/tcl/tests/cmdIL.test.

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
15
16
17
18
19
20
21

22
23
24
25
26
27
28







-








::tcltest::loadTestedCommands
catch [list package require -exact Tcltest [info patchlevel]]

# Used for constraining memory leak tests
testConstraint memory [llength [info commands memory]]
testConstraint testobj [llength [info commands testobj]]
testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD"}]

test cmdIL-1.1 {Tcl_LsortObjCmd procedure} -returnCodes error -body {
    lsort
} -result {wrong # args: should be "lsort ?-option value ...? list"}
test cmdIL-1.2 {Tcl_LsortObjCmd procedure} -returnCodes error -body {
    lsort -foo {1 3 2 5}
} -result {bad option "-foo": must be -ascii, -command, -decreasing, -dictionary, -increasing, -index, -indices, -integer, -nocase, -real, -stride, or -unique}
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
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







-
+


-
+


-
+


-
+


-
+


-
+


-
+







    test_lsort 0
} -result 0 -cleanup {
    rename test_lsort ""
}
test cmdIL-5.6 {lsort with multiple list-style index options} {
    lsort -index {1 2 3} -index 0 {{a b} {c d} {b e}}
} {{a b} {b e} {c d}}
test cmdIL-5.7 {lsort unicode beyond U+FFFF} fullutf {
test cmdIL-5.7 {lsort unicode beyond U+FFFF} {
    lsort {\uD83D\uDE03 \uD83D\uDE02 \uD83D\uDE04}
} "\uD83D\uDE02 \uD83D\uDE03 \uD83D\udE04"
test cmdIL-5.7 {lsort unicode beyond U+FFFF} fullutf {
test cmdIL-5.7 {lsort unicode beyond U+FFFF} {
    lsort -decreasing {\uD83D\uDE03 \uD83D\uDE02 \uD83D\uDE04}
} "\uD83D\uDE04 \uD83D\uDE03 \uD83D\udE02"
test cmdIL-5.8 {lsort unicode beyond U+FFFF} fullutf {
test cmdIL-5.8 {lsort unicode beyond U+FFFF} {
    lsort -nocase {\U0001F603 \U0001F602 \U0001F604}
} "\U0001F602 \U0001F603 \U0001F604"
test cmdIL-5.9 {lsort unicode beyond U+FFFF} fullutf {
test cmdIL-5.9 {lsort unicode beyond U+FFFF} {
    lsort -dictionary {\U0001F603x1 \U0001F602y1 \U0001F602y \U0001F603xx}
} "\U0001F602y \U0001F602y1 \U0001F603x1 \U0001F603xx"
test cmdIL-5.9 {lsort unicode beyond U+FFFF} fullutf {
test cmdIL-5.9 {lsort unicode beyond U+FFFF} {
    lsort -dictionary {b\U0001F60320 c\U0001F60230 c\U0001F6023x b\U0001F6032}
} "b\U0001F6032 b\U0001F60320 c\U0001F6023x c\U0001F60230"
test cmdIL-5.10 {lsort unicode beyond U+FFFF} fullutf {
test cmdIL-5.10 {lsort unicode beyond U+FFFF} {
    lsort -nocase {b\U00010428a B\U00010400C}
} "b\U00010428a B\U00010400C"
test cmdIL-5.11 {lsort unicode beyond U+FFFF} fullutf {
test cmdIL-5.11 {lsort unicode beyond U+FFFF} {
    lsort -dictionary -nocase {b\U00010428a B\U00010400C}
} "b\U00010428a B\U00010400C"

# Compiled version
test cmdIL-6.1 {lassign command syntax} -returnCodes error -body {
    apply {{} { lassign }}
} -result {wrong # args: should be "lassign list ?varName ...?"}

Changes to jni/tcl/tests/encoding.test.

33
34
35
36
37
38
39
40
41

42
43
44
45
46
47
48
33
34
35
36
37
38
39


40
41
42
43
44
45
46
47







-
-
+







proc runtests {} {
    variable x

# Some tests require the testencoding command
testConstraint testencoding [llength [info commands testencoding]]
testConstraint testbytestring [llength [info commands testbytestring]]
testConstraint teststringbytes [llength [info commands teststringbytes]]
testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD"}]
testConstraint not389 [expr {[string length \U010000] == 1}]
testConstraint ucs4 [expr {[string length \U010000] == 1}]
testConstraint exec [llength [info commands exec]]
testConstraint testgetdefenc [llength [info commands testgetdefenc]]

# TclInitEncodingSubsystem is tested by the rest of this file
# TclFinalizeEncodingSubsystem is not currently tested

test encoding-1.1 {Tcl_GetEncoding: system encoding} -setup {
303
304
305
306
307
308
309
310
311
312




313
314

315
316
317
318
319
320
321
302
303
304
305
306
307
308



309
310
311
312
313

314
315
316
317
318
319
320
321







-
-
-
+
+
+
+

-
+







    append x [encoding convertfrom jis0208 8C&A]
} "8C&A\u4e4e\u3b1"
test encoding-12.5 {LoadTableEncoding: symbol encoding} {
    set x [encoding convertto symbol \u3b3]
    append x [encoding convertto symbol \u67]
    append x [encoding convertfrom symbol \x67]
} "\x67\x67\u3b3"
test encoding-12.6 {LoadTableEncoding: overflow in char value} {
    fullutf not389
} {
test encoding-12.6.1 {LoadTableEncoding: overflow in char value} ucs4 {
    encoding convertto iso8859-3 \U010000
} "?"
test encoding-12.6.2 {LoadTableEncoding: overflow in char value} !ucs4 {
    encoding convertto iso8859-3 \U010000
} "?"
} "??"

test encoding-13.1 {LoadEscapeTable} {
    viewable [set x [encoding convertto iso2022 ab\u4e4e\u68d9g]]
} [viewable "ab\x1b\$B8C\x1b\$\(DD%\x1b(Bg"]

test encoding-14.1 {BinaryProc} {
    encoding convertto identity \x12\x34\x56\xff\x69
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
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







-
-
-
+
+
+
+
+
+



-
+
-
-
-
-
-
-
-
-
+
-
-




-
-
+
+
-
-




-
-
+
+
-
-




-
-
+
+
-
-




-
-
+
+
-
-




-
-
+
+
-
-




-
-
+
+
-
-




-
-
+
+
-
-




-
-
+
+
-
-




-
-
+
+
-
-




-
+










-
+







+
+
+
+
+

-
+







    set z
} 00
test encoding-15.3 {UtfToUtfProc null character input} teststringbytes {
    set y [encoding convertfrom utf-8 [encoding convertto utf-8 \u0000]]
    binary scan [teststringbytes $y] H* z
    set z
} c080
test encoding-15.4 {UtfToUtfProc emoji character input} -constraints {
    fullutf
} -body {
test encoding-15.4 {UtfToUtfProc emoji character input} {
    set x \xed\xa0\xbd\xed\xb8\x82
    set y [encoding convertfrom utf-8 \xed\xa0\xbd\xed\xb8\x82]
    list [string length $x] $y
} "6 \uD83D\uDE02"
test encoding-15.5 {UtfToUtfProc emoji character input} {
    set x \xf0\x9f\x98\x82
    set y [encoding convertfrom utf-8 \xf0\x9f\x98\x82]
    list [string length $x] $y
} -result "4 \uD83D\uDE02"
} "4 \uD83D\uDE02"
test encoding-15.5 {UtfToUtfProc emoji character input} -constraints {
    fullutf
} -body {
    set x \xF0\x9F\x98\x82
    set y [encoding convertfrom utf-8 \xF0\x9F\x98\x82]
    list [string length $x] $y
} -result "4 \uD83D\uDE02"
test encoding-15.6 {UtfToUtfProc emoji character output} -constraints {
test encoding-15.6 {UtfToUtfProc emoji character output} {
    fullutf not389
} -body {
    set x \uDE02\uD83D\uDE02\uD83D
    set y [encoding convertto utf-8 \uDE02\uD83D\uDE02\uD83D]
    binary scan $y H* z
    list [string length $y] $z
} -result {10 edb882f09f9882eda0bd}
test encoding-15.7 {UtfToUtfProc emoji character output} -constraints {
} {10 edb882f09f9882eda0bd}
test encoding-15.7 {UtfToUtfProc emoji character output} {
    fullutf not389
} -body {
    set x \uDE02\uD83D\uD83D
    set y [encoding convertto utf-8 \uDE02\uD83D\uD83D]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {3 9 edb882eda0bdeda0bd}
test encoding-15.8 {UtfToUtfProc emoji character output} -constraints {
} {3 9 edb882eda0bdeda0bd}
test encoding-15.8 {UtfToUtfProc emoji character output} {
    fullutf not389
} -body {
    set x \uDE02\uD83D\xE9
    set y [encoding convertto utf-8 \uDE02\uD83D\xE9]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {3 8 edb882eda0bdc3a9}
test encoding-15.9 {UtfToUtfProc emoji character output} -constraints {
} {3 8 edb882eda0bdc3a9}
test encoding-15.9 {UtfToUtfProc emoji character output} {
    fullutf not389
} -body {
    set x \uDE02\uD83DX
    set y [encoding convertto utf-8 \uDE02\uD83DX]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {3 7 edb882eda0bd58}
test encoding-15.10 {UtfToUtfProc high surrogate character output} -constraints {
} {3 7 edb882eda0bd58}
test encoding-15.10 {UtfToUtfProc high surrogate character output} {
    fullutf not389
} -body {
    set x \uDE02\xE9
    set y [encoding convertto utf-8 \uDE02\xE9]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {2 5 edb882c3a9}
test encoding-15.11 {UtfToUtfProc low surrogate character output} -constraints {
} {2 5 edb882c3a9}
test encoding-15.11 {UtfToUtfProc low surrogate character output} {
    fullutf not389
} -body {
    set x \uDA02\xE9
    set y [encoding convertto utf-8 \uDA02\xE9]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {2 5 eda882c3a9}
test encoding-15.12 {UtfToUtfProc high surrogate character output} -constraints {
} {2 5 eda882c3a9}
test encoding-15.12 {UtfToUtfProc high surrogate character output} {
    fullutf not389
} -body {
    set x \uDE02Y
    set y [encoding convertto utf-8 \uDE02Y]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {2 4 edb88259}
test encoding-15.13 {UtfToUtfProc low surrogate character output} -constraints {
} {2 4 edb88259}
test encoding-15.13 {UtfToUtfProc low surrogate character output} {
    fullutf not389
} -body {
    set x \uDA02Y
    set y [encoding convertto utf-8 \uDA02Y]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {2 4 eda88259}
test encoding-15.14 {UtfToUtfProc high surrogate character output} -constraints {
} {2 4 eda88259}
test encoding-15.14 {UtfToUtfProc high surrogate character output} {
    fullutf not389
} -body {
    set x \uDE02
    set y [encoding convertto utf-8 \uDE02]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {1 3 edb882}
test encoding-15.15 {UtfToUtfProc low surrogate character output} -constraints {
} {1 3 edb882}
test encoding-15.15 {UtfToUtfProc low surrogate character output} {
    fullutf not389
} -body {
    set x \uDA02
    set y [encoding convertto utf-8 \uDA02]
    binary scan $y H* z
    list [string length $x] [string length $y] $z
} -result {1 3 eda882}
} {1 3 eda882}
test encoding-15.16 {UtfToUtfProc: Invalid 4-byte UTF-8, see [ed29806ba]} {
    set x \xF0\xA0\xA1\xC2
    set y [encoding convertfrom utf-8 \xF0\xA0\xA1\xC2]
    list [string length $x] $y
} "4 \xF0\xA0\xA1\xC2"

test encoding-16.1 {UnicodeToUtfProc} {
    set val [encoding convertfrom unicode NN]
    list $val [format %x [scan $val %c]]
} "\u4e4e 4e4e"
test encoding-16.2 {UnicodeToUtfProc} -constraints fullutf -body {
test encoding-16.2 {UnicodeToUtfProc} -body {
    set val [encoding convertfrom unicode "\xD8\xD8\xDC\xDC"]
    list $val [format %x [scan $val %c]]
} -result "\U460DC 460dc"
test encoding-16.3 {UnicodeToUtfProc} -body {
    set val [encoding convertfrom unicode "\xDC\xDC"]
    list $val [format %x [scan $val %c]]
} -result "\uDCDC dcdc"
test encoding-16.3 {UnicodeToUtfProc} -body {
    set val [encoding convertfrom unicode \
	[testbytestring "\xdc\xdc\xd8\xd8\xee\xee"]]
    list "\udcdc\ud8d8\ueeee" [format %x [scan $val %c]]
} -result [list "\udcdc\ud8d8\ueeee" dcdc]

test encoding-17.1 {UtfToUnicodeProc} -constraints fullutf -body {
test encoding-17.1 {UtfToUnicodeProc} -body {
    encoding convertto unicode "\U460DC"
} -result "\xD8\xD8\xDC\xDC"
test encoding-17.2 {UtfToUnicodeProc} -body {
    encoding convertto unicode "\uDCDC"
} -result "\xDC\xDC"
test encoding-17.3 {UtfToUnicodeProc} -body {
    encoding convertto unicode "\uD8D8"

Changes to jni/tcl/tests/scan.test.

83
84
85
86
87
88
89
90


91
92
93
94
95
96
97
83
84
85
86
87
88
89

90
91
92
93
94
95
96
97
98







-
+
+







	}
    }
}

testConstraint ieeeFloatingPoint [testIEEE]
testConstraint wideIs64bit \
	[expr {(wide(0x80000000) > 0) && (wide(0x8000000000000000) < 0)}]
testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD"}]
testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD" && \
	[string length \U010000] == 1}]

test scan-1.1 {BuildCharSet, CharInSet} {
    list [scan foo {%[^o]} x] $x
} {1 f}
test scan-1.2 {BuildCharSet, CharInSet} {
    list [scan \]foo {%[]f]} x] $x
} {1 \]f}

Changes to jni/tcl/tests/split.test.

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
11
12
13
14
15
16
17

18
19
20
21
22
23
24







-







# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {"::tcltest" ni [namespace children]} {
    package require tcltest 2.5
    namespace import -force ::tcltest::*
}
testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD"}]

test split-1.1 {basic split commands} {
    split "a\n b\t\r c\n "
} {a {} b {} {} c {} {}}
test split-1.2 {basic split commands} {
    split "word 1xyzword 2zword 3" xyz
} {{word 1} {} {} {word 2} {word 3}}
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
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







-
+


-
+


-
+


-
+


-
+







} {{} ab cd {} ef {}}
test split-1.13 {basic split commands} {
    split "12,34,56," {,}
} {12 34 56 {}}
test split-1.14 {basic split commands} {
    split ",12,,,34,56," {,}
} {{} 12 {} {} 34 56 {}}
test split-1.15 {basic split commands} fullutf {
test split-1.15 {basic split commands} {
    split "a\U0001F4A9b" {}
} "a \U0001F4A9 b"
test split-1.16 {basic split commands} fullutf {
test split-1.16 {basic split commands} {
    split "\uD83D\uDE02Hello\uD83D\uDE02World\uD83D\uDE02" \U0001F602
} {{} Hello World {}}
test split-1.17 {basic split commands} fullutf {
test split-1.17 {basic split commands} {
    split "\U0001F602Hello\U0001F602World\U0001F602" \uD83D\uDE02
} {{} Hello World {}}
test split-1.18 {basic split commands} fullutf {
test split-1.18 {basic split commands} {
    split "\U0001F602\U0001F602\U0001F602" \uD83D\uDE02
} {{} {} {} {}}
test split-1.19 {basic split commands} fullutf {
test split-1.19 {basic split commands} {
    proc foo args {
        tailcall split {*}$args
    }
    foo "\U0001F602Hello\U0001F602World\U0001F602" \U0001F602
} {{} Hello World {}}

test split-2.1 {split errors} {

Changes to jni/tcl/tests/string.test.

21
22
23
24
25
26
27
28
29

30
31
32
33
34
35
36
37
21
22
23
24
25
26
27


28

29
30
31
32
33
34
35







-
-
+
-







catch [list package require -exact Tcltest [info patchlevel]]

# Some tests require the testobj command

testConstraint testobj [expr {[info commands testobj] != {}}]
testConstraint testindexobj [expr {[info commands testindexobj] != {}}]
testConstraint testevalex [expr {[info commands testevalex] != {}}]
testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD"}]
testConstraint tip389 [expr {[string length \U010000] == 2}]
testConstraint wtf8 [expr {[string length \U00010000] != 1}]
testConstraint ucs4 [expr {[string length \U010000] == 1 && [format %c 0x010000] ne "\uFFFD"}]
testConstraint testbytestring  [expr {[info commands testbytestring] != {}}]

# Used for constraining memory leak tests
testConstraint memory [llength [info commands memory]]

foreach noComp {0 1} {

1327
1328
1329
1330
1331
1332
1333
1334

1335
1336



1337
1338
1339
1340
1341
1342
1343
1325
1326
1327
1328
1329
1330
1331

1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344







-
+


+
+
+







    list $input_hex $rxBuffer_hex $rxCRC_hex
} {000341 000341 0341}
test string-12.22.$noComp {string range, shimmering binary/index} {
    set s 0000000001
    binary scan $s a* x
    run {string range $s $s end}
} 000000001
test string-12.23.$noComp {string range, surrogates, bug [11ae2be95dac9417]} tip389 {
test string-12.23.$noComp {string range, surrogates, bug [11ae2be95dac9417]} wtf8 {
    run {list [string range a\U00100000b 1 1] [string range a\U00100000b 2 2] [string range a\U00100000b 3 3]}
} [list \U00100000 {} b]
test string-12.23.$noComp {string range, surrogates, bug [11ae2be95dac9417]} !wtf8 {
    run {list [string range a\U00100000b 1 1] [string range a\U00100000b 2 2] [string range a\U00100000b 3 3]}
} [list \U00100000 b {}]

test string-13.1.$noComp {string repeat} {
    list [catch {run {string repeat}} msg] $msg
} {1 {wrong # args: should be "string repeat string count"}}
test string-13.2.$noComp {string repeat} {
    list [catch {run {string repeat abc 10 oops}} msg] $msg
} {1 {wrong # args: should be "string repeat string count"}}
1661
1662
1663
1664
1665
1666
1667
1668

1669
1670
1671


1672
1673
1674


1675
1676

1677
1678
1679
1680
1681
1682
1683
1662
1663
1664
1665
1666
1667
1668

1669
1670


1671
1672
1673


1674
1675
1676

1677
1678
1679
1680
1681
1682
1683
1684







-
+

-
-
+
+

-
-
+
+

-
+







} -result 6
test string-21.13.$noComp {string wordend, unicode} -body {
    run {string wordend "xyz\u2045de fg" 0}
} -result 3
test string-21.14.$noComp {string wordend, unicode} -body {
    run {string wordend "\uC700\uC700 abc" 8}
} -result 6
test string-21.15.$noComp {string trim, unicode} fullutf {
test string-21.15.$noComp {string trim, unicode} -body {
    run {string trim "\U1F602Hello world!\U1F602" \U1F602}
} "Hello world!"
test string-21.16.$noComp {string trimleft, unicode} fullutf {
} -result "Hello world!"
test string-21.16.$noComp {string trimleft, unicode} -body {
    run {string trimleft "\U1F602Hello world!\U1F602" \U1F602}
} "Hello world!\U1F602"
test string-21.17.$noComp {string trimright, unicode} fullutf {
} -result "Hello world!\U1F602"
test string-21.17.$noComp {string trimright, unicode} -body {
    run {string trimright "\U1F602Hello world!\U1F602" \U1F602}
} "\U1F602Hello world!"
} -result "\U1F602Hello world!"

test string-22.1.$noComp {string wordstart} -body {
    list [catch {run {string word a}} msg] $msg
} -result {1 {unknown or ambiguous subcommand "word": must be bytelength, cat, compare, equal, first, index, is, last, length, map, match, range, repeat, replace, reverse, tolower, totitle, toupper, trim, trimleft, trimright, wordend, or wordstart}}
test string-22.2.$noComp {string wordstart} -body {
    list [catch {run {string wordstart a}} msg] $msg
} -result {1 {wrong # args: should be "string wordstart string index"}}
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
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







-
+


-
+

-
-
+
+


-
+


-
+







    run {string wordstart "\uC700\uC700 abc" 8}
} -result 3
test string-22.14.$noComp {string wordstart, invalid UTF-8} -constraints testbytestring -body {
    # See Bug c61818e4c9
    set demo [testbytestring "abc def\xE0\xA9ghi"]
    run {string index $demo [string wordstart $demo 10]}
} -result g
test string-22.15.$noComp {string wordstart, unicode} tip389 {
test string-22.15.$noComp {string wordstart, unicode} wtf8 {
    run {string wordstart "ab\uD83D\uDCA3 cdef ghi" 12}
} 10
test string-22.16.$noComp {string wordstart, unicode} tip389 {
test string-22.16.$noComp {string wordstart, unicode} wtf8 {
    run {string wordstart "ab\uD83D\uDCA3 cdef ghi" 3}
} 3
test string-22.17.$noComp {string wordstart, unicode} fullutf {
} 2
test string-22.17.$noComp {string wordstart, unicode} !wtf8 {
    run {string wordstart "ab\uD83D\uDCA3 cdef ghi" 2}
} 2
test string-22.18.$noComp {string wordstart, unicode} tip389 {
test string-22.18.$noComp {string wordstart, unicode} wtf8 {
    run {string wordstart "ab\uD83D\uDCA3cdef" 4}
} 4
test string-22.19.$noComp {string wordstart, unicode} ucs4 {
test string-22.19.$noComp {string wordstart, unicode} !wtf8 {
    run {string wordstart "ab\uD83D\uDCA3cdef" 3}
} 3

test string-23.0.$noComp {string is boolean, Bug 1187123} testindexobj {
    set x 5
    catch {testindexobj $x foo bar soom}
    run {string is boolean $x}
1841
1842
1843
1844
1845
1846
1847
1848

1849
1850
1851

1852
1853
1854

1855
1856
1857
1858
1859
1860

1861
1862
1863
1864
1865
1866
1867
1842
1843
1844
1845
1846
1847
1848

1849
1850
1851

1852
1853
1854

1855
1856
1857
1858
1859
1860

1861
1862
1863
1864
1865
1866
1867
1868







-
+


-
+


-
+





-
+







    binary scan [run {string reverse [binary format H* 010203]}] H* x
    set x
} 030201
test string-24.15.$noComp {string reverse command - pure bytearray} {
    binary scan [run {tcl::string::reverse [binary format H* 010203]}] H* x
    set x
} 030201
test string-24.16.$noComp {string reverse command - fullutf} fullutf {
test string-24.16.$noComp {string reverse command - surrogates} {
    run {string reverse \u0444bulb\ud83d\ude02}
} \ud83d\ude02blub\u0444
test string-24.17.$noComp {string reverse command - fullutf} fullutf {
test string-24.17.$noComp {string reverse command - surrogates} {
    run {string reverse \ud83d\ude02hello\ud83d\ude02}
} \ud83d\ude02olleh\ud83d\ude02
test string-24.18.$noComp {string reverse command - ucs4} ucs4 {
test string-24.18.$noComp {string reverse command - surrogates} {
    set s \u0444bulb\ud83d\ude02
    # shim shimmery ...
    string index $s 0
    run {string reverse $s}
} \ud83d\ude02blub\u0444
test string-24.19.$noComp {string reverse command - ucs4} ucs4 {
test string-24.19.$noComp {string reverse command - surrogates} {
    set s \ud83d\ude02hello\ud83d\ude02
    # shim shimmery ...
    string index $s 0
    run {string reverse $s}
} \ud83d\ude02olleh\ud83d\ude02

test string-25.1.$noComp {string is list} {

Changes to jni/tcl/tests/stringComp.test.

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
22
23
24
25
26
27
28

29
30
31
32
33
34
35







-








::tcltest::loadTestedCommands
catch [list package require -exact Tcltest [info patchlevel]]

# Some tests require the testobj command

testConstraint testobj [expr {[info commands testobj] != {}}]
testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD"}]
testConstraint memory [llength [info commands memory]]
if {[testConstraint memory]} {
    proc getbytes {} {
	set lines [split [memory info] \n]
	return [lindex $lines 3 3]
    }
    proc leaktest {script {iterations 3}} {
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
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







-
-
-
-
-
-


-
+


-
+








-
+


-
+







    } 0 {}
    {unicode beyond U+FFFF} {
	string compare \uD83D\uDE00 \uD83D\uDE01\U0001F600\U0001F601
    } -1 {}
} {
    if {$tname eq ""} { continue }
    if {$tcode eq ""} { set tcode ok }
    # not nice but...
    if {$tname in {{unicode corner cases} {unicode beyond U+FFFF}}} {
        set cnstr fullutf
    } else {
        set cnstr {}
    }
    test stringComp-2.[incr i] "string compare, $tname" \
	-body [list eval $tbody] \
	-returnCodes $tcode -result $tresult -constraints $cnstr
	-returnCodes $tcode -result $tresult
    test stringComp-2.[incr i] "string compare bc, $tname" \
	-body "[list proc foo {} $tbody];foo" \
	-returnCodes $tcode -result $tresult -constraints $cnstr
	-returnCodes $tcode -result $tresult
    if {"error" ni $tcode} {
	set tresult [expr {!$tresult}]
    } else {
	set tresult [string map {compare equal} $tresult]
    }
    set tbody [string map {compare equal} $tbody]
    test stringComp-2.[incr i] "string equal, $tname" \
	-body [list eval $tbody] \
	-returnCodes $tcode -result $tresult -constraints $cnstr
	-returnCodes $tcode -result $tresult
    test stringComp-2.[incr i] "string equal bc, $tname" \
	-body "[list proc foo {} $tbody];foo" \
	-returnCodes $tcode -result $tresult -constraints $cnstr
	-returnCodes $tcode -result $tresult
}

# need a few extra tests short abbr cmd
test stringComp-3.1 {string compare, shortest method name} {
    proc foo {} {string co abcde ABCDE}
    foo
} 1
733
734
735
736
737
738
739
740

741
742
743

744
745
746

747
748
749

750
751
752
753
754
755
756
726
727
728
729
730
731
732

733
734
735

736
737
738

739
740
741

742
743
744
745
746
747
748
749







-
+


-
+


-
+


-
+







		[string match *a*l*\u0000*123 $longString] \
		[string match *a*l*\u0000*123* $longString] \
		[string match *a*l*\u0000*cba* $longString] \
		[string match *===* $longString]
    }
    foo
} {0 1 1 1 0 0}
test stringComp-11.55 {string match, unicode} fullutf {
test stringComp-11.55 {string match, unicode} {
    string match *\U1F602* Hello\U1F602World
} 1
test stringComp-11.56 {string match, unicode} fullutf {
test stringComp-11.56 {string match, unicode} {
    string match *\[\U1F602\]* Hello\U1F602World
} 1
test stringComp-11.57 {string match, unicode} fullutf {
test stringComp-11.57 {string match, unicode} {
    string match *\[\U1F602-\U1F604\]* Hello\U1F603World
} 1
test stringComp-11.58 {string match, unicode} fullutf {
test stringComp-11.58 {string match, unicode} {
    proc foo {p s} {
        return [string match $p $s]
    }
    list \
        [foo *\[\U1F602-\U1F604\]* Hello\uD83D\uDE03World] \
        [foo *\[\U1F602-\U1F604\]* Hello\uD83D\uDE05World] \
        [foo *\[\U1F602-\U1F604\]* Hello\uD83DWorld] \

Changes to jni/tcl/tests/utf.test.

14
15
16
17
18
19
20
21
22
23

24
25
26
27
28
29
30
14
15
16
17
18
19
20

21

22
23
24
25
26
27
28
29







-

-
+







}

namespace path ::tcl::mathop

::tcltest::loadTestedCommands
catch [list package require -exact Tcltest [info patchlevel]]

testConstraint ucs2 [expr {[format %c 0x010000] eq "\uFFFD"}]
testConstraint fullutf [expr {[format %c 0x010000] ne "\uFFFD"}]
testConstraint tip389 [expr {[string length [format %c 0x10000]] == 2}]
testConstraint wtf8 [expr {[string length \U010000] eq 2}]
testConstraint ucs4 [expr {[testConstraint fullutf]
		&& [string length [format %c 0x10000]] == 1}]

testConstraint testbytestring [llength [info commands testbytestring]]
testConstraint testfindfirst [llength [info commands testfindfirst]]
testConstraint testfindlast [llength [info commands testfindlast]]
testConstraint testnumutfchars [llength [info commands testnumutfchars]]
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
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







-
-
+
+

-
+

-
+












-
+








-
+
+
+
+



-
+
+
+
+


-
+
+
+
+







} 1
test utf-1.5 {Tcl_UniCharToUtf: overflowed Tcl_UniChar} testbytestring {
    expr {[format %c 0x110000] eq [testbytestring \xEF\xBF\xBD]}
} 1
test utf-1.6 {Tcl_UniCharToUtf: negative Tcl_UniChar} testbytestring {
    expr {[format %c -1] eq [testbytestring \xEF\xBF\xBD]}
} 1
test utf-1.7.0 {Tcl_UniCharToUtf: 4 byte sequences} {fullutf testbytestring} {
    expr {"\U014E4E" eq [testbytestring \xF0\x94\xB9\x8E]}
test utf-1.7.0 {Tcl_UniCharToUtf: 4 byte sequences} {testbytestring wtf8} {
    expr {"\U014E4E" eq [testbytestring "\xED\xA0\x93\xED\xB9\x8E"]}
} 1
test utf-1.7.1 {Tcl_UniCharToUtf: 4 byte sequences} {ucs2 testbytestring} {
test utf-1.7.1 {Tcl_UniCharToUtf: 4 byte sequences} {testbytestring ucs4} {
    expr {"\U014E4E" eq [testbytestring \xF0\x94\xB9\x8E]}
} 0
} 1
test utf-1.8 {Tcl_UniCharToUtf: 3 byte sequence, high surrogate} testbytestring {
    expr {"\uD842" eq [testbytestring \xED\xA1\x82]}
} 1
test utf-1.9 {Tcl_UniCharToUtf: 3 byte sequence, low surrogate} testbytestring {
    expr {"\uDC42" eq [testbytestring \xED\xB1\x82]}
} 1
test utf-1.10 {Tcl_UniCharToUtf: 3 byte sequence, high surrogate} testbytestring {
    expr {[format %c 0xD842] eq [testbytestring \xED\xA1\x82]}
} 1
test utf-1.11 {Tcl_UniCharToUtf: 3 byte sequence, low surrogate} testbytestring {
    expr {[format %c 0xDC42] eq [testbytestring \xED\xB1\x82]}
} 1
test utf-1.12 {Tcl_UniCharToUtf: 4 byte sequence, high/low surrogate} {testbytestring fullutf} {
test utf-1.12 {Tcl_UniCharToUtf: 4 byte sequence, high/low surrogate} {testbytestring ucs4} {
    expr {"\uD842\uDC42" eq [testbytestring \xF0\xA0\xA1\x82]}
} 1
test utf-1.13 {Tcl_UniCharToUtf: sequence of high surrogates} testbytestring {
    expr {"\uD842\uD842\uD842" eq [testbytestring \xED\xA1\x82\xED\xA1\x82\xED\xA1\x82]}
} 1
test utf-1.14 {Tcl_UniCharToUtf: sequence of low surrogates} testbytestring {
    expr {"\uDC42\uDC42\uDC42" eq [testbytestring \xED\xB1\x82\xED\xB1\x82\xED\xB1\x82]}
} 1
test utf-1.15 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring fullutf} {
test utf-1.15.0 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring wtf8} {
    expr {"\uDC42\uD842\uDC42" eq [testbytestring \xED\xB1\x82\xED\xA1\x82\xED\xB1\x82]}
} 1
test utf-1.15.1 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring ucs4} {
    expr {"\uDC42\uD842\uDC42" eq [testbytestring \xED\xB1\x82\xF0\xA0\xA1\x82]}
} 1
    string compare "\uDC42\uD842\uDC42" [testbytestring \xED\xB1\x82\xF0\xA0\xA1\x82]
test utf-1.16 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring fullutf} {
test utf-1.16.0 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring wtf8} {
    expr {"\uD842\uD842\uDC42" eq [testbytestring \xED\xA1\x82\xED\xA1\x82\xED\xB1\x82]}
} 1
test utf-1.16.1 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring ucs4} {
    expr {"\uD842\uD842\uDC42" eq [testbytestring \xED\xA1\x82\xF0\xA0\xA1\x82]}
} 1
test utf-1.17 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring fullutf} {
test utf-1.17.0 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring wtf8} {
    expr {"\uD842\uDC42\uD842" eq [testbytestring \xED\xA1\x82\xED\xB1\x82\xED\xA1\x82]}
} 1
test utf-1.17.1 {Tcl_UniCharToUtf: mix of surrogates} {testbytestring ucs4} {
    expr {"\uD842\uDC42\uD842" eq [testbytestring \xF0\xA0\xA1\x82\xED\xA1\x82]}
} 1

test utf-2.1 {Tcl_UtfToUniChar: low ascii} {
    string length "abc"
} 3
test utf-2.2 {Tcl_UtfToUniChar: naked trail bytes} testbytestring {
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
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







-
+





-
-
-
-
+





-
-
-



-
+



-
+







} 1
test utf-2.6 {Tcl_UtfToUniChar: lead (3-byte) followed by 1 trail} testbytestring {
    string length [testbytestring \xE2\xA2]
} 2
test utf-2.7 {Tcl_UtfToUniChar: lead (3-byte) followed by 2 trail} testbytestring {
    string length [testbytestring \xE4\xB9\x8E]
} 1
test utf-2.8.0 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail} {testbytestring ucs2} {
test utf-2.8.0 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail} {testbytestring wtf8} {
    string length [testbytestring \xF0\x90\x80\x80]
} 4
test utf-2.8.1 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail} {testbytestring ucs4} {
    string length [testbytestring \xF0\x90\x80\x80]
} 1
test utf-2.8.2 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail} {testbytestring tip389} {
    string length [testbytestring \xF0\x90\x80\x80]
} 2
test utf-2.9.0 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail} {testbytestring ucs2} {
test utf-2.9.0 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail} {testbytestring wtf8} {
    string length [testbytestring \xF4\x8F\xBF\xBF]
} 4
test utf-2.9.1 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail} ucs4 {
    string length \U10FFFF
} 1
test utf-2.9.2 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail} tip389 {
    string length \uDBFF\uDFFF
} 2
test utf-2.10 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail, underflow} testbytestring {
    string length [testbytestring \xF0\x8F\xBF\xBF]
} 4
test utf-2.11 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail, overflow} testbytestring {
test utf-2.11 {Tcl_UtfToUniChar: lead (4-byte) followed by 3 trail, overflow} {testbytestring wtf8} {
    # Would decode to U+110000 but that is outside the Unicode range.
    string length [testbytestring \xF4\x90\x80\x80]
} 4
test utf-2.12 {Tcl_UtfToUniChar: longer UTF sequences not supported} testbytestring {
test utf-2.12 {Tcl_UtfToUniChar: longer UTF sequences not supported} {testbytestring wtf8} {
    string length [testbytestring \xF8\xA2\xA2\xA2\xA2]
} 5

test utf-3.1 {Tcl_UtfCharComplete} {
} {}

test utf-4.1 {Tcl_NumUtfChars: zero length} testnumutfchars {
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
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







-
+
+


-
+
+


-
+
-
-
-
+

-
+






-
+





-
-
-
-
+





-
-
-







# Bug [2738427]: Tcl_NumUtfChars(...) no overflow check
test utf-4.9 {Tcl_NumUtfChars: #u20AC, calc len, incomplete} {testnumutfchars testbytestring} {
    testnumutfchars [testbytestring \xE2\x82\xAC] end-1
} 2
test utf-4.10 {Tcl_NumUtfChars: #u0000, calc len, overcomplete} {testnumutfchars testbytestring} {
    testnumutfchars [testbytestring \x00] end+1
} 2
test utf-4.11 {Tcl_NumUtfChars: 3 bytes of 4-byte UTF-8 characater} {testnumutfchars testbytestring} {
test utf-4.11 {Tcl_NumUtfChars: 3 bytes of 4-byte UTF-8 characater} {
    testnumutfchars testbytestring ucs4} {
    testnumutfchars [testbytestring \xF0\x9F\x92\xA9] end-1
} 3
test utf-4.12.0 {Tcl_NumUtfChars: #4-byte UTF-8 character} {testnumutfchars testbytestring ucs2} {
test utf-4.12.0 {Tcl_NumUtfChars: #4-byte UTF-8 character} {
    testnumutfchars testbytestring wtf8} {
    testnumutfchars [testbytestring \xF0\x9F\x92\xA9] end
} 4
test utf-4.12.1 {Tcl_NumUtfChars: #4-byte UTF-8 character} {testnumutfchars testbytestring ucs4} {
test utf-4.12.1 {Tcl_NumUtfChars: #4-byte UTF-8 character} {
    testnumutfchars [testbytestring \xF0\x9F\x92\xA9] end
} 1
test utf-4.12.2 {Tcl_NumUtfChars: #4-byte UTF-8 character} {testnumutfchars testbytestring tip389} {
    testnumutfchars testbytestring ucs4} {
    testnumutfchars [testbytestring \xF0\x9F\x92\xA9] end
} 2
} 1
test utf-4.13 {Tcl_NumUtfChars: high surrogates} {
    string length \uD842\uD842\uD842
} 3
test utf-4.14 {Tcl_NumUtfChars: low surrogates} {
    string length \uDE42\uDE42\uDE42
} 3
test utf-4.15.0 {Tcl_NumUtfChars: mixed surrogates} ucs2 {
test utf-4.15.0 {Tcl_NumUtfChars: mixed surrogates} wtf8 {
    string length \uDE42\uD842\uDE42
} 3
test utf-4.15.1 {Tcl_NumUtfChars: mixed surrogates} ucs4 {
    string length \uDE42\uD842\uDE42
} 2
test utf-4.15.2 {Tcl_NumUtfChars: mixed surrogates} tip389 {
    string length \uDE42\uD842\uDE42
} 3
test utf-4.16.0 {Tcl_NumUtfChars: mixed surrogates} ucs2 {
test utf-4.16.0 {Tcl_NumUtfChars: mixed surrogates} wtf8 {
    string length \uD842\uDE42\uD842
} 3
test utf-4.16.1 {Tcl_NumUtfChars: mixed surrogates} ucs4 {
    string length \uD842\uDE42\uD842
} 2
test utf-4.16.2 {Tcl_NumUtfChars: mixed surrogates} tip389 {
    string length \uD842\uDE42\uD842
} 3

test utf-5.1 {Tcl_UtfFindFirst} {testfindfirst testbytestring} {
    testfindfirst [testbytestring abcbc] 98
} bcbc
test utf-5.2 {Tcl_UtfFindLast} {testfindlast testbytestring} {
    testfindlast [testbytestring abcbc] 98
} bc
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
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







-
+


-
+














-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+







} 3
test utf-6.67 {Tcl_UtfNext} testutfnext {
    testutfnext \xE8\xA0\xA0\xF8
} 3
test utf-6.68 {Tcl_UtfNext} testutfnext {
    testutfnext \xF2\xA0\xA0G
} 1
test utf-6.69.0 {Tcl_UtfNext} {testutfnext ucs2} {
test utf-6.69.0 {Tcl_UtfNext} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0
} 1
test utf-6.69.1 {Tcl_UtfNext} {testutfnext fullutf} {
test utf-6.69.1 {Tcl_UtfNext} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0
} 4
test utf-6.70 {Tcl_UtfNext} testutfnext {
    testutfnext \xF2\xA0\xA0\xD0
} 1
test utf-6.71 {Tcl_UtfNext} testutfnext {
    testutfnext \xF2\xA0\xA0\xE8
} 1
test utf-6.72 {Tcl_UtfNext} testutfnext {
    testutfnext \xF2\xA0\xA0\xF2
} 1
test utf-6.73 {Tcl_UtfNext} testutfnext {
    testutfnext \xF2\xA0\xA0\xF8
} 1
test utf-6.74.0 {Tcl_UtfNext} {testutfnext ucs2} {
test utf-6.74.0 {Tcl_UtfNext} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0G
} 1
test utf-6.74.1 {Tcl_UtfNext} {testutfnext fullutf} {
test utf-6.74.1 {Tcl_UtfNext} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0G
} 4
test utf-6.75.0 {Tcl_UtfNext} {testutfnext ucs2} {
test utf-6.75.0 {Tcl_UtfNext} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0\xA0
} 1
test utf-6.75.1 {Tcl_UtfNext} {testutfnext fullutf} {
test utf-6.75.1 {Tcl_UtfNext} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0\xA0
} 4
test utf-6.76.0 {Tcl_UtfNext} {testutfnext ucs2} {
test utf-6.76.0 {Tcl_UtfNext} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0\xD0
} 1
test utf-6.76.1 {Tcl_UtfNext} {testutfnext fullutf} {
test utf-6.76.1 {Tcl_UtfNext} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0\xD0
} 4
test utf-6.77.0 {Tcl_UtfNext} {testutfnext ucs2} {
test utf-6.77.0 {Tcl_UtfNext} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0\xE8
} 1
test utf-6.77.1 {Tcl_UtfNext} {testutfnext fullutf} {
test utf-6.77.1 {Tcl_UtfNext} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0\xE8
} 4
test utf-6.78.0 {Tcl_UtfNext} {testutfnext ucs2} {
test utf-6.78.0 {Tcl_UtfNext} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0\xF2
} 1
test utf-6.78.1 {Tcl_UtfNext} {testutfnext fullutf} {
test utf-6.78.1 {Tcl_UtfNext} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0\xF2
} 4
test utf-6.79.0 {Tcl_UtfNext} {testutfnext ucs2} {
test utf-6.79.0 {Tcl_UtfNext} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0G\xF8
} 1
test utf-6.79.1 {Tcl_UtfNext} {testutfnext fullutf} {
test utf-6.79.1 {Tcl_UtfNext} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0G\xF8
} 4
test utf-6.80 {Tcl_UtfNext - overlong sequences} testutfnext {
    testutfnext \xC0\x80
} 2
test utf-6.81 {Tcl_UtfNext - overlong sequences} testutfnext {
    testutfnext \xC0\x81
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
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







-
+


-
+








-
+


-
+


-
+


-
+





-
+
-
-
-





-
+







} 1
test utf-6.85 {Tcl_UtfNext - overlong sequences} testutfnext {
    testutfnext \xE0\xA0\x80
} 3
test utf-6.86 {Tcl_UtfNext - overlong sequences} testutfnext {
    testutfnext \xF0\x80\x80\x80
} 1
test utf-6.87.0 {Tcl_UtfNext - overlong sequences} {testutfnext ucs2} {
test utf-6.87.0 {Tcl_UtfNext - overlong sequences} {testutfnext wtf8} {
    testutfnext \xF0\x90\x80\x80
} 1
test utf-6.87.1 {Tcl_UtfNext - overlong sequences} {testutfnext fullutf} {
test utf-6.87.1 {Tcl_UtfNext - overlong sequences} {testutfnext ucs4} {
    testutfnext \xF0\x90\x80\x80
} 4
test utf-6.88 {Tcl_UtfNext, pointing to 2th byte of 3-byte valid sequence} testutfnext {
    testutfnext \xA0\xA0
} 1
test utf-6.89 {Tcl_UtfNext, pointing to 2th byte of 3-byte invalid sequence} testutfnext {
    testutfnext \x80\x80
} 1
test utf-6.90.0 {Tcl_UtfNext, validity check [493dccc2de]} {testutfnext ucs2} {
test utf-6.90.0 {Tcl_UtfNext, validity check [493dccc2de]} {testutfnext wtf8} {
    testutfnext \xF4\x8F\xBF\xBF
} 1
test utf-6.90.1 {Tcl_UtfNext, validity check [493dccc2de]} {testutfnext fullutf} {
test utf-6.90.1 {Tcl_UtfNext, validity check [493dccc2de]} {testutfnext ucs4} {
    testutfnext \xF4\x8F\xBF\xBF
} 4
test utf-6.91.0 {Tcl_UtfNext, validity check [493dccc2de]} {testutfnext ucs2} {
test utf-6.91.0 {Tcl_UtfNext, validity check [493dccc2de]} {testutfnext wtf8} {
    testutfnext \xF4\x90\x80\x80
} 1
test utf-6.91.1 {Tcl_UtfNext, validity check [493dccc2de]} {testutfnext fullutf} {
test utf-6.91.1 {Tcl_UtfNext, validity check [493dccc2de]} {testutfnext ucs4} {
    testutfnext \xF4\x90\x80\x80
} 1
test utf-6.92 {Tcl_UtfNext, pointing to 2th byte of 4-byte valid sequence} testutfnext {
    testutfnext \xA0\xA0\xA0
} 1
test utf-6.93.0 {Tcl_UtfNext, pointing to 2th byte of 4-byte invalid sequence} {testutfnext ucs2} {
test utf-6.93 {Tcl_UtfNext, pointing to 2th byte of 4-byte invalid sequence} {testutfnext wtf8} {
    testutfnext \x80\x80\x80
} 1
test utf-6.93.1 {Tcl_UtfNext, pointing to 2th byte of 4-byte invalid sequence} {testutfnext fullutf} {
    testutfnext \x80\x80\x80
} 1
test utf-6.94 {Tcl_UtfNext, pointing to 2th byte of 5-byte invalid sequence} testutfnext {
    testutfnext \xA0\xA0\xA0\xA0
} 1
test utf-6.95 {Tcl_UtfNext, pointing to 2th byte of 5-byte invalid sequence} {testutfnext ucs2} {
test utf-6.95 {Tcl_UtfNext, pointing to 2th byte of 5-byte invalid sequence} {testutfnext wtf8} {
    testutfnext \x80\x80\x80\x80
} 1
test utf-6.96 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext G 0
} 0
test utf-6.97 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xA0 0
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
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







+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+

-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+


-
+





-
+


-
+


-
+


-
+







} 0
test utf-6.108 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xE8\xA0\xA0\xA0 2
} 0
test utf-6.109 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xE8\xA0\xA0\xA0 3
} 3
test utf-6.108.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0G 1
} 1
test utf-6.108.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0G 1
} 0
test utf-6.109.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0G 2
} 1
test utf-6.109.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0G 2
} 0
test utf-6.110.0 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
    testutfnext \xF2\xA0\xA0\xA0G 1
test utf-6.110.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0G 3
} 1
test utf-6.110.1 {Tcl_UtfNext, read limits} {testutfnext fullutf} {
test utf-6.110.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0G 1
} 0
test utf-6.111.0 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.111.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0G 2
} 1
test utf-6.111.1 {Tcl_UtfNext, read limits} {testutfnext fullutf} {
test utf-6.111.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0G 2
} 0
test utf-6.112.0 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.112.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0G 3
} 1
test utf-6.112.1 {Tcl_UtfNext, read limits} {testutfnext fullutf} {
test utf-6.112.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0G 3
} 0
test utf-6.113.0 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.113.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0G 4
} 1
test utf-6.113.1 {Tcl_UtfNext, read limits} {testutfnext fullutf} {
test utf-6.113.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0G 4
} 4
test utf-6.114.0 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.114.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0\xA0 1
} 1
test utf-6.114.1 {Tcl_UtfNext, read limits} {testutfnext fullutf} {
test utf-6.114.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0\xA0 1
} 0
test utf-6.115.0 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.115.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0\xA0 2
} 1
test utf-6.115.1 {Tcl_UtfNext, read limits} {testutfnext fullutf} {
test utf-6.115.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0\xA0 2
} 0
test utf-6.116.0 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.116.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0\xA0 3
} 1
test utf-6.116.1 {Tcl_UtfNext, read limits} {testutfnext fullutf} {
test utf-6.116.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0\xA0 3
} 0
test utf-6.117.0 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.117.0 {Tcl_UtfNext, read limits} {testutfnext wtf8} {
    testutfnext \xF2\xA0\xA0\xA0\xA0 4
} 1
test utf-6.117.1 {Tcl_UtfNext, read limits} {testutfnext fullutf} {
test utf-6.117.1 {Tcl_UtfNext, read limits} {testutfnext ucs4} {
    testutfnext \xF2\xA0\xA0\xA0\xA0 4
} 4
test utf-6.118 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xA0G 0
} 0
test utf-6.119 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.119 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xA0G 1
} 1
test utf-6.120 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.120 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xA0\xA0 1
} 1
test utf-6.121 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.121 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xA0\xA0G 2
} 1
test utf-6.122 {Tcl_UtfNext, read limits} {testutfnext ucs2} {
test utf-6.122 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xA0\xA0\xA0 2
} 1
test utf-6.123 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xA0\xA0\xA0G 3
} 1
test utf-6.124 {Tcl_UtfNext, read limits} testutfnext {
    testutfnext \xA0\xA0\xA0\xA0 3
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
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







-
+


-
+


-
+


-
+


-
+


-
+







} 2
test utf-7.9.1 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xF8\xA0\xA0\xA0 3
} 2
test utf-7.9.2 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xF8\xA0\xF8\xA0 3
} 2
test utf-7.10.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.10.0 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xF2\xA0
} 2
test utf-7.10.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.10.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xF2\xA0
} 1
test utf-7.10.2 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.10.1.0 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xF2\xA0\xA0\xA0 3
} 2
test utf-7.10.3 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.10.1.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xF2\xA0\xA0\xA0 3
} 1
test utf-7.10.4 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.10.2.0 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xF2\xA0\xF8\xA0 3
} 2
test utf-7.10.5 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.10.2.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xF2\xA0\xF8\xA0 3
} 1
test utf-7.11 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xE8\xA0
} 1
test utf-7.11.1 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xE8\xA0\xA0\xA0 3
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
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







-
+


-
+


-
+


-
+


-
+


-
+




















-
+


-
-
-
-
-
+
+
+
+
+

-
-
+
+

-
-
-
-
+
+
+
+


-
+


-
+


-
-
-
-
+


-
+


-
+


-
+


-
+


-
+


-
+







} 3
test utf-7.14.1 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xF8\xA0\xA0\xA0 4
} 3
test utf-7.14.2 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xF8\xA0\xA0\xF8 4
} 3
test utf-7.15.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.15.0 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xF2\xA0\xA0
} 3
test utf-7.15.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.15.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xF2\xA0\xA0
} 1
test utf-7.15.1.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.15.1.0 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xF2\xA0\xA0\xA0 4
} 3
test utf-7.15.1.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.15.1.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xF2\xA0\xA0\xA0 4
} 1
test utf-7.15.2.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.15.2.0 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xF2\xA0\xA0\xF8 4
} 3
test utf-7.15.2.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.15.2.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xF2\xA0\xA0\xF8 4
} 1
test utf-7.16 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xE8\xA0\xA0
} 1
test utf-7.16.1 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xE8\xA0\xA0\xA0 4
} 1
test utf-7.16.2 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xE8\xA0\xA0\xF8 4
} 1
test utf-7.17 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xD0\xA0\xA0
} 3
test utf-7.17.1 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xD0\xA0\xA0\xA0 4
} 3
test utf-7.17.2 {Tcl_UtfPrev} testutfprev {
    testutfprev A\xD0\xA0\xA0\xF8 4
} 3
test utf-7.18.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.18 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xA0\xA0\xA0
} 1
test utf-7.18.1 {Tcl_UtfPrev} {testutfprev fullutf} {
    testutfprev A\xA0\xA0\xA0
} 3
test utf-7.18.2 {Tcl_UtfPrev} {testutfprev ucs2} {
    testutfprev A\xA0\xA0\xA0\xA0 4
test utf-7.18.1 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xA0\xA0\xA0\xA0 4
} 1
test utf-7.18.2 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xA0\xA0\xA0\xF8 4
} 1
test utf-7.18.3 {Tcl_UtfPrev} {testutfprev fullutf} {
    testutfprev A\xA0\xA0\xA0\xA0 4
test utf-7.18.3 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xA0\xA0\xA0
} 3
test utf-7.18.4 {Tcl_UtfPrev} {testutfprev ucs2} {
    testutfprev A\xA0\xA0\xA0\xF8 4
} 1
test utf-7.18.5 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.18.4 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xA0\xA0\xA0\xA0 4
} 3
test utf-7.18.5 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xA0\xA0\xA0\xF8 4
} 3
test utf-7.19.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.19 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xF8\xA0\xA0\xA0
} 2
test utf-7.19.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.19.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xF8\xA0\xA0\xA0
} 4
test utf-7.20.0 {Tcl_UtfPrev} {testutfprev ucs2} {
    testutfprev A\xF4\xA0\xA0\xA0
} 2
test utf-7.20.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.20.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xF4\xA0\xA0\xA0
} 4
test utf-7.21.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.21 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xE8\xA0\xA0\xA0
} 2
test utf-7.21.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.21.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xE8\xA0\xA0\xA0
} 4
test utf-7.22.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.22 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xD0\xA0\xA0\xA0
} 2
test utf-7.22.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.22.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xD0\xA0\xA0\xA0
} 4
test utf-7.23.0 {Tcl_UtfPrev} {testutfprev ucs2} {
test utf-7.23 {Tcl_UtfPrev} {testutfprev wtf8} {
    testutfprev A\xA0\xA0\xA0\xA0
} 2
test utf-7.23.1 {Tcl_UtfPrev} {testutfprev fullutf} {
test utf-7.23.1 {Tcl_UtfPrev} {testutfprev ucs4} {
    testutfprev A\xA0\xA0\xA0\xA0
} 4
test utf-7.24 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xC0\x81
} 2
test utf-7.25 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xC0\x81 2
878
879
880
881
882
883
884
885

886
887
888

889
890
891
892
893
894
895
880
881
882
883
884
885
886

887
888
889

890
891
892
893
894
895
896
897







-
+


-
+







} 2
test utf-7.28 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xE0
} 1
test utf-7.28.1 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xE0\x80\x80 2
} 1
test utf-7.29.0 {Tcl_UtfPrev -- overlong sequence}  {testutfprev ucs2} {
test utf-7.29 {Tcl_UtfPrev -- overlong sequence}  {testutfprev wtf8} {
    testutfprev A\xF0\x80\x80\x80
} 2
test utf-7.29.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev fullutf} {
test utf-7.29.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev ucs4} {
    testutfprev A\xF0\x80\x80\x80
} 4
test utf-7.30 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xF0\x80\x80\x80 4
} 3
test utf-7.31 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xF0\x80\x80\x80 3
911
912
913
914
915
916
917
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

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
999

1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011

1012
1013
1014
1015
1016
1017
1018


1019
1020
1021
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
1051
1052
1053
1054
1055
1056
1057

1058
1059

1060
1061
1062
1063
1064
1065
1066

1067
1068

1069
1070
1071
1072
1073
1074
1075

1076
1077

1078
1079
1080
1081
1082
1083
1084
1085
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
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162






1163
1164
1165
1166
1167
1168
1169
913
914
915
916
917
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
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
999
1000

1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012

1013
1014
1015
1016
1017
1018


1019
1020
1021
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
1051
1052




1053
1054

1055
1056
1057
1058




1059
1060

1061
1062
1063
1064



1065
1066
1067
1068
1069
1070


1071
1072
1073





1074
1075
1076


1077
1078
1079






1080

1081
1082
1083


1084
1085




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







-
+


-
+


-
+


-
+


-
+


-
+














-
+


-
+











-
+


-
+


-
+


+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-


-
+


-
+


-
+


-
+


-
+


-
+











-
+





-
-
+
+





-
-
-



-
+





-
-
-
-
+





-
-
-
-
+





-
-
-
-
+

-
+



-
-
-
-
+

-
+



-
-
-
-
+

-
+



-
-
-






-
-
+
+

-
-
-
-
-
+
+

-
-
+
+

-
-
-
-
-
-

-
+


-
-
+
+
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

















-
+
+
+
+
+
+







} 1
test utf-7.37 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xE0\xA0\x80 3
} 1
test utf-7.38 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xE0\xA0\x80 2
} 1
test utf-7.39.0 {Tcl_UtfPrev -- overlong sequence}  {testutfprev ucs2} {
test utf-7.39.0 {Tcl_UtfPrev -- overlong sequence}  {testutfprev wtf8} {
    testutfprev A\xF0\x90\x80\x80
} 2
test utf-7.39.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev fullutf} {
test utf-7.39.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev ucs4} {
    testutfprev A\xF0\x90\x80\x80
} 1
test utf-7.40.0 {Tcl_UtfPrev -- overlong sequence}  {testutfprev ucs2} {
test utf-7.40.0 {Tcl_UtfPrev -- overlong sequence}  {testutfprev wtf8} {
    testutfprev A\xF0\x90\x80\x80 4
} 3
test utf-7.40.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev fullutf} {
test utf-7.40.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev ucs4} {
    testutfprev A\xF0\x90\x80\x80 4
} 1
test utf-7.41.0 {Tcl_UtfPrev -- overlong sequence}  {testutfprev ucs2} {
test utf-7.41.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev wtf8} {
    testutfprev A\xF0\x90\x80\x80 3
} 2
test utf-7.41.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev fullutf} {
test utf-7.41.1 {Tcl_UtfPrev -- overlong sequence}  {testutfprev ucs4} {
    testutfprev A\xF0\x90\x80\x80 3
} 1
test utf-7.42 {Tcl_UtfPrev -- overlong sequence}  testutfprev {
    testutfprev A\xF0\x90\x80\x80 2
} 1
test utf-7.43 {Tcl_UtfPrev -- no lead byte at start}  testutfprev {
    testutfprev \xA0
} 0
test utf-7.44 {Tcl_UtfPrev -- no lead byte at start}  testutfprev {
    testutfprev \xA0\xA0
} 1
test utf-7.45 {Tcl_UtfPrev -- no lead byte at start}  testutfprev {
    testutfprev \xA0\xA0\xA0
} 2
test utf-7.46.0 {Tcl_UtfPrev -- no lead byte at start}  {testutfprev ucs2} {
test utf-7.46.0 {Tcl_UtfPrev -- no lead byte at start}  {testutfprev wtf8} {
    testutfprev \xA0\xA0\xA0\xA0
} 1
test utf-7.46.1 {Tcl_UtfPrev -- no lead byte at start}  {testutfprev fullutf} {
test utf-7.46.1 {Tcl_UtfPrev -- no lead byte at start}  {testutfprev ucs4} {
    testutfprev \xA0\xA0\xA0\xA0
} 3
test utf-7.47 {Tcl_UtfPrev, pointing to 3th byte of 3-byte valid sequence} testutfprev {
    testutfprev \xE8\xA0
} 0
test utf-7.47.1 {Tcl_UtfPrev, pointing to 3th byte of 3-byte valid sequence} testutfprev {
    testutfprev \xE8\xA0\xA0 2
} 0
test utf-7.47.2 {Tcl_UtfPrev, pointing to 3th byte of 3-byte invalid sequence} testutfprev {
    testutfprev \xE8\xA0\x00 2
} 0
test utf-7.48.0 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs2} {
test utf-7.48.0 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev wtf8} {
    testutfprev A\xF4\x8F\xBF\xBF
} 2
test utf-7.48.1 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev fullutf} {
test utf-7.48.1 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs4} {
    testutfprev A\xF4\x8F\xBF\xBF
} 1
test utf-7.48.2 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs2} {
test utf-7.48.1.0 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev wtf8} {
    testutfprev A\xF4\x8F\xBF\xBF 4
} 3
test utf-7.48.1.1 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs4} {
    testutfprev A\xF4\x8F\xBF\xBF 4
} 1
test utf-7.48.2.0 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev wtf8} {
    testutfprev A\xF4\x8F\xBF\xBF 3
} 2
test utf-7.48.2.1 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs4} {
    testutfprev A\xF4\x8F\xBF\xBF 3
} 1
test utf-7.48.3 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev fullutf} {
test utf-7.48.3 {Tcl_UtfPrev, validity check [493dccc2de]} testutfprev {
    testutfprev A\xF4\x8F\xBF\xBF 4
} 1
test utf-7.48.4 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs2} {
    testutfprev A\xF4\x8F\xBF\xBF 3
} 2
test utf-7.48.5 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev fullutf} {
    testutfprev A\xF4\x8F\xBF\xBF 3
} 1
test utf-7.48.6 {Tcl_UtfPrev, validity check [493dccc2de]} testutfprev {
    testutfprev A\xF4\x8F\xBF\xBF 2
} 1
test utf-7.49.0 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs2} {
test utf-7.49.0 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev wtf8} {
    testutfprev A\xF4\x90\x80\x80
} 2
test utf-7.49.1 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev fullutf} {
test utf-7.49.1 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs4} {
    testutfprev A\xF4\x90\x80\x80
} 4
test utf-7.49.2 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs2} {
test utf-7.49.2 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev wtf8} {
    testutfprev A\xF4\x90\x80\x80 4
} 3
test utf-7.49.3 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev fullutf} {
test utf-7.49.3 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs4} {
    testutfprev A\xF4\x90\x80\x80 4
} 3
test utf-7.49.4 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs2} {
test utf-7.49.4 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev wtf8} {
    testutfprev A\xF4\x90\x80\x80 3
} 2
test utf-7.49.5 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev fullutf} {
test utf-7.49.5 {Tcl_UtfPrev, validity check [493dccc2de]} {testutfprev ucs4} {
    testutfprev A\xF4\x90\x80\x80 3
} 2
test utf-7.49.6 {Tcl_UtfPrev, validity check [493dccc2de]} testutfprev {
    testutfprev A\xF4\x90\x80\x80 2
} 1

test utf-8.1 {Tcl_UniCharAtIndex: index = 0} {
    string index abcd 0
} a
test utf-8.2 {Tcl_UniCharAtIndex: index = 0} {
    string index \u4E4E\u25A 0
} "\u4E4E"
} \u4E4E
test utf-8.3 {Tcl_UniCharAtIndex: index > 0} {
    string index abcd 2
} c
test utf-8.4 {Tcl_UniCharAtIndex: index > 0} {
    string index \u4E4E\u25A\xFF\u543 2
} "\uFF"
test utf-8.5.0 {Tcl_UniCharAtIndex: high surrogate} ucs2 {
} \uFF
test utf-8.5.0 {Tcl_UniCharAtIndex: high surrogate} wtf8 {
    string index \uD842 0
} "\uD842"
test utf-8.5.1 {Tcl_UniCharAtIndex: high surrogate} ucs4 {
    string index \uD842 0
} "\uD842"
test utf-8.5.2 {Tcl_UniCharAtIndex: high surrogate} tip389 {
    string index \uD842 0
} "\uD842"
test utf-8.6 {Tcl_UniCharAtIndex: low surrogate} {
    string index \uDC42 0
} "\uDC42"
test utf-8.7.0 {Tcl_UniCharAtIndex: Emoji} ucs2 {
test utf-8.7.0 {Tcl_UniCharAtIndex: Emoji} wtf8 {
    string index \uD83D\uDE00G 0
} "\uD83D"
test utf-8.7.1 {Tcl_UniCharAtIndex: Emoji} ucs4 {
    string index \uD83D\uDE00G 0
} "\U1F600"
test utf-8.7.2 {Tcl_UniCharAtIndex: Emoji} tip389 {
    string index \uD83D\uDE00G 0
} "\uD83D"
test utf-8.8.0 {Tcl_UniCharAtIndex: Emoji} ucs2 {
test utf-8.8.0 {Tcl_UniCharAtIndex: Emoji} wtf8 {
    string index \uD83D\uDE00G 1
} "\uDE00"
test utf-8.8.1 {Tcl_UniCharAtIndex: Emoji} ucs4 {
    string index \uD83D\uDE00G 1
} G
test utf-8.8.2 {Tcl_UniCharAtIndex: Emoji} tip389 {
    string index \uD83D\uDE00G 1
} "\uDE00"
test utf-8.9.0 {Tcl_UniCharAtIndex: Emoji} ucs2 {
test utf-8.9.0 {Tcl_UniCharAtIndex: Emoji} wtf8 {
    string index \uD83D\uDE00G 2
} G
test utf-8.9.1 {Tcl_UniCharAtIndex: Emoji} ucs4 {
    string index \uD83D\uDE00G 2
} {}
test utf-8.9.2 {Tcl_UniCharAtIndex: Emoji} tip389 {
    string index \uD83D\uDE00G 2
} G
test utf-8.10.0 {Tcl_UniCharAtIndex: Emoji} ucs2 {
test utf-8.10.0 {Tcl_UniCharAtIndex: Emoji} wtf8 {
    string index \U1F600G 0
} "\uFFFD"
} "\uD83D"
test utf-8.10.1 {Tcl_UniCharAtIndex: Emoji} ucs4 {
    string index \U1F600G 0
} "\U1F600"
test utf-8.10.2 {Tcl_UniCharAtIndex: Emoji} tip389 {
    string index \U1F600G 0
} "\uD83D"
test utf-8.11.0 {Tcl_UniCharAtIndex: Emoji} ucs2 {
test utf-8.11.0 {Tcl_UniCharAtIndex: Emoji} wtf8 {
    string index \U1F600G 1
} G
} "\uDE00"
test utf-8.11.1 {Tcl_UniCharAtIndex: Emoji} ucs4 {
    string index \U1F600G 1
} G
test utf-8.11.2 {Tcl_UniCharAtIndex: Emoji} tip389 {
    string index \U1F600G 1
} "\uDE00"
test utf-8.12.0 {Tcl_UniCharAtIndex: Emoji} ucs2 {
test utf-8.12.0 {Tcl_UniCharAtIndex: Emoji} wtf8 {
    string index \U1F600G 2
} {}
} G 
test utf-8.12.1 {Tcl_UniCharAtIndex: Emoji} ucs4 {
    string index \U1F600G 2
} {}
test utf-8.12.2 {Tcl_UniCharAtIndex: Emoji} tip389 {
    string index \U1F600G 2
} G

test utf-9.1 {Tcl_UtfAtIndex: index = 0} {
    string range abcd 0 2
} abc
test utf-9.2 {Tcl_UtfAtIndex: index > 0} {
    string range \u4E4E\u25A\xFF\u543klmnop 1 5
} "\u25A\xFF\u543kl"
test utf-9.3.0 {Tcl_UtfAtIndex: index = 0, Emoji} ucs2 {
} \u25A\xFF\u543kl
test utf-9.3.0 {Tcl_UtfAtIndex: index = 0, Emoji} wtf8 {
    string range \uD83D\uDE00G 0 0
} "\uD83D"
test utf-9.3.1 {Tcl_UtfAtIndex: index = 0, Emoji} ucs4 {
    string range \uD83D\uDE00G 0 0
} "\U1F600"
test utf-9.3.2 {Tcl_UtfAtIndex: index = 0, Emoji} tip389 {
} \U1F600
test utf-9.3.1 {Tcl_UtfAtIndex: index = 0, Emoji} ucs4 {
    string range \uD83D\uDE00G 0 0
} "\U1F600"
test utf-9.4.0 {Tcl_UtfAtIndex: index > 0, Emoji} ucs2 {
} \U1F600
test utf-9.4.0 {Tcl_UtfAtIndex: index > 0, Emoji} wtf8 {
    string range \uD83D\uDE00G 1 1
} "\uDE00"
test utf-9.4.1 {Tcl_UtfAtIndex: index > 0, Emoji} ucs4 {
    string range \uD83D\uDE00G 1 1
} "G"
test utf-9.4.2 {Tcl_UtfAtIndex: index > 0, Emoji} tip389 {
    string range \uD83D\uDE00G 1 1
} {}
test utf-9.5.0 {Tcl_UtfAtIndex: index > 0, Emoji} ucs2 {
test utf-9.4.1 {Tcl_UtfAtIndex: index > 0, Emoji} wtf8 {
    string range \uD83D\uDE00G 2 2
} G
test utf-9.5.1 {Tcl_UtfAtIndex: index > 0, Emoji} ucs4 {
    string range \uD83D\uDE00G 2 2
test utf-9.4.2 {Tcl_UtfAtIndex: index > 0, Emoji} ucs4 {
    string range \uD83D\uDE00G 1 1
} {}
test utf-9.5.2 {Tcl_UtfAtIndex: index > 0, Emoji} tip389 {
    string range \uD83D\uDE00G 2 2
} G
} G 
test utf-9.6.0 {Tcl_UtfAtIndex: index = 0, Emoji} ucs2 {
    string range \U1f600G 0 0
} "\uFFFD"
test utf-9.6.1 {Tcl_UtfAtIndex: index = 0, Emoji} ucs4 {
    string range \U1f600G 0 0
} "\U1F600"
test utf-9.6.2 {Tcl_UtfAtIndex: index = 0, Emoji} tip389 {
    string range \U1f600G 0 0
} "\U1F600"
test utf-9.7.0 {Tcl_UtfAtIndex: index > 0, Emoji} ucs2 {
    string range \U1f600G 1 1
} G
test utf-9.7.1 {Tcl_UtfAtIndex: index > 0, Emoji} ucs4 {
    string range \U1f600G 1 1
} "G"
test utf-9.7.2 {Tcl_UtfAtIndex: index > 0, Emoji} tip389 {
    string range \U1f600G 1 1
} {}
test utf-9.8.0 {Tcl_UtfAtIndex: index > 0, Emoji} ucs2 {
    string range \U1f600G 2 2
} {}
test utf-9.8.1 {Tcl_UtfAtIndex: index > 0, Emoji} ucs4 {
    string range \U1f600G 2 2
} {}
test utf-9.8.2 {Tcl_UtfAtIndex: index > 0, Emoji} tip389 {
    string range \U1f600G 2 2
} G

test utf-10.1 {Tcl_UtfBackslash: dst == NULL} {
    set x \n
} {
}
test utf-10.2 {Tcl_UtfBackslash: \u subst} testbytestring {
    expr {"\uA2" eq [testbytestring \xC2\xA2]}
} 1
test utf-10.3 {Tcl_UtfBackslash: longer \u subst} testbytestring {
    expr {"\u4E21" eq [testbytestring \xE4\xB8\xA1]}
} 1
test utf-10.4 {Tcl_UtfBackslash: stops at first non-hex} testbytestring {
    expr {"\u4E2k" eq "[testbytestring \xD3\xA2]k"}
} 1
test utf-10.5 {Tcl_UtfBackslash: stops after 4 hex chars} testbytestring {
    expr {"\u4E216" eq "[testbytestring \xE4\xB8\xA1]6"}
} 1
test utf-10.6 {Tcl_UtfBackslash: stops after 8 hex chars} {fullutf testbytestring} {
test utf-10.6.0 {Tcl_UtfBackslash: stops after 8 hex chars} {
    testbytestring wtf8} {
    expr {"\U0001E2165" eq "[testbytestring \xED\xA0\xB8\xED\xB8\x96]5"}
} 1
test utf-10.6.1 {Tcl_UtfBackslash: stops after 8 hex chars} {
    testbytestring ucs4} {
    expr {"\U0001E2165" eq "[testbytestring \xF0\x9E\x88\x96]5"}
} 1

proc bsCheck {char num {constraints {}}} {
    global errNum
    test utf-10.$errNum {backslash substitution} $constraints {
	scan $char %c value
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

1275
1276
1277

1278
1279
1280

1281
1282
1283
1284
1285
1286
1287
1186
1187
1188
1189
1190
1191
1192

1193
1194
1195

1196
1197
1198

1199



1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217

1218
1219
1220

1221
1222
1223

1224
1225
1226
1227
1228
1229
1230
1231







-
+


-
+


-
+
-
-
-


















-
+


-
+


-
+







} \u00C3AB
test utf-11.4 {Tcl_UtfToUpper} {
    string toupper \u01E3AB
} \u01E2AB
test utf-11.5 {Tcl_UtfToUpper Georgian (new in Unicode 11)} {
    string toupper \u10D0\u1C90
} \u1C90\u1C90
test utf-11.6 {Tcl_UtfToUpper beyond U+FFFF} fullutf {
test utf-11.6 {Tcl_UtfToUpper beyond U+FFFF} {
    string toupper \U10428
} \U10400
test utf-11.7 {Tcl_UtfToUpper beyond U+FFFF} fullutf {
test utf-11.7 {Tcl_UtfToUpper beyond U+FFFF} {
    string toupper \uD801\uDC28
} \uD801\uDC00
test utf-11.8.0 {Tcl_UtfToUpper low/high surrogate)} ucs2 {
test utf-11.8 {Tcl_UtfToUpper low/high surrogate)} {
    string toupper \uDC24\uD824
} \uDC24\uD824
test utf-11.8.1 {Tcl_UtfToUpper low/high surrogate)} fullutf {
    string toupper \uDC24\uD824
} \uDC24\uD824

test utf-12.1 {Tcl_UtfToLower} {
    string tolower {}
} {}
test utf-12.2 {Tcl_UtfToLower} {
    string tolower ABC
} abc
test utf-12.3 {Tcl_UtfToLower} {
    string tolower \u00C3AB
} \u00E3ab
test utf-12.4 {Tcl_UtfToLower} {
    string tolower \u01E2AB
} \u01E3ab
test utf-12.5 {Tcl_UtfToLower Georgian (new in Unicode 11)} {
    string tolower \u10D0\u1C90
} \u10D0\u10D0
test utf-12.6 {Tcl_UtfToLower low/high surrogate)} {
test utf-12.6 {Tcl_UtfToUpper low/high surrogate)} {
    string tolower \uDC24\uD824
} \uDC24\uD824
test utf-12.7 {Tcl_UtfToLower beyond U+FFFF} fullutf {
test utf-12.7 {Tcl_UtfToLower beyond U+FFFF} {
    string tolower \U10400
} \U10428
test utf-12.8 {Tcl_UtfToLower beyond U+FFFF} fullutf {
test utf-12.8 {Tcl_UtfToLower beyond U+FFFF} {
    string tolower \uD801\uDC00
} \uD801\uDC28

test utf-13.1 {Tcl_UtfToTitle} {
    string totitle {}
} {}
test utf-13.2 {Tcl_UtfToTitle} {
1298
1299
1300
1301
1302
1303
1304
1305

1306
1307
1308

1309
1310
1311
1312
1313
1314
1315
1242
1243
1244
1245
1246
1247
1248

1249
1250
1251

1252
1253
1254
1255
1256
1257
1258
1259







-
+


-
+







} \u10D0\u1C90
test utf-13.6 {Tcl_UtfToTitle Georgian (new in Unicode 11)} {
    string totitle \u1C90\u10D0
} \u1C90\u10D0
test utf-13.7 {Tcl_UtfToTitle low/high surrogate)} {
    string totitle \uDC24\uD824
} \uDC24\uD824
test utf-13.8 {Tcl_UtfToTitle beyond U+FFFF} fullutf {
test utf-13.8 {Tcl_UtfToTitle beyond U+FFFF} {
    string totitle \U10428
} \U10400
test utf-13.9 {Tcl_UtfToTitle beyond U+FFFF} fullutf {
test utf-13.9 {Tcl_UtfToTitle beyond U+FFFF} {
    string totitle \uD801\uDC28\uD801\uDC00
} \uD801\uDC00\uD801\uDC28

test utf-14.1 {Tcl_UtfNcasecmp} {
    string compare -nocase a b
} -1
test utf-14.2 {Tcl_UtfNcasecmp} {
1361
1362
1363
1364
1365
1366
1367
1368

1369
1370
1371
1372
1373
1374

1375
1376
1377
1378
1379
1380
1381
1305
1306
1307
1308
1309
1310
1311

1312
1313
1314
1315
1316
1317

1318
1319
1320
1321
1322
1323
1324
1325







-
+





-
+








test utf-19.1 {TclUniCharLen} -body {
    list [regexp \\d abc456def foo] $foo
} -cleanup {
    unset -nocomplain foo
} -result {1 4}

test utf-20.1 {TclUniCharNcmp} {tip389 knownBug} {
test utf-20.1 {TclUniCharNcmp} {wtf8 knownBug} {
    string compare [string range [format %c 0xFFFF] 0 0] [string range [format %c 0x10000] 0 0]
} -1
test utf-20.2 {TclUniCharNcmp} ucs4 {
    string compare [string range [format %c 0xFFFF] 0 0] [string range [format %c 0x10000] 0 0]
} -1
test utf-20.3 {[4c591fa487] TclUniCharNcmp/TclUtfNcmp} {ucs2 knownBug} {
test utf-20.3 {[4c591fa487] TclUniCharNcmp/TclUtfNcmp} {wtf8 knownBug} {
    set one [format %c 0xFFFF]
    set two [format %c 0x10000]
    set first [string compare $one $two]
    string range $one 0 0
    string range $two 0 0
    set second [string compare $one $two]
    expr {($first == $second) ? "agree" : "disagree"}
1441
1442
1443
1444
1445
1446
1447
1448

1449
1450
1451
1452
1453
1454
1455
1385
1386
1387
1388
1389
1390
1391

1392
1393
1394
1395
1396
1397
1398
1399







-
+








test utf-22.1 {Tcl_UniCharIsWordChar} {
    string wordend "xyz123_bar fg" 0
} 10
test utf-22.2 {Tcl_UniCharIsWordChar} {
    string wordend "x\u5080z123_bar\u203C fg" 0
} 10
test utf-22.3 {Tcl_UniCharIsWordChar} fullutf {
test utf-22.3 {Tcl_UniCharIsWordChar} {
    string wordend "x\u5080\uD83D\uDCA3z123_bar\uD83D\uDCA3 fg" 0
} 2

test utf-23.1 {Tcl_UniCharIsAlpha} {
    # this returns 1 with Unicode 7 compliance
    string is alpha \u021F\u0220\u037F\u052F
} 1
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508




1509
1510
1511
1512
1513
1514
1515
1442
1443
1444
1445
1446
1447
1448




1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459







-
-
-
-
+
+
+
+







    incr count
}
variable count 1
UniCharCaseCmpTest < a b
UniCharCaseCmpTest > b a
UniCharCaseCmpTest > B a
UniCharCaseCmpTest > aBcB abca
UniCharCaseCmpTest < \uFFFF [format %c 0x10000] fullutf
UniCharCaseCmpTest < \uFFFF \U10000		fullutf
UniCharCaseCmpTest > [format %c 0x10000] \uFFFF	fullutf
UniCharCaseCmpTest > \U10000 \uFFFF		fullutf
UniCharCaseCmpTest < \uFFFF [format %c 0x10000]
UniCharCaseCmpTest < \uFFFF \U10000
UniCharCaseCmpTest > [format %c 0x10000] \uFFFF
UniCharCaseCmpTest > \U10000 \uFFFF





unset count
rename UniCharCaseCmpTest {}

Changes to jni/tcl/win/tclWinError.c.

173
174
175
176
177
178
179
180

181
182
183
184
185
186
187
173
174
175
176
177
178
179

180
181
182
183
184
185
186
187







-
+







    EINVAL,	/* 154 */
    EINVAL,	/* 155 */
    EINVAL,	/* 156 */
    EINVAL,	/* 157 */
    EACCES,	/* ERROR_NOT_LOCKED		158 */
    EINVAL,	/* 159 */
    EINVAL,	/* 160 */
    ENOENT,	/* ERROR_BAD_PATHNAME	        161 */
    ENOENT,	/* ERROR_BAD_PATHNAME		161 */
    EINVAL,	/* 162 */
    EINVAL,	/* 163 */
    EINVAL,	/* 164 */
    EINVAL,	/* 165 */
    EINVAL,	/* 166 */
    EACCES,	/* ERROR_LOCK_FAILED		167 */
    EINVAL,	/* 168 */
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
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







-
+

+


-
+
-


-
+
+


-
-
+
+
+


+







    const char *format, ...)
{
#define TCL_MAX_WARN_LEN 1024
    va_list argList;
    va_start(argList, format);

    if (IsDebuggerPresent()) {
	WCHAR msgString[TCL_MAX_WARN_LEN];
	WCHAR *msgString;
	char buf[TCL_MAX_WARN_LEN * TCL_UTF_MAX];
	Tcl_DString ds;

	vsnprintf(buf, sizeof(buf), format, argList);
	msgString[TCL_MAX_WARN_LEN-1] = L'\0';
	msgString = (WCHAR *) Tcl_WinUtfToTChar(buf, -1, &ds);
	MultiByteToWideChar(CP_UTF8, 0, buf, -1, msgString, TCL_MAX_WARN_LEN);

	/*
	 * Truncate MessageBox string if it is too long to not overflow the buffer.
	 * Truncate MessageBox string if it is too long to not
	 * overflow the buffer.
	 */

	if (msgString[TCL_MAX_WARN_LEN-1] != L'\0') {
	    memcpy(msgString + (TCL_MAX_WARN_LEN - 5), L" ...", 5 * sizeof(WCHAR));
	if (Tcl_DStringLength(&ds) >= TCL_MAX_WARN_LEN * sizeof(WCHAR)) {
	    memcpy(msgString + (TCL_MAX_WARN_LEN - 5), L" ...",
		5 * sizeof(WCHAR));
	}
	OutputDebugStringW(msgString);
	Tcl_DStringFree(&ds);
    } else {
	vfprintf(stderr, format, argList);
	fprintf(stderr, "\n");
	fflush(stderr);
    }
#   if defined(__GNUC__)
    __builtin_trap();

Changes to jni/tcl/win/tclWinFile.c.

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







-
+
+




-
+
-






-
+









+
+
+







TCL_NORETURN void
tclWinDebugPanic(
    const char *format, ...)
{
#define TCL_MAX_WARN_LEN 1024
    va_list argList;
    char buf[TCL_MAX_WARN_LEN * TCL_UTF_MAX];
    WCHAR msgString[TCL_MAX_WARN_LEN];
    WCHAR *msgString;
    Tcl_DString ds;

    va_start(argList, format);
    vsnprintf(buf, sizeof(buf), format, argList);

    msgString[TCL_MAX_WARN_LEN-1] = L'\0';
    msgString = (WCHAR *) Tcl_WinUtfToTChar(buf, -1, &ds);
    MultiByteToWideChar(CP_UTF8, 0, buf, -1, msgString, TCL_MAX_WARN_LEN);

    /*
     * Truncate MessageBox string if it is too long to not overflow the screen
     * and cause possible oversized window error.
     */

    if (msgString[TCL_MAX_WARN_LEN-1] != L'\0') {
    if (Tcl_DStringLength(&ds) >= TCL_MAX_WARN_LEN * sizeof(WCHAR)) {
	memcpy(msgString + (TCL_MAX_WARN_LEN - 5), L" ...", 5 * sizeof(WCHAR));
    }
    if (IsDebuggerPresent()) {
	OutputDebugStringW(msgString);
    } else {
	MessageBeep(MB_ICONEXCLAMATION);
	MessageBoxW(NULL, msgString, L"Fatal Error",
		MB_ICONSTOP | MB_OK | MB_TASKMODAL | MB_SETFOREGROUND);
    }

    Tcl_DStringFree(&ds);

#if defined(__GNUC__)
    __builtin_trap();
#elif defined(_WIN64)
    __debugbreak();
#elif defined(_MSC_VER) && defined (_M_IX86)
    _asm {int 3}
#else
879
880
881
882
883
884
885

886

887
888
889
890
891
892
893
894
895
896

897






898









899
900
901
902
903
904
905
882
883
884
885
886
887
888
889

890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908

909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924







+
-
+










+

+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+








void
TclpFindExecutable(
    const char *argv0)		/* If NULL, install PanicMessageBox, otherwise
				 * ignore. */
{
    WCHAR wName[MAX_PATH];
    int i;
    char name[MAX_PATH * TCL_UTF_MAX];
    char *p, name[(MAX_PATH + 1) * TCL_UTF_MAX];

    /*
     * Under Windows we ignore argv0, and return the path for the file used to
     * create this process. Only if it is NULL, install a new panic handler.
     */

    if (argv0 == NULL) {
	Tcl_SetPanicProc(tclWinDebugPanic);
    }

    wName[0] = L'\0';
    GetModuleFileNameW(NULL, wName, sizeof(wName)/sizeof(WCHAR));
    i = 0;
    p = name;
    while (wName[i] != L'\0') {
	if (p > name + (MAX_PATH * TCL_UTF_MAX)) {
	    break;
	}
    WideCharToMultiByte(CP_UTF8, 0, wName, -1, name, sizeof(name), NULL, NULL);

	/*
	 * We make WTF-8 here implicitely.
	 */

	p += Tcl_UniCharToUtf((Tcl_UniChar) wName[i], p);
	i++;
    }
    *p = '\0';
    TclWinNoBackslash(name);
    TclSetObjNameOfExecutable(Tcl_NewStringObj(name, -1), NULL);
}

/*
 *----------------------------------------------------------------------
 *
3038
3039
3040
3041
3042
3043
3044

3045
3046
3047
3048
3049
3050
3051
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071







+







    Tcl_Obj *pathPtr)
{
    WCHAR *nativePathPtr = NULL;
    const char *str;
    Tcl_Obj *validPathPtr;
    size_t len;
    WCHAR *wp;
    Tcl_DString ds;

    if (TclFSCwdIsNative()) {
	/*
	 * The cwd is native, which means we can use the translated path
	 * without worrying about normalization (this will also usually be
	 * shorter so the utf-to-external conversion will be somewhat faster).
	 */
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
3120




3121
3122
3123

3124
3125
3126
3127
3128


3129
3130
3131
3132
3133
3134
3135
3109
3110
3111
3112
3113
3114
3115
3116


3117
3118



3119
3120
3121


3122
3123


3124




3125


3126
3127
3128
3129
3130
3131



3132
3133
3134
3135
3136


3137




3138
3139
3140
3141
3142
3143
3144
3145
3146
3147







+
-
-
+
+
-
-
-
+
+

-
-
+
+
-
-
+
-
-
-
-
+
-
-






-
-
-
+
+
+
+

-
-
+
-
-
-
-

+
+







    }

    /*
     * For a reserved device, strip a possible postfix ':'
     */

    len = WinIsReserved(str);
    wp = (WCHAR *) Tcl_WinUtfToTChar(str, (len == 0) ? -1 : len, &ds);
    if (len == 0) {
	/*

    /*
	 * Let MultiByteToWideChar check for other invalid sequences, like
	 * 0xC0 0x80 (== overlong NUL). See bug [3118489]: NUL in filenames
	 */
     * Watch out for '\0' in native path, this is invalid.
     */

	len = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, -1, 0, 0);
	if (len==0) {
    len = wcslen(wp);
    if (len != Tcl_DStringLength(&ds) / sizeof(WCHAR)) {
	    if (GetLastError() == ERROR_INVALID_FLAGS) {
		/* Win NT/2000? */
	Tcl_DStringFree(&ds);
		len = MultiByteToWideChar(CP_UTF8, 0, str, -1, 0, 0);
	    }
	    if (len==0) {
		goto done;
	goto done;
	    }
	}
    }

    /*
     * Overallocate 6 chars, making some room for extended paths
     */

    wp = nativePathPtr = (WCHAR *) ckalloc((len + 6) * sizeof(WCHAR));
    if (nativePathPtr==0) {
      goto done;
    nativePathPtr = (WCHAR *) attemptckalloc((len + 6) * sizeof(WCHAR));
    if (nativePathPtr == NULL) {
	Tcl_DStringFree(&ds);
	goto done;
    }
    if ((MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, str, -1,
		nativePathPtr, len+2) == 0) &&
    memcpy(nativePathPtr, wp, len * sizeof(WCHAR));
	(GetLastError() == ERROR_INVALID_FLAGS)) {
	/* Win NT/2000? */
	MultiByteToWideChar(CP_UTF8, 0, str, -1, nativePathPtr, len+2);
    }
    nativePathPtr[len] = 0;
    wp = nativePathPtr;
    Tcl_DStringFree(&ds);

    /*
     * If path starts with "//?/" or "\\?\" (extended path), translate any
     * slashes to backslashes but leave the '?' intact
     */

    if ((str[0] == '\\' || str[0] == '/') && (str[1] == '\\' || str[1] == '/')

Changes to jni/tcl/win/tclWinInit.c.

496
497
498
499
500
501
502
503

504
505
506
507
508
509
510
496
497
498
499
500
501
502

503
504
505
506
507
508
509
510







-
+







    const WCHAR *wSrc,
    char *dst)
{
    char *start;

    start = dst;
    while (*wSrc != '\0') {
#if TCL_UTF_MAX >= 4
#if TCL_UTF_MAX > 3
	Tcl_UniChar ch = *wSrc;

	if ((ch & 0xF800) == 0xD800) {
	    if (ch & 0x0400) {
		/* Low surrogate */
		dst[3] = (char) ((ch | 0x80) & 0xBF);
		dst[2] |= (char) (((ch >> 6) | 0x80) & 0x8F);

Changes to jni/tcl/win/tclWinSerial.c.

1807
1808
1809
1810
1811
1812
1813
1814

1815
1816
1817
1818
1819
1820
1821
1822

1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1807
1808
1809
1810
1811
1812
1813

1814



1815
1816
1817
1818

1819



1820
1821
1822
1823
1824
1825
1826







-
+
-
-
-




-
+
-
-
-








	dcb.XonChar = argv[0][0];
	dcb.XoffChar = argv[1][0];
	if (argv[0][0] & 0x80 || argv[1][0] & 0x80) {
	    Tcl_UniChar character;
	    int charLen;

	    charLen = Tcl_UtfToUniChar(argv[0], &character);
	    charLen = TclUtfToUniChar(argv[0], &character);
#if TCL_UTF_MAX == 4
	    /* Character > 0xFFFF: charLen is 0, next test fails. */
#endif
	    if ((character & ~0xFF) || argv[0][charLen]) {
		goto badXchar;
	    }
	    dcb.XonChar = (char) character;
	    charLen = Tcl_UtfToUniChar(argv[1], &character);
	    charLen = TclUtfToUniChar(argv[1], &character);
#if TCL_UTF_MAX == 4
	    /* Character > 0xFFFF: charLen is 0, next test fails. */
#endif
	    if ((character & ~0xFF) || argv[1][charLen]) {
		goto badXchar;
	    }
	    dcb.XoffChar = (char) character;
	}
	ckfree(argv);

Changes to jni/tdom/expat/xmltok.c.

166
167
168
169
170
171
172


173
174
175

176
177
178
179
180
181
182
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185







+
+



+








static int PTRFASTCALL
utf8_isInvalid2(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return UTF8_INVALID2((const unsigned char *)p);
}

int XmlAllowWTF = 0; /* when 1, don't treat 0xD800..0xDFFF as invalid */

static int PTRFASTCALL
utf8_isInvalid3(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  if (XmlAllowWTF) return 0;
  return UTF8_INVALID3((const unsigned char *)p);
}

static int PTRFASTCALL
utf8_isInvalid4(const ENCODING *enc, const char *p) {
  UNUSED_P(enc);
  return UTF8_INVALID4((const unsigned char *)p);

Changes to jni/tdom/generic/dom.c.

2301
2302
2303
2304
2305
2306
2307




2308
2309
2310
2311
2312
2313
2314
2315






2316
2317
2318
2319
2320
2321
2322
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332







+
+
+
+








+
+
+
+
+
+







        info.parser = parser;
        info.currentNode = doc->rootNode;
    }

    if (channel == NULL) {
        status = XML_Parse (parser, xml, length, 1);
    } else {
#if TCL_UTF_MAX == 3
	extern int XmlAllowWTF;
#endif

        Tcl_DStringInit (&dStr);
        if (Tcl_GetChannelOption (interp, channel, "-encoding", &dStr)
            != TCL_OK) {
            domFreeDocument (doc, NULL, NULL);
            *resultcode = TCL_ERROR;
            doc = NULL;
            goto cleanup;
        }
#if TCL_UTF_MAX == 3
	if (XmlAllowWTF) {
            bufObj = Tcl_NewObj();
            Tcl_SetObjLength (bufObj, 6144);
	} else
#endif
        if (strcmp (Tcl_DStringValue (&dStr), "utf-8")==0 ) {
            useBinary = 1;
        } else {
            bufObj = Tcl_NewObj();
            Tcl_SetObjLength (bufObj, 6144);
        }
        Tcl_DStringFree (&dStr);

Changes to jni/tdom/generic/tdominit.c.

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







-
+
-
-
+

-
-
-
-
-

-
-
-

-







+
+
+
+
+

+
+
+
+
+
+
+
+
+
+








#ifdef USE_TCL_STUBS
    if (Tcl_InitStubs(interp, "8.4", 0) == NULL) {
	return TCL_ERROR;
    }
#endif

    nrOfBytes = Tcl_UtfToUniChar ("\xF4\x82\xA2\xA2", uniChar);
    nrOfBytes = Tcl_UtfToUniChar("\xF4\x82\xA2\xA2", uniChar);
#if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION == 6)
# if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
    if (nrOfBytes != 4)
# elif TCL_UTF_MAX == 4
    if (nrOfBytes != 0)
# else
    if (nrOfBytes > 1)
# endif
#else
# if TCL_UTF_MAX > 3
    if (nrOfBytes != 4)
# else
    if (nrOfBytes > 1)
# endif
#endif
    {
        Tcl_SetResult(interp, "This interpreter and tDOM are build with"
                      " different Tcl_UniChar types and therefore not"
                      " binary compatible.", NULL);
        return TCL_ERROR;
    }
#if TCL_UTF_MAX == 3
    if (nrOfBytes == 1) {
	Tcl_DString ds;
	Tcl_Encoding enc;
	extern int XmlAllowWTF;

	/* Find out if WTF-8 is used in the tcl core. */
	enc = Tcl_GetEncoding(NULL, "utf-8");
	Tcl_ExternalToUtfDString(enc, "\xF4\x82\xA2\xA2", 4, &ds);
	if (Tcl_DStringLength(&ds) == 6) {
	    XmlAllowWTF = 1;
	}
	Tcl_DStringFree(&ds);
	Tcl_FreeEncoding(enc);
    }
#endif
    domModuleInitialize();

#ifdef TCL_THREADS
    tcldom_initialize();
#endif /* TCL_THREADS */

#ifndef TDOM_NO_UNKNOWN_CMD

Changes to jni/tdom/renxpat.h.

64
65
66
67
68
69
70

71
72
73
74
75
76
77
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78







+







#define XML_SetUnknownEncodingHandler tdom_XML_SetUnknownEncodingHandler
#define XML_SetUnparsedEntityDeclHandler tdom_XML_SetUnparsedEntityDeclHandler
#define XML_SetUserData tdom_XML_SetUserData
#define XML_SetXmlDeclHandler tdom_XML_SetXmlDeclHandler
#define XML_StopParser tdom_XML_StopParser
#define XML_UseForeignDTD tdom_XML_UseForeignDTD
#define XML_UseParserAsHandlerArg tdom_XML_UseParserAsHandlerArg
#define XmlAllowWTF tdom_XmlAllowWTF
#define XmlPrologStateInit tdom_XmlPrologStateInit
#define XmlPrologStateInitExternalEntity tdom_XmlPrologStateInitExternalEntity
#define XML_SimpleParseDocument tdom_XML_SimpleParseDocument
#define _INTERNAL_trim_to_complete_utf8_characters tdom__INTERNAL_trim_to_complete_utf8_characters
#define XmlGetUtf16InternalEncoding tdom_XmlGetUtf16InternalEncoding
#define XmlGetUtf16InternalEncodingNS tdom_XmlGetUtf16InternalEncodingNS
#define XmlGetUtf8InternalEncoding tdom_XmlGetUtf8InternalEncoding

Changes to jni/tkpath/sdl/tkSDLAGGPath.cpp.

634
635
636
637
638
639
640









641
642
643
644
645
646
647
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656







+
+
+
+
+
+
+
+
+







     * but we need a 1:1 mapping here.
     */
    char *up = utf8;
    while (up < utf8 + length) {
	Tcl_UniChar uch;
	up += Tcl_UtfToUniChar(up, &uch);
	chu = uch;
#if TCL_UTF_MAX == 3
	if (((uch & 0xFC00) == 0xD800) && (up < utf8 + length)) {
	    int n = Tcl_UtfToUniChar(up, &uch);
	    if ((uch &0xFC00) == 0xDC00) {
		up += n;
		chu = (((chu & 0x3FF) << 10) | (uch & 0x3FF)) + 0x10000;
	    }
	}
#endif
	Tcl_DStringAppend(&ds, (const char *) &chu, sizeof(unsigned));
    }
#else
    Tcl_Encoding enc = Tcl_GetEncoding(NULL, "ucs-4");
    Tcl_UtfToExternalDString(enc, utf8, -1, &ds);
    Tcl_FreeEncoding(enc);
#endif

Changes to jni/tkzinc/generic/Draw.c.

2008
2009
2010
2011
2012
2013
2014
2015

2016
2017
2018
2019
2020
2021

2022
2023
2024
2025
2026
2027
2028
2008
2009
2010
2011
2012
2013
2014

2015
2016
2017
2018
2019
2020

2021
2022
2023
2024
2025
2026
2027
2028







-
+





-
+







ZnRenderString(ZnTexFontInfo    *tfi,
               unsigned char    *string,
               unsigned int     len)
{
  unsigned int  clen;
  int           ch;
  Tcl_UniChar   c = 0;
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
  Tcl_UniChar   c2;
#endif

  while (len) {
    clen = Tcl_UtfToUniChar((char *) string, &c);
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
    if ((c & 0xFC00) == 0xD800) {
      ch = 0xFFFD;
      clen = Tcl_UtfToUniChar((char *) string, &c2);
      if ((c2 & 0xFC00) == 0xDC00) {
        ch = ((c & 0x3FF) << 10) + 0x10000 + (c2 & 0x3FF);
      }
    }

Changes to jni/vu/generic/tkCombobox.c.

2004
2005
2006
2007
2008
2009
2010



2011

2012
2013
2014
2015
2016
2017
2018
2019
2020




2021
2022














2023
2024
2025
2026
2027
2028
2029
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013

2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027


2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048







+
+
+
-
+









+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    /*
     * If we're displaying a special character instead of the value of
     * the combo, recompute the displayString.
     */

    if (comboPtr->showChar != NULL) {
	Tcl_UniChar ch;
#if TCL_UTF_MAX == 3
	Tcl_UniChar ch2 = 0;
#endif
	char buf[TCL_UTF_MAX];
	char buf[6];
	int size;

	/*
	 * Normalize the special character so we can safely duplicate it
	 * in the display string.  If we didn't do this, then two malformed
	 * characters might end up looking like one valid UTF character in
	 * the resulting string.
	 */

	size = Tcl_UtfToUniChar(comboPtr->showChar, &ch);
#if TCL_UTF_MAX == 3
	if ((ch & 0xFC00) == 0xD800) {
	    if (comboPtr->showChar[size] != '\0') {
	Tcl_UtfToUniChar(comboPtr->showChar, &ch);
	size = Tcl_UniCharToUtf(ch, buf);
		Tcl_UtfToUniChar(comboPtr->showChar + size, &ch2);
	    }
	    size = 0;
	    if ((ch2 & 0xFC00) == 0xDC00) {
		size = Tcl_UniCharToUtf(ch, buf);
		ch = ch2;
	    }
	} else {
	    size = 0;
	}
	size += Tcl_UniCharToUtf(ch, buf + size);
#else
	size = Tcl_UniCharToUtf(ch, buf);
#endif

	comboPtr->numDisplayBytes = comboPtr->numChars * size;
	comboPtr->displayString =
		(char *) ckalloc((unsigned) (comboPtr->numDisplayBytes + 1));

	p = comboPtr->displayString;
	for (i = comboPtr->numChars; --i >= 0; ) {

Changes to undroid/build-undroidwish-freebsd.sh.

70
71
72
73
74
75
76
77
78


79
80
81
82
83
84
85
70
71
72
73
74
75
76


77
78
79
80
81
82
83
84
85







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="cc -DTCL_UTF_MAX=6"
CXX="c++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CC="cc -DTCL_UTF_MAX=3"
CXX="c++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk v4l2"
1351
1352
1353
1354
1355
1356
1357
1358

1359
1360
1361
1362
1363
1364
1365
1351
1352
1353
1354
1355
1356
1357

1358
1359
1360
1361
1362
1363
1364
1365







-
+







echo -n "build ffidl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd ffidl
  test -e build-stamp && echo >&3 "already done" && exit 0
  mkdir -p lib-src && rm -rf lib-src/libffi && cp -rp ../libffi lib-src
  CC="gcc10 -DTCL_UTF_MAX=6" DESTDIR=${HERE} ./configure --prefix=${PFX} \
  CC="gcc10 -DTCL_UTF_MAX=3" DESTDIR=${HERE} ./configure --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads \
    --enable-libffi || exit 1
  MAKE=gmake gmake || exit 1
  MAKE=gmake gmake install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

Changes to undroid/build-undroidwish-generic.sh.

70
71
72
73
74
75
76
77
78


79
80
81
82
83
84
85
70
71
72
73
74
75
76


77
78
79
80
81
82
83
84
85







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -DTCL_UTF_MAX=6"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CC="gcc -DTCL_UTF_MAX=3"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk v4l2"

Changes to undroid/build-undroidwish-haiku.sh.

81
82
83
84
85
86
87
88
89


90
91
92


93
94
95
96
97
98
99
81
82
83
84
85
86
87


88
89
90


91
92
93
94
95
96
97
98
99







-
-
+
+

-
-
+
+







fi
if type g++-$(uname -p) >/dev/null 2>&1 ; then
  CXX=g++-$(uname -p)
else
  CXX=g++
fi
if type ccache >/dev/null 2>&1 ; then
  CC="ccache $CC -DTCL_UTF_MAX=6"
  CXX="ccache $CXX -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
  CC="ccache $CC -DTCL_UTF_MAX=3"
  CXX="ccache $CXX -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
else
  CC="$CC -DTCL_UTF_MAX=6"
  CXX="$CXX -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
  CC="$CC -DTCL_UTF_MAX=3"
  CXX="$CXX -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
fi
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"

Changes to undroid/build-undroidwish-illumos.sh.

79
80
81
82
83
84
85
86
87


88
89
90
91
92
93
94
79
80
81
82
83
84
85


86
87
88
89
90
91
92
93
94







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -DTCL_UTF_MAX=6"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CC="gcc -DTCL_UTF_MAX=3"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint tcl-augeas"

Changes to undroid/build-undroidwish-kmsdrm.sh.

74
75
76
77
78
79
80
81
82


83
84
85
86
87
88
89
74
75
76
77
78
79
80


81
82
83
84
85
86
87
88
89







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -DTCL_UTF_MAX=6"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CC="gcc -DTCL_UTF_MAX=3"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk v4l2"

Changes to undroid/build-undroidwish-linux32.sh.

75
76
77
78
79
80
81
82
83


84
85
86
87
88
89
90
75
76
77
78
79
80
81


82
83
84
85
86
87
88
89
90







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=6"
CXX="g++ -m32 -march=i586 -mtune=generic -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CC="gcc -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=3"
CXX="g++ -m32 -march=i586 -mtune=generic -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk v4l2"

Changes to undroid/build-undroidwish-linux64.sh.

75
76
77
78
79
80
81
82
83


84
85
86
87
88
89
90
75
76
77
78
79
80
81


82
83
84
85
86
87
88
89
90







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -m64 -DTCL_UTF_MAX=6"
CXX="g++ -m64 -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CC="gcc -m64 -DTCL_UTF_MAX=3"
CXX="g++ -m64 -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk v4l2"

Changes to undroid/build-undroidwish-macosx.sh.

75
76
77
78
79
80
81
82
83


84
85
86
87
88
89
90
75
76
77
78
79
80
81


82
83
84
85
86
87
88
89
90







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP="strip -S -x"
AR=ar
RANLIB=ranlib
CC="cc -DTCL_UTF_MAX=6 -mmacosx-version-min=10.10"
CXX="c++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6 -mmacosx-version-min=10.10"
CC="cc -DTCL_UTF_MAX=3 -mmacosx-version-min=10.10"
CXX="c++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3 -mmacosx-version-min=10.10"
NM=nm
# RPATH for binaries
ADD_RPATH="/Applications/VLC.app/Contents/MacOS/lib"
export STRIP AR RANLIB CC CXX NM ADD_RPATH

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"

Changes to undroid/build-undroidwish-openbsd.sh.

70
71
72
73
74
75
76
77
78


79
80
81
82
83
84
85
70
71
72
73
74
75
76


77
78
79
80
81
82
83
84
85







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="cc -DTCL_UTF_MAX=6"
CXX="c++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CC="cc -DTCL_UTF_MAX=3"
CXX="c++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk v4l2"

Changes to undroid/build-undroidwish-termux.sh.

74
75
76
77
78
79
80
81
82
83




84
85
86
87
88
89
90
74
75
76
77
78
79
80



81
82
83
84
85
86
87
88
89
90
91







-
-
-
+
+
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -DTCL_UTF_MAX=6"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CVCXX="g++ -DTCL_UTF_MAX=6"
CC="gcc -DTCL_UTF_MAX=3"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
CVCXX="g++ -DTCL_UTF_MAX=3"
CVCXX="g++ -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX CVCXX NM

SUBDIRS="tcl libressl zlib tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk"

Changes to undroid/build-undroidwish-wayland.sh.

74
75
76
77
78
79
80
81
82


83
84
85
86
87
88
89
74
75
76
77
78
79
80


81
82
83
84
85
86
87
88
89







-
-
+
+







echo "undroidwish - AndroWish sans the borg, a project just for pun."
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -DTCL_UTF_MAX=6"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6"
CC="gcc -DTCL_UTF_MAX=3"
CXX="g++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"
SUBDIRS="${SUBDIRS} Memchan TclCurl freetype SDL2 sdl2tk blt jpeg-turbo"
SUBDIRS="${SUBDIRS} 3dcanvas tkimg trf tktable tktreectrl tkpath itk v4l2"

Changes to undroid/build-undroidwish-win32.sh.

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







-
-
-
+
+
+












-
-
-
+
+
+







  # SSE like VIA C3 
  echo using toolchain from /opt/mingw64/bin
  PATH="/opt/mingw64/bin:$PATH"
  STRIP="x86_64-w64-mingw32-strip"
  OBJCOPY="x86_64-w64-mingw32-objcopy"
  AR="x86_64-w64-mingw32-ar"
  RANLIB="x86_64-w64-mingw32-ranlib"
  CC="x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 -DTCL_UTF_MAX=6"
  CC_OLD="x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 -D_WIN32_WINNT=0x0400 -DTCL_UTF_MAX=6"
  CXX="x86_64-w64-mingw32-g++ -m32 -march=i386 -mtune=i386 -fno-exceptions -DTCL_UTF_MAX=6"
  CC="x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 -DTCL_UTF_MAX=3"
  CC_OLD="x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 -D_WIN32_WINNT=0x0400 -DTCL_UTF_MAX=3"
  CXX="x86_64-w64-mingw32-g++ -m32 -march=i386 -mtune=i386 -fno-exceptions -DTCL_UTF_MAX=3"
  RC="x86_64-w64-mingw32-windres -F pe-i386"
  NM="x86_64-w64-mingw32-nm"
  export STRIP OBJCOPY AR RANLIB CC CC_OLD CXX RC NM
else
  # would like to use -march=i386 -mtune=i386, too, but then gcc-4.8
  # cannot link due to missing atomic support for this CPU, thus must
  # have Pentium at least
  echo using toolchain prefix i686-w64-mingw32
  STRIP="i686-w64-mingw32-strip"
  OBJCOPY="i686-w64-mingw32-objcopy"
  AR="i686-w64-mingw32-ar"
  RANLIB="i686-w64-mingw32-ranlib"
  CC="i686-w64-mingw32-gcc -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=6"
  CC_OLD="i686-w64-mingw32-gcc -m32 -march=i586 -mtune=generic -D_WIN32_WINNT=0x0400 -DTCL_UTF_MAX=6"
  CXX="i686-w64-mingw32-g++ -m32 -march=i586 -mtune=generic -fno-exceptions -DTCL_UTF_MAX=6"
  CC="i686-w64-mingw32-gcc -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=3"
  CC_OLD="i686-w64-mingw32-gcc -m32 -march=i586 -mtune=generic -D_WIN32_WINNT=0x0400 -DTCL_UTF_MAX=3"
  CXX="i686-w64-mingw32-g++ -m32 -march=i586 -mtune=generic -fno-exceptions -DTCL_UTF_MAX=3"
  RC="i686-w64-mingw32-windres -F pe-i386"
  NM="i686-w64-mingw32-nm"
  TWAPI_LDFLAGS="-L${AWDIR}/undroid/compat/win32/lib32"
  export STRIP OBJCOPY AR RANLIB CC CC_OLD CXX RC NM TWAPI_LDFLAGS
fi

SUBDIRS="tcl w32compat libressl zlib curl tcludp tdom tclvfs tclkit trofs"

Changes to undroid/build-undroidwish-win64.sh.

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







-
-
+
+









-
-
+
+







if test -d /opt/mingw64/bin ; then
  echo using toolchain from /opt/mingw64/bin
  PATH="/opt/mingw64/bin:$PATH"
  STRIP="x86_64-w64-mingw32-strip"
  OBJCOPY="x86_64-w64-mingw32-objcopy"
  AR="x86_64-w64-mingw32-ar"
  RANLIB="x86_64-w64-mingw32-ranlib"
  CC="x86_64-w64-mingw32-gcc -D_WIN32_WINNT=0x0600 -DTCL_UTF_MAX=6"
  CXX="x86_64-w64-mingw32-g++ -D_WIN32_WINNT=0x0600 -DTCL_UTF_MAX=6"
  CC="x86_64-w64-mingw32-gcc -D_WIN32_WINNT=0x0600 -DTCL_UTF_MAX=3"
  CXX="x86_64-w64-mingw32-g++ -D_WIN32_WINNT=0x0600 -DTCL_UTF_MAX=3"
  RC="x86_64-w64-mingw32-windres"
  NM="x86_64-w64-mingw32-nm"
  export STRIP OBJCOPY AR RANLIB CC CXX RC NM
else
  echo using toolchain prefix x86_64-w64-mingw32
  STRIP="x86_64-w64-mingw32-strip"
  OBJCOPY="x86_64-w64-mingw32-objcopy"
  AR="x86_64-w64-mingw32-ar"
  RANLIB="x86_64-w64-mingw32-ranlib"
  CC="x86_64-w64-mingw32-gcc -DTCL_UTF_MAX=6"
  CXX="x86_64-w64-mingw32-g++ -DTCL_UTF_MAX=6"
  CC="x86_64-w64-mingw32-gcc -DTCL_UTF_MAX=3"
  CXX="x86_64-w64-mingw32-g++ -DTCL_UTF_MAX=3"
  RC="x86_64-w64-mingw32-windres"
  NM="x86_64-w64-mingw32-nm"
  export STRIP OBJCOPY AR RANLIB CC CXX RC NM
fi

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload"
SUBDIRS="${SUBDIRS} tls libwebsockets"

Changes to undroid/build-vanilla-freebsd.sh.

71
72
73
74
75
76
77
78
79


80
81
82
83
84
85
86
71
72
73
74
75
76
77


78
79
80
81
82
83
84
85
86







-
-
+
+







echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="cc -DTCL_UTF_MAX=6"
CXX="c++ -DTCL_UTF_MAX=6"
CC="cc -DTCL_UTF_MAX=3"
CXX="c++ -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl sdl2tk blt jpeg-turbo 3dcanvas"
SUBDIRS="${SUBDIRS} tkimg trf tktable tktreectrl tkpath itk v4l2 tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint"
1301
1302
1303
1304
1305
1306
1307
1308

1309
1310
1311
1312
1313
1314
1315
1301
1302
1303
1304
1305
1306
1307

1308
1309
1310
1311
1312
1313
1314
1315







-
+







echo -n "build ffidl ... "
(
  exec 3>&1
  exec >> build.log 2>&1
  cd ffidl
  test -e build-stamp && echo >&3 "already done" && exit 0
  mkdir -p lib-src && rm -rf lib-src/libffi && cp -rp ../libffi lib-src
  CC="gcc10 -DTCL_UTF_MAX=6" DESTDIR=${HERE} ./configure --prefix=${PFX} \
  CC="gcc10 -DTCL_UTF_MAX=3" DESTDIR=${HERE} ./configure --prefix=${PFX} \
    --with-tcl=${HERE}/tcl/unix --enable-threads \
    --enable-libffi || exit 1
  MAKE=gmake gmake || exit 1
  MAKE=gmake gmake install DESTDIR=${HERE} || exit 1
  touch build-stamp
  echo >&3 "done"
) || fail

Changes to undroid/build-vanilla-generic.sh.

71
72
73
74
75
76
77
78
79


80
81
82
83
84
85
86
71
72
73
74
75
76
77


78
79
80
81
82
83
84
85
86







-
-
+
+







echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -DTCL_UTF_MAX=6"
CXX="g++ -DTCL_UTF_MAX=6"
CC="gcc -DTCL_UTF_MAX=3"
CXX="g++ -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl sdl2tk blt jpeg-turbo 3dcanvas"
SUBDIRS="${SUBDIRS} tkimg trf tktable tktreectrl tkpath itk v4l2 tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint"

Changes to undroid/build-vanilla-illumos.sh.

80
81
82
83
84
85
86
87
88


89
90
91
92
93
94
95
80
81
82
83
84
85
86


87
88
89
90
91
92
93
94
95







-
-
+
+







echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -DTCL_UTF_MAX=6"
CXX="g++ -DTCL_UTF_MAX=6"
CC="gcc -DTCL_UTF_MAX=3"
CXX="g++ -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl sdl2tk blt jpeg-turbo 3dcanvas"
SUBDIRS="${SUBDIRS} tkimg trf tktable tktreectrl tkpath itk tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint"

Changes to undroid/build-vanilla-linux32.sh.

75
76
77
78
79
80
81
82
83


84
85
86
87
88
89
90
75
76
77
78
79
80
81


82
83
84
85
86
87
88
89
90







-
-
+
+







echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=6"
CXX="g++ -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=6"
CC="gcc -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=3"
CXX="g++ -m32 -march=i586 -mtune=generic -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl libxft sdl2tk blt jpeg-turbo 3dcanvas"
SUBDIRS="${SUBDIRS} tkimg trf tktable tktreectrl tkpath itk v4l2 tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint"

Changes to undroid/build-vanilla-linux64.sh.

75
76
77
78
79
80
81
82
83


84
85
86
87
88
89
90
75
76
77
78
79
80
81


82
83
84
85
86
87
88
89
90







-
-
+
+







echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -m64 -DTCL_UTF_MAX=6"
CXX="g++ -m64 -DTCL_UTF_MAX=6"
CC="gcc -m64 -DTCL_UTF_MAX=3"
CXX="g++ -m64 -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl libxft sdl2tk blt jpeg-turbo 3dcanvas"
SUBDIRS="${SUBDIRS} tkimg trf tktable tktreectrl tkpath itk v4l2 tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint"

Changes to undroid/build-vanilla-macosx.sh.

76
77
78
79
80
81
82
83
84
85



86
87
88
89
90
91
92
76
77
78
79
80
81
82



83
84
85
86
87
88
89
90
91
92







-
-
-
+
+
+







echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP="strip -S -x"
AR=ar
RANLIB=ranlib
CC="cc -DTCL_UTF_MAX=6 -mmacosx-version-min=10.10"
CXX="c++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=6 -mmacosx-version-min=10.10"
CXX_CV="c++ -DTCL_UTF_MAX=6 -mmacosx-version-min=10.10"
CC="cc -DTCL_UTF_MAX=3 -mmacosx-version-min=10.10"
CXX="c++ -fno-exceptions -fno-rtti -DTCL_UTF_MAX=3 -mmacosx-version-min=10.10"
CXX_CV="c++ -DTCL_UTF_MAX=3 -mmacosx-version-min=10.10"
NM=nm
# RPATH for binaries
ADD_RPATH="/Applications/VLC.app/Contents/MacOS/lib"
export STRIP AR RANLIB CC CXX CXX_CV NM ADD_RPATH

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl sdl2tk jpeg-turbo"

Changes to undroid/build-vanilla-openbsd.sh.

71
72
73
74
75
76
77
78
79


80
81
82
83
84
85
86
71
72
73
74
75
76
77


78
79
80
81
82
83
84
85
86







-
-
+
+







echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="cc -DTCL_UTF_MAX=6"
CXX="c++ -DTCL_UTF_MAX=6"
CC="cc -DTCL_UTF_MAX=3"
CXX="c++ -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl sdl2tk blt jpeg-turbo 3dcanvas"
SUBDIRS="${SUBDIRS} tkimg trf tktable tktreectrl tkpath itk v4l2 tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint"

Changes to undroid/build-vanilla-termux.sh.

71
72
73
74
75
76
77
78
79


80
81
82
83
84
85
86
71
72
73
74
75
76
77


78
79
80
81
82
83
84
85
86







-
-
+
+







echo "vanilla{tclsh,wish} - Build script for standard Tcl/Tk"
echo

# the toolchain
STRIP=strip
AR=ar
RANLIB=ranlib
CC="gcc -DTCL_UTF_MAX=6"
CXX="g++ -DTCL_UTF_MAX=6"
CC="gcc -DTCL_UTF_MAX=3"
CXX="g++ -DTCL_UTF_MAX=3"
NM=nm
export STRIP AR RANLIB CC CXX NM

SUBDIRS="tcl libressl zlib tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl sdl2tk blt jpeg-turbo 3dcanvas"
SUBDIRS="${SUBDIRS} tkimg trf tktable tktreectrl tkpath itk tkhtml"
SUBDIRS="${SUBDIRS} dbus-tcl dbus-intf tclx libdmtx ZBar zint"

Changes to undroid/build-vanilla-win32.sh.

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







-
-
-
+
+
+












-
-
-
+
+
+







  # SSE like VIA C3 
  echo using toolchain from /opt/mingw64/bin
  PATH="/opt/mingw64/bin:$PATH"
  STRIP="x86_64-w64-mingw32-strip"
  OBJCOPY="x86_64-w64-mingw32-objcopy"
  AR="x86_64-w64-mingw32-ar"
  RANLIB="x86_64-w64-mingw32-ranlib"
  CC="x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 -D_WIN32_WINNT=0x0601 -DTCL_UTF_MAX=6"
  CC_OLD="x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 -D_WIN32_WINNT=0x0400 -DTCL_UTF_MAX=6"
  CXX="x86_64-w64-mingw32-g++ -m32 -march=i386 -mtune=i386 -fno-exceptions -D_WIN32_WINNT=0x0601 -DTCL_UTF_MAX=6"
  CC="x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 -D_WIN32_WINNT=0x0601 -DTCL_UTF_MAX=3"
  CC_OLD="x86_64-w64-mingw32-gcc -m32 -march=i386 -mtune=i386 -D_WIN32_WINNT=0x0400 -DTCL_UTF_MAX=3"
  CXX="x86_64-w64-mingw32-g++ -m32 -march=i386 -mtune=i386 -fno-exceptions -D_WIN32_WINNT=0x0601 -DTCL_UTF_MAX=3"
  RC="x86_64-w64-mingw32-windres -F pe-i386"
  NM="x86_64-w64-mingw32-nm"
  export STRIP OBJCOPY AR RANLIB CC CC_OLD CXX RC NM
else
  # would like to use -march=i386 -mtune=i386, too, but then gcc-4.8
  # cannot link due to missing atomic support for this CPU, thus must
  # have Pentium at least
  echo using toolchain prefix i686-w64-mingw32
  STRIP="i686-w64-mingw32-strip"
  OBJCOPY="i686-w64-mingw32-objcopy"
  AR="i686-w64-mingw32-ar"
  RANLIB="i686-w64-mingw32-ranlib"
  CC="i686-w64-mingw32-gcc -m32 -march=i586 -mtune=generic -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -DTCL_UTF_MAX=6"
  CC_OLD="i686-w64-mingw32-gcc -m32 -march=i586 -mtune=generic -D_WIN32_WINNT=0x0400 -DWINVER=0x0400 -DTCL_UTF_MAX=6"
  CXX="i686-w64-mingw32-g++ -m32 -march=i586 -mtune=generic -fno-exceptions -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -DTCL_UTF_MAX=6"
  CC="i686-w64-mingw32-gcc -m32 -march=i586 -mtune=generic -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -DTCL_UTF_MAX=3"
  CC_OLD="i686-w64-mingw32-gcc -m32 -march=i586 -mtune=generic -D_WIN32_WINNT=0x0400 -DWINVER=0x0400 -DTCL_UTF_MAX=3"
  CXX="i686-w64-mingw32-g++ -m32 -march=i586 -mtune=generic -fno-exceptions -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -DTCL_UTF_MAX=3"
  RC="i686-w64-mingw32-windres -F pe-i386"
  NM="i686-w64-mingw32-nm"
  TWAPI_LDFLAGS="-L${AWDIR}/undroid/compat/win32/lib32"
  export STRIP OBJCOPY AR RANLIB CC CC_OLD CXX RC NM TWAPI_LDFLAGS
fi

SUBDIRS="tcl w32compat libressl zlib curl tcludp tdom tclvfs tclkit"

Changes to undroid/build-vanilla-win64.sh.

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







-
-
+
+









-
-
+
+







if test -d /opt/mingw64/bin ; then
  echo using toolchain from /opt/mingw64/bin
  PATH="/opt/mingw64/bin:$PATH"
  STRIP="x86_64-w64-mingw32-strip"
  OBJCOPY="x86_64-w64-mingw32-objcopy"
  AR="x86_64-w64-mingw32-ar"
  RANLIB="x86_64-w64-mingw32-ranlib"
  CC="x86_64-w64-mingw32-gcc -D_WIN32_WINNT=0x0601 -DTCL_UTF_MAX=6"
  CXX="x86_64-w64-mingw32-g++ -D_WIN32_WINNT=0x0601 -DTCL_UTF_MAX=6"
  CC="x86_64-w64-mingw32-gcc -D_WIN32_WINNT=0x0601 -DTCL_UTF_MAX=3"
  CXX="x86_64-w64-mingw32-g++ -D_WIN32_WINNT=0x0601 -DTCL_UTF_MAX=3"
  RC="x86_64-w64-mingw32-windres"
  NM="x86_64-w64-mingw32-nm"
  export STRIP OBJCOPY AR RANLIB CC CXX RC NM
else
  echo using toolchain prefix x86_64-w64-mingw32
  STRIP="x86_64-w64-mingw32-strip"
  OBJCOPY="x86_64-w64-mingw32-objcopy"
  AR="x86_64-w64-mingw32-ar"
  RANLIB="x86_64-w64-mingw32-ranlib"
  CC="x86_64-w64-mingw32-gcc -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -DTCL_UTF_MAX=6"
  CXX="x86_64-w64-mingw32-g++ -D_WIN32_WINT=0x0601 -DWINVER=0x0601 -DTCL_UTF_MAX=6"
  CC="x86_64-w64-mingw32-gcc -D_WIN32_WINNT=0x0601 -DWINVER=0x0601 -DTCL_UTF_MAX=3"
  CXX="x86_64-w64-mingw32-g++ -D_WIN32_WINT=0x0601 -DWINVER=0x0601 -DTCL_UTF_MAX=3"
  RC="x86_64-w64-mingw32-windres"
  NM="x86_64-w64-mingw32-nm"
  export STRIP OBJCOPY AR RANLIB CC CXX RC NM
fi

SUBDIRS="tcl libressl zlib curl tcludp tdom tclvfs tclkit trofs tbcload tls"
SUBDIRS="${SUBDIRS} Memchan TclCurl sdl2tk blt jpeg-turbo 3dcanvas"

Changes to undroid/ck8.x/ck.h.

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
42
43
44
45
46
47
48






49
50
51
52
53
54
55







-
-
-
-
-
-







#define CK_VERSION "8.2"
#define CK_MINOR_VERSION 2
#elif (TCL_MINOR_VERSION == 1)
#define CK_VERSION "8.1"
#define CK_MINOR_VERSION 1
#else
#error unsupported Tcl minor version
#endif

#ifdef TCL_UTF_MAX
#if TCL_UTF_MAX == 4
#error TCL_UTF_MAX=4 is unsupported
#endif
#endif

#ifndef RESOURCE_INCLUDED

#ifdef __STDC__
#include <stddef.h>
#endif

Changes to undroid/ck8.x/ckBind.c.

1415
1416
1417
1418
1419
1420
1421













1422
1423

1424
1425
1426
1427
1428
1429
1430
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444







+
+
+
+
+
+
+
+
+
+
+
+
+


+







				NULL, &numc, NULL);
			    numChars += numc;
			} else {
		    	    numStorage[numChars++] = eventPtr->key.keycode;
			}
		    }
		    if (eventPtr->key.is_uch) {
#if TCL_UTF_MAX == 3
			int uch = eventPtr->key.uch;

			numChars = 0;
			if (uch >= 0x10000) {
			    uch -= 0x10000;
			    numChars += Tcl_UniCharToUtf((uch>>10) | 0xd800,
							 numStorage);
			    uch = (uch&0x3ff) | 0xdc00;
			}
			numChars += Tcl_UniCharToUtf(uch,
						     numStorage + numChars);
#else
			numChars = Tcl_UniCharToUtf(eventPtr->key.uch,
						    numStorage);
#endif
		    }
		    numStorage[numChars] = '\0';
		    string = numStorage;
		} else if (eventPtr->type == CK_EV_BARCODE) {
		    string = CkGetBarcodeData(winPtr->mainPtr);
		    if (string == NULL) {
			numStorage[0] = '\0';

Changes to undroid/ck8.x/ckEntry.c.

564
565
566
567
568
569
570

















571
572
573
574
575
576
577
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







			charsPerPage = 1;
		    index += charsPerPage*count;
		    break;
		case CK_SCROLL_UNITS:
		    index += count;
		    break;
	    }
#if TCL_UTF_MAX == 3
	    /*
	     * Adjust to begin of surrogate pair.
	     */
	    if (index > 0) {
		char *cPtr = Tcl_UtfAtIndex(entryPtr->string, index);
		Tcl_UniChar ch;

		Tcl_UtfToUniChar(cPtr, &ch);
		if ((ch & 0xfc00) == 0xdc00) {
		    cPtr = Tcl_UtfPrev(cPtr, entryPtr->string);
		    Tcl_UtfToUniChar(cPtr, &ch);
		    if ((ch & 0xfc00) == 0xd800)
			index--;
		}
	    }
#endif
	}
	if (index >= entryPtr->numChars) {
	    index = entryPtr->numChars-1;
	}
	if (index < 0) {
	    index = 0;
	}
916
917
918
919
920
921
922
923

924
925

926

927

928
929
930
931
932
933
934
935
933
934
935
936
937
938
939

940
941

942
943
944

945

946
947
948
949
950
951
952







-
+

-
+

+
-
+
-







     */

    if (entryPtr->displayString != NULL) {
	ckfree(entryPtr->displayString);
	entryPtr->displayString = NULL;
    }
    if (entryPtr->showChar != NULL) {
	int ulen;
	int nc, ulen;

	entryPtr->displayString = (char *) ckalloc(entryPtr->numChars * 3 + 1);
	nc = entryPtr->numChars;
	ulen = Tcl_UtfNext(entryPtr->showChar) - entryPtr->showChar;
	entryPtr->displayString = (char *) ckalloc(nc * ulen + 1);
	for (p = entryPtr->displayString, i = entryPtr->numChars; i > 0;
	for (p = entryPtr->displayString, i = nc; i > 0; i--) {
		i--) {
	    memcpy(p, entryPtr->showChar, ulen);
	    p += ulen;
	}
	*p = 0;
	displayString = entryPtr->displayString;
    } else {
	displayString = entryPtr->string;
976
977
978
979
980
981
982



















983
984
985
986
987
988
989
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	    maxOffScreen += 1;
	}
	if (entryPtr->leftIndex > maxOffScreen) {
	    entryPtr->leftIndex = maxOffScreen;
	}
	leftIndex = Tcl_UtfAtIndex(displayString, entryPtr->leftIndex) -
	    displayString;
#if TCL_UTF_MAX == 3
	/*
	 * Adjust to begin of surrogate pair.
	 */
	if (entryPtr->leftIndex > 0) {
	    char *cPtr = displayString + leftIndex;
	    Tcl_UniChar ch;

	    Tcl_UtfToUniChar(cPtr, &ch);
	    if ((ch & 0xfc00) == 0xdc00) {
		cPtr = Tcl_UtfPrev(cPtr, displayString);
		Tcl_UtfToUniChar(cPtr, &ch);
		if ((ch & 0xfc00) == 0xd800) {
		    entryPtr->leftIndex--;
		    leftIndex = cPtr - displayString;
		}
	    }
	}
#endif
	CkMeasureChars(winPtr->mainPtr, displayString, leftIndex,
	    0, INT_MAX, 0,
	    CK_NEWLINES_NOT_SPECIAL|CK_PARTIAL_OK, &rightX, &dummy);
	entryPtr->leftX = 0;
	entryPtr->tabOrigin = entryPtr->leftX - rightX;
    }

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
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
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







+
+
+








+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







				 * element. */
    char *string)		/* New characters to add (NULL-terminated
				 * string). */
{
    int length, clength;
    char *new;
    int inspos;
#if TCL_UTF_MAX == 3
    Tcl_UniChar ch;
#endif

    length = strlen(string);
    if (length == 0) {
	return;
    }
    clength = Tcl_NumUtfChars(string, -1);
    new = (char *) ckalloc((unsigned) (entryPtr->numBytes + length + 1));
    inspos = Tcl_UtfAtIndex(entryPtr->string, index) - entryPtr->string;
#if TCL_UTF_MAX == 3
    if (length) {
	char *prevPtr;

	Tcl_UtfToUniChar(string + length, &ch);
	if ((ch & 0xfc00) == 0xdc00) {
	    prevPtr = Tcl_UtfPrev(string + length, string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xfc00) == 0xd800) {
		length = prevPtr - string;
		index -= 1;
	    }
	}
    }
#endif
    strncpy(new, entryPtr->string, (size_t) inspos);
    strcpy(new+inspos, string);
    strcpy(new+inspos+length, entryPtr->string+inspos);
    ckfree(entryPtr->string);
    entryPtr->string = new;
    entryPtr->numChars += clength;
    entryPtr->numBytes += length;

#if TCL_UTF_MAX == 3
    /*
     * Account for high surrogate at end of inserted string.
     */
    if (length > 1) {
	char *lastPtr = Tcl_UtfPrev(new+inspos+length, new);

	Tcl_UtfToUniChar(lastPtr, &ch);
	if ((ch & 0xfc00) == 0xd800) {
	    /*
	     * A high surrogate. If followed by low surrogate,
	     * adjust index after it.
	     */
	    lastPtr = Tcl_UtfNext(lastPtr);
	    Tcl_UtfToUniChar(lastPtr, &ch);
	    if ((ch & 0xfc00) == 0xdc00)
		index++;
	}
    }
#endif

    /*
     * Inserting characters invalidates all indexes into the string.
     * Touch up the indexes so that they still refer to the same
     * characters (at new positions).  When updating the selection
     * end-points, don't include the new text in the selection unless
     * it was completely surrounded by the selection.
1104
1105
1106
1107
1108
1109
1110

1111






























1112
1113




















1114
1115
1116
1117
1118
1119
1120
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246







+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    if ((index + count) > entryPtr->numChars) {
	count = entryPtr->numChars - index;
    }
    if (count <= 0) {
	return;
    }

#if TCL_UTF_MAX == 3
    delpos = Tcl_UtfAtIndex(entryPtr->string, index) - entryPtr->string;

    /*
     * Delete complete surrogate pairs.
     */
    if (delpos >= 0) {
	Tcl_UniChar ch;
	char *prevPtr, *nextPtr;

	Tcl_UtfToUniChar(entryPtr->string + delpos, &ch);
	if ((ch & 0xfc00) == 0xdc00) {
	    prevPtr = Tcl_UtfPrev(entryPtr->string + delpos, entryPtr->string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xfc00) == 0xd800) {
		delpos = prevPtr - entryPtr->string;
		count++;
		index--;
	    }
	} else if ((count == 1) && ((ch & 0xfc00) == 0xd800)) {
	    nextPtr = Tcl_UtfNext(entryPtr->string + delpos);
	    Tcl_UtfToUniChar(nextPtr, &ch);
	    if ((ch & 0xfc00) == 0xdc00) {
		count++;
	    }
	}
	if ((index + count) > entryPtr->numChars) {
	    count = entryPtr->numChars - index;
	}
    }
#endif

    delcount = Tcl_UtfAtIndex(entryPtr->string + delpos, count) -
			(entryPtr->string + delpos);

#if TCL_UTF_MAX == 3
    if (delcount) {
	int len;
	Tcl_UniChar ch;
	char *prevPtr;

	len = Tcl_UtfToUniChar(entryPtr->string + delpos + delcount, &ch);
	if ((ch & 0xfc00) == 0xdc00) {
	    prevPtr = Tcl_UtfPrev(entryPtr->string + delpos + delcount,
			    entryPtr->string);
	    Tcl_UtfToUniChar(prevPtr, &ch);
	    if ((ch & 0xfc00) == 0xd800) {
		delcount += len;
		count++;
	    }
	}
    }
#endif

    new = (char *) ckalloc((unsigned) (entryPtr->numBytes + 1 - delcount));
    strncpy(new, entryPtr->string, (size_t) delpos);
    strcpy(new+delpos, entryPtr->string+delpos+delcount);
    entryPtr->numChars = Tcl_NumUtfChars(new, -1);
    entryPtr->numBytes = strlen(new);
    ckfree(entryPtr->string);
    entryPtr->string = new;
1291
1292
1293
1294
1295
1296
1297




1298
1299
1300
1301
1302
1303
1304
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434







+
+
+
+







				 * specified. */
    char *string,		/* Specifies character in entryPtr. */
    int *indexPtr)		/* Where to store converted index. */
{
    size_t length;
    int dummy;
    CkWindow *winPtr = entryPtr->winPtr;
    int roundUp = 0;
#if TCL_UTF_MAX == 3
    int oldInsertPos = entryPtr->insertPos;
#endif

    length = strlen(string);

    if (string[0] == 'a') {
	if (strncmp(string, "anchor", length) == 0) {
	    *indexPtr = entryPtr->selectAnchor;
	} else {
1338
1339
1340
1341
1342
1343
1344
1345

1346
1347
1348
1349
1350
1351
1352
1468
1469
1470
1471
1472
1473
1474

1475
1476
1477
1478
1479
1480
1481
1482







-
+







	    *indexPtr = entryPtr->selectFirst;
	} else if (strncmp(string, "sel.last", length) == 0) {
	    *indexPtr = entryPtr->selectLast;
	} else {
	    goto badIndex;
	}
    } else if (string[0] == '@') {
	int x, roundUp;
	int x;

	if (Tcl_GetInt(interp, string+1, &x) != TCL_OK) {
	    goto badIndex;
	}
	if (x < 0) {
	    x = 0;
	}
1379
1380
1381
1382
1383
1384
1385




1386
























1387
1388
1389
1390
1391
1392
1393
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
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551







+
+
+
+

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	    goto badIndex;
	}
	if (*indexPtr < 0){
	    *indexPtr = 0;
	} else if (*indexPtr > entryPtr->numChars) {
	    *indexPtr = entryPtr->numChars;
	}
#if TCL_UTF_MAX == 3
	roundUp = (indexPtr == &entryPtr->insertPos) &&
		(*indexPtr == (oldInsertPos + 1));
#endif
    }

#if TCL_UTF_MAX == 3
    /*
     * Enforce index on start or end of surrogate pair.
     */
    if (*indexPtr) {
	Tcl_UniChar ch;

	string = Tcl_UtfAtIndex(entryPtr->string, *indexPtr);
	Tcl_UtfToUniChar(string, &ch);
	if ((ch & 0xfc00) == 0xdc00) {
	    if (roundUp) {
		*indexPtr += 1;
	    } else {
		string = Tcl_UtfPrev(string, entryPtr->string);
		Tcl_UtfToUniChar(string, &ch);
		if ((ch & 0xfc00) == 0xd800) {
		    *indexPtr -= 1;
		}
	    }
	}
    }
#endif

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * EntrySelectTo --
1417
1418
1419
1420
1421
1422
1423




















1424
1425
1426
1427
1428
1429
1430
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







     * Pick new starting and ending points for the selection.
     */

    if (entryPtr->selectAnchor > entryPtr->numChars) {
	entryPtr->selectAnchor = entryPtr->numChars;
    }
    if (entryPtr->selectAnchor <= index) {
#if TCL_UTF_MAX == 3
	/*
	 * Correct ending point for surrogate pair.
	 */
	Tcl_UniChar ch;
	char *string;

	string = Tcl_UtfAtIndex(entryPtr->string, index);
	string += Tcl_UtfToUniChar(string, &ch);
	if (((ch & 0xfc00) == 0xdc00) && (index + 1 < entryPtr->numChars)) {
	    index += 1;
	} else if (((ch & 0xfc00) == 0xd800) &&
		   (index + 1 < entryPtr->numChars) &&
		   (index == entryPtr->insertPos)) {
	    Tcl_UtfToUniChar(string, &ch);
	    if ((ch & 0xfc00) == 0xdc00) {
		index += 2;
	    }
	}
#endif
	newFirst = entryPtr->selectAnchor;
	newLast = index;
    } else {
	newFirst = index;
	newLast = entryPtr->selectAnchor;
	if (newLast < 0) {
	    newFirst = newLast = -1;

Changes to undroid/ck8.x/ckEvent.c.

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







-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+


+
+
+







		code2 = code;
		break;
	    }
	}
	nodelay(curscr, TRUE);
done_uc:
	ucbuf[ucp] = '\0';
#if TCL_UTF_MAX == 4
#if TCL_UTF_MAX == 3
	{
	    int n;

	    n = Tcl_UtfToUniChar(ucbuf, &uch);
	    ch = uch;
	    if (n == 0) {
		Tcl_UtfToUniChar(ucbuf, &uch);
		ch = (((ch&0x3ff)<<10) | (uch&0x3ff)) + 0x10000;
	if ((ucp > 3) && ((ucbuf[0] & 0xff) < 0xf8)) {
	    if (((ucbuf[1] & 0xc0) == 0x80) &&
		((ucbuf[2] & 0xc0) == 0x80) &&
		((ucbuf[3] & 0xc0) == 0x80)) {
		ch = ((ucbuf[0] & 0x0f) << 18) |
		     ((ucbuf[1] & 0x3f) << 12) |
		     ((ucbuf[2] & 0x3f) << 6) |
		     (ucbuf[3] & 0x3f);
		goto decDone;
	    }
	}
	Tcl_UtfToUniChar(ucbuf, &uch);
	ch = uch;
decDone:
#else
	Tcl_UtfToUniChar(ucbuf, &uch);
	ch = uch;
#endif
	code = 0;
    }

Changes to undroid/ck8.x/ckText.c.

456
457
458
459
460
461
462
463

464
465
466
467
468
469
470
456
457
458
459
460
461
462

463
464
465
466
467
468
469
470







-
+







	    if (segPtr->typePtr == &ckTextCharType) {
		savedChar = segPtr->body.chars[last];
		segPtr->body.chars[last] = 0;
		Tcl_AppendResult(interp, segPtr->body.chars + offset,
			(char *) NULL);
		segPtr->body.chars[last] = savedChar;
	    }
	    CkTextIndexForwChars(&index1, last-offset, &index1);
	    CkTextIndexForwBytes(&index1, last-offset, &index1);
	}
    } else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
	    && (length >= 3)) {
	char buffer[64];

	if (argc != 3) {
	    Tcl_AppendResult(interp, "wrong # args: should be \"",
497
498
499
500
501
502
503
504

505
506
507
508
509
510
511
497
498
499
500
501
502
503

504
505
506
507
508
509
510
511







-
+







	    result = TCL_ERROR;
	    goto done;
	}
	if (textPtr->state == ckTextNormalUid) {
	    for (j = 3;  j < argc; j += 2) {
		InsertChars(textPtr, &index1, argv[j]);
		if (argc > (j+1)) {
		    CkTextIndexForwChars(&index1, (int) strlen(argv[j]),
		    CkTextIndexForwBytes(&index1, (int) strlen(argv[j]),
			    &index2);
		    oldTagArrayPtr = CkBTreeGetTags(&index1, &numTags);
		    if (oldTagArrayPtr != NULL) {
			for (i = 0; i < numTags; i++) {
			    CkBTreeTag(&index1, &index2, oldTagArrayPtr[i], 0);
			}
			ckfree((char *) oldTagArrayPtr);

Changes to undroid/ck8.x/ckTextIndex.c.

822
823
824
825
826
827
828












829
830
831
832
833
834
835
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







+
+
+
+
+
+
+
+
+
+
+
+







	 */

	for ( ; segPtr != NULL; segPtr = segPtr->nextPtr) {
	    if (segPtr->typePtr == &ckTextCharType) {
		start = segPtr->body.chars + byteOffset;
		end = segPtr->body.chars + segPtr->size;
		for (p = start; p < end; p += Tcl_UtfToUniChar(p, &ch)) {
#if TCL_UTF_MAX == 3
		    if (((ch & 0xfc00) == 0xd800) && (p < end)) {
			char *pp = p;

			pp += Tcl_UtfToUniChar(pp, &ch);
			if ((ch & 0xfc00) == 0xdc00) {
			    p = pp;
			    if (count > 0)
				count--;
			}
		    }
#endif
		    if (count == 0) {
			dstPtr->charIndex += (p - start);
			return;
		    }
		    count--;
		}
	    } else {
979
980
981
982
983
984
985

















986
987
988
989
990
991
992
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	segSize -= segPtr->size;
    }
    while (1) {
	if (segPtr->typePtr == &ckTextCharType) {
	    start = segPtr->body.chars;
	    end = segPtr->body.chars + segSize;
	    for (p = end; ; p = Tcl_UtfPrev(p, start)) {
#if TCL_UTF_MAX == 3
		Tcl_UniChar ch;

		Tcl_UtfToUniChar(p, &ch);
		if (((ch & 0xfc00) == 0xdc00) && (p > start)) {
		    char *pp = Tcl_UtfPrev(p, start);

		    if (pp != NULL) {
			Tcl_UtfToUniChar(pp, &ch);
			if ((ch & 0xfc00) == 0xd800) {
			    p = pp;
			    if (count > 0)
				count--;
			}
		    }
		}
#endif
		if (count == 0) {
		    dstPtr->charIndex -= (end - p);
		    return;
		}
		if (p == start) {
		    break;
		}

Changes to undroid/ck8.x/ckUtil.c.

496
497
498
499
500
501
502































































































503
504
505
506
507
508
509
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







    Tcl_SetResult(interp, EncodingTable[Encoding].name, TCL_STATIC);
    return TCL_OK;
}

/*
 *--------------------------------------------------------------
 *
 * NumUtfChars, UtfToUniChar, UtfAtIndex --
 *
 *	Wrappers for Tcl_NumUtfChars(), Tcl_UtfToUniChar(), and
 *	Tcl_UtAtIndex() counting/producing 32 bit codepoints.
 *
 *--------------------------------------------------------------
 */

static int
NumUtfChars(buf, len)
    char *buf;
    int len;
{
    Tcl_UniChar ch;
    char *end;
    int n, i = 0;

    if (len < 0) {
	len = strlen(buf);
    }
    end = buf + len;
    while (buf < end) {
	n = Tcl_UtfToUniChar(buf, &ch);
#if TCL_UTF_MAX == 3
	if ((ch & 0xfc00) == 0xd800) {
	    int n2;
	    Tcl_UniChar ch2;

	    n2 = Tcl_UtfToUniChar(buf + n, &ch2);
	    if ((ch2 & 0xfc00) == 0xdc00)
		buf += n2;
	}
#endif
	buf += n;
	i++;
    }
    return i;
}

static int
UtfToUniChar(buf, chPtr)
    char *buf;
    int *chPtr;
{
    int len;
    Tcl_UniChar ch;

    len = Tcl_UtfToUniChar(buf, &ch);
#if TCL_UTF_MAX == 3
    if ((ch & 0xfc00) == 0xd800) {
	int len2;
	Tcl_UniChar ch2;

	len2 = Tcl_UtfToUniChar(buf + len, &ch2);
	if ((ch2 & 0xfc00) == 0xdc00) {
	    *chPtr = (((ch & 0x3ff) << 10) | (ch2 & 0x3ff)) + 0x10000;
	    len += len2;
	    return len;
	}
    }
#endif
    *chPtr = ch;
    return len;
}

static CONST char *
UtfAtIndex(src, index)
    CONST char *src;
    int index;
{
    int len;
    Tcl_UniChar ch = 0;

    while (index-- > 0) {
	len = Tcl_UtfToUniChar(src, &ch);
#if TCL_UTF_MAX == 3
	if ((ch & 0xfc00) == 0xd800) {
	    int len2;
	    Tcl_UniChar ch2;

	    len2 = Tcl_UtfToUniChar(src + len, &ch2);
	    if ((ch2 & 0xfc00) == 0xdc00) {
		len += len2;
		index++;
	    }
	}
#endif
	src += len;
    }
    return src;
}

/*
 *--------------------------------------------------------------
 *
 * MakeUCRepl --
 *
 *	Make replacement for unprintable chars.
 *
 *--------------------------------------------------------------
 */

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







-
+
-













-
+







    char *term;			/* Pointer to most recent character that
				 * may legally be a terminating character. */
    int termX;			/* X-position just after term. */
    int curX;			/* X-position corresponding to p. */
    int newX;			/* X-position corresponding to p+1. */
    int rem;
    int nChars = 0;
    int n, m, srcRead, dstWrote, dstChars;
    int uch, n, m, srcRead, dstWrote, dstChars;
    Tcl_UniChar uch;
    char buf[TCL_UTF_MAX*2], buf2[TCL_UTF_MAX*2];

    /*
     * Scan the input string one character at a time, until a character
     * is found that crosses maxX.
     */

    newX = curX = startX;
    termX = 0;
    term = source;
    for (p = source; *p != '\0' && maxChars > 0;) {
        char *p2;

	n = Tcl_UtfToUniChar(p, &uch);
	n = UtfToUniChar(p, &uch);
	p2 = p + n;
	++nChars;
	maxChars -= n;
	m = Tcl_UniCharToUtf(uch, buf);
	if (mainPtr->isoEncoding) {
	    buf2[0] = '\0';
	    Tcl_UtfToExternal(NULL, mainPtr->isoEncoding, buf, m,
688
689
690
691
692
693
694
695

696
697
698
699
700
701
702
782
783
784
785
786
787
788

789
790
791
792
793
794
795
796







-
+







	    }
	}
	if (newX > maxX) {
	    break;
	}
	p = p2;
	if (maxChars > 1) {
	    n = Tcl_UtfToUniChar(p, &uch);
	    n = UtfToUniChar(p, &uch);
	    p2 = p + n;
	    m = Tcl_UniCharToUtf(uch, buf);
	    if (mainPtr->isoEncoding) {
		buf2[0] = '\0';
		Tcl_UtfToExternal(NULL, mainPtr->isoEncoding, buf, m,
			      TCL_ENCODING_START | TCL_ENCODING_END,
			      NULL, buf2, sizeof (buf2), &srcRead,
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
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







-
+









-
+







     * P points to the first character that doesn't fit in the desired
     * span. Use the flags to figure out what to return.
     */

    if ((flags & CK_PARTIAL_OK) && (curX < maxX)) {
	curX = newX;
	if (*p) {
	    n = Tcl_UtfToUniChar(p, &uch);
	    n = UtfToUniChar(p, &uch);
	    p += n;
	    ++nChars;
	}
    }
    if ((flags & CK_AT_LEAST_ONE) && (term == source) && (maxChars > 0)
	     && !isspace((unsigned char) *term)) {
	term = p;
	termX = curX;
	if (term == source) {
	    n = Tcl_UtfToUniChar(term, &uch);
	    n = UtfToUniChar(term, &uch);
	    term += n;
	    ++nChars;
	}
    } else if ((maxChars == 0) || !(flags & CK_WHOLE_WORDS)) {
	term = p;
	termX = curX;
    }
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
888
889
890
891
892
893
894

895
896

897
898
899
900
901
902
903
904


905
906
907
908

909
910
911
912
913
914
915
916







-
+

-
+







-
-
+



-
+







    /*
     * Scan the string one character at a time and display the
     * character.
     */

    getmaxyx(window, dummy, maxX);
    p = string;
    nc = Tcl_NumUtfChars(p, numChars);
    nc = NumUtfChars(p, numChars);
    if (nc > maxX - x)
	numChars = Tcl_UtfAtIndex(p, maxX - x) - p;
	numChars = UtfAtIndex(p, maxX - x) - p;
    else
	numChars = nc;
    if (numChars > maxX - x)
	numChars = maxX - x;
    startX = curX = x;
    wmove(window, y, (x > 0) ? x : 0);
    for (; numChars > 0; numChars--, p += nc) {
	Tcl_UniChar uch;
	int len;
	int uch, len;

	if (*p == '\0')
	    break;
	nc = Tcl_UtfToUniChar(p, &uch);
	nc = UtfToUniChar(p, &uch);
	if (mainPtr->isoEncoding) {
	    int srcRead, dstWrote, dstChars;
	    char buf[TCL_UTF_MAX*2];

	    buf[0] = '\0';
	    Tcl_UtfToExternal(NULL, mainPtr->isoEncoding, p, nc,
			  TCL_ENCODING_START | TCL_ENCODING_END,
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
1064
1065
1066
1067
1068
1069
1070

1071
1072

1073
1074
1075
1076
1077
1078
1079
1080


1081
1082
1083
1084

1085
1086
1087
1088
1089
1090
1091
1092







-
+

-
+







-
-
+



-
+







     * Scan the string one character at a time and display the
     * character.
     */

    count = 0;
    getmaxyx(window, dummy, maxX);
    p = string;
    nc = Tcl_NumUtfChars(p, numChars);
    nc = NumUtfChars(p, numChars);
    if (nc > maxX - x)
	numChars = Tcl_UtfAtIndex(p, maxX - x) - p;
	numChars = UtfAtIndex(p, maxX - x) - p;
    else
	numChars = nc;
    if (numChars > maxX - x)
	numChars = maxX - x;
    startX = curX = x;
    wmove(window, y, (x > 0) ? x : 0);
    for (; numChars > 0 && count <= last; numChars -= nc, count++, p += nc) {
	Tcl_UniChar uch;
	int len;
	int uch, len;

	if (*p == '\0')
	    break;
	nc = Tcl_UtfToUniChar(p, &uch);
	nc = UtfToUniChar(p, &uch);
	if (mainPtr->isoEncoding) {
	    int srcRead, dstWrote, dstChars;
	    char buf[TCL_UTF_MAX*2];

	    buf[0] = '\0';
	    Tcl_UtfToExternal(NULL, mainPtr->isoEncoding, p, nc,
			  TCL_ENCODING_START | TCL_ENCODING_END,

Changes to undroid/ck8.x/library/text.tcl.

271
272
273
274
275
276
277





278
279
280
281
282
283
284
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289







+
+
+
+
+







    scan $i "%d.%d" line char
    if {$ckPriv(prevPos) ne $i} {
	set ckPriv(char) $char
    }
    set new [$w index [expr {$line + $n}].$ckPriv(char)]
    if {[$w compare $new == end] || [$w compare $new == "insert linestart"]} {
	set new $i
    } else {
	scan [$w get $new] "%c" char
	if {($char & 0xfffffc00) == 0xdc00} {
	    set new [$w index "$new + 1c"]
	}
    }
    set ckPriv(prevPos) $new
    return $new
}

# ckTextScrollPages --
# This is a utility procedure used in bindings for moving up and down

Changes to undroid/tclbsd/generic/bsd.c.

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







+







+


+



+







    Tcl_Interp   *interp;
    int           objc;
    Tcl_Obj      *const objv[];
{
    char         *path;
    Tcl_Obj      *resultObj = Tcl_GetObjResult (interp);
    struct statfs statfsbuf;
    Tcl_DString   ds;

    if (objc != 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "path");
	return TCL_ERROR;
    }

    path = Tcl_GetStringFromObj (objv[1], NULL);
    path = Tcl_UtfToExternalDString (NULL, path, -1, &ds);

    if (statfs (path, &statfsbuf) < 0) {
	Tcl_DStringFree (&ds);
	Tcl_SetStringObj (resultObj, Tcl_PosixError (interp), -1);
	return TCL_ERROR;
    }
    Tcl_DStringFree (&ds);

    return StatfsBufToList (interp, resultObj, &statfsbuf);
}

/*-----------------------------------------------------------------------------
 * BSD_GetfsstatObjCmd --
 *  
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
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







-
+
+












+



+
+
+
+







BSD_SetProcTitleObjCmd (clientData, interp, objc, objv)
    ClientData    clientData;
    Tcl_Interp   *interp;
    int           objc;
    Tcl_Obj      *const objv[];
{
#ifdef HAVE_SETPROCTITLE
    char *titleString;
    char       *titleString;
    Tcl_DString ds;
#endif

    if (objc > 2) {
	Tcl_WrongNumArgs (interp, 1, objv, "[string]");
	return TCL_ERROR;
    }

#ifdef HAVE_SETPROCTITLE
    if (objc == 1) {
	titleString = NULL;
    } else {
	titleString = Tcl_GetString (objv[1]);
	titleString = Tcl_UtfToExternalDString (NULL, titleString, -1, &ds);
    }

    setproctitle ("-%s", titleString);

    if (titleString != NULL) {
	Tcl_DStringFree (&ds);
    }
#endif
    return TCL_OK;
}

/*-----------------------------------------------------------------------------
 * BSD_AbortCmd --
 *  

Changes to undroid/tclbsd/generic/bsdsyslog.c.

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







-
+
+











-
+
+
+











+
+












-
-
-
-
-
-
-



















+







    if (Tcl_GetIndexFromObj(interp, objv[1], options, "option", TCL_EXACT,
        &optIndex) != TCL_OK) {
	    return TCL_ERROR;
    }

    switch ((enum options) optIndex) {
      case OPT_LOG: {
        int priority;
        int         priority;
	Tcl_DString ds;

	if (objc != 4) {
	    Tcl_WrongNumArgs (interp, 2, objv, "priority message");
	    return TCL_ERROR;
	}

	priority = GetSyslogPriority (interp, Tcl_GetString (objv[2]), TCLBSD_LOG_ERROR);
	if (priority == -1) {
	   return TCL_ERROR;
	}

	syslog (priority, "%s", Tcl_GetString (objv[3]));
	syslog (priority, "%s", Tcl_UtfToExternalDString (NULL,
	    Tcl_GetString (objv[3]), -1, &ds));
	Tcl_DStringFree (&ds);
	break;
      }

      case OPT_OPEN: {
        int        facility;
        char      *ident;
	int        logopt = 0;
	int        logoptIndex;
	int        logoptObjc;
	int        i;
	Tcl_Obj  **logoptObjv;

	static Tcl_DString identDs;

	static const char *logopts[] = {
	    "console", "no_delay", "perror", "pid", (char *)NULL
	};

	int logOpts[] = {LOG_CONS, LOG_NDELAY, LOG_PERROR, LOG_PID};

	if (objc != 5) {
	    Tcl_WrongNumArgs (interp, 2, objv, "ident logopt facility");
	    return TCL_ERROR;
	}

	/* OK, ident needs to be a const char *, i.e. it needs to not change
	 * behind openlog/syslog's back.  How shall we implement that?
	 * Should we malloc and copy?  Nah, let's just increment the
	 * reference count on the object so that Tcl will leave it the
	 * heck alone. -kl
	 */
	 Tcl_IncrRefCount (objv[2]);
	ident = Tcl_GetString (objv[2]);

	if (Tcl_ListObjGetElements (interp, objv[3], &logoptObjc, &logoptObjv) == TCL_ERROR) {
	    Tcl_AppendResult (interp, " while getting list of log options", NULL);
	    return TCL_ERROR;
	}

	for (i = 0; i < logoptObjc; i++) {
	    if (Tcl_GetIndexFromObj (interp, logoptObjv[i], logopts, "logopts", TCL_EXACT, &logoptIndex) != TCL_OK) {
	        return TCL_ERROR;
	    }
	    logopt |= logOpts[logoptIndex];
	}

	facility = GetSyslogFacility (interp, Tcl_GetString (objv[4]), TCLBSD_LOG_ERROR);
	if (facility == -1) {
	   return TCL_ERROR;
	}

	ident = Tcl_UtfToExternalDString (NULL, ident, -1, &identDs);
	openlog (ident, logopt, facility);
	break;
      }

      case OPT_CLOSE:
	if (objc != 2) {
	    Tcl_WrongNumArgs (interp, 2, objv, "");

Changes to undroid/twapi/twapi/base/errors.c.

347
348
349
350
351
352
353
354

355
356
357
358
359


360
361
362
363
364
365
366
347
348
349
350
351
352
353

354


355
356
357
358
359
360
361
362
363
364
365
366







-
+
-
-



+
+







    errorCodeObj = Twapi_MakeWindowsErrorCodeObj(error, extra);

    /* Third element of error code is also the message */
    if (ObjListIndex(NULL, errorCodeObj, 2, &msgObj) == TCL_OK &&
        msgObj != NULL) {
        Tcl_Obj *resultObj = ObjDuplicate(ObjGetResult(interp));
        if (ObjCharLength(resultObj)) {
#if TCL_UTF_MAX <= 4
#if TCL_UTF_MAX > 3
            Tcl_AppendUnicodeToObj(resultObj, L" ", 1);
#else
            /* Tcl_UniChar is int. So cannot use AppendUnicode. Have
               to force a shimmer to string */
            Tcl_AppendToObj(resultObj, " ", 1);
#else
            Tcl_AppendUnicodeToObj(resultObj, L" ", 1);
#endif
        }
        Tcl_AppendObjToObj(resultObj, msgObj);
        (void) ObjSetResult(interp, resultObj);
    }
    Tcl_SetObjErrorCode(interp, errorCodeObj);

Changes to undroid/twapi/twapi/base/tclobjs.c.

4377
4378
4379
4380
4381
4382
4383
4384



4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400



4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4377
4378
4379
4380
4381
4382
4383

4384
4385
4386
4387
4388
4389
4390
4391


4392
4393
4394
4395
4396
4397
4398
4399

4400
4401
4402
4403
4404
4405
4406
4407


4408
4409
4410
4411
4412
4413
4414







-
+
+
+





-
-








-
+
+
+





-
-







}

TWAPI_EXTERN Tcl_Obj *ObjFromTclUniCharN(const Tcl_UniChar *ws, int len)
{
    if (ws == NULL)
        return ObjFromEmptyString();

#if TCL_UTF_MAX <= 4
#if TCL_UTF_MAX > 3
    return Tcl_NewUnicodeObj(ws, len);
#else
    TWAPI_ASSERT(sizeof(Tcl_UniChar) == sizeof(WCHAR));
    if (gBaseSettings.use_unicode_obj)
        return Tcl_NewUnicodeObj(ws, len);
    else
        return TwapiUtf8ObjFromWinChars(ws, len);
#else
    return Tcl_NewUnicodeObj(ws, len);
#endif
}

TWAPI_EXTERN Tcl_Obj *ObjFromTclUniChar(const Tcl_UniChar *ws)
{
    if (ws == NULL)
        return ObjFromEmptyString(); /* TBD - log ? */

#if TCL_UTF_MAX <= 4
#if TCL_UTF_MAX > 3
    return Tcl_NewUnicodeObj(ws, -1);
#else
    TWAPI_ASSERT(sizeof(Tcl_UniChar) == sizeof(WCHAR));
    if (gBaseSettings.use_unicode_obj)
        return Tcl_NewUnicodeObj(ws, -1);
    else
        return TwapiUtf8ObjFromWinChars(ws, -1);
#else
    return Tcl_NewUnicodeObj(ws, -1);
#endif
}

TWAPI_EXTERN char *ObjToString(Tcl_Obj *objP)
{
    return Tcl_GetString(objP);
}

Changes to undroid/twapi/twapi/base/winchars.c.

11
12
13
14
15
16
17
18

19
20
21
22
23
24
25
11
12
13
14
15
16
17

18
19
20
21
22
23
24
25







-
+







/*
 * This implementation is only enabled when TCL_MAX_UTF > 4 resulting in
 * sizeof(Tcl_UniChar) != sizeof(WCHAR). Otherwise the Obj{To,From}WinChars
 * are inlined as Obj{To,From}TclUniChar in twapi.h
 *
 * TWAPI_FORCE_WINCHARS is used for testing purposes.
 */
#if (TCL_UTF_MAX > 4) || defined(TWAPI_FORCE_WINCHARS)
#if (TCL_UTF_MAX > 3) || defined(TWAPI_FORCE_WINCHARS)

/*
 * Implements a new Tcl_Obj intrep to hold WCHARs.
 */

typedef struct WinChars {
    int nchars; /* Num of characters not counting terminating \0.

Changes to undroid/twapi/twapi/include/twapi.h.

1519
1520
1521
1522
1523
1524
1525
1526

1527
1528
1529
1530
1531
1532
1533
1519
1520
1521
1522
1523
1524
1525

1526
1527
1528
1529
1530
1531
1532
1533







-
+








TWAPI_EXTERN Tcl_UniChar *ObjToTclUniChar(Tcl_Obj *objP);
TWAPI_EXTERN Tcl_UniChar *ObjToTclUniCharN(Tcl_Obj *objP, int *lenP);
TWAPI_EXTERN Tcl_UniChar *ObjToTclUniCharDW(Tcl_Obj *objP, DWORD *lenP);
TWAPI_EXTERN Tcl_Obj *ObjFromTclUniCharN(const Tcl_UniChar *ws, int len);
TWAPI_EXTERN Tcl_Obj *ObjFromTclUniChar(const Tcl_UniChar *ws);

#if (TCL_UTF_MAX > 4) || defined(TWAPI_FORCE_WINCHARS)
#if (TCL_UTF_MAX > 3) || defined(TWAPI_FORCE_WINCHARS)
TWAPI_EXTERN WCHAR *ObjToWinChars(Tcl_Obj *objP);
TWAPI_EXTERN WCHAR *ObjToWinCharsN(Tcl_Obj *objP, int *lenP);
TWAPI_EXTERN WCHAR *ObjToWinCharsDW(Tcl_Obj *objP, DWORD *lenP);
TWAPI_EXTERN Tcl_Obj *ObjFromWinCharsN(const WCHAR *ws, int len);
TWAPI_EXTERN Tcl_Obj *ObjFromWinChars(const WCHAR *ws);
#else
#define ObjToWinChars ObjToTclUniChar

Changes to undroid/twapi/twapi/input/input.c.

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







-
-
+
+

-
+




-
+







-
+
-
-













+
+








    num_chars = Tcl_GetCharLength(input_obj);

    /* Now loop through every character adding it to the input event array */
    /* Win2K and up, accepts unicode characters */

    /* NUmber of events is twice number of chars (keydown + keyup) */
#if TCL_UTF_MAX < 4
    max_input_records = 2 * num_chars;
#if TCL_UTF_MAX > 3
    max_input_records = 4 * num_chars;
#else
    max_input_records = 4 * num_chars;
    max_input_records = 2 * num_chars;
#endif
    input = MemLifoAlloc(ticP->memlifoP, max_input_records * sizeof(*input), NULL);
    for (i = 0, j = 0; i < num_chars; ++i) {
        WCHAR wch;
#if TCL_UTF_MAX > 4
#if TCL_UTF_MAX > 3
	Tcl_UniChar uch;
#endif

#ifndef KEYEVENTF_UNICODE
#define KEYEVENTF_UNICODE     0x0004
#endif

#if TCL_UTF_MAX <= 4
#if TCL_UTF_MAX > 3
        wch = Tcl_GetUniChar(input_obj, i);
#else
        uch = Tcl_GetUniChar(input_obj, i);
	if (uch > 0xFFFF) {
            wch = (((uch - 0x10000) >> 10) & 0x3FF) | 0xD800;
            init_keyboard_input(&input[j], 0, KEYEVENTF_UNICODE);
            input[j].ki.wScan = wch;
            ++j;
            init_keyboard_input(&input[j], 0, KEYEVENTF_UNICODE|KEYEVENTF_KEYUP);
            input[j].ki.wScan  = wch;
            j++;
            wch = ((uch - 0x10000) & 0x3FF) | 0xDC00;
	} else {
            wch = (WCHAR) uch;
	}
#else
        wch = Tcl_GetUniChar(input_obj, i);
#endif
        init_keyboard_input(&input[j], 0, KEYEVENTF_UNICODE);
        input[j].ki.wScan = wch;
        ++j;
        init_keyboard_input(&input[j], 0, KEYEVENTF_UNICODE|KEYEVENTF_KEYUP);
        input[j].ki.wScan  = wch;
        ++j;

Changes to undroid/twapi/twapi/storage/dirmonitor.c.

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







-
+
-
-













+
+


        ++pattern;
    } else if (pattern[0] == L'+') {
        include = 1;
        ++pattern;
    } else
        include = 1;            /* Default is inclusive pattern */

#if TCL_UTF_MAX <= 4
#if TCL_UTF_MAX > 3
    return Tcl_UniCharCaseMatch(path, pattern, 1) ? include : 0;
#else
    {
        Tcl_Obj *patObj, *pathObj;
        Tcl_UniChar *pathuni, *patuni;
        int match_result;
        patObj = ObjFromWinChars(pattern);
        patuni = Tcl_GetUnicode(patObj);
        pathObj = ObjFromWinChars(path);
        pathuni = Tcl_GetUnicode(pathObj);
        match_result = Tcl_UniCharCaseMatch(pathuni, patuni, 1) ? include : 0;
        ObjDecrRefs(patObj);
        ObjDecrRefs(pathObj);
        return match_result;
    }
#else
    return Tcl_UniCharCaseMatch(path, pattern, 1) ? include : 0;
#endif
}