Mercurial > projects > ddmd
annotate dmd/OutBuffer.d @ 93:df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
and some additional ones I came across
author | Trass3r |
---|---|
date | Mon, 30 Aug 2010 22:50:30 +0200 |
parents | ee3a9f34dc48 |
children | e28b18c23469 |
rev | line source |
---|---|
0 | 1 module dmd.OutBuffer; |
2 | |
3 import std.stdarg; | |
34 | 4 import std.exception; |
0 | 5 |
6 import core.stdc.stdlib; | |
7 import core.stdc.string; | |
8 | |
4 | 9 import core.memory; |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
10 import core.stdc.stdlib; |
2 | 11 |
0 | 12 class OutBuffer |
13 { | |
14 ubyte* data; | |
15 uint offset; | |
16 uint size; | |
17 | |
18 this() | |
19 { | |
20 // do nothing | |
21 } | |
22 | |
23 final void* extractData() | |
24 { | |
25 void* p = cast(void*)data; | |
26 | |
27 data = null; | |
28 offset = 0; | |
29 size = 0; | |
30 | |
31 return p; | |
32 } | |
33 | |
34 void mark() | |
35 { | |
36 assert(false); | |
37 } | |
38 | |
39 final void reserve(uint nbytes) | |
40 { | |
41 //printf("OutBuffer::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes); | |
42 if (size - offset < nbytes) | |
43 { | |
44 size = (offset + nbytes) * 2; | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
45 data = cast(ubyte*)realloc(data, size); |
0 | 46 } |
47 } | |
48 | |
49 final void setsize(uint size) | |
50 { | |
51 assert(false); | |
52 } | |
53 | |
54 final void reset() | |
55 { | |
56 offset = 0; | |
57 } | |
58 | |
59 final void write(const(void)* data, uint nbytes) | |
60 { | |
61 reserve(nbytes); | |
62 memcpy(this.data + offset, data, nbytes); | |
63 offset += nbytes; | |
64 } | |
65 | |
66 final void writebstring(ubyte* string_) | |
67 { | |
68 assert(false); | |
69 } | |
70 | |
71 final void writestring(const(char)[] string_) | |
72 { | |
73 write(string_.ptr , string_.length); | |
74 } | |
75 | |
76 final void writedstring(const(char)* string_) | |
77 { | |
78 assert(false); | |
79 } | |
80 | |
81 final void writedstring(const(wchar)* string_) | |
82 { | |
83 assert(false); | |
84 } | |
85 | |
86 final void prependstring(const(char)[] string_) | |
87 { | |
88 uint len = string_.length; | |
89 reserve(len); | |
90 memmove(data + len, data, offset); | |
91 memcpy(data, string_.ptr, len); | |
92 offset += len; | |
93 } | |
94 | |
95 final void writenl() // write newline | |
96 { | |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
97 version (_WIN32) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
98 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
99 version (M_UNICODE) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
100 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
101 write4(0x000A000D); // newline is CR,LF on Microsoft OS's |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
102 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
103 else |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
104 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
105 writeword(0x0A0D); // newline is CR,LF on Microsoft OS's |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
106 } |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
107 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
108 else |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
109 { |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
110 version (M_UNICODE) |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
111 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
112 writeword('\n'); |
93
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
113 } |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
114 else |
df6d0f967680
implemented a whole bunch of methods to make phobos 2.035 compile
Trass3r
parents:
68
diff
changeset
|
115 { |
68
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
116 writeByte('\n'); |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
117 } |
ee3a9f34dc48
final bits of codegen implementation to compile Phobos
korDen
parents:
34
diff
changeset
|
118 } |
0 | 119 } |
120 | |
121 final void writeByte(uint b) | |
122 { | |
123 reserve(1); | |
124 this.data[offset] = cast(ubyte)b; | |
125 offset++; | |
126 } | |
127 | |
128 final void writebyte(uint b) { writeByte(b); } | |
129 | |
130 final void writeUTF8(uint b) | |
131 { | |
132 reserve(6); | |
133 if (b <= 0x7F) | |
134 { | |
135 this.data[offset] = cast(ubyte)b; | |
136 offset++; | |
137 } | |
138 else if (b <= 0x7FF) | |
139 { | |
140 this.data[offset + 0] = cast(ubyte)((b >> 6) | 0xC0); | |
141 this.data[offset + 1] = cast(ubyte)((b & 0x3F) | 0x80); | |
142 offset += 2; | |
143 } | |
144 else if (b <= 0xFFFF) | |
145 { | |
146 this.data[offset + 0] = cast(ubyte)((b >> 12) | 0xE0); | |
147 this.data[offset + 1] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
148 this.data[offset + 2] = cast(ubyte)((b & 0x3F) | 0x80); | |
149 offset += 3; | |
150 } | |
151 else if (b <= 0x1FFFFF) | |
152 { | |
153 this.data[offset + 0] = cast(ubyte)((b >> 18) | 0xF0); | |
154 this.data[offset + 1] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
155 this.data[offset + 2] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
156 this.data[offset + 3] = cast(ubyte)((b & 0x3F) | 0x80); | |
157 offset += 4; | |
158 } | |
159 else if (b <= 0x3FFFFFF) | |
160 { | |
161 this.data[offset + 0] = cast(ubyte)((b >> 24) | 0xF8); | |
162 this.data[offset + 1] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); | |
163 this.data[offset + 2] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
164 this.data[offset + 3] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
165 this.data[offset + 4] = cast(ubyte)((b & 0x3F) | 0x80); | |
166 offset += 5; | |
167 } | |
168 else if (b <= 0x7FFFFFFF) | |
169 { | |
170 this.data[offset + 0] = cast(ubyte)((b >> 30) | 0xFC); | |
171 this.data[offset + 1] = cast(ubyte)(((b >> 24) & 0x3F) | 0x80); | |
172 this.data[offset + 2] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); | |
173 this.data[offset + 3] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
174 this.data[offset + 4] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
175 this.data[offset + 5] = cast(ubyte)((b & 0x3F) | 0x80); | |
176 offset += 6; | |
177 } | |
178 else | |
179 assert(0); | |
180 } | |
181 | |
182 final void writedchar(uint b) | |
183 { | |
184 assert(false); | |
185 } | |
186 | |
187 final void prependbyte(uint b) | |
188 { | |
189 assert(false); | |
190 } | |
191 | |
192 final void writeword(uint w) | |
193 { | |
194 reserve(2); | |
195 *cast(ushort*)(this.data + offset) = cast(ushort)w; | |
196 offset += 2; | |
197 } | |
198 | |
199 final void writeUTF16(uint w) | |
200 { | |
201 reserve(4); | |
202 if (w <= 0xFFFF) | |
203 { | |
204 *cast(ushort*)(this.data + offset) = cast(ushort)w; | |
205 offset += 2; | |
206 } | |
207 else if (w <= 0x10FFFF) | |
208 { | |
209 *cast(ushort*)(this.data + offset) = cast(ushort)((w >> 10) + 0xD7C0); | |
210 *cast(ushort*)(this.data + offset + 2) = cast(ushort)((w & 0x3FF) | 0xDC00); | |
211 offset += 4; | |
212 } | |
213 else | |
214 assert(0); | |
215 } | |
216 | |
217 final void write4(uint w) | |
218 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
219 reserve(4); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
220 *cast(uint*)(this.data + offset) = w; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
221 offset += 4; |
0 | 222 } |
223 | |
224 final void write(OutBuffer buf) | |
225 { | |
226 if (buf) | |
227 { | |
228 reserve(buf.offset); | |
229 memcpy(data + offset, buf.data, buf.offset); | |
230 offset += buf.offset; | |
231 } | |
232 } | |
233 | |
234 final void write(Object obj) | |
235 { | |
236 assert(false); | |
237 } | |
238 | |
239 final void fill0(uint nbytes) | |
240 { | |
241 reserve(nbytes); | |
242 memset(data + offset, 0, nbytes); | |
243 offset += nbytes; | |
244 } | |
245 | |
246 final void align_(uint size) | |
247 { | |
248 assert(false); | |
249 } | |
250 | |
251 void vprintf(const(char)* format, va_list args) | |
252 { | |
253 assert(false); | |
254 } | |
255 | |
256 void printf(T...)(string format, T t) | |
257 { | |
258 string s = std.string.format(format, t); | |
259 writestring(s); | |
260 } | |
261 | |
262 version (M_UNICODE) { | |
263 /// void vprintf(const uint short *format, va_list args); | |
264 /// void printf(const uint short *format, ...); | |
265 } | |
266 final void bracket(char left, char right) | |
267 { | |
268 assert(false); | |
269 } | |
270 | |
271 final uint bracket(uint i, const(char)* left, uint j, const(char)* right) | |
272 { | |
273 assert(false); | |
274 } | |
275 | |
276 final void spread(uint offset, uint nbytes) | |
277 { | |
278 assert(false); | |
279 } | |
280 | |
281 final uint insert(uint offset, const(void)* data, uint nbytes) | |
282 { | |
283 assert(false); | |
284 } | |
285 | |
286 final void remove(uint offset, uint nbytes) | |
287 { | |
288 assert(false); | |
289 } | |
290 | |
291 string toChars() | |
292 { | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
293 char[] s = getString(); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
294 char* copy = cast(char*)malloc(s.length); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
295 memcpy(copy, s.ptr, s.length); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
296 return assumeUnique(copy[0..s.length]); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
297 |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
298 //return getString().idup; |
0 | 299 } |
300 | |
301 final string extractString() | |
302 { | |
303 char[] s = getString(); | |
304 data = null; | |
305 offset = 0; | |
306 size = 0; | |
307 | |
308 return assumeUnique(s); | |
309 } | |
310 | |
311 final char[] getString() | |
312 { | |
313 char* s = cast(char*)data; | |
314 return s[0..offset]; | |
315 } | |
316 } |