Debian Codegraph @

pTk/mTk/tclGeneric/tclUtf.c

raw symbols SCIP
1 /*
2 * tclUtf.c --
3 *
4 * Routines for manipulating UTF-8 strings.
5 *
6 * Copyright (c) 1997-1998 Sun Microsystems, Inc.
7 *
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id: tclUtf.c,v 1.30.2.2 2003/10/08 14:21:20 dkf Exp $
12 */
13
14 #include "tclInt.h"
15
16 /*
17 * Include the static character classification tables and macros.
18 */
19
20 #include "tclUniData.c"
21
22 /*
23 * The following macros are used for fast character category tests. The
24 * x_BITS values are shifted right by the category value to determine whether
25 * the given category is included in the set.
26 */
27
28 #define ALPHA_BITS ((1 << UPPERCASE_LETTER) | (1 << LOWERCASE_LETTER) \
29 | (1 << TITLECASE_LETTER) | (1 << MODIFIER_LETTER) | (1 << OTHER_LETTER))
30
31 #define DIGIT_BITS (1 << DECIMAL_DIGIT_NUMBER)
32
33 #define SPACE_BITS ((1 << SPACE_SEPARATOR) | (1 << LINE_SEPARATOR) \
34 | (1 << PARAGRAPH_SEPARATOR))
35
36 #define CONNECTOR_BITS (1 << CONNECTOR_PUNCTUATION)
37
38 #define PRINT_BITS (ALPHA_BITS | DIGIT_BITS | SPACE_BITS | \
39 (1 << NON_SPACING_MARK) | (1 << ENCLOSING_MARK) | \
40 (1 << COMBINING_SPACING_MARK) | (1 << LETTER_NUMBER) | \
41 (1 << OTHER_NUMBER) | (1 << CONNECTOR_PUNCTUATION) | \
42 (1 << DASH_PUNCTUATION) | (1 << OPEN_PUNCTUATION) | \
43 (1 << CLOSE_PUNCTUATION) | (1 << INITIAL_QUOTE_PUNCTUATION) | \
44 (1 << FINAL_QUOTE_PUNCTUATION) | (1 << OTHER_PUNCTUATION) | \
45 (1 << MATH_SYMBOL) | (1 << CURRENCY_SYMBOL) | \
46 (1 << MODIFIER_SYMBOL) | (1 << OTHER_SYMBOL))
47
48 #define PUNCT_BITS ((1 << CONNECTOR_PUNCTUATION) | \
49 (1 << DASH_PUNCTUATION) | (1 << OPEN_PUNCTUATION) | \
50 (1 << CLOSE_PUNCTUATION) | (1 << INITIAL_QUOTE_PUNCTUATION) | \
51 (1 << FINAL_QUOTE_PUNCTUATION) | (1 << OTHER_PUNCTUATION))
52
53 /*
54 * Unicode characters less than this value are represented by themselves
55 * in UTF-8 strings.
56 */
57
58 #define UNICODE_SELF 0x80
59
60 /*
61 * The following structures are used when mapping between Unicode (UCS-2)
62 * and UTF-8.
63 */
64
65 static CONST unsigned char totalBytes[256] = {
66 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,
67 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,
68 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,
69 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,
70 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,
71 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,
72 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,
73 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
74 #if TCL_UTF_MAX > 3
75 4,4,4,4,4,4,4,4,
76 #else
77 1,1,1,1,1,1,1,1,
78 #endif
79 #if TCL_UTF_MAX > 4
80 5,5,5,5,
81 #else
82 1,1,1,1,
83 #endif
84 #if TCL_UTF_MAX > 5
85 6,6,6,6
86 #else
87 1,1,1,1
88 #endif
89 };
90
91 /*
92 * Procedures used only in this module.
93 */
94
95 static int UtfCount _ANSI_ARGS_((int ch));
96
97
98 /*
99 *---------------------------------------------------------------------------
100 *
101 * UtfCount --
102 *
103 * Find the number of bytes in the Utf character "ch".
104 *
105 * Results:
106 * The return values is the number of bytes in the Utf character "ch".
107 *
108 * Side effects:
109 * None.
110 *
111 *---------------------------------------------------------------------------
112 */
113
114 INLINE static int
115 UtfCount(ch)
116 int ch; /* The Tcl_UniChar whose size is returned. */
117 {
118 if ((ch > 0) && (ch < UNICODE_SELF)) {
119 return 1;
120 }
121 if (ch <= 0x7FF) {
122 return 2;
123 }
124 if (ch <= 0xFFFF) {
125 return 3;
126 }
127 #if TCL_UTF_MAX > 3
128 if (ch <= 0x1FFFFF) {
129 return 4;
130 }
131 if (ch <= 0x3FFFFFF) {
132 return 5;
133 }
134 if (ch <= 0x7FFFFFFF) {
135 return 6;
136 }
137 #endif
138 return 3;
139 }
140
141 /*
142 *---------------------------------------------------------------------------
143 *
144 * Tcl_UniCharToUtf --
145 *
146 * Store the given Tcl_UniChar as a sequence of UTF-8 bytes in the
147 * provided buffer. Equivalent to Plan 9 runetochar().
148 *
149 * Results:
150 * The return values is the number of bytes in the buffer that
151 * were consumed.
152 *
153 * Side effects:
154 * None.
155 *
156 *---------------------------------------------------------------------------
157 */
158
159 INLINE int
160 Tcl_UniCharToUtf(ch, str)
161 int ch; /* The Tcl_UniChar to be stored in the
162 * buffer. */
163 char *str; /* Buffer in which the UTF-8 representation
164 * of the Tcl_UniChar is stored. Buffer must
165 * be large enough to hold the UTF-8 character
166 * (at most TCL_UTF_MAX bytes). */
167 {
168 if ((ch > 0) && (ch < UNICODE_SELF)) {
169 str[0] = (char) ch;
170 return 1;
171 }
172 if (ch <= 0x7FF) {
173 str[1] = (char) ((ch | 0x80) & 0xBF);
174 str[0] = (char) ((ch >> 6) | 0xC0);
175 return 2;
176 }
177 if (ch <= 0xFFFF) {
178 three:
179 str[2] = (char) ((ch | 0x80) & 0xBF);
180 str[1] = (char) (((ch >> 6) | 0x80) & 0xBF);
181 str[0] = (char) ((ch >> 12) | 0xE0);
182 return 3;
183 }
184
185 #if TCL_UTF_MAX > 3
186 if (ch <= 0x1FFFFF) {
187 str[3] = (char) ((ch | 0x80) & 0xBF);
188 str[2] = (char) (((ch >> 6) | 0x80) & 0xBF);
189 str[1] = (char) (((ch >> 12) | 0x80) & 0xBF);
190 str[0] = (char) ((ch >> 18) | 0xF0);
191 return 4;
192 }
193 if (ch <= 0x3FFFFFF) {
194 str[4] = (char) ((ch | 0x80) & 0xBF);
195 str[3] = (char) (((ch >> 6) | 0x80) & 0xBF);
196 str[2] = (char) (((ch >> 12) | 0x80) & 0xBF);
197 str[1] = (char) (((ch >> 18) | 0x80) & 0xBF);
198 str[0] = (char) ((ch >> 24) | 0xF8);
199 return 5;
200 }
201 if (ch <= 0x7FFFFFFF) {
202 str[5] = (char) ((ch | 0x80) & 0xBF);
203 str[4] = (char) (((ch >> 6) | 0x80) & 0xBF);
204 str[3] = (char) (((ch >> 12) | 0x80) & 0xBF);
205 str[2] = (char) (((ch >> 18) | 0x80) & 0xBF);
206 str[1] = (char) (((ch >> 24) | 0x80) & 0xBF);
207 str[0] = (char) ((ch >> 30) | 0xFC);
208 return 6;
209 }
210 #endif
211
212 ch = 0xFFFD;
213 goto three;
214 }
215
216 /*
217 *---------------------------------------------------------------------------
218 *
219 * Tcl_UniCharToUtfDString --
220 *
221 * Convert the given Unicode string to UTF-8.
222 *
223 * Results:
224 * The return value is a pointer to the UTF-8 representation of the
225 * Unicode string. Storage for the return value is appended to the
226 * end of dsPtr.
227 *
228 * Side effects:
229 * None.
230 *
231 *---------------------------------------------------------------------------
232 */
233
234 char *
235 Tcl_UniCharToUtfDString(wString, numChars, dsPtr)
236 CONST Tcl_UniChar *wString; /* Unicode string to convert to UTF-8. */
237 int numChars; /* Length of Unicode string in Tcl_UniChars
238 * (must be >= 0). */
239 Tcl_DString *dsPtr; /* UTF-8 representation of string is
240 * appended to this previously initialized
241 * DString. */
242 {
243 CONST Tcl_UniChar *w, *wEnd;
244 char *p, *string;
245 int oldLength;
246
247 /*
248 * UTF-8 string length in bytes will be <= Unicode string length *
249 * TCL_UTF_MAX.
250 */
251
252 oldLength = Tcl_DStringLength(dsPtr);
253 Tcl_DStringSetLength(dsPtr, (oldLength + numChars + 1) * TCL_UTF_MAX);
254 string = Tcl_DStringValue(dsPtr) + oldLength;
255
256 p = string;
257 wEnd = wString + numChars;
258 for (w = wString; w < wEnd; ) {
259 p += Tcl_UniCharToUtf(*w, p);
260 w++;
261 }
262 Tcl_DStringSetLength(dsPtr, oldLength + (p - string));
263
264 return string;
265 }
266
267 /*
268 *---------------------------------------------------------------------------
269 *
270 * Tcl_UtfToUniChar --
271 *
272 * Extract the Tcl_UniChar represented by the UTF-8 string. Bad
273 * UTF-8 sequences are converted to valid Tcl_UniChars and processing
274 * continues. Equivalent to Plan 9 chartorune().
275 *
276 * The caller must ensure that the source buffer is long enough that
277 * this routine does not run off the end and dereference non-existent
278 * memory looking for trail bytes. If the source buffer is known to
279 * be '\0' terminated, this cannot happen. Otherwise, the caller
280 * should call Tcl_UtfCharComplete() before calling this routine to
281 * ensure that enough bytes remain in the string.
282 *
283 * Results:
284 * *chPtr is filled with the Tcl_UniChar, and the return value is the
285 * number of bytes from the UTF-8 string that were consumed.
286 *
287 * Side effects:
288 * None.
289 *
290 *---------------------------------------------------------------------------
291 */
292
293 int
294 Tcl_UtfToUniChar(str, chPtr)
295 register CONST char *str; /* The UTF-8 string. */
296 register Tcl_UniChar *chPtr; /* Filled with the Tcl_UniChar represented
297 * by the UTF-8 string. */
298 {
299 register int byte;
300
301 /*
302 * Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones.
303 */
304
305 byte = *((unsigned char *) str);
306 if (byte < 0xC0) {
307 /*
308 * Handles properly formed UTF-8 characters between 0x01 and 0x7F.
309 * Also treats \0 and naked trail bytes 0x80 to 0xBF as valid
310 * characters representing themselves.
311 */
312
313 *chPtr = (Tcl_UniChar) byte;
314 return 1;
315 } else if (byte < 0xE0) {
316 if ((str[1] & 0xC0) == 0x80) {
317 /*
318 * Two-byte-character lead-byte followed by a trail-byte.
319 */
320
321 *chPtr = (Tcl_UniChar) (((byte & 0x1F) << 6) | (str[1] & 0x3F));
322 return 2;
323 }
324 /*
325 * A two-byte-character lead-byte not followed by trail-byte
326 * represents itself.
327 */
328
329 *chPtr = (Tcl_UniChar) byte;
330 return 1;
331 } else if (byte < 0xF0) {
332 if (((str[1] & 0xC0) == 0x80) && ((str[2] & 0xC0) == 0x80)) {
333 /*
334 * Three-byte-character lead byte followed by two trail bytes.
335 */
336
337 *chPtr = (Tcl_UniChar) (((byte & 0x0F) << 12)
338 | ((str[1] & 0x3F) << 6) | (str[2] & 0x3F));
339 return 3;
340 }
341 /*
342 * A three-byte-character lead-byte not followed by two trail-bytes
343 * represents itself.
344 */
345
346 *chPtr = (Tcl_UniChar) byte;
347 return 1;
348 }
349 #if TCL_UTF_MAX > 3
350 else {
351 int ch, total, trail;
352
353 total = totalBytes[byte];
354 trail = total - 1;
355 if (trail > 0) {
356 ch = byte & (0x3F >> trail);
357 do {
358 str++;
359 if ((*str & 0xC0) != 0x80) {
360 *chPtr = byte;
361 return 1;
362 }
363 ch <<= 6;
364 ch |= (*str & 0x3F);
365 trail--;
366 } while (trail > 0);
367 *chPtr = ch;
368 return total;
369 }
370 }
371 #endif
372
373 *chPtr = (Tcl_UniChar) byte;
374 return 1;
375 }
376
377 /*
378 *---------------------------------------------------------------------------
379 *
380 * Tcl_UtfToUniCharDString --
381 *
382 * Convert the UTF-8 string to Unicode.
383 *
384 * Results:
385 * The return value is a pointer to the Unicode representation of the
386 * UTF-8 string. Storage for the return value is appended to the
387 * end of dsPtr. The Unicode string is terminated with a Unicode
388 * NULL character.
389 *
390 * Side effects:
391 * None.
392 *
393 *---------------------------------------------------------------------------
394 */
395
396 Tcl_UniChar *
397 Tcl_UtfToUniCharDString(string, length, dsPtr)
398 CONST char *string; /* UTF-8 string to convert to Unicode. */
399 int length; /* Length of UTF-8 string in bytes, or -1
400 * for strlen(). */
401 Tcl_DString *dsPtr; /* Unicode representation of string is
402 * appended to this previously initialized
403 * DString. */
404 {
405 Tcl_UniChar *w, *wString;
406 CONST char *p, *end;
407 int oldLength;
408
409 if (length < 0) {
410 length = strlen(string);
411 }
412
413 /*
414 * Unicode string length in Tcl_UniChars will be <= UTF-8 string length
415 * in bytes.
416 */
417
418 oldLength = Tcl_DStringLength(dsPtr);
419 Tcl_DStringSetLength(dsPtr,
420 (int) ((oldLength + length + 1) * sizeof(Tcl_UniChar)));
421 wString = (Tcl_UniChar *) (Tcl_DStringValue(dsPtr) + oldLength);
422
423 w = wString;
424 end = string + length;
425 for (p = string; p < end; ) {
426 p += TclUtfToUniChar(p, w);
427 w++;
428 }
429 *w = '\0';
430 Tcl_DStringSetLength(dsPtr,
431 (oldLength + ((char *) w - (char *) wString)));
432
433 return wString;
434 }
435
436 /*
437 *---------------------------------------------------------------------------
438 *
439 * Tcl_UtfCharComplete --
440 *
441 * Determine if the UTF-8 string of the given length is long enough
442 * to be decoded by Tcl_UtfToUniChar(). This does not ensure that the
443 * UTF-8 string is properly formed. Equivalent to Plan 9 fullrune().
444 *
445 * Results:
446 * The return value is 0 if the string is not long enough, non-zero
447 * otherwise.
448 *
449 * Side effects:
450 * None.
451 *
452 *---------------------------------------------------------------------------
453 */
454
455 int
456 Tcl_UtfCharComplete(str, len)
457 CONST char *str; /* String to check if first few bytes
458 * contain a complete UTF-8 character. */
459 int len; /* Length of above string in bytes. */
460 {
461 int ch;
462
463 ch = *((unsigned char *) str);
464 return len >= totalBytes[ch];
465 }
466
467 /*
468 *---------------------------------------------------------------------------
469 *
470 * Tcl_NumUtfChars --
471 *
472 * Returns the number of characters (not bytes) in the UTF-8 string,
473 * not including the terminating NULL byte. This is equivalent to
474 * Plan 9 utflen() and utfnlen().
475 *
476 * Results:
477 * As above.
478 *
479 * Side effects:
480 * None.
481 *
482 *---------------------------------------------------------------------------
483 */
484
485 int
486 Tcl_NumUtfChars(str, len)
487 register CONST char *str; /* The UTF-8 string to measure. */
488 int len; /* The length of the string in bytes, or -1
489 * for strlen(string). */
490 {
491 Tcl_UniChar ch;
492 register Tcl_UniChar *chPtr = &ch;
493 register int i;
494
495 /*
496 * The separate implementations are faster.
497 *
498 * Since this is a time-sensitive function, we also do the check for
499 * the single-byte char case specially.
500 */
501
502 i = 0;
503 if (len < 0) {
504 while (*str != '\0') {
505 str += TclUtfToUniChar(str, chPtr);
506 i++;
507 }
508 } else {
509 register int n;
510
511 while (len > 0) {
512 if (UCHAR(*str) < 0xC0) {
513 len--;
514 str++;
515 } else {
516 n = Tcl_UtfToUniChar(str, chPtr);
517 len -= n;
518 str += n;
519 }
520 i++;
521 }
522 }
523 return i;
524 }
525
526 /*
527 *---------------------------------------------------------------------------
528 *
529 * Tcl_UtfFindFirst --
530 *
531 * Returns a pointer to the first occurance of the given Tcl_UniChar
532 * in the NULL-terminated UTF-8 string. The NULL terminator is
533 * considered part of the UTF-8 string. Equivalent to Plan 9
534 * utfrune().
535 *
536 * Results:
537 * As above. If the Tcl_UniChar does not exist in the given string,
538 * the return value is NULL.
539 *
540 * Side effects:
541 * None.
542 *
543 *---------------------------------------------------------------------------
544 */
545 CONST char *
546 Tcl_UtfFindFirst(string, ch)
547 CONST char *string; /* The UTF-8 string to be searched. */
548 int ch; /* The Tcl_UniChar to search for. */
549 {
550 int len;
551 Tcl_UniChar find;
552
553 while (1) {
554 len = TclUtfToUniChar(string, &find);
555 if (find == ch) {
556 return string;
557 }
558 if (*string == '\0') {
559 return NULL;
560 }
561 string += len;
562 }
563 }
564
565 /*
566 *---------------------------------------------------------------------------
567 *
568 * Tcl_UtfFindLast --
569 *
570 * Returns a pointer to the last occurance of the given Tcl_UniChar
571 * in the NULL-terminated UTF-8 string. The NULL terminator is
572 * considered part of the UTF-8 string. Equivalent to Plan 9
573 * utfrrune().
574 *
575 * Results:
576 * As above. If the Tcl_UniChar does not exist in the given string,
577 * the return value is NULL.
578 *
579 * Side effects:
580 * None.
581 *
582 *---------------------------------------------------------------------------
583 */
584
585 CONST char *
586 Tcl_UtfFindLast(string, ch)
587 CONST char *string; /* The UTF-8 string to be searched. */
588 int ch; /* The Tcl_UniChar to search for. */
589 {
590 int len;
591 Tcl_UniChar find;
592 CONST char *last;
593
594 last = NULL;
595 while (1) {
596 len = TclUtfToUniChar(string, &find);
597 if (find == ch) {
598 last = string;
599 }
600 if (*string == '\0') {
601 break;
602 }
603 string += len;
604 }
605 return last;
606 }
607
608 /*
609 *---------------------------------------------------------------------------
610 *
611 * Tcl_UtfNext --
612 *
613 * Given a pointer to some current location in a UTF-8 string,
614 * move forward one character. The caller must ensure that they
615 * are not asking for the next character after the last character
616 * in the string.
617 *
618 * Results:
619 * The return value is the pointer to the next character in
620 * the UTF-8 string.
621 *
622 * Side effects:
623 * None.
624 *
625 *---------------------------------------------------------------------------
626 */
627
628 CONST char *
629 Tcl_UtfNext(str)
630 CONST char *str; /* The current location in the string. */
631 {
632 Tcl_UniChar ch;
633
634 return str + TclUtfToUniChar(str, &ch);
635 }
636
637 /*
638 *---------------------------------------------------------------------------
639 *
640 * Tcl_UtfPrev --
641 *
642 * Given a pointer to some current location in a UTF-8 string,
643 * move backwards one character. This works correctly when the
644 * pointer is in the middle of a UTF-8 character.
645 *
646 * Results:
647 * The return value is a pointer to the previous character in the
648 * UTF-8 string. If the current location was already at the
649 * beginning of the string, the return value will also be a
650 * pointer to the beginning of the string.
651 *
652 * Side effects:
653 * None.
654 *
655 *---------------------------------------------------------------------------
656 */
657
658 CONST char *
659 Tcl_UtfPrev(str, start)
660 CONST char *str; /* The current location in the string. */
661 CONST char *start; /* Pointer to the beginning of the
662 * string, to avoid going backwards too
663 * far. */
664 {
665 CONST char *look;
666 int i, byte;
667
668 str--;
669 look = str;
670 for (i = 0; i < TCL_UTF_MAX; i++) {
671 if (look < start) {
672 if (str < start) {
673 str = start;
674 }
675 break;
676 }
677 byte = *((unsigned char *) look);
678 if (byte < 0x80) {
679 break;
680 }
681 if (byte >= 0xC0) {
682 return look;
683 }
684 look--;
685 }
686 return str;
687 }
688
689 /*
690 *---------------------------------------------------------------------------
691 *
692 * Tcl_UniCharAtIndex --
693 *
694 * Returns the Unicode character represented at the specified
695 * character (not byte) position in the UTF-8 string.
696 *
697 * Results:
698 * As above.
699 *
700 * Side effects:
701 * None.
702 *
703 *---------------------------------------------------------------------------
704 */
705
706 Tcl_UniChar
707 Tcl_UniCharAtIndex(src, index)
708 register CONST char *src; /* The UTF-8 string to dereference. */
709 register int index; /* The position of the desired character. */
710 {
711 Tcl_UniChar ch;
712
713 while (index >= 0) {
714 index--;
715 src += TclUtfToUniChar(src, &ch);
716 }
717 return ch;
718 }
719
720 /*
721 *---------------------------------------------------------------------------
722 *
723 * Tcl_UtfAtIndex --
724 *
725 * Returns a pointer to the specified character (not byte) position
726 * in the UTF-8 string.
727 *
728 * Results:
729 * As above.
730 *
731 * Side effects:
732 * None.
733 *
734 *---------------------------------------------------------------------------
735 */
736
737 CONST char *
738 Tcl_UtfAtIndex(src, index)
739 register CONST char *src; /* The UTF-8 string. */
740 register int index; /* The position of the desired character. */
741 {
742 Tcl_UniChar ch;
743
744 while (index > 0) {
745 index--;
746 src += TclUtfToUniChar(src, &ch);
747 }
748 return src;
749 }
750
751 /*
752 *---------------------------------------------------------------------------
753 *
754 * Tcl_UtfBackslash --
755 *
756 * Figure out how to handle a backslash sequence.
757 *
758 * Results:
759 * Stores the bytes represented by the backslash sequence in dst and
760 * returns the number of bytes written to dst. At most TCL_UTF_MAX
761 * bytes are written to dst; dst must have been large enough to accept
762 * those bytes. If readPtr isn't NULL then it is filled in with a
763 * count of the number of bytes in the backslash sequence.
764 *
765 * Side effects:
766 * The maximum number of bytes it takes to represent a Unicode
767 * character in UTF-8 is guaranteed to be less than the number of
768 * bytes used to express the backslash sequence that represents
769 * that Unicode character. If the target buffer into which the
770 * caller is going to store the bytes that represent the Unicode
771 * character is at least as large as the source buffer from which
772 * the backslashed sequence was extracted, no buffer overruns should
773 * occur.
774 *
775 *---------------------------------------------------------------------------
776 */
777
778 int
779 Tcl_UtfBackslash(src, readPtr, dst)
780 CONST char *src; /* Points to the backslash character of
781 * a backslash sequence. */
782 int *readPtr; /* Fill in with number of characters read
783 * from src, unless NULL. */
784 char *dst; /* Filled with the bytes represented by the
785 * backslash sequence. */
786 {
787 #define LINE_LENGTH 128
788 int numRead;
789 int result;
790
791 result = TclParseBackslash(src, LINE_LENGTH, &numRead, dst);
792 if (numRead == LINE_LENGTH) {
793 /* We ate a whole line. Pay the price of a strlen() */
794 result = TclParseBackslash(src, (int)strlen(src), &numRead, dst);
795 }
796 if (readPtr != NULL) {
797 *readPtr = numRead;
798 }
799 return result;
800 }
801
802 /*
803 *----------------------------------------------------------------------
804 *
805 * Tcl_UtfToUpper --
806 *
807 * Convert lowercase characters to uppercase characters in a UTF
808 * string in place. The conversion may shrink the UTF string.
809 *
810 * Results:
811 * Returns the number of bytes in the resulting string
812 * excluding the trailing null.
813 *
814 * Side effects:
815 * Writes a terminating null after the last converted character.
816 *
817 *----------------------------------------------------------------------
818 */
819
820 int
821 Tcl_UtfToUpper(str)
822 char *str; /* String to convert in place. */
823 {
824 Tcl_UniChar ch, upChar;
825 char *src, *dst;
826 int bytes;
827
828 /*
829 * Iterate over the string until we hit the terminating null.
830 */
831
832 src = dst = str;
833 while (*src) {
834 bytes = TclUtfToUniChar(src, &ch);
835 upChar = Tcl_UniCharToUpper(ch);
836
837 /*
838 * To keep badly formed Utf strings from getting inflated by
839 * the conversion (thereby causing a segfault), only copy the
840 * upper case char to dst if its size is <= the original char.
841 */
842
843 if (bytes < UtfCount(upChar)) {
844 memcpy(dst, src, (size_t) bytes);
845 dst += bytes;
846 } else {
847 dst += Tcl_UniCharToUtf(upChar, dst);
848 }
849 src += bytes;
850 }
851 *dst = '\0';
852 return (dst - str);
853 }
854
855 /*
856 *----------------------------------------------------------------------
857 *
858 * Tcl_UtfToLower --
859 *
860 * Convert uppercase characters to lowercase characters in a UTF
861 * string in place. The conversion may shrink the UTF string.
862 *
863 * Results:
864 * Returns the number of bytes in the resulting string
865 * excluding the trailing null.
866 *
867 * Side effects:
868 * Writes a terminating null after the last converted character.
869 *
870 *----------------------------------------------------------------------
871 */
872
873 int
874 Tcl_UtfToLower(str)
875 char *str; /* String to convert in place. */
876 {
877 Tcl_UniChar ch, lowChar;
878 char *src, *dst;
879 int bytes;
880
881 /*
882 * Iterate over the string until we hit the terminating null.
883 */
884
885 src = dst = str;
886 while (*src) {
887 bytes = TclUtfToUniChar(src, &ch);
888 lowChar = Tcl_UniCharToLower(ch);
889
890 /*
891 * To keep badly formed Utf strings from getting inflated by
892 * the conversion (thereby causing a segfault), only copy the
893 * lower case char to dst if its size is <= the original char.
894 */
895
896 if (bytes < UtfCount(lowChar)) {
897 memcpy(dst, src, (size_t) bytes);
898 dst += bytes;
899 } else {
900 dst += Tcl_UniCharToUtf(lowChar, dst);
901 }
902 src += bytes;
903 }
904 *dst = '\0';
905 return (dst - str);
906 }
907
908 /*
909 *----------------------------------------------------------------------
910 *
911 * Tcl_UtfToTitle --
912 *
913 * Changes the first character of a UTF string to title case or
914 * uppercase and the rest of the string to lowercase. The
915 * conversion happens in place and may shrink the UTF string.
916 *
917 * Results:
918 * Returns the number of bytes in the resulting string
919 * excluding the trailing null.
920 *
921 * Side effects:
922 * Writes a terminating null after the last converted character.
923 *
924 *----------------------------------------------------------------------
925 */
926
927 int
928 Tcl_UtfToTitle(str)
929 char *str; /* String to convert in place. */
930 {
931 Tcl_UniChar ch, titleChar, lowChar;
932 char *src, *dst;
933 int bytes;
934
935 /*
936 * Capitalize the first character and then lowercase the rest of the
937 * characters until we get to a null.
938 */
939
940 src = dst = str;
941
942 if (*src) {
943 bytes = TclUtfToUniChar(src, &ch);
944 titleChar = Tcl_UniCharToTitle(ch);
945
946 if (bytes < UtfCount(titleChar)) {
947 memcpy(dst, src, (size_t) bytes);
948 dst += bytes;
949 } else {
950 dst += Tcl_UniCharToUtf(titleChar, dst);
951 }
952 src += bytes;
953 }
954 while (*src) {
955 bytes = TclUtfToUniChar(src, &ch);
956 lowChar = Tcl_UniCharToLower(ch);
957
958 if (bytes < UtfCount(lowChar)) {
959 memcpy(dst, src, (size_t) bytes);
960 dst += bytes;
961 } else {
962 dst += Tcl_UniCharToUtf(lowChar, dst);
963 }
964 src += bytes;
965 }
966 *dst = '\0';
967 return (dst - str);
968 }
969
970 /*
971 *----------------------------------------------------------------------
972 *
973 * TclpUtfNcmp2 --
974 *
975 * Compare at most n bytes of utf-8 strings cs and ct. Both cs
976 * and ct are assumed to be at least n bytes long.
977 *
978 * Results:
979 * Return <0 if cs < ct, 0 if cs == ct, or >0 if cs > ct.
980 *
981 * Side effects:
982 * None.
983 *
984 *----------------------------------------------------------------------
985 */
986
987 int
988 TclpUtfNcmp2(cs, ct, n)
989 CONST char *cs; /* UTF string to compare to ct. */
990 CONST char *ct; /* UTF string cs is compared to. */
991 unsigned long n; /* Number of *bytes* to compare. */
992 {
993 /*
994 * We can't simply call 'memcmp(cs, ct, n);' because we need to check
995 * for Tcl's \xC0\x80 non-utf-8 null encoding.
996 * Otherwise utf-8 lexes fine in the strcmp manner.
997 */
998 register int result = 0;
999
1000 for ( ; n != 0; n--, cs++, ct++) {
1001 if (*cs != *ct) {
1002 result = UCHAR(*cs) - UCHAR(*ct);
1003 break;
1004 }
1005 }
1006 if (n && ((UCHAR(*cs) == 0xC0) || (UCHAR(*ct) == 0xC0))) {
1007 unsigned char c1, c2;
1008 c1 = ((UCHAR(*cs) == 0xC0) && (UCHAR(cs[1]) == 0x80)) ? 0 : UCHAR(*cs);
1009 c2 = ((UCHAR(*ct) == 0xC0) && (UCHAR(ct[1]) == 0x80)) ? 0 : UCHAR(*ct);
1010 result = (c1 - c2);
1011 }
1012 return result;
1013 }
1014
1015 /*
1016 *----------------------------------------------------------------------
1017 *
1018 * Tcl_UtfNcmp --
1019 *
1020 * Compare at most n UTF chars of string cs to string ct. Both cs
1021 * and ct are assumed to be at least n UTF chars long.
1022 *
1023 * Results:
1024 * Return <0 if cs < ct, 0 if cs == ct, or >0 if cs > ct.
1025 *
1026 * Side effects:
1027 * None.
1028 *
1029 *----------------------------------------------------------------------
1030 */
1031
1032 int
1033 Tcl_UtfNcmp(cs, ct, n)
1034 CONST char *cs; /* UTF string to compare to ct. */
1035 CONST char *ct; /* UTF string cs is compared to. */
1036 unsigned long n; /* Number of UTF chars to compare. */
1037 {
1038 Tcl_UniChar ch1, ch2;
1039 /*
1040 * Cannot use 'memcmp(cs, ct, n);' as byte representation of
1041 * \u0000 (the pair of bytes 0xc0,0x80) is larger than byte
1042 * representation of \u0001 (the byte 0x01.)
1043 */
1044 while (n-- > 0) {
1045 /*
1046 * n must be interpreted as chars, not bytes.
1047 * This should be called only when both strings are of
1048 * at least n chars long (no need for \0 check)
1049 */
1050 cs += TclUtfToUniChar(cs, &ch1);
1051 ct += TclUtfToUniChar(ct, &ch2);
1052 if (ch1 != ch2) {
1053 return (ch1 - ch2);
1054 }
1055 }
1056 return 0;
1057 }
1058
1059 /*
1060 *----------------------------------------------------------------------
1061 *
1062 * Tcl_UtfNcasecmp --
1063 *
1064 * Compare at most n UTF chars of string cs to string ct case
1065 * insensitive. Both cs and ct are assumed to be at least n
1066 * UTF chars long.
1067 *
1068 * Results:
1069 * Return <0 if cs < ct, 0 if cs == ct, or >0 if cs > ct.
1070 *
1071 * Side effects:
1072 * None.
1073 *
1074 *----------------------------------------------------------------------
1075 */
1076
1077 int
1078 Tcl_UtfNcasecmp(cs, ct, n)
1079 CONST char *cs; /* UTF string to compare to ct. */
1080 CONST char *ct; /* UTF string cs is compared to. */
1081 unsigned long n; /* Number of UTF chars to compare. */
1082 {
1083 Tcl_UniChar ch1, ch2;
1084 while (n-- > 0) {
1085 /*
1086 * n must be interpreted as chars, not bytes.
1087 * This should be called only when both strings are of
1088 * at least n chars long (no need for \0 check)
1089 */
1090 cs += TclUtfToUniChar(cs, &ch1);
1091 ct += TclUtfToUniChar(ct, &ch2);
1092 if (ch1 != ch2) {
1093 ch1 = Tcl_UniCharToLower(ch1);
1094 ch2 = Tcl_UniCharToLower(ch2);
1095 if (ch1 != ch2) {
1096 return (ch1 - ch2);
1097 }
1098 }
1099 }
1100 return 0;
1101 }
1102
1103 /*
1104 *----------------------------------------------------------------------
1105 *
1106 * Tcl_UniCharToUpper --
1107 *
1108 * Compute the uppercase equivalent of the given Unicode character.
1109 *
1110 * Results:
1111 * Returns the uppercase Unicode character.
1112 *
1113 * Side effects:
1114 * None.
1115 *
1116 *----------------------------------------------------------------------
1117 */
1118
1119 Tcl_UniChar
1120 Tcl_UniCharToUpper(ch)
1121 int ch; /* Unicode character to convert. */
1122 {
1123 int info = GetUniCharInfo(ch);
1124
1125 if (GetCaseType(info) & 0x04) {
1126 return (Tcl_UniChar) (ch - GetDelta(info));
1127 } else {
1128 return ch;
1129 }
1130 }
1131
1132 /*
1133 *----------------------------------------------------------------------
1134 *
1135 * Tcl_UniCharToLower --
1136 *
1137 * Compute the lowercase equivalent of the given Unicode character.
1138 *
1139 * Results:
1140 * Returns the lowercase Unicode character.
1141 *
1142 * Side effects:
1143 * None.
1144 *
1145 *----------------------------------------------------------------------
1146 */
1147
1148 Tcl_UniChar
1149 Tcl_UniCharToLower(ch)
1150 int ch; /* Unicode character to convert. */
1151 {
1152 int info = GetUniCharInfo(ch);
1153
1154 if (GetCaseType(info) & 0x02) {
1155 return (Tcl_UniChar) (ch + GetDelta(info));
1156 } else {
1157 return ch;
1158 }
1159 }
1160
1161 /*
1162 *----------------------------------------------------------------------
1163 *
1164 * Tcl_UniCharToTitle --
1165 *
1166 * Compute the titlecase equivalent of the given Unicode character.
1167 *
1168 * Results:
1169 * Returns the titlecase Unicode character.
1170 *
1171 * Side effects:
1172 * None.
1173 *
1174 *----------------------------------------------------------------------
1175 */
1176
1177 Tcl_UniChar
1178 Tcl_UniCharToTitle(ch)
1179 int ch; /* Unicode character to convert. */
1180 {
1181 int info = GetUniCharInfo(ch);
1182 int mode = GetCaseType(info);
1183
1184 if (mode & 0x1) {
1185 /*
1186 * Subtract or add one depending on the original case.
1187 */
1188
1189 return (Tcl_UniChar) (ch + ((mode & 0x4) ? -1 : 1));
1190 } else if (mode == 0x4) {
1191 return (Tcl_UniChar) (ch - GetDelta(info));
1192 } else {
1193 return ch;
1194 }
1195 }
1196
1197 /*
1198 *----------------------------------------------------------------------
1199 *
1200 * Tcl_UniCharLen --
1201 *
1202 * Find the length of a UniChar string. The str input must be null
1203 * terminated.
1204 *
1205 * Results:
1206 * Returns the length of str in UniChars (not bytes).
1207 *
1208 * Side effects:
1209 * None.
1210 *
1211 *----------------------------------------------------------------------
1212 */
1213
1214 int
1215 Tcl_UniCharLen(str)
1216 CONST Tcl_UniChar *str; /* Unicode string to find length of. */
1217 {
1218 int len = 0;
1219
1220 while (*str != '\0') {
1221 len++;
1222 str++;
1223 }
1224 return len;
1225 }
1226
1227 /*
1228 *----------------------------------------------------------------------
1229 *
1230 * Tcl_UniCharNcmp --
1231 *
1232 * Compare at most n unichars of string cs to string ct. Both cs
1233 * and ct are assumed to be at least n unichars long.
1234 *
1235 * Results:
1236 * Return <0 if cs < ct, 0 if cs == ct, or >0 if cs > ct.
1237 *
1238 * Side effects:
1239 * None.
1240 *
1241 *----------------------------------------------------------------------
1242 */
1243
1244 int
1245 Tcl_UniCharNcmp(cs, ct, n)
1246 CONST Tcl_UniChar *cs; /* Unicode string to compare to ct. */
1247 CONST Tcl_UniChar *ct; /* Unicode string cs is compared to. */
1248 unsigned long n; /* Number of unichars to compare. */
1249 {
1250 #ifdef WORDS_BIGENDIAN
1251 /*
1252 * We are definitely on a big-endian machine; memcmp() is safe
1253 */
1254 return memcmp(cs, ct, n*sizeof(Tcl_UniChar));
1255
1256 #else /* !WORDS_BIGENDIAN */
1257 /*
1258 * We can't simply call memcmp() because that is not lexically correct.
1259 */
1260 for ( ; n != 0; cs++, ct++, n--) {
1261 if (*cs != *ct) {
1262 return (*cs - *ct);
1263 }
1264 }
1265 return 0;
1266 #endif /* WORDS_BIGENDIAN */
1267 }
1268
1269 /*
1270 *----------------------------------------------------------------------
1271 *
1272 * Tcl_UniCharNcasecmp --
1273 *
1274 * Compare at most n unichars of string cs to string ct case
1275 * insensitive. Both cs and ct are assumed to be at least n
1276 * unichars long.
1277 *
1278 * Results:
1279 * Return <0 if cs < ct, 0 if cs == ct, or >0 if cs > ct.
1280 *
1281 * Side effects:
1282 * None.
1283 *
1284 *----------------------------------------------------------------------
1285 */
1286
1287 int
1288 Tcl_UniCharNcasecmp(cs, ct, n)
1289 CONST Tcl_UniChar *cs; /* Unicode string to compare to ct. */
1290 CONST Tcl_UniChar *ct; /* Unicode string cs is compared to. */
1291 unsigned long n; /* Number of unichars to compare. */
1292 {
1293 for ( ; n != 0; n--, cs++, ct++) {
1294 if (*cs != *ct) {
1295 Tcl_UniChar lcs = Tcl_UniCharToLower(*cs);
1296 Tcl_UniChar lct = Tcl_UniCharToLower(*ct);
1297 if (lcs != lct) {
1298 return (lcs - lct);
1299 }
1300 }
1301 }
1302 return 0;
1303 }
1304
1305 /*
1306 *----------------------------------------------------------------------
1307 *
1308 * Tcl_UniCharIsAlnum --
1309 *
1310 * Test if a character is an alphanumeric Unicode character.
1311 *
1312 * Results:
1313 * Returns 1 if character is alphanumeric.
1314 *
1315 * Side effects:
1316 * None.
1317 *
1318 *----------------------------------------------------------------------
1319 */
1320
1321 int
1322 Tcl_UniCharIsAlnum(ch)
1323 int ch; /* Unicode character to test. */
1324 {
1325 register int category = (GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK);
1326
1327 return (((ALPHA_BITS | DIGIT_BITS) >> category) & 1);
1328 }
1329
1330 /*
1331 *----------------------------------------------------------------------
1332 *
1333 * Tcl_UniCharIsAlpha --
1334 *
1335 * Test if a character is an alphabetic Unicode character.
1336 *
1337 * Results:
1338 * Returns 1 if character is alphabetic.
1339 *
1340 * Side effects:
1341 * None.
1342 *
1343 *----------------------------------------------------------------------
1344 */
1345
1346 int
1347 Tcl_UniCharIsAlpha(ch)
1348 int ch; /* Unicode character to test. */
1349 {
1350 register int category = (GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK);
1351 return ((ALPHA_BITS >> category) & 1);
1352 }
1353
1354 /*
1355 *----------------------------------------------------------------------
1356 *
1357 * Tcl_UniCharIsControl --
1358 *
1359 * Test if a character is a Unicode control character.
1360 *
1361 * Results:
1362 * Returns non-zero if character is a control.
1363 *
1364 * Side effects:
1365 * None.
1366 *
1367 *----------------------------------------------------------------------
1368 */
1369
1370 int
1371 Tcl_UniCharIsControl(ch)
1372 int ch; /* Unicode character to test. */
1373 {
1374 return ((GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK) == CONTROL);
1375 }
1376
1377 /*
1378 *----------------------------------------------------------------------
1379 *
1380 * Tcl_UniCharIsDigit --
1381 *
1382 * Test if a character is a numeric Unicode character.
1383 *
1384 * Results:
1385 * Returns non-zero if character is a digit.
1386 *
1387 * Side effects:
1388 * None.
1389 *
1390 *----------------------------------------------------------------------
1391 */
1392
1393 int
1394 Tcl_UniCharIsDigit(ch)
1395 int ch; /* Unicode character to test. */
1396 {
1397 return ((GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK)
1398 == DECIMAL_DIGIT_NUMBER);
1399 }
1400
1401 /*
1402 *----------------------------------------------------------------------
1403 *
1404 * Tcl_UniCharIsGraph --
1405 *
1406 * Test if a character is any Unicode print character except space.
1407 *
1408 * Results:
1409 * Returns non-zero if character is printable, but not space.
1410 *
1411 * Side effects:
1412 * None.
1413 *
1414 *----------------------------------------------------------------------
1415 */
1416
1417 int
1418 Tcl_UniCharIsGraph(ch)
1419 int ch; /* Unicode character to test. */
1420 {
1421 register int category = (GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK);
1422 return (((PRINT_BITS >> category) & 1) && ((unsigned char) ch != ' '));
1423 }
1424
1425 /*
1426 *----------------------------------------------------------------------
1427 *
1428 * Tcl_UniCharIsLower --
1429 *
1430 * Test if a character is a lowercase Unicode character.
1431 *
1432 * Results:
1433 * Returns non-zero if character is lowercase.
1434 *
1435 * Side effects:
1436 * None.
1437 *
1438 *----------------------------------------------------------------------
1439 */
1440
1441 int
1442 Tcl_UniCharIsLower(ch)
1443 int ch; /* Unicode character to test. */
1444 {
1445 return ((GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK) == LOWERCASE_LETTER);
1446 }
1447
1448 /*
1449 *----------------------------------------------------------------------
1450 *
1451 * Tcl_UniCharIsPrint --
1452 *
1453 * Test if a character is a Unicode print character.
1454 *
1455 * Results:
1456 * Returns non-zero if character is printable.
1457 *
1458 * Side effects:
1459 * None.
1460 *
1461 *----------------------------------------------------------------------
1462 */
1463
1464 int
1465 Tcl_UniCharIsPrint(ch)
1466 int ch; /* Unicode character to test. */
1467 {
1468 register int category = (GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK);
1469 return ((PRINT_BITS >> category) & 1);
1470 }
1471
1472 /*
1473 *----------------------------------------------------------------------
1474 *
1475 * Tcl_UniCharIsPunct --
1476 *
1477 * Test if a character is a Unicode punctuation character.
1478 *
1479 * Results:
1480 * Returns non-zero if character is punct.
1481 *
1482 * Side effects:
1483 * None.
1484 *
1485 *----------------------------------------------------------------------
1486 */
1487
1488 int
1489 Tcl_UniCharIsPunct(ch)
1490 int ch; /* Unicode character to test. */
1491 {
1492 register int category = (GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK);
1493 return ((PUNCT_BITS >> category) & 1);
1494 }
1495
1496 /*
1497 *----------------------------------------------------------------------
1498 *
1499 * Tcl_UniCharIsSpace --
1500 *
1501 * Test if a character is a whitespace Unicode character.
1502 *
1503 * Results:
1504 * Returns non-zero if character is a space.
1505 *
1506 * Side effects:
1507 * None.
1508 *
1509 *----------------------------------------------------------------------
1510 */
1511
1512 int
1513 Tcl_UniCharIsSpace(ch)
1514 int ch; /* Unicode character to test. */
1515 {
1516 register int category;
1517
1518 /*
1519 * If the character is within the first 127 characters, just use the
1520 * standard C function, otherwise consult the Unicode table.
1521 */
1522
1523 if (ch < 0x80) {
1524 return isspace(UCHAR(ch)); /* INTL: ISO space */
1525 } else {
1526 category = (GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK);
1527 return ((SPACE_BITS >> category) & 1);
1528 }
1529 }
1530
1531 /*
1532 *----------------------------------------------------------------------
1533 *
1534 * Tcl_UniCharIsUpper --
1535 *
1536 * Test if a character is a uppercase Unicode character.
1537 *
1538 * Results:
1539 * Returns non-zero if character is uppercase.
1540 *
1541 * Side effects:
1542 * None.
1543 *
1544 *----------------------------------------------------------------------
1545 */
1546
1547 int
1548 Tcl_UniCharIsUpper(ch)
1549 int ch; /* Unicode character to test. */
1550 {
1551 return ((GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK) == UPPERCASE_LETTER);
1552 }
1553
1554 /*
1555 *----------------------------------------------------------------------
1556 *
1557 * Tcl_UniCharIsWordChar --
1558 *
1559 * Test if a character is alphanumeric or a connector punctuation
1560 * mark.
1561 *
1562 * Results:
1563 * Returns 1 if character is a word character.
1564 *
1565 * Side effects:
1566 * None.
1567 *
1568 *----------------------------------------------------------------------
1569 */
1570
1571 int
1572 Tcl_UniCharIsWordChar(ch)
1573 int ch; /* Unicode character to test. */
1574 {
1575 register int category = (GetUniCharInfo(ch) & UNICODE_CATEGORY_MASK);
1576
1577 return (((ALPHA_BITS | DIGIT_BITS | CONNECTOR_BITS) >> category) & 1);
1578 }
1579
1580 /*
1581 *----------------------------------------------------------------------
1582 *
1583 * Tcl_UniCharCaseMatch --
1584 *
1585 * See if a particular Unicode string matches a particular pattern.
1586 * Allows case insensitivity. This is the Unicode equivalent of
1587 * the char* Tcl_StringCaseMatch. The UniChar strings must be
1588 * NULL-terminated. This has no provision for counted UniChar
1589 * strings, thus should not be used where NULLs are expected in the
1590 * UniChar string. Use TclUniCharMatch where possible.
1591 *
1592 * Results:
1593 * The return value is 1 if string matches pattern, and
1594 * 0 otherwise. The matching operation permits the following
1595 * special characters in the pattern: *?\[] (see the manual
1596 * entry for details on what these mean).
1597 *
1598 * Side effects:
1599 * None.
1600 *
1601 *----------------------------------------------------------------------
1602 */
1603
1604 int
1605 Tcl_UniCharCaseMatch(string, pattern, nocase)
1606 CONST Tcl_UniChar *string; /* Unicode String. */
1607 CONST Tcl_UniChar *pattern; /* Pattern, which may contain special
1608 * characters. */
1609 int nocase; /* 0 for case sensitive, 1 for insensitive */
1610 {
1611 Tcl_UniChar ch1, p;
1612
1613 while (1) {
1614 p = *pattern;
1615
1616 /*
1617 * See if we're at the end of both the pattern and the string. If
1618 * so, we succeeded. If we're at the end of the pattern but not at
1619 * the end of the string, we failed.
1620 */
1621
1622 if (p == 0) {
1623 return (*string == 0);
1624 }
1625 if ((*string == 0) && (p != '*')) {
1626 return 0;
1627 }
1628
1629 /*
1630 * Check for a "*" as the next pattern character. It matches any
1631 * substring. We handle this by skipping all the characters up to the
1632 * next matching one in the pattern, and then calling ourselves
1633 * recursively for each postfix of string, until either we match or we
1634 * reach the end of the string.
1635 */
1636
1637 if (p == '*') {
1638 /*
1639 * Skip all successive *'s in the pattern
1640 */
1641 while (*(++pattern) == '*') {}
1642 p = *pattern;
1643 if (p == 0) {
1644 return 1;
1645 }
1646 if (nocase) {
1647 p = Tcl_UniCharToLower(p);
1648 }
1649 while (1) {
1650 /*
1651 * Optimization for matching - cruise through the string
1652 * quickly if the next char in the pattern isn't a special
1653 * character
1654 */
1655 if ((p != '[') && (p != '?') && (p != '\\')) {
1656 if (nocase) {
1657 while (*string && (p != *string)
1658 && (p != Tcl_UniCharToLower(*string))) {
1659 string++;
1660 }
1661 } else {
1662 while (*string && (p != *string)) { string++; }
1663 }
1664 }
1665 if (Tcl_UniCharCaseMatch(string, pattern, nocase)) {
1666 return 1;
1667 }
1668 if (*string == 0) {
1669 return 0;
1670 }
1671 string++;
1672 }
1673 }
1674
1675 /*
1676 * Check for a "?" as the next pattern character. It matches
1677 * any single character.
1678 */
1679
1680 if (p == '?') {
1681 pattern++;
1682 string++;
1683 continue;
1684 }
1685
1686 /*
1687 * Check for a "[" as the next pattern character. It is followed
1688 * by a list of characters that are acceptable, or by a range
1689 * (two characters separated by "-").
1690 */
1691
1692 if (p == '[') {
1693 Tcl_UniChar startChar, endChar;
1694
1695 pattern++;
1696 ch1 = (nocase ? Tcl_UniCharToLower(*string) : *string);
1697 string++;
1698 while (1) {
1699 if ((*pattern == ']') || (*pattern == 0)) {
1700 return 0;
1701 }
1702 startChar = (nocase ? Tcl_UniCharToLower(*pattern) : *pattern);
1703 pattern++;
1704 if (*pattern == '-') {
1705 pattern++;
1706 if (*pattern == 0) {
1707 return 0;
1708 }
1709 endChar = (nocase ? Tcl_UniCharToLower(*pattern)
1710 : *pattern);
1711 pattern++;
1712 if (((startChar <= ch1) && (ch1 <= endChar))
1713 || ((endChar <= ch1) && (ch1 <= startChar))) {
1714 /*
1715 * Matches ranges of form [a-z] or [z-a].
1716 */
1717 break;
1718 }
1719 } else if (startChar == ch1) {
1720 break;
1721 }
1722 }
1723 while (*pattern != ']') {
1724 if (*pattern == 0) {
1725 pattern--;
1726 break;
1727 }
1728 pattern++;
1729 }
1730 pattern++;
1731 continue;
1732 }
1733
1734 /*
1735 * If the next pattern character is '\', just strip off the '\'
1736 * so we do exact matching on the character that follows.
1737 */
1738
1739 if (p == '\\') {
1740 if (*(++pattern) == '\0') {
1741 return 0;
1742 }
1743 }
1744
1745 /*
1746 * There's no special character. Just make sure that the next
1747 * bytes of each string match.
1748 */
1749
1750 if (nocase) {
1751 if (Tcl_UniCharToLower(*string) != Tcl_UniCharToLower(*pattern)) {
1752 return 0;
1753 }
1754 } else if (*string != *pattern) {
1755 return 0;
1756 }
1757 string++;
1758 pattern++;
1759 }
1760 }
1761
1762 /*
1763 *----------------------------------------------------------------------
1764 *
1765 * TclUniCharMatch --
1766 *
1767 * See if a particular Unicode string matches a particular pattern.
1768 * Allows case insensitivity. This is the Unicode equivalent of the
1769 * char* Tcl_StringCaseMatch. This variant of Tcl_UniCharCaseMatch
1770 * uses counted Strings, so embedded NULLs are allowed.
1771 *
1772 * Results:
1773 * The return value is 1 if string matches pattern, and
1774 * 0 otherwise. The matching operation permits the following
1775 * special characters in the pattern: *?\[] (see the manual
1776 * entry for details on what these mean).
1777 *
1778 * Side effects:
1779 * None.
1780 *
1781 *----------------------------------------------------------------------
1782 */
1783
1784 int
1785 TclUniCharMatch(string, strLen, pattern, ptnLen, nocase)
1786 CONST Tcl_UniChar *string; /* Unicode String. */
1787 int strLen; /* length of String */
1788 CONST Tcl_UniChar *pattern; /* Pattern, which may contain special
1789 * characters. */
1790 int ptnLen; /* length of Pattern */
1791 int nocase; /* 0 for case sensitive, 1 for insensitive */
1792 {
1793 CONST Tcl_UniChar *stringEnd, *patternEnd;
1794 Tcl_UniChar p;
1795
1796 stringEnd = string + strLen;
1797 patternEnd = pattern + ptnLen;
1798
1799 while (1) {
1800 /*
1801 * See if we're at the end of both the pattern and the string. If
1802 * so, we succeeded. If we're at the end of the pattern but not at
1803 * the end of the string, we failed.
1804 */
1805
1806 if (pattern == patternEnd) {
1807 return (string == stringEnd);
1808 }
1809 p = *pattern;
1810 if ((string == stringEnd) && (p != '*')) {
1811 return 0;
1812 }
1813
1814 /*
1815 * Check for a "*" as the next pattern character. It matches any
1816 * substring. We handle this by skipping all the characters up to the
1817 * next matching one in the pattern, and then calling ourselves
1818 * recursively for each postfix of string, until either we match or we
1819 * reach the end of the string.
1820 */
1821
1822 if (p == '*') {
1823 /*
1824 * Skip all successive *'s in the pattern
1825 */
1826 while (*(++pattern) == '*') {}
1827 if (pattern == patternEnd) {
1828 return 1;
1829 }
1830 p = *pattern;
1831 if (nocase) {
1832 p = Tcl_UniCharToLower(p);
1833 }
1834 while (1) {
1835 /*
1836 * Optimization for matching - cruise through the string
1837 * quickly if the next char in the pattern isn't a special
1838 * character
1839 */
1840 if ((p != '[') && (p != '?') && (p != '\\')) {
1841 if (nocase) {
1842 while ((string < stringEnd) && (p != *string)
1843 && (p != Tcl_UniCharToLower(*string))) {
1844 string++;
1845 }
1846 } else {
1847 while ((string < stringEnd) && (p != *string)) {
1848 string++;
1849 }
1850 }
1851 }
1852 if (TclUniCharMatch(string, stringEnd - string,
1853 pattern, patternEnd - pattern, nocase)) {
1854 return 1;
1855 }
1856 if (string == stringEnd) {
1857 return 0;
1858 }
1859 string++;
1860 }
1861 }
1862
1863 /*
1864 * Check for a "?" as the next pattern character. It matches
1865 * any single character.
1866 */
1867
1868 if (p == '?') {
1869 pattern++;
1870 string++;
1871 continue;
1872 }
1873
1874 /*
1875 * Check for a "[" as the next pattern character. It is followed
1876 * by a list of characters that are acceptable, or by a range
1877 * (two characters separated by "-").
1878 */
1879
1880 if (p == '[') {
1881 Tcl_UniChar ch1, startChar, endChar;
1882
1883 pattern++;
1884 ch1 = (nocase ? Tcl_UniCharToLower(*string) : *string);
1885 string++;
1886 while (1) {
1887 if ((*pattern == ']') || (pattern == patternEnd)) {
1888 return 0;
1889 }
1890 startChar = (nocase ? Tcl_UniCharToLower(*pattern) : *pattern);
1891 pattern++;
1892 if (*pattern == '-') {
1893 pattern++;
1894 if (pattern == patternEnd) {
1895 return 0;
1896 }
1897 endChar = (nocase ? Tcl_UniCharToLower(*pattern)
1898 : *pattern);
1899 pattern++;
1900 if (((startChar <= ch1) && (ch1 <= endChar))
1901 || ((endChar <= ch1) && (ch1 <= startChar))) {
1902 /*
1903 * Matches ranges of form [a-z] or [z-a].
1904 */
1905 break;
1906 }
1907 } else if (startChar == ch1) {
1908 break;
1909 }
1910 }
1911 while (*pattern != ']') {
1912 if (pattern == patternEnd) {
1913 pattern--;
1914 break;
1915 }
1916 pattern++;
1917 }
1918 pattern++;
1919 continue;
1920 }
1921
1922 /*
1923 * If the next pattern character is '\', just strip off the '\'
1924 * so we do exact matching on the character that follows.
1925 */
1926
1927 if (p == '\\') {
1928 if (++pattern == patternEnd) {
1929 return 0;
1930 }
1931 }
1932
1933 /*
1934 * There's no special character. Just make sure that the next
1935 * bytes of each string match.
1936 */
1937
1938 if (nocase) {
1939 if (Tcl_UniCharToLower(*string) != Tcl_UniCharToLower(*pattern)) {
1940 return 0;
1941 }
1942 } else if (*string != *pattern) {
1943 return 0;
1944 }
1945 string++;
1946 pattern++;
1947 }
1948 }