2 * Part of the D programming language runtime library.
6 * Copyright (C) 2004-2006 by Digital Mars, www.digitalmars.com
7 * Written by Walter Bright
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.
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
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
29 * Modified by Sean Kelly for use with the D Runtime Project
34 /* This code handles decoding UTF strings for foreach loops.
35 * There are 6 combinations of conversions between char, wchar,
36 * and dchar, and 2 of each of those.
39 private import util.utf;
41 /**********************************************
44 // dg is D, but _aApplycd() is C
45 extern (D) typedef int delegate(void *) dg_t;
47 extern (C) int _aApplycd1(char[] aa, dg_t dg)
50 size_t len = aa.length;
52 debug(apply) printf("_aApplycd1(), len = %d\n", len);
53 for (i = 0; i < len; )
61 result = dg(cast(void *)&d);
68 extern (C) int _aApplywd1(wchar[] aa, dg_t dg)
71 size_t len = aa.length;
73 debug(apply) printf("_aApplywd1(), len = %d\n", len);
74 for (i = 0; i < len; )
82 result = dg(cast(void *)&d);
89 extern (C) int _aApplycw1(char[] aa, dg_t dg)
92 size_t len = aa.length;
94 debug(apply) printf("_aApplycw1(), len = %d\n", len);
95 for (i = 0; i < len; )
106 w = cast(wchar)((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
107 result = dg(cast(void *)&w);
110 w = cast(wchar)(((d - 0x10000) & 0x3FF) + 0xDC00);
115 result = dg(cast(void *)&w);
122 extern (C) int _aApplywc1(wchar[] aa, dg_t dg)
125 size_t len = aa.length;
127 debug(apply) printf("_aApplywc1(), len = %d\n", len);
128 for (i = 0; i < len; )
139 auto b = toUTF8(buf, d);
142 result = dg(cast(void *)&c2);
152 result = dg(cast(void *)&c);
159 extern (C) int _aApplydc1(dchar[] aa, dg_t dg)
162 debug(apply) printf("_aApplydc1(), len = %d\n", aa.length);
163 foreach (dchar d; aa)
171 auto b = toUTF8(buf, d);
174 result = dg(cast(void *)&c2);
184 result = dg(cast(void *)&c);
191 extern (C) int _aApplydw1(dchar[] aa, dg_t dg)
194 debug(apply) printf("_aApplydw1(), len = %d\n", aa.length);
195 foreach (dchar d; aa)
203 w = cast(wchar)((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
204 result = dg(cast(void *)&w);
207 w = cast(wchar)(((d - 0x10000) & 0x3FF) + 0xDC00);
209 result = dg(cast(void *)&w);
217 /****************************************************************************/
219 // dg is D, but _aApplycd2() is C
220 extern (D) typedef int delegate(void *, void *) dg2_t;
222 extern (C) int _aApplycd2(char[] aa, dg2_t dg)
226 size_t len = aa.length;
228 debug(apply) printf("_aApplycd2(), len = %d\n", len);
229 for (i = 0; i < len; i += n)
241 result = dg(&i, cast(void *)&d);
248 extern (C) int _aApplywd2(wchar[] aa, dg2_t dg)
252 size_t len = aa.length;
254 debug(apply) printf("_aApplywd2(), len = %d\n", len);
255 for (i = 0; i < len; i += n)
267 result = dg(&i, cast(void *)&d);
274 extern (C) int _aApplycw2(char[] aa, dg2_t dg)
278 size_t len = aa.length;
280 debug(apply) printf("_aApplycw2(), len = %d\n", len);
281 for (i = 0; i < len; i += n)
294 w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
295 result = dg(&i, cast(void *)&w);
298 w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00);
303 result = dg(&i, cast(void *)&w);
310 extern (C) int _aApplywc2(wchar[] aa, dg2_t dg)
314 size_t len = aa.length;
316 debug(apply) printf("_aApplywc2(), len = %d\n", len);
317 for (i = 0; i < len; i += n)
330 auto b = toUTF8(buf, d);
333 result = dg(&i, cast(void *)&c2);
343 result = dg(&i, cast(void *)&c);
350 extern (C) int _aApplydc2(dchar[] aa, dg2_t dg)
353 size_t len = aa.length;
355 debug(apply) printf("_aApplydc2(), len = %d\n", len);
356 for (i = 0; i < len; i++)
365 auto b = toUTF8(buf, d);
368 result = dg(&i, cast(void *)&c2);
377 result = dg(&i, cast(void *)&c);
384 extern (C) int _aApplydw2(dchar[] aa, dg2_t dg)
387 debug(apply) printf("_aApplydw2(), len = %d\n", aa.length);
388 foreach (size_t i, dchar d; aa)
397 w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
398 result = dg(&j, cast(void *)&w);
401 w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00);
403 result = dg(&j, cast(void *)&w);