Mercurial > projects > ddmd
annotate dmd/OutBuffer.d @ 12:832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
Some unittest-specific functions implemented
author | korDen |
---|---|
date | Mon, 12 Apr 2010 15:13:00 +0400 |
parents | 63623152e82a |
children | 544b922227c7 |
rev | line source |
---|---|
0 | 1 module dmd.OutBuffer; |
2 | |
3 import std.stdarg; | |
4 import std.contracts; | |
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 { | |
97 assert(false); | |
98 } | |
99 | |
100 final void writeByte(uint b) | |
101 { | |
102 reserve(1); | |
103 this.data[offset] = cast(ubyte)b; | |
104 offset++; | |
105 } | |
106 | |
107 final void writebyte(uint b) { writeByte(b); } | |
108 | |
109 final void writeUTF8(uint b) | |
110 { | |
111 reserve(6); | |
112 if (b <= 0x7F) | |
113 { | |
114 this.data[offset] = cast(ubyte)b; | |
115 offset++; | |
116 } | |
117 else if (b <= 0x7FF) | |
118 { | |
119 this.data[offset + 0] = cast(ubyte)((b >> 6) | 0xC0); | |
120 this.data[offset + 1] = cast(ubyte)((b & 0x3F) | 0x80); | |
121 offset += 2; | |
122 } | |
123 else if (b <= 0xFFFF) | |
124 { | |
125 this.data[offset + 0] = cast(ubyte)((b >> 12) | 0xE0); | |
126 this.data[offset + 1] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
127 this.data[offset + 2] = cast(ubyte)((b & 0x3F) | 0x80); | |
128 offset += 3; | |
129 } | |
130 else if (b <= 0x1FFFFF) | |
131 { | |
132 this.data[offset + 0] = cast(ubyte)((b >> 18) | 0xF0); | |
133 this.data[offset + 1] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
134 this.data[offset + 2] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
135 this.data[offset + 3] = cast(ubyte)((b & 0x3F) | 0x80); | |
136 offset += 4; | |
137 } | |
138 else if (b <= 0x3FFFFFF) | |
139 { | |
140 this.data[offset + 0] = cast(ubyte)((b >> 24) | 0xF8); | |
141 this.data[offset + 1] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); | |
142 this.data[offset + 2] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
143 this.data[offset + 3] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
144 this.data[offset + 4] = cast(ubyte)((b & 0x3F) | 0x80); | |
145 offset += 5; | |
146 } | |
147 else if (b <= 0x7FFFFFFF) | |
148 { | |
149 this.data[offset + 0] = cast(ubyte)((b >> 30) | 0xFC); | |
150 this.data[offset + 1] = cast(ubyte)(((b >> 24) & 0x3F) | 0x80); | |
151 this.data[offset + 2] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); | |
152 this.data[offset + 3] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); | |
153 this.data[offset + 4] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); | |
154 this.data[offset + 5] = cast(ubyte)((b & 0x3F) | 0x80); | |
155 offset += 6; | |
156 } | |
157 else | |
158 assert(0); | |
159 } | |
160 | |
161 final void writedchar(uint b) | |
162 { | |
163 assert(false); | |
164 } | |
165 | |
166 final void prependbyte(uint b) | |
167 { | |
168 assert(false); | |
169 } | |
170 | |
171 final void writeword(uint w) | |
172 { | |
173 reserve(2); | |
174 *cast(ushort*)(this.data + offset) = cast(ushort)w; | |
175 offset += 2; | |
176 } | |
177 | |
178 final void writeUTF16(uint w) | |
179 { | |
180 reserve(4); | |
181 if (w <= 0xFFFF) | |
182 { | |
183 *cast(ushort*)(this.data + offset) = cast(ushort)w; | |
184 offset += 2; | |
185 } | |
186 else if (w <= 0x10FFFF) | |
187 { | |
188 *cast(ushort*)(this.data + offset) = cast(ushort)((w >> 10) + 0xD7C0); | |
189 *cast(ushort*)(this.data + offset + 2) = cast(ushort)((w & 0x3FF) | 0xDC00); | |
190 offset += 4; | |
191 } | |
192 else | |
193 assert(0); | |
194 } | |
195 | |
196 final void write4(uint w) | |
197 { | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
198 reserve(4); |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
199 *cast(uint*)(this.data + offset) = w; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
5
diff
changeset
|
200 offset += 4; |
0 | 201 } |
202 | |
203 final void write(OutBuffer buf) | |
204 { | |
205 if (buf) | |
206 { | |
207 reserve(buf.offset); | |
208 memcpy(data + offset, buf.data, buf.offset); | |
209 offset += buf.offset; | |
210 } | |
211 } | |
212 | |
213 final void write(Object obj) | |
214 { | |
215 assert(false); | |
216 } | |
217 | |
218 final void fill0(uint nbytes) | |
219 { | |
220 reserve(nbytes); | |
221 memset(data + offset, 0, nbytes); | |
222 offset += nbytes; | |
223 } | |
224 | |
225 final void align_(uint size) | |
226 { | |
227 assert(false); | |
228 } | |
229 | |
230 void vprintf(const(char)* format, va_list args) | |
231 { | |
232 assert(false); | |
233 } | |
234 | |
235 void printf(T...)(string format, T t) | |
236 { | |
237 string s = std.string.format(format, t); | |
238 writestring(s); | |
239 } | |
240 | |
241 version (M_UNICODE) { | |
242 /// void vprintf(const uint short *format, va_list args); | |
243 /// void printf(const uint short *format, ...); | |
244 } | |
245 final void bracket(char left, char right) | |
246 { | |
247 assert(false); | |
248 } | |
249 | |
250 final uint bracket(uint i, const(char)* left, uint j, const(char)* right) | |
251 { | |
252 assert(false); | |
253 } | |
254 | |
255 final void spread(uint offset, uint nbytes) | |
256 { | |
257 assert(false); | |
258 } | |
259 | |
260 final uint insert(uint offset, const(void)* data, uint nbytes) | |
261 { | |
262 assert(false); | |
263 } | |
264 | |
265 final void remove(uint offset, uint nbytes) | |
266 { | |
267 assert(false); | |
268 } | |
269 | |
270 string toChars() | |
271 { | |
5
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
272 char[] s = getString(); |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
273 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
|
274 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
|
275 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
|
276 |
63623152e82a
Fixed memory corruption bug which was introduced when attempting to restore GC functionality
dkoroskin <>
parents:
4
diff
changeset
|
277 //return getString().idup; |
0 | 278 } |
279 | |
280 final string extractString() | |
281 { | |
282 char[] s = getString(); | |
283 data = null; | |
284 offset = 0; | |
285 size = 0; | |
286 | |
287 return assumeUnique(s); | |
288 } | |
289 | |
290 final char[] getString() | |
291 { | |
292 char* s = cast(char*)data; | |
293 return s[0..offset]; | |
294 } | |
295 } |