Showing with 6 additions and 36 deletions.
  1. +6 −36 std/outbuffer.d
42 changes: 6 additions & 36 deletions std/outbuffer.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@ Serialize data to $(D ubyte) arrays.
*/
module std.outbuffer;

private
{
import core.memory;
import core.stdc.stdarg;
import core.stdc.stdio;
import core.stdc.stdlib;
import std.algorithm;
import std.string;
}
import core.stdc.stdarg; // : va_list;

/*********************************************
* OutBuffer provides a way to build up an array of bytes out
Expand All @@ -39,21 +31,14 @@ class OutBuffer

invariant()
{
//printf("this = %p, offset = %x, data.length = %u\n", this, offset, data.length);
assert(offset <= data.length);
}

pure nothrow @safe
{
this()
{
//printf("in OutBuffer constructor\n");
}

/*********************************
* Convert to array of bytes.
*/

ubyte[] toBytes() { return data[0 .. offset]; }

/***********************************
Expand All @@ -63,8 +48,6 @@ class OutBuffer
* speed optimization, a good guess at the maximum size of the resulting
* buffer will improve performance by eliminating reallocations and copying.
*/


void reserve(size_t nbytes) @trusted
in
{
Expand All @@ -76,7 +59,6 @@ class OutBuffer
}
body
{
//c.stdio.printf("OutBuffer.reserve: length = %d, offset = %d, nbytes = %d\n", data.length, offset, nbytes);
if (data.length < offset + nbytes)
{
void[] vdata = data;
Expand Down Expand Up @@ -257,6 +239,10 @@ class OutBuffer

void vprintf(string format, va_list args) @trusted nothrow
{
import std.string : toStringz;
import core.stdc.stdio : vsnprintf;
import core.stdc.stdlib : alloca;

version (unittest)
char[3] buffer = void; // trigger reallocation
else
Expand Down Expand Up @@ -303,11 +289,6 @@ class OutBuffer
else
break;

/+
if (p != buffer)
c.stdlib.free(p);
p = (char *) c.stdlib.malloc(psize); // buffer too small, try again with larger size
+/
p = cast(char *) alloca(psize); // buffer too small, try again with larger size
}
else
Expand All @@ -316,13 +297,6 @@ class OutBuffer
}
}
write(cast(ubyte[]) p[0 .. count]);
/+
version (Posix)
{
if (p != buffer)
c.stdlib.free(p);
}
+/
}

/*****************************************
Expand Down Expand Up @@ -415,19 +389,15 @@ class OutBuffer

@safe unittest
{
//printf("Starting OutBuffer test\n");
import std.string : cmp;

OutBuffer buf = new OutBuffer();

//printf("buf = %p\n", buf);
//printf("buf.offset = %x\n", buf.offset);
assert(buf.offset == 0);
buf.write("hello"[]);
buf.write(cast(byte)0x20);
buf.write("world"[]);
buf.printf(" %d", 62665);
//auto s = buf.toString();
//printf("buf = '%.*s'\n", s.length, s.ptr);
assert(cmp(buf.toString(), "hello world 62665") == 0);
}

Expand Down