comparison dmd/OutBuffer.d @ 0:10317f0c89a5

Initial commit
author korDen
date Sat, 24 Oct 2009 08:42:06 +0400
parents
children 7427ded8caf7
comparison
equal deleted inserted replaced
-1:000000000000 0:10317f0c89a5
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
9 class OutBuffer
10 {
11 ubyte* data;
12 uint offset;
13 uint size;
14
15 this()
16 {
17 // do nothing
18 }
19
20 final void* extractData()
21 {
22 void* p = cast(void*)data;
23
24 data = null;
25 offset = 0;
26 size = 0;
27
28 return p;
29 }
30
31 void mark()
32 {
33 assert(false);
34 }
35
36 final void reserve(uint nbytes)
37 {
38 //printf("OutBuffer::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes);
39 if (size - offset < nbytes)
40 {
41 size = (offset + nbytes) * 2;
42 data = cast(ubyte*)realloc(data, size);
43 }
44 }
45
46 final void setsize(uint size)
47 {
48 assert(false);
49 }
50
51 final void reset()
52 {
53 offset = 0;
54 }
55
56 final void write(const(void)* data, uint nbytes)
57 {
58 reserve(nbytes);
59 memcpy(this.data + offset, data, nbytes);
60 offset += nbytes;
61 }
62
63 final void writebstring(ubyte* string_)
64 {
65 assert(false);
66 }
67
68 final void writestring(const(char)[] string_)
69 {
70 write(string_.ptr , string_.length);
71 }
72
73 final void writedstring(const(char)* string_)
74 {
75 assert(false);
76 }
77
78 final void writedstring(const(wchar)* string_)
79 {
80 assert(false);
81 }
82
83 final void prependstring(const(char)[] string_)
84 {
85 uint len = string_.length;
86 reserve(len);
87 memmove(data + len, data, offset);
88 memcpy(data, string_.ptr, len);
89 offset += len;
90 }
91
92 final void writenl() // write newline
93 {
94 assert(false);
95 }
96
97 final void writeByte(uint b)
98 {
99 reserve(1);
100 this.data[offset] = cast(ubyte)b;
101 offset++;
102 }
103
104 final void writebyte(uint b) { writeByte(b); }
105
106 final void writeUTF8(uint b)
107 {
108 reserve(6);
109 if (b <= 0x7F)
110 {
111 this.data[offset] = cast(ubyte)b;
112 offset++;
113 }
114 else if (b <= 0x7FF)
115 {
116 this.data[offset + 0] = cast(ubyte)((b >> 6) | 0xC0);
117 this.data[offset + 1] = cast(ubyte)((b & 0x3F) | 0x80);
118 offset += 2;
119 }
120 else if (b <= 0xFFFF)
121 {
122 this.data[offset + 0] = cast(ubyte)((b >> 12) | 0xE0);
123 this.data[offset + 1] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80);
124 this.data[offset + 2] = cast(ubyte)((b & 0x3F) | 0x80);
125 offset += 3;
126 }
127 else if (b <= 0x1FFFFF)
128 {
129 this.data[offset + 0] = cast(ubyte)((b >> 18) | 0xF0);
130 this.data[offset + 1] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80);
131 this.data[offset + 2] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80);
132 this.data[offset + 3] = cast(ubyte)((b & 0x3F) | 0x80);
133 offset += 4;
134 }
135 else if (b <= 0x3FFFFFF)
136 {
137 this.data[offset + 0] = cast(ubyte)((b >> 24) | 0xF8);
138 this.data[offset + 1] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80);
139 this.data[offset + 2] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80);
140 this.data[offset + 3] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80);
141 this.data[offset + 4] = cast(ubyte)((b & 0x3F) | 0x80);
142 offset += 5;
143 }
144 else if (b <= 0x7FFFFFFF)
145 {
146 this.data[offset + 0] = cast(ubyte)((b >> 30) | 0xFC);
147 this.data[offset + 1] = cast(ubyte)(((b >> 24) & 0x3F) | 0x80);
148 this.data[offset + 2] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80);
149 this.data[offset + 3] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80);
150 this.data[offset + 4] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80);
151 this.data[offset + 5] = cast(ubyte)((b & 0x3F) | 0x80);
152 offset += 6;
153 }
154 else
155 assert(0);
156 }
157
158 final void writedchar(uint b)
159 {
160 assert(false);
161 }
162
163 final void prependbyte(uint b)
164 {
165 assert(false);
166 }
167
168 final void writeword(uint w)
169 {
170 reserve(2);
171 *cast(ushort*)(this.data + offset) = cast(ushort)w;
172 offset += 2;
173 }
174
175 final void writeUTF16(uint w)
176 {
177 reserve(4);
178 if (w <= 0xFFFF)
179 {
180 *cast(ushort*)(this.data + offset) = cast(ushort)w;
181 offset += 2;
182 }
183 else if (w <= 0x10FFFF)
184 {
185 *cast(ushort*)(this.data + offset) = cast(ushort)((w >> 10) + 0xD7C0);
186 *cast(ushort*)(this.data + offset + 2) = cast(ushort)((w & 0x3FF) | 0xDC00);
187 offset += 4;
188 }
189 else
190 assert(0);
191 }
192
193 final void write4(uint w)
194 {
195 assert(false);
196 }
197
198 final void write(OutBuffer buf)
199 {
200 if (buf)
201 {
202 reserve(buf.offset);
203 memcpy(data + offset, buf.data, buf.offset);
204 offset += buf.offset;
205 }
206 }
207
208 final void write(Object obj)
209 {
210 assert(false);
211 }
212
213 final void fill0(uint nbytes)
214 {
215 reserve(nbytes);
216 memset(data + offset, 0, nbytes);
217 offset += nbytes;
218 }
219
220 final void align_(uint size)
221 {
222 assert(false);
223 }
224
225 void vprintf(const(char)* format, va_list args)
226 {
227 assert(false);
228 }
229
230 void printf(T...)(string format, T t)
231 {
232 string s = std.string.format(format, t);
233 writestring(s);
234 }
235
236 version (M_UNICODE) {
237 /// void vprintf(const uint short *format, va_list args);
238 /// void printf(const uint short *format, ...);
239 }
240 final void bracket(char left, char right)
241 {
242 assert(false);
243 }
244
245 final uint bracket(uint i, const(char)* left, uint j, const(char)* right)
246 {
247 assert(false);
248 }
249
250 final void spread(uint offset, uint nbytes)
251 {
252 assert(false);
253 }
254
255 final uint insert(uint offset, const(void)* data, uint nbytes)
256 {
257 assert(false);
258 }
259
260 final void remove(uint offset, uint nbytes)
261 {
262 assert(false);
263 }
264
265 string toChars()
266 {
267 return getString().idup;
268 }
269
270 final string extractString()
271 {
272 char[] s = getString();
273 data = null;
274 offset = 0;
275 size = 0;
276
277 return assumeUnique(s);
278 }
279
280 final char[] getString()
281 {
282 char* s = cast(char*)data;
283 return s[0..offset];
284 }
285 }