2 * Contains support code for switch blocks using string constants.
4 * Copyright: Copyright Digital Mars 2004 - 2010.
5 * License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 * Authors: Walter Bright, Sean Kelly
9 /* Copyright Digital Mars 2004 - 2010.
10 * Distributed under the Boost Software License, Version 1.0.
11 * (See accompanying file LICENSE or copy at
12 * http://www.boost.org/LICENSE_1_0.txt)
16 private import core
.stdc
.string
;
18 /******************************************************
19 * Support for switch statements switching on strings.
21 * table[] sorted array of strings generated by compiler
22 * ca string to look up in table
24 * result index of match in table[]
30 int _d_switch_string(char[][] table
, char[] ca
)
33 //printf("in _d_switch_string()\n");
34 assert(table
.length
>= 0);
35 assert(ca
.length
>= 0);
37 // Make sure table[] is sorted correctly
38 for (size_t j
= 1u; j
< table
.length
; j
++)
40 auto len1
= table
[j
- 1].length
;
41 auto len2
= table
[j
].length
;
48 ci
= memcmp(table
[j
- 1].ptr
, table
[j
].ptr
, len1
);
49 assert(ci
< 0); // ci==0 means a duplicate
57 //printf("out _d_switch_string()\n");
61 for (auto i
= 0u; i
< table
.length
; i
++)
63 if (table
[i
].length
== ca
.length
)
64 { cj
= memcmp(table
[i
].ptr
, ca
.ptr
, ca
.length
);
71 assert(0 <= result
&& cast(size_t
)result
< table
.length
);
72 for (auto i
= 0u; 1; i
++)
74 assert(i
< table
.length
);
75 if (table
[i
].length
== ca
.length
)
77 cj
= memcmp(table
[i
].ptr
, ca
.ptr
, ca
.length
);
89 //printf("body _d_switch_string(%.*s)\n", ca.length, ca.ptr);
91 size_t high
= table
.length
;
96 printf("ca[] = '%s'\n", ca
.length
, ca
.ptr
);
97 for (auto i
= 0; i
< high
; i
++)
100 printf("table[%d] = %d, '%.*s'\n", i
, pca
.length
, pca
.length
, pca
.ptr
);
104 ca
.length
>= table
[0].length
&&
105 ca
.length
<= table
[high
- 1].length
)
107 // Looking for 0 length string, which would only be at the beginning
116 auto mid
= (low
+ high
) >> 1;
117 auto pca
= table
[mid
];
118 auto c
= cast(sizediff_t
)(ca
.length
- pca
.length
);
121 c
= cast(ubyte)c1
- cast(ubyte)pca
[0];
124 c
= memcmp(ca
.ptr
, pca
.ptr
, ca
.length
);
126 { //printf("found %d\n", mid);
142 //printf("not found\n");
143 return -1; // not found
148 switch (cast(char []) "c")
155 int bug5381(string s
)
159 case "unittest": return 1;
160 case "D_Version2": return 2;
161 case "none": return 3;
162 case "all": return 4;
166 int rc
= bug5381("none");
170 /**********************************
171 * Same thing, but for wide chars.
174 int _d_switch_ustring(wchar[][] table
, wchar[] ca
)
177 //printf("in _d_switch_ustring()\n");
178 assert(table
.length
>= 0);
179 assert(ca
.length
>= 0);
181 // Make sure table[] is sorted correctly
182 for (size_t j
= 1u; j
< table
.length
; j
++)
184 auto len1
= table
[j
- 1].length
;
185 auto len2
= table
[j
].length
;
187 assert(len1
<= len2
);
192 c
= memcmp(table
[j
- 1].ptr
, table
[j
].ptr
, len1
* wchar.sizeof
);
193 assert(c
< 0); // c==0 means a duplicate
201 //printf("out _d_switch_ustring()\n");
205 for (auto i
= 0u; i
< table
.length
; i
++)
207 if (table
[i
].length
== ca
.length
)
208 { c
= memcmp(table
[i
].ptr
, ca
.ptr
, ca
.length
* wchar.sizeof
);
215 assert(0 <= result
&& cast(size_t
)result
< table
.length
);
216 for (auto i
= 0u; 1; i
++)
218 assert(i
< table
.length
);
219 if (table
[i
].length
== ca
.length
)
221 c
= memcmp(table
[i
].ptr
, ca
.ptr
, ca
.length
* wchar.sizeof
);
233 //printf("body _d_switch_ustring()\n");
235 auto high
= table
.length
;
240 wprintf("ca[] = '%.*s'\n", ca
.length
, ca
.ptr
);
241 for (auto i
= 0; i
< high
; i
++)
244 wprintf("table[%d] = %d, '%.*s'\n", i
, pca
.length
, pca
.length
, pca
.ptr
);
251 auto mid
= (low
+ high
) >> 1;
252 auto pca
= table
[mid
];
253 auto c
= cast(sizediff_t
)(ca
.length
- pca
.length
);
256 c
= memcmp(ca
.ptr
, pca
.ptr
, ca
.length
* wchar.sizeof
);
258 { //printf("found %d\n", mid);
271 //printf("not found\n");
272 return -1; // not found
278 switch (cast(wchar []) "c")
285 int bug5381(wstring ws
)
289 case "unittest": return 1;
290 case "D_Version2": return 2;
291 case "none": return 3;
292 case "all": return 4;
296 int rc
= bug5381("none"w
);
300 /**********************************
301 * Same thing, but for wide chars.
304 int _d_switch_dstring(dchar[][] table
, dchar[] ca
)
307 //printf("in _d_switch_dstring()\n");
308 assert(table
.length
>= 0);
309 assert(ca
.length
>= 0);
311 // Make sure table[] is sorted correctly
312 for (auto j
= 1u; j
< table
.length
; j
++)
314 auto len1
= table
[j
- 1].length
;
315 auto len2
= table
[j
].length
;
317 assert(len1
<= len2
);
320 auto c
= memcmp(table
[j
- 1].ptr
, table
[j
].ptr
, len1
* dchar.sizeof
);
321 assert(c
< 0); // c==0 means a duplicate
327 //printf("out _d_switch_dstring()\n");
331 for (auto i
= 0u; i
< table
.length
; i
++)
333 if (table
[i
].length
== ca
.length
)
334 { auto c
= memcmp(table
[i
].ptr
, ca
.ptr
, ca
.length
* dchar.sizeof
);
341 assert(0 <= result
&& cast(size_t
)result
< table
.length
);
342 for (auto i
= 0u; 1; i
++)
344 assert(i
< table
.length
);
345 if (table
[i
].length
== ca
.length
)
347 auto c
= memcmp(table
[i
].ptr
, ca
.ptr
, ca
.length
* dchar.sizeof
);
359 //printf("body _d_switch_dstring()\n");
361 auto high
= table
.length
;
366 wprintf("ca[] = '%.*s'\n", ca
.length
, ca
.ptr
);
367 for (auto i
= 0; i
< high
; i
++)
370 wprintf("table[%d] = %d, '%.*s'\n", i
, pca
.length
, pca
.length
, pca
.ptr
);
377 auto mid
= (low
+ high
) >> 1;
378 auto pca
= table
[mid
];
379 auto c
= cast(sizediff_t
)(ca
.length
- pca
.length
);
382 c
= memcmp(ca
.ptr
, pca
.ptr
, ca
.length
* dchar.sizeof
);
384 { //printf("found %d\n", mid);
397 //printf("not found\n");
398 return -1; // not found
404 switch (cast(dchar []) "c")
411 int bug5381(dstring
ds)
415 case "unittest": return 1;
416 case "D_Version2": return 2;
417 case "none": return 3;
418 case "all": return 4;
422 int rc
= bug5381("none"d
);