comparison lphobos/internal/aApply.d @ 86:fd32135dca3e trunk

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