annotate dmd/String.d @ 178:e3afd1303184

Many small bugs fixed Made all classes derive from TObject to detect memory leaks (functionality is disabled for now) Began work on overriding backend memory allocations (to avoid memory leaks)
author korDen
date Sun, 17 Oct 2010 07:42:00 +0400
parents e28b18c23469
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.String;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 72
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import core.stdc.string : strlen;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import std.string : cmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import std.stdio : writefln;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10
178
e3afd1303184 Many small bugs fixed
korDen
parents: 114
diff changeset
11 import dmd.TObject;
e3afd1303184 Many small bugs fixed
korDen
parents: 114
diff changeset
12
e3afd1303184 Many small bugs fixed
korDen
parents: 114
diff changeset
13 class String : TObject
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 string str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 this(string str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 114
diff changeset
19 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 this.str = str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 static hash_t calcHash(const(char)* str, size_t len)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 hash_t hash = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 for (;;)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 switch (len)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 case 0:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 return hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 case 1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 hash *= 37;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 hash += *cast(ubyte*)str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 return hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 case 2:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 hash *= 37;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 hash += *cast(ushort*)str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 return hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 case 3:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 hash *= 37;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 hash += (*(cast(ushort*)str) << 8) +
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 (cast(ubyte*)str)[2];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 return hash;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 hash *= 37;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 hash += *cast(uint*)str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 str += 4;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 len -= 4;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 static hash_t calcHash(string str)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 return calcHash(str.ptr, str.length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 hash_t hashCode()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 return calcHash(str.ptr, str.length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 uint len()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 return str.length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74
45
ccbc1e0bb3f0 StringExp.equals implemented
korDen
parents: 0
diff changeset
75 bool equals(Object obj)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 return str == (cast(String)obj).str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
80 override int opCmp(Object obj)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 return cmp(str, (cast(String)obj).str);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 string toChars()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 return str;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 void print()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 writefln("String '%s'", str);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
94 }