comparison druntime/src/compiler/dmd/aApply.d @ 1458:e0b2d67cfe7c

Added druntime (this should be removed once it works).
author Robert Clipsham <robert@octarineparrot.com>
date Tue, 02 Jun 2009 17:43:06 +0100
parents
children
comparison
equal deleted inserted replaced
1456:7b218ec1044f 1458:e0b2d67cfe7c
1 /**
2 * This code handles decoding UTF strings for foreach loops. There are 6
3 * combinations of conversions between char, wchar, and dchar, and 2 of each
4 * of those.
5 *
6 * Copyright: Copyright Digital Mars 2004 - 2009.
7 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>.
8 * Authors: Walter Bright
9 *
10 * Copyright Digital Mars 2004 - 2009.
11 * Distributed under the Boost Software License, Version 1.0.
12 * (See accompanying file LICENSE_1_0.txt or copy at
13 * http://www.boost.org/LICENSE_1_0.txt)
14 */
15 module rt.aApply;
16
17 private import rt.util.utf;
18
19 /**********************************************
20 */
21
22 // dg is D, but _aApplycd() is C
23 extern (D) typedef int delegate(void *) dg_t;
24
25 extern (C) int _aApplycd1(char[] aa, dg_t dg)
26 { int result;
27 size_t i;
28 size_t len = aa.length;
29
30 debug(apply) printf("_aApplycd1(), len = %d\n", len);
31 for (i = 0; i < len; )
32 { dchar d;
33
34 d = aa[i];
35 if (d & 0x80)
36 d = decode(aa, i);
37 else
38 i++;
39 result = dg(cast(void *)&d);
40 if (result)
41 break;
42 }
43 return result;
44 }
45
46 extern (C) int _aApplywd1(wchar[] aa, dg_t dg)
47 { int result;
48 size_t i;
49 size_t len = aa.length;
50
51 debug(apply) printf("_aApplywd1(), len = %d\n", len);
52 for (i = 0; i < len; )
53 { dchar d;
54
55 d = aa[i];
56 if (d & ~0x7F)
57 d = decode(aa, i);
58 else
59 i++;
60 result = dg(cast(void *)&d);
61 if (result)
62 break;
63 }
64 return result;
65 }
66
67 extern (C) int _aApplycw1(char[] aa, dg_t dg)
68 { int result;
69 size_t i;
70 size_t len = aa.length;
71
72 debug(apply) printf("_aApplycw1(), len = %d\n", len);
73 for (i = 0; i < len; )
74 { dchar d;
75 wchar w;
76
77 w = aa[i];
78 if (w & 0x80)
79 { d = decode(aa, i);
80 if (d <= 0xFFFF)
81 w = cast(wchar) d;
82 else
83 {
84 w = cast(wchar)((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
85 result = dg(cast(void *)&w);
86 if (result)
87 break;
88 w = cast(wchar)(((d - 0x10000) & 0x3FF) + 0xDC00);
89 }
90 }
91 else
92 i++;
93 result = dg(cast(void *)&w);
94 if (result)
95 break;
96 }
97 return result;
98 }
99
100 extern (C) int _aApplywc1(wchar[] aa, dg_t dg)
101 { int result;
102 size_t i;
103 size_t len = aa.length;
104
105 debug(apply) printf("_aApplywc1(), len = %d\n", len);
106 for (i = 0; i < len; )
107 { dchar d;
108 wchar w;
109 char c;
110
111 w = aa[i];
112 if (w & ~0x7F)
113 {
114 char[4] buf;
115
116 d = decode(aa, i);
117 auto b = toUTF8(buf, d);
118 foreach (char c2; b)
119 {
120 result = dg(cast(void *)&c2);
121 if (result)
122 return result;
123 }
124 continue;
125 }
126 else
127 { c = cast(char)w;
128 i++;
129 }
130 result = dg(cast(void *)&c);
131 if (result)
132 break;
133 }
134 return result;
135 }
136
137 extern (C) int _aApplydc1(dchar[] aa, dg_t dg)
138 { int result;
139
140 debug(apply) printf("_aApplydc1(), len = %d\n", aa.length);
141 foreach (dchar d; aa)
142 {
143 char c;
144
145 if (d & ~0x7F)
146 {
147 char[4] buf;
148
149 auto b = toUTF8(buf, d);
150 foreach (char c2; b)
151 {
152 result = dg(cast(void *)&c2);
153 if (result)
154 return result;
155 }
156 continue;
157 }
158 else
159 {
160 c = cast(char)d;
161 }
162 result = dg(cast(void *)&c);
163 if (result)
164 break;
165 }
166 return result;
167 }
168
169 extern (C) int _aApplydw1(dchar[] aa, dg_t dg)
170 { int result;
171
172 debug(apply) printf("_aApplydw1(), len = %d\n", aa.length);
173 foreach (dchar d; aa)
174 {
175 wchar w;
176
177 if (d <= 0xFFFF)
178 w = cast(wchar) d;
179 else
180 {
181 w = cast(wchar)((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
182 result = dg(cast(void *)&w);
183 if (result)
184 break;
185 w = cast(wchar)(((d - 0x10000) & 0x3FF) + 0xDC00);
186 }
187 result = dg(cast(void *)&w);
188 if (result)
189 break;
190 }
191 return result;
192 }
193
194
195 /****************************************************************************/
196
197 // dg is D, but _aApplycd2() is C
198 extern (D) typedef int delegate(void *, void *) dg2_t;
199
200 extern (C) int _aApplycd2(char[] aa, dg2_t dg)
201 { int result;
202 size_t i;
203 size_t n;
204 size_t len = aa.length;
205
206 debug(apply) printf("_aApplycd2(), len = %d\n", len);
207 for (i = 0; i < len; i += n)
208 { dchar d;
209
210 d = aa[i];
211 if (d & 0x80)
212 {
213 n = i;
214 d = decode(aa, n);
215 n -= i;
216 }
217 else
218 n = 1;
219 result = dg(&i, cast(void *)&d);
220 if (result)
221 break;
222 }
223 return result;
224 }
225
226 extern (C) int _aApplywd2(wchar[] aa, dg2_t dg)
227 { int result;
228 size_t i;
229 size_t n;
230 size_t len = aa.length;
231
232 debug(apply) printf("_aApplywd2(), len = %d\n", len);
233 for (i = 0; i < len; i += n)
234 { dchar d;
235
236 d = aa[i];
237 if (d & ~0x7F)
238 {
239 n = i;
240 d = decode(aa, n);
241 n -= i;
242 }
243 else
244 n = 1;
245 result = dg(&i, cast(void *)&d);
246 if (result)
247 break;
248 }
249 return result;
250 }
251
252 extern (C) int _aApplycw2(char[] aa, dg2_t dg)
253 { int result;
254 size_t i;
255 size_t n;
256 size_t len = aa.length;
257
258 debug(apply) printf("_aApplycw2(), len = %d\n", len);
259 for (i = 0; i < len; i += n)
260 { dchar d;
261 wchar w;
262
263 w = aa[i];
264 if (w & 0x80)
265 { n = i;
266 d = decode(aa, n);
267 n -= i;
268 if (d <= 0xFFFF)
269 w = cast(wchar) d;
270 else
271 {
272 w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
273 result = dg(&i, cast(void *)&w);
274 if (result)
275 break;
276 w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00);
277 }
278 }
279 else
280 n = 1;
281 result = dg(&i, cast(void *)&w);
282 if (result)
283 break;
284 }
285 return result;
286 }
287
288 extern (C) int _aApplywc2(wchar[] aa, dg2_t dg)
289 { int result;
290 size_t i;
291 size_t n;
292 size_t len = aa.length;
293
294 debug(apply) printf("_aApplywc2(), len = %d\n", len);
295 for (i = 0; i < len; i += n)
296 { dchar d;
297 wchar w;
298 char c;
299
300 w = aa[i];
301 if (w & ~0x7F)
302 {
303 char[4] buf;
304
305 n = i;
306 d = decode(aa, n);
307 n -= i;
308 auto b = toUTF8(buf, d);
309 foreach (char c2; b)
310 {
311 result = dg(&i, cast(void *)&c2);
312 if (result)
313 return result;
314 }
315 continue;
316 }
317 else
318 { c = cast(char)w;
319 n = 1;
320 }
321 result = dg(&i, cast(void *)&c);
322 if (result)
323 break;
324 }
325 return result;
326 }
327
328 extern (C) int _aApplydc2(dchar[] aa, dg2_t dg)
329 { int result;
330 size_t i;
331 size_t len = aa.length;
332
333 debug(apply) printf("_aApplydc2(), len = %d\n", len);
334 for (i = 0; i < len; i++)
335 { dchar d;
336 char c;
337
338 d = aa[i];
339 if (d & ~0x7F)
340 {
341 char[4] buf;
342
343 auto b = toUTF8(buf, d);
344 foreach (char c2; b)
345 {
346 result = dg(&i, cast(void *)&c2);
347 if (result)
348 return result;
349 }
350 continue;
351 }
352 else
353 { c = cast(char)d;
354 }
355 result = dg(&i, cast(void *)&c);
356 if (result)
357 break;
358 }
359 return result;
360 }
361
362 extern (C) int _aApplydw2(dchar[] aa, dg2_t dg)
363 { int result;
364
365 debug(apply) printf("_aApplydw2(), len = %d\n", aa.length);
366 foreach (size_t i, dchar d; aa)
367 {
368 wchar w;
369 auto j = i;
370
371 if (d <= 0xFFFF)
372 w = cast(wchar) d;
373 else
374 {
375 w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800);
376 result = dg(&j, cast(void *)&w);
377 if (result)
378 break;
379 w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00);
380 }
381 result = dg(&j, cast(void *)&w);
382 if (result)
383 break;
384 }
385 return result;
386 }