Mercurial > projects > ddmd
diff dmd/OutBuffer.d @ 0:10317f0c89a5
Initial commit
author | korDen |
---|---|
date | Sat, 24 Oct 2009 08:42:06 +0400 |
parents | |
children | 7427ded8caf7 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dmd/OutBuffer.d Sat Oct 24 08:42:06 2009 +0400 @@ -0,0 +1,285 @@ +module dmd.OutBuffer; + +import std.stdarg; +import std.contracts; + +import core.stdc.stdlib; +import core.stdc.string; + +class OutBuffer +{ + ubyte* data; + uint offset; + uint size; + + this() + { + // do nothing + } + + final void* extractData() + { + void* p = cast(void*)data; + + data = null; + offset = 0; + size = 0; + + return p; + } + + void mark() + { + assert(false); + } + + final void reserve(uint nbytes) + { + //printf("OutBuffer::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes); + if (size - offset < nbytes) + { + size = (offset + nbytes) * 2; + data = cast(ubyte*)realloc(data, size); + } + } + + final void setsize(uint size) + { + assert(false); + } + + final void reset() + { + offset = 0; + } + + final void write(const(void)* data, uint nbytes) + { + reserve(nbytes); + memcpy(this.data + offset, data, nbytes); + offset += nbytes; + } + + final void writebstring(ubyte* string_) + { + assert(false); + } + + final void writestring(const(char)[] string_) + { + write(string_.ptr , string_.length); + } + + final void writedstring(const(char)* string_) + { + assert(false); + } + + final void writedstring(const(wchar)* string_) + { + assert(false); + } + + final void prependstring(const(char)[] string_) + { + uint len = string_.length; + reserve(len); + memmove(data + len, data, offset); + memcpy(data, string_.ptr, len); + offset += len; + } + + final void writenl() // write newline + { + assert(false); + } + + final void writeByte(uint b) + { + reserve(1); + this.data[offset] = cast(ubyte)b; + offset++; + } + + final void writebyte(uint b) { writeByte(b); } + + final void writeUTF8(uint b) + { + reserve(6); + if (b <= 0x7F) + { + this.data[offset] = cast(ubyte)b; + offset++; + } + else if (b <= 0x7FF) + { + this.data[offset + 0] = cast(ubyte)((b >> 6) | 0xC0); + this.data[offset + 1] = cast(ubyte)((b & 0x3F) | 0x80); + offset += 2; + } + else if (b <= 0xFFFF) + { + this.data[offset + 0] = cast(ubyte)((b >> 12) | 0xE0); + this.data[offset + 1] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); + this.data[offset + 2] = cast(ubyte)((b & 0x3F) | 0x80); + offset += 3; + } + else if (b <= 0x1FFFFF) + { + this.data[offset + 0] = cast(ubyte)((b >> 18) | 0xF0); + this.data[offset + 1] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); + this.data[offset + 2] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); + this.data[offset + 3] = cast(ubyte)((b & 0x3F) | 0x80); + offset += 4; + } + else if (b <= 0x3FFFFFF) + { + this.data[offset + 0] = cast(ubyte)((b >> 24) | 0xF8); + this.data[offset + 1] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); + this.data[offset + 2] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); + this.data[offset + 3] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); + this.data[offset + 4] = cast(ubyte)((b & 0x3F) | 0x80); + offset += 5; + } + else if (b <= 0x7FFFFFFF) + { + this.data[offset + 0] = cast(ubyte)((b >> 30) | 0xFC); + this.data[offset + 1] = cast(ubyte)(((b >> 24) & 0x3F) | 0x80); + this.data[offset + 2] = cast(ubyte)(((b >> 18) & 0x3F) | 0x80); + this.data[offset + 3] = cast(ubyte)(((b >> 12) & 0x3F) | 0x80); + this.data[offset + 4] = cast(ubyte)(((b >> 6) & 0x3F) | 0x80); + this.data[offset + 5] = cast(ubyte)((b & 0x3F) | 0x80); + offset += 6; + } + else + assert(0); + } + + final void writedchar(uint b) + { + assert(false); + } + + final void prependbyte(uint b) + { + assert(false); + } + + final void writeword(uint w) + { + reserve(2); + *cast(ushort*)(this.data + offset) = cast(ushort)w; + offset += 2; + } + + final void writeUTF16(uint w) + { + reserve(4); + if (w <= 0xFFFF) + { + *cast(ushort*)(this.data + offset) = cast(ushort)w; + offset += 2; + } + else if (w <= 0x10FFFF) + { + *cast(ushort*)(this.data + offset) = cast(ushort)((w >> 10) + 0xD7C0); + *cast(ushort*)(this.data + offset + 2) = cast(ushort)((w & 0x3FF) | 0xDC00); + offset += 4; + } + else + assert(0); + } + + final void write4(uint w) + { + assert(false); + } + + final void write(OutBuffer buf) + { + if (buf) + { + reserve(buf.offset); + memcpy(data + offset, buf.data, buf.offset); + offset += buf.offset; + } + } + + final void write(Object obj) + { + assert(false); + } + + final void fill0(uint nbytes) + { + reserve(nbytes); + memset(data + offset, 0, nbytes); + offset += nbytes; + } + + final void align_(uint size) + { + assert(false); + } + + void vprintf(const(char)* format, va_list args) + { + assert(false); + } + + void printf(T...)(string format, T t) + { + string s = std.string.format(format, t); + writestring(s); + } + +version (M_UNICODE) { +/// void vprintf(const uint short *format, va_list args); +/// void printf(const uint short *format, ...); +} + final void bracket(char left, char right) + { + assert(false); + } + + final uint bracket(uint i, const(char)* left, uint j, const(char)* right) + { + assert(false); + } + + final void spread(uint offset, uint nbytes) + { + assert(false); + } + + final uint insert(uint offset, const(void)* data, uint nbytes) + { + assert(false); + } + + final void remove(uint offset, uint nbytes) + { + assert(false); + } + + string toChars() + { + return getString().idup; + } + + final string extractString() + { + char[] s = getString(); + data = null; + offset = 0; + size = 0; + + return assumeUnique(s); + } + + final char[] getString() + { + char* s = cast(char*)data; + return s[0..offset]; + } +} \ No newline at end of file