0
|
1 module dmd.String;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.Array;
|
|
5
|
|
6 import core.stdc.string : strlen;
|
|
7 import std.string : cmp;
|
|
8
|
|
9 import std.stdio : writefln;
|
|
10
|
178
|
11 import dmd.TObject;
|
|
12
|
|
13 class String : TObject
|
0
|
14 {
|
|
15 string str;
|
|
16
|
|
17 this(string str)
|
|
18 {
|
178
|
19 register();
|
0
|
20 this.str = str;
|
|
21 }
|
|
22
|
|
23 static hash_t calcHash(const(char)* str, size_t len)
|
|
24 {
|
|
25 hash_t hash = 0;
|
|
26
|
|
27 for (;;)
|
|
28 {
|
|
29 switch (len)
|
|
30 {
|
|
31 case 0:
|
|
32 return hash;
|
|
33
|
|
34 case 1:
|
|
35 hash *= 37;
|
|
36 hash += *cast(ubyte*)str;
|
|
37 return hash;
|
|
38
|
|
39 case 2:
|
|
40 hash *= 37;
|
|
41 hash += *cast(ushort*)str;
|
|
42 return hash;
|
|
43
|
|
44 case 3:
|
|
45 hash *= 37;
|
|
46 hash += (*(cast(ushort*)str) << 8) +
|
|
47 (cast(ubyte*)str)[2];
|
|
48 return hash;
|
|
49
|
|
50 default:
|
|
51 hash *= 37;
|
|
52 hash += *cast(uint*)str;
|
|
53 str += 4;
|
|
54 len -= 4;
|
|
55 break;
|
|
56 }
|
|
57 }
|
|
58 }
|
|
59
|
|
60 static hash_t calcHash(string str)
|
|
61 {
|
|
62 return calcHash(str.ptr, str.length);
|
|
63 }
|
|
64
|
|
65 hash_t hashCode()
|
|
66 {
|
|
67 return calcHash(str.ptr, str.length);
|
|
68 }
|
|
69
|
|
70 uint len()
|
|
71 {
|
|
72 return str.length;
|
|
73 }
|
|
74
|
45
|
75 bool equals(Object obj)
|
0
|
76 {
|
|
77 return str == (cast(String)obj).str;
|
|
78 }
|
|
79
|
72
|
80 override int opCmp(Object obj)
|
0
|
81 {
|
|
82 return cmp(str, (cast(String)obj).str);
|
|
83 }
|
|
84
|
|
85 string toChars()
|
|
86 {
|
|
87 return str;
|
|
88 }
|
|
89
|
|
90 void print()
|
|
91 {
|
|
92 writefln("String '%s'", str);
|
|
93 }
|
72
|
94 }
|