2 * Copyright (C) 2004-2007 by Digital Mars, www.digitalmars.com
3 * Written by Walter Bright
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, in both source and binary form, subject to the following
14 * o The origin of this software must not be misrepresented; you must not
15 * claim that you wrote the original software. If you use this software
16 * in a product, an acknowledgment in the product documentation would be
17 * appreciated but is not required.
18 * o Altered source versions must be plainly marked as such, and must not
19 * be misrepresented as being the original software.
20 * o This notice may not be removed or altered from any source
25 * Modified by Sean Kelly for use with the D Runtime Project
30 private import core.stdc.string;
32 /******************************************************
33 * Support for switch statements switching on strings.
35 * table[] sorted array of strings generated by compiler
36 * ca string to look up in table
38 * result index of match in table[]
44 int _d_switch_string(char[][] table, char[] ca)
47 //printf("in _d_switch_string()\n");
48 assert(table.length >= 0);
49 assert(ca.length >= 0);
51 // Make sure table[] is sorted correctly
54 for (j = 1; j < table.length; j++)
56 int len1 = table[j - 1].length;
57 int len2 = table[j].length;
64 ci = memcmp(table[j - 1].ptr, table[j].ptr, len1);
65 assert(ci < 0); // ci==0 means a duplicate
74 //printf("out _d_switch_string()\n");
78 for (i = 0; i < table.length; i++)
80 if (table[i].length == ca.length)
81 { cj = memcmp(table[i].ptr, ca.ptr, ca.length);
88 assert(0 <= result && result < table.length);
91 assert(i < table.length);
92 if (table[i].length == ca.length)
94 cj = memcmp(table[i].ptr, ca.ptr, ca.length);
106 //printf("body _d_switch_string(%.*s)\n", ca);
119 printf("ca[] = '%s'\n", cast(char *)ca);
120 for (mid = 0; mid < high; mid++)
123 printf("table[%d] = %d, '%.*s'\n", mid, pca.length, pca);
127 ca.length >= table[0].length &&
128 ca.length <= table[high - 1].length)
130 // Looking for 0 length string, which would only be at the beginning
139 mid = (low + high) >> 1;
141 c = ca.length - pca.length;
144 c = cast(ubyte)c1 - cast(ubyte)pca[0];
147 c = memcmp(ca.ptr, pca.ptr, ca.length);
149 { //printf("found %d\n", mid);
165 //printf("not found\n");
166 return -1; // not found
171 switch (cast(char []) "c")
179 /**********************************
180 * Same thing, but for wide chars.
183 int _d_switch_ustring(wchar[][] table, wchar[] ca)
186 //printf("in _d_switch_ustring()\n");
187 assert(table.length >= 0);
188 assert(ca.length >= 0);
190 // Make sure table[] is sorted correctly
193 for (j = 1; j < table.length; j++)
195 int len1 = table[j - 1].length;
196 int len2 = table[j].length;
198 assert(len1 <= len2);
203 c = memcmp(table[j - 1].ptr, table[j].ptr, len1 * wchar.sizeof);
204 assert(c < 0); // c==0 means a duplicate
213 //printf("out _d_switch_string()\n");
217 for (i = 0; i < table.length; i++)
219 if (table[i].length == ca.length)
220 { c = memcmp(table[i].ptr, ca.ptr, ca.length * wchar.sizeof);
227 assert(0 <= result && result < table.length);
230 assert(i < table.length);
231 if (table[i].length == ca.length)
233 c = memcmp(table[i].ptr, ca.ptr, ca.length * wchar.sizeof);
245 //printf("body _d_switch_ustring()\n");
257 wprintf("ca[] = '%.*s'\n", ca);
258 for (mid = 0; mid < high; mid++)
261 wprintf("table[%d] = %d, '%.*s'\n", mid, pca.length, pca);
268 mid = (low + high) >> 1;
270 c = ca.length - pca.length;
273 c = memcmp(ca.ptr, pca.ptr, ca.length * wchar.sizeof);
275 { //printf("found %d\n", mid);
288 //printf("not found\n");
289 return -1; // not found
295 switch (cast(wchar []) "c")
304 /**********************************
305 * Same thing, but for wide chars.
308 int _d_switch_dstring(dchar[][] table, dchar[] ca)
311 //printf("in _d_switch_dstring()\n");
312 assert(table.length >= 0);
313 assert(ca.length >= 0);
315 // Make sure table[] is sorted correctly
318 for (j = 1; j < table.length; j++)
320 int len1 = table[j - 1].length;
321 int len2 = table[j].length;
323 assert(len1 <= len2);
328 c = memcmp(table[j - 1].ptr, table[j].ptr, len1 * dchar.sizeof);
329 assert(c < 0); // c==0 means a duplicate
338 //printf("out _d_switch_string()\n");
342 for (i = 0; i < table.length; i++)
344 if (table[i].length == ca.length)
345 { c = memcmp(table[i].ptr, ca.ptr, ca.length * dchar.sizeof);
352 assert(0 <= result && result < table.length);
355 assert(i < table.length);
356 if (table[i].length == ca.length)
358 c = memcmp(table[i].ptr, ca.ptr, ca.length * dchar.sizeof);
370 //printf("body _d_switch_ustring()\n");
382 wprintf("ca[] = '%.*s'\n", ca);
383 for (mid = 0; mid < high; mid++)
386 wprintf("table[%d] = %d, '%.*s'\n", mid, pca.length, pca);
393 mid = (low + high) >> 1;
395 c = ca.length - pca.length;
398 c = memcmp(ca.ptr, pca.ptr, ca.length * dchar.sizeof);
400 { //printf("found %d\n", mid);
413 //printf("not found\n");
414 return -1; // not found
420 switch (cast(dchar []) "c")