132
|
1 /**
|
|
2 * Part of the D programming language runtime library.
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * Copyright (C) 2004-2006 by Digital Mars, www.digitalmars.com
|
|
7 * Written by Walter Bright
|
|
8 *
|
|
9 * This software is provided 'as-is', without any express or implied
|
|
10 * warranty. In no event will the authors be held liable for any damages
|
|
11 * arising from the use of this software.
|
|
12 *
|
|
13 * Permission is granted to anyone to use this software for any purpose,
|
|
14 * including commercial applications, and to alter it and redistribute it
|
|
15 * freely, in both source and binary form, subject to the following
|
|
16 * restrictions:
|
|
17 *
|
|
18 * o The origin of this software must not be misrepresented; you must not
|
|
19 * claim that you wrote the original software. If you use this software
|
|
20 * in a product, an acknowledgment in the product documentation would be
|
|
21 * appreciated but is not required.
|
|
22 * o Altered source versions must be plainly marked as such, and must not
|
|
23 * be misrepresented as being the original software.
|
|
24 * o This notice may not be removed or altered from any source
|
|
25 * distribution.
|
|
26 */
|
|
27
|
|
28 /*
|
|
29 * Modified by Sean Kelly <sean@f4.ca> for use with Tango.
|
|
30 */
|
|
31
|
|
32 /* This code handles decoding UTF strings for foreach loops.
|
|
33 * There are 6 combinations of conversions between char, wchar,
|
|
34 * and dchar, and 2 of each of those.
|
|
35 */
|
|
36
|
|
37 private import util.utf;
|
|
38
|
|
39 /**********************************************
|
|
40 */
|
|
41
|
|
42 // dg is D, but _aApplycd() is C
|
|
43 extern (D) typedef int delegate(void *) dg_t;
|
|
44
|
|
45 extern (C) int _aApplycd1(char[] aa, dg_t dg)
|
|
46 { int result;
|
|
47 size_t i;
|
|
48 size_t len = aa.length;
|
|
49
|
|
50 debug(apply) printf("_aApplycd1(), len = %d\n", len);
|
|
51 for (i = 0; i < len; )
|
|
52 { dchar d;
|
|
53
|
|
54 d = aa[i];
|
|
55 if (d & 0x80)
|
|
56 d = decode(aa, i);
|
|
57 else
|
|
58 i++;
|
|
59 result = dg(cast(void *)&d);
|
|
60 if (result)
|
|
61 break;
|
|
62 }
|
|
63 return result;
|
|
64 }
|
|
65
|
|
66 extern (C) int _aApplywd1(wchar[] aa, dg_t dg)
|
|
67 { int result;
|
|
68 size_t i;
|
|
69 size_t len = aa.length;
|
|
70
|
|
71 debug(apply) printf("_aApplywd1(), len = %d\n", len);
|
|
72 for (i = 0; i < len; )
|
|
73 { dchar d;
|
|
74
|
|
75 d = aa[i];
|
|
76 if (d & ~0x7F)
|
|
77 d = decode(aa, i);
|
|
78 else
|
|
79 i++;
|
|
80 result = dg(cast(void *)&d);
|
|
81 if (result)
|
|
82 break;
|
|
83 }
|
|
84 return result;
|
|
85 }
|
|
86
|
|
87 extern (C) int _aApplycw1(char[] aa, dg_t dg)
|
|
88 { int result;
|
|
89 size_t i;
|
|
90 size_t len = aa.length;
|
|
91
|
|
92 debug(apply) printf("_aApplycw1(), len = %d\n", len);
|
|
93 for (i = 0; i < len; )
|
|
94 { dchar d;
|
|
95 wchar w;
|
|
96
|
|
97 w = aa[i];
|
|
98 if (w & 0x80)
|
|
99 { d = decode(aa, i);
|
|
100 if (d <= 0xFFFF)
|
|
101 w = cast(wchar) d;
|
|
102 else
|
|
103 {
|
|
104 w = cast(wchar)((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
|
|
105 result = dg(cast(void *)&w);
|
|
106 if (result)
|
|
107 break;
|
|
108 w = cast(wchar)(((d - 0x10000) & 0x3FF) + 0xDC00);
|
|
109 }
|
|
110 }
|
|
111 else
|
|
112 i++;
|
|
113 result = dg(cast(void *)&w);
|
|
114 if (result)
|
|
115 break;
|
|
116 }
|
|
117 return result;
|
|
118 }
|
|
119
|
|
120 extern (C) int _aApplywc1(wchar[] aa, dg_t dg)
|
|
121 { int result;
|
|
122 size_t i;
|
|
123 size_t len = aa.length;
|
|
124
|
|
125 debug(apply) printf("_aApplywc1(), len = %d\n", len);
|
|
126 for (i = 0; i < len; )
|
|
127 { dchar d;
|
|
128 wchar w;
|
|
129 char c;
|
|
130
|
|
131 w = aa[i];
|
|
132 if (w & ~0x7F)
|
|
133 {
|
|
134 char[4] buf;
|
|
135
|
|
136 d = decode(aa, i);
|
|
137 auto b = toUTF8(buf, d);
|
|
138 foreach (char c2; b)
|
|
139 {
|
|
140 result = dg(cast(void *)&c2);
|
|
141 if (result)
|
|
142 return result;
|
|
143 }
|
|
144 continue;
|
|
145 }
|
|
146 else
|
|
147 { c = cast(char)w;
|
|
148 i++;
|
|
149 }
|
|
150 result = dg(cast(void *)&c);
|
|
151 if (result)
|
|
152 break;
|
|
153 }
|
|
154 return result;
|
|
155 }
|
|
156
|
|
157 extern (C) int _aApplydc1(dchar[] aa, dg_t dg)
|
|
158 { int result;
|
|
159
|
|
160 debug(apply) printf("_aApplydc1(), len = %d\n", aa.length);
|
|
161 foreach (dchar d; aa)
|
|
162 {
|
|
163 char c;
|
|
164
|
|
165 if (d & ~0x7F)
|
|
166 {
|
|
167 char[4] buf;
|
|
168
|
|
169 auto b = toUTF8(buf, d);
|
|
170 foreach (char c2; b)
|
|
171 {
|
|
172 result = dg(cast(void *)&c2);
|
|
173 if (result)
|
|
174 return result;
|
|
175 }
|
|
176 continue;
|
|
177 }
|
|
178 else
|
|
179 {
|
|
180 c = cast(char)d;
|
|
181 }
|
|
182 result = dg(cast(void *)&c);
|
|
183 if (result)
|
|
184 break;
|
|
185 }
|
|
186 return result;
|
|
187 }
|
|
188
|
|
189 extern (C) int _aApplydw1(dchar[] aa, dg_t dg)
|
|
190 { int result;
|
|
191
|
|
192 debug(apply) printf("_aApplydw1(), len = %d\n", aa.length);
|
|
193 foreach (dchar d; aa)
|
|
194 {
|
|
195 wchar w;
|
|
196
|
|
197 if (d <= 0xFFFF)
|
|
198 w = cast(wchar) d;
|
|
199 else
|
|
200 {
|
|
201 w = cast(wchar)((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
|
|
202 result = dg(cast(void *)&w);
|
|
203 if (result)
|
|
204 break;
|
|
205 w = cast(wchar)(((d - 0x10000) & 0x3FF) + 0xDC00);
|
|
206 }
|
|
207 result = dg(cast(void *)&w);
|
|
208 if (result)
|
|
209 break;
|
|
210 }
|
|
211 return result;
|
|
212 }
|
|
213
|
|
214
|
|
215 /****************************************************************************/
|
|
216
|
|
217 // dg is D, but _aApplycd2() is C
|
|
218 extern (D) typedef int delegate(void *, void *) dg2_t;
|
|
219
|
|
220 extern (C) int _aApplycd2(char[] aa, dg2_t dg)
|
|
221 { int result;
|
|
222 size_t i;
|
|
223 size_t n;
|
|
224 size_t len = aa.length;
|
|
225
|
|
226 debug(apply) printf("_aApplycd2(), len = %d\n", len);
|
|
227 for (i = 0; i < len; i += n)
|
|
228 { dchar d;
|
|
229
|
|
230 d = aa[i];
|
|
231 if (d & 0x80)
|
|
232 {
|
|
233 n = i;
|
|
234 d = decode(aa, n);
|
|
235 n -= i;
|
|
236 }
|
|
237 else
|
|
238 n = 1;
|
|
239 result = dg(&i, cast(void *)&d);
|
|
240 if (result)
|
|
241 break;
|
|
242 }
|
|
243 return result;
|
|
244 }
|
|
245
|
|
246 extern (C) int _aApplywd2(wchar[] aa, dg2_t dg)
|
|
247 { int result;
|
|
248 size_t i;
|
|
249 size_t n;
|
|
250 size_t len = aa.length;
|
|
251
|
|
252 debug(apply) printf("_aApplywd2(), len = %d\n", len);
|
|
253 for (i = 0; i < len; i += n)
|
|
254 { dchar d;
|
|
255
|
|
256 d = aa[i];
|
|
257 if (d & ~0x7F)
|
|
258 {
|
|
259 n = i;
|
|
260 d = decode(aa, n);
|
|
261 n -= i;
|
|
262 }
|
|
263 else
|
|
264 n = 1;
|
|
265 result = dg(&i, cast(void *)&d);
|
|
266 if (result)
|
|
267 break;
|
|
268 }
|
|
269 return result;
|
|
270 }
|
|
271
|
|
272 extern (C) int _aApplycw2(char[] aa, dg2_t dg)
|
|
273 { int result;
|
|
274 size_t i;
|
|
275 size_t n;
|
|
276 size_t len = aa.length;
|
|
277
|
|
278 debug(apply) printf("_aApplycw2(), len = %d\n", len);
|
|
279 for (i = 0; i < len; i += n)
|
|
280 { dchar d;
|
|
281 wchar w;
|
|
282
|
|
283 w = aa[i];
|
|
284 if (w & 0x80)
|
|
285 { n = i;
|
|
286 d = decode(aa, n);
|
|
287 n -= i;
|
|
288 if (d <= 0xFFFF)
|
|
289 w = cast(wchar) d;
|
|
290 else
|
|
291 {
|
|
292 w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
|
|
293 result = dg(&i, cast(void *)&w);
|
|
294 if (result)
|
|
295 break;
|
|
296 w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00);
|
|
297 }
|
|
298 }
|
|
299 else
|
|
300 n = 1;
|
|
301 result = dg(&i, cast(void *)&w);
|
|
302 if (result)
|
|
303 break;
|
|
304 }
|
|
305 return result;
|
|
306 }
|
|
307
|
|
308 extern (C) int _aApplywc2(wchar[] aa, dg2_t dg)
|
|
309 { int result;
|
|
310 size_t i;
|
|
311 size_t n;
|
|
312 size_t len = aa.length;
|
|
313
|
|
314 debug(apply) printf("_aApplywc2(), len = %d\n", len);
|
|
315 for (i = 0; i < len; i += n)
|
|
316 { dchar d;
|
|
317 wchar w;
|
|
318 char c;
|
|
319
|
|
320 w = aa[i];
|
|
321 if (w & ~0x7F)
|
|
322 {
|
|
323 char[4] buf;
|
|
324
|
|
325 n = i;
|
|
326 d = decode(aa, n);
|
|
327 n -= i;
|
|
328 auto b = toUTF8(buf, d);
|
|
329 foreach (char c2; b)
|
|
330 {
|
|
331 result = dg(&i, cast(void *)&c2);
|
|
332 if (result)
|
|
333 return result;
|
|
334 }
|
|
335 continue;
|
|
336 }
|
|
337 else
|
|
338 { c = cast(char)w;
|
|
339 n = 1;
|
|
340 }
|
|
341 result = dg(&i, cast(void *)&c);
|
|
342 if (result)
|
|
343 break;
|
|
344 }
|
|
345 return result;
|
|
346 }
|
|
347
|
|
348 extern (C) int _aApplydc2(dchar[] aa, dg2_t dg)
|
|
349 { int result;
|
|
350 size_t i;
|
|
351 size_t len = aa.length;
|
|
352
|
|
353 debug(apply) printf("_aApplydc2(), len = %d\n", len);
|
|
354 for (i = 0; i < len; i++)
|
|
355 { dchar d;
|
|
356 char c;
|
|
357
|
|
358 d = aa[i];
|
|
359 if (d & ~0x7F)
|
|
360 {
|
|
361 char[4] buf;
|
|
362
|
|
363 auto b = toUTF8(buf, d);
|
|
364 foreach (char c2; b)
|
|
365 {
|
|
366 result = dg(&i, cast(void *)&c2);
|
|
367 if (result)
|
|
368 return result;
|
|
369 }
|
|
370 continue;
|
|
371 }
|
|
372 else
|
|
373 { c = cast(char)d;
|
|
374 }
|
|
375 result = dg(&i, cast(void *)&c);
|
|
376 if (result)
|
|
377 break;
|
|
378 }
|
|
379 return result;
|
|
380 }
|
|
381
|
|
382 extern (C) int _aApplydw2(dchar[] aa, dg2_t dg)
|
|
383 { int result;
|
|
384
|
|
385 debug(apply) printf("_aApplydw2(), len = %d\n", aa.length);
|
|
386 foreach (size_t i, dchar d; aa)
|
|
387 {
|
|
388 wchar w;
|
|
389 auto j = i;
|
|
390
|
|
391 if (d <= 0xFFFF)
|
|
392 w = cast(wchar) d;
|
|
393 else
|
|
394 {
|
|
395 w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
|
|
396 result = dg(&j, cast(void *)&w);
|
|
397 if (result)
|
|
398 break;
|
|
399 w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00);
|
|
400 }
|
|
401 result = dg(&j, cast(void *)&w);
|
|
402 if (result)
|
|
403 break;
|
|
404 }
|
|
405 return result;
|
|
406 }
|