1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
-
+
|
/*
* tclUtf.c --
*
* Routines for manipulating UTF-8 strings.
*
* Copyright (c) 1997-1998 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclUtf.c,v 1.2 1999/04/16 00:46:55 stanton Exp $
* RCS: @(#) $Id: tclUtf.c,v 1.3 1999/04/29 00:04:41 hershey Exp $
*/
#include "tclInt.h"
/*
* Include the static character classification tables and macros.
*/
|
| ︙ | | |
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
#if TCL_UTF_MAX > 5
6,6,6,6
#else
1,1,1,1
#endif
};
/*
* Procedures used only in this module.
*/
static int UtfCount _ANSI_ARGS_((int ch));
/*
*---------------------------------------------------------------------------
*
* Tcl_UniCharToUtf --
*
* Store the given Tcl_UniChar as a sequence of UTF-8 bytes in the
* Utf character "ch".
*
* Results:
* The return values is the number of bytes in the Utf character "ch".
*
* Side effects:
* None.
*
*---------------------------------------------------------------------------
*/
static int
UtfCount(ch)
int ch; /* The Tcl_UniChar whose size is returned. */
{
if ((ch > 0) && (ch < UNICODE_SELF)) {
return 1;
}
if (ch <= 0x7FF) {
return 2;
}
if (ch <= 0xFFFF) {
return 3;
}
#if TCL_UTF_MAX > 3
if (ch <= 0x1FFFFF) {
return 4;
}
if (ch <= 0x3FFFFFF) {
return 5;
}
if (ch <= 0x7FFFFFFF) {
return 6;
}
#endif
return 3;
}
/*
*---------------------------------------------------------------------------
*
* Tcl_UniCharToUtf --
*
* Store the given Tcl_UniChar as a sequence of UTF-8 bytes in the
* provided buffer. Equivalent to Plan 9 runetochar().
|
| ︙ | | |
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
|
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
|
-
+
+
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
*----------------------------------------------------------------------
*/
int
Tcl_UtfToUpper(str)
char *str; /* String to convert in place. */
{
Tcl_UniChar ch;
Tcl_UniChar ch, upChar;
char *src, *dst;
int bytes;
/*
* Iterate over the string until we hit the terminating null.
*/
src = dst = str;
while (*src) {
src += Tcl_UtfToUniChar(src, &ch);
dst += Tcl_UniCharToUtf(Tcl_UniCharToUpper(ch), dst);
bytes = Tcl_UtfToUniChar(src, &ch);
upChar = Tcl_UniCharToUpper(ch);
/*
* 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 (bytes < UtfCount(upChar)) {
memcpy(dst, src, (size_t) bytes);
dst += bytes;
} else {
dst += Tcl_UniCharToUtf(upChar, dst);
}
src += bytes;
}
*dst = '\0';
return (dst - str);
}
/*
*----------------------------------------------------------------------
|
| ︙ | | |
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
|
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
|
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
*----------------------------------------------------------------------
*/
int
Tcl_UtfToLower(str)
char *str; /* String to convert in place. */
{
Tcl_UniChar ch;
Tcl_UniChar ch, lowChar;
char *src, *dst;
int bytes;
/*
* Iterate over the string until we hit the terminating null.
*/
src = dst = str;
while (*src) {
src += Tcl_UtfToUniChar(src, &ch);
dst += Tcl_UniCharToUtf(Tcl_UniCharToLower(ch), dst);
bytes = Tcl_UtfToUniChar(src, &ch);
lowChar = Tcl_UniCharToLower(ch);
/*
* 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 (bytes < UtfCount(lowChar)) {
memcpy(dst, src, (size_t) bytes);
dst += bytes;
} else {
dst += Tcl_UniCharToUtf(lowChar, dst);
}
src += bytes;
}
*dst = '\0';
return (dst - str);
}
/*
*----------------------------------------------------------------------
|
| ︙ | | |