mstask: Use wide-char string literals.
[wine.git] / libs / wine / fold.c
blobef4e88c81c469d0fc93c0a27fadeae0531b484a2
1 /*
2 * String folding
4 * Copyright 2003 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/asm.h"
23 #ifdef __ASM_OBSOLETE
25 #include "wine/unicode.h"
27 static inline WCHAR to_unicode_digit( WCHAR ch )
29 extern const WCHAR wine_digitmap[] DECLSPEC_HIDDEN;
30 WCHAR ret = wine_digitmap[wine_digitmap[wine_digitmap[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0xf)];
31 return ret ? ret : ch;
34 static inline WCHAR to_unicode_native( WCHAR ch )
36 extern const WCHAR wine_compatmap[] DECLSPEC_HIDDEN;
37 return ch + wine_compatmap[wine_compatmap[ch >> 8] + (ch & 0xff)];
40 static const WCHAR wine_ligatures[] =
42 0x00c6, 0x00de, 0x00df, 0x00e6, 0x00fe, 0x0132, 0x0133, 0x0152,
43 0x0153, 0x01c4, 0x01c5, 0x01c6, 0x01c7, 0x01c8, 0x01c9, 0x01ca,
44 0x01cb, 0x01cc, 0x01e2, 0x01e3, 0x01f1, 0x01f2, 0x01f3, 0x01fc,
45 0x01fd, 0x05f0, 0x05f1, 0x05f2, 0xfb00, 0xfb01, 0xfb02, 0xfb03,
46 0xfb04, 0xfb05, 0xfb06
49 /* Unicode expanded ligatures */
50 static const WCHAR wine_expanded_ligatures[][4] =
52 { 'A','E','\0',1 },
53 { 'T','H','\0',1 },
54 { 's','s','\0',1 },
55 { 'a','e','\0',1 },
56 { 't','h','\0',1 },
57 { 'I','J','\0',1 },
58 { 'i','j','\0',1 },
59 { 'O','E','\0',1 },
60 { 'o','e','\0',1 },
61 { 'D',0x017d,'\0',1 },
62 { 'D',0x017e,'\0',1 },
63 { 'd',0x017e,'\0',1 },
64 { 'L','J','\0',1 },
65 { 'L','j','\0',1 },
66 { 'l','j','\0',1 },
67 { 'N','J','\0',1 },
68 { 'N','j','\0',1 },
69 { 'n','j','\0',1 },
70 { 0x0100,0x0112,'\0',1 },
71 { 0x0101,0x0113,'\0',1 },
72 { 'D','Z','\0',1 },
73 { 'D','z','\0',1 },
74 { 'd','z','\0',1 },
75 { 0x00c1,0x00c9,'\0',1 },
76 { 0x00e1,0x00e9,'\0',1 },
77 { 0x05d5,0x05d5,'\0',1 },
78 { 0x05d5,0x05d9,'\0',1 },
79 { 0x05d9,0x05d9,'\0',1 },
80 { 'f','f','\0',1 },
81 { 'f','i','\0',1 },
82 { 'f','l','\0',1 },
83 { 'f','f','i',2 },
84 { 'f','f','l',2 },
85 { 0x017f,'t','\0',1 },
86 { 's','t','\0',1 }
89 static inline int get_ligature_len( WCHAR wc )
91 int low = 0, high = sizeof(wine_ligatures)/sizeof(WCHAR) -1;
92 while (low <= high)
94 int pos = (low + high) / 2;
95 if (wine_ligatures[pos] < wc)
96 low = pos + 1;
97 else if (wine_ligatures[pos] > wc)
98 high = pos - 1;
99 else
100 return wine_expanded_ligatures[pos][3];
102 return 0;
105 static inline const WCHAR* get_ligature( WCHAR wc )
107 static const WCHAR empty_ligature[] = { '\0','\0','\0', 0 };
108 int low = 0, high = sizeof(wine_ligatures)/sizeof(WCHAR) -1;
109 while (low <= high)
111 int pos = (low + high) / 2;
112 if (wine_ligatures[pos] < wc)
113 low = pos + 1;
114 else if (wine_ligatures[pos] > wc)
115 high = pos - 1;
116 else
117 return wine_expanded_ligatures[pos];
119 return empty_ligature;
122 /* fold a unicode string */
123 int wine_fold_string_obsolete( int flags, const WCHAR *src, int srclen, WCHAR *dst, int dstlen )
125 WCHAR *dstbase = dst;
126 const WCHAR *expand;
127 int i;
129 if (srclen == -1)
130 srclen = strlenW(src) + 1; /* Include terminating NUL in count */
132 if (!dstlen)
134 /* Calculate the required size for dst */
135 dstlen = srclen;
137 if (flags & MAP_EXPAND_LIGATURES)
139 while (srclen--)
141 dstlen += get_ligature_len(*src);
142 src++;
145 else if (flags & MAP_COMPOSITE)
147 /* FIXME */
149 else if (flags & MAP_PRECOMPOSED)
151 /* FIXME */
153 return dstlen;
156 if (srclen > dstlen)
157 return 0;
159 dstlen -= srclen;
161 /* Actually perform the mapping(s) specified */
162 for (i = 0; i < srclen; i++)
164 WCHAR ch = *src;
166 if (flags & MAP_EXPAND_LIGATURES)
168 expand = get_ligature(ch);
169 if (expand[0])
171 if (!dstlen--)
172 return 0;
173 dst[0] = expand[0];
174 if (expand[2])
176 if (!dstlen--)
177 return 0;
178 *++dst = expand[1];
179 ch = expand[2];
181 else
182 ch = expand[1];
183 dst++;
186 else if (flags & MAP_COMPOSITE)
188 /* FIXME */
190 else if (flags & MAP_PRECOMPOSED)
192 /* FIXME */
194 if (flags & MAP_FOLDDIGITS)
195 ch = to_unicode_digit(ch);
196 if (flags & MAP_FOLDCZONE)
197 ch = to_unicode_native(ch);
199 *dst++ = ch;
200 src++;
202 return dst - dstbase;
205 __ASM_OBSOLETE(wine_fold_string);
207 #endif /* __ASM_OBSOLETE */