comparison lphobos/std/typeinfounsupported/ti_Ag.d @ 1:c53b6e3fe49a trunk

[svn r5] Initial commit. Most things are very rough.
author lindquist
date Sat, 01 Sep 2007 21:43:27 +0200
parents
children
comparison
equal deleted inserted replaced
0:a9e71648e74d 1:c53b6e3fe49a
1
2 module std.typeinfo.ti_Ag;
3
4 private import std.string;
5 private import std.c.string;
6
7 // byte[]
8
9 class TypeInfo_Ag : TypeInfo
10 {
11 char[] toString() { return "byte[]"; }
12
13 hash_t getHash(void *p)
14 { byte[] s = *cast(byte[]*)p;
15 size_t len = s.length;
16 byte *str = s.ptr;
17 hash_t hash = 0;
18
19 while (1)
20 {
21 switch (len)
22 {
23 case 0:
24 return hash;
25
26 case 1:
27 hash *= 9;
28 hash += *cast(ubyte *)str;
29 return hash;
30
31 case 2:
32 hash *= 9;
33 hash += *cast(ushort *)str;
34 return hash;
35
36 case 3:
37 hash *= 9;
38 hash += (*cast(ushort *)str << 8) +
39 (cast(ubyte *)str)[2];
40 return hash;
41
42 default:
43 hash *= 9;
44 hash += *cast(uint *)str;
45 str += 4;
46 len -= 4;
47 break;
48 }
49 }
50
51 return hash;
52 }
53
54 int equals(void *p1, void *p2)
55 {
56 byte[] s1 = *cast(byte[]*)p1;
57 byte[] s2 = *cast(byte[]*)p2;
58
59 return s1.length == s2.length &&
60 memcmp(cast(byte *)s1, cast(byte *)s2, s1.length) == 0;
61 }
62
63 int compare(void *p1, void *p2)
64 {
65 byte[] s1 = *cast(byte[]*)p1;
66 byte[] s2 = *cast(byte[]*)p2;
67 size_t len = s1.length;
68
69 if (s2.length < len)
70 len = s2.length;
71 for (size_t u = 0; u < len; u++)
72 {
73 int result = s1[u] - s2[u];
74 if (result)
75 return result;
76 }
77 return cast(int)s1.length - cast(int)s2.length;
78 }
79
80 size_t tsize()
81 {
82 return (byte[]).sizeof;
83 }
84
85 uint flags()
86 {
87 return 1;
88 }
89
90 TypeInfo next()
91 {
92 return typeid(byte);
93 }
94 }
95
96
97 // ubyte[]
98
99 class TypeInfo_Ah : TypeInfo_Ag
100 {
101 char[] toString() { return "ubyte[]"; }
102
103 int compare(void *p1, void *p2)
104 {
105 char[] s1 = *cast(char[]*)p1;
106 char[] s2 = *cast(char[]*)p2;
107
108 return std.string.cmp(s1, s2);
109 }
110
111 TypeInfo next()
112 {
113 return typeid(ubyte);
114 }
115 }
116
117 // void[]
118
119 class TypeInfo_Av : TypeInfo_Ah
120 {
121 char[] toString() { return "void[]"; }
122
123 TypeInfo next()
124 {
125 return typeid(void);
126 }
127 }
128
129 // bool[]
130
131 class TypeInfo_Ab : TypeInfo_Ah
132 {
133 char[] toString() { return "bool[]"; }
134
135 TypeInfo next()
136 {
137 return typeid(bool);
138 }
139 }
140
141 // char[]
142
143 class TypeInfo_Aa : TypeInfo_Ag
144 {
145 char[] toString() { return "char[]"; }
146
147 hash_t getHash(void *p)
148 { char[] s = *cast(char[]*)p;
149 hash_t hash = 0;
150
151 version (all)
152 {
153 foreach (char c; s)
154 hash = hash * 11 + c;
155 }
156 else
157 {
158 size_t len = s.length;
159 char *str = s;
160
161 while (1)
162 {
163 switch (len)
164 {
165 case 0:
166 return hash;
167
168 case 1:
169 hash *= 9;
170 hash += *cast(ubyte *)str;
171 return hash;
172
173 case 2:
174 hash *= 9;
175 hash += *cast(ushort *)str;
176 return hash;
177
178 case 3:
179 hash *= 9;
180 hash += (*cast(ushort *)str << 8) +
181 (cast(ubyte *)str)[2];
182 return hash;
183
184 default:
185 hash *= 9;
186 hash += *cast(uint *)str;
187 str += 4;
188 len -= 4;
189 break;
190 }
191 }
192 }
193 return hash;
194 }
195
196 TypeInfo next()
197 {
198 return typeid(char);
199 }
200 }
201
202