Skip to content

Commit

Permalink
Replaced OutBuffer with Appender.
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoathaydes committed Jan 19, 2024
1 parent 4e8508c commit 8937b80
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/d/src/dencoder.d
Expand Up @@ -3,7 +3,7 @@ import std.conv : to;
import std.outbuffer : OutBuffer;
import std.ascii : isAlpha, isDigit;
import std.algorithm.iteration : filter;
import std.container.array;
import std.container.array : Array;

// @safe:

Expand Down Expand Up @@ -165,27 +165,35 @@ interface ISolutionHandler

final class Printer : ISolutionHandler
{
private OutBuffer buf = new OutBuffer();
private int counter;
import std.array : Appender;

this()
{
buf.reserve(4096);
private enum capacity = 10 * 4_096;

private Appender!(char[]) buf;

this() {
buf.reserve(capacity);
}

void flush()
{
write(buf.toString());
buf.clear();
write(buf[]);
buf.shrinkTo(0);
}

void put(string number, in Array!string words)
{
buf.writefln("%s:%-( %s%)", number, words[]);
counter++;
if (counter >= 100)
buf ~= number;
buf ~= ":";
foreach (word; words)
{
buf ~= ' ';
buf ~= word;
}
buf ~= '\n';

if (buf[].length > capacity - 256)
{
counter = 0;
flush();
}
}
Expand Down

0 comments on commit 8937b80

Please sign in to comment.