On 1/11/18 6:21 AM, tipdbmp wrote:
string push_stuff(char[] buf, int x) {
     if (x == 1) {
         buf ~= 'A';
         buf ~= 'B';
         buf ~= 'C';
         return cast(string) buf[0 .. 3];
     }
     else {
         buf ~= 'A';
         buf ~= 'B';
         return cast(string) buf[0 .. 2];
     }
}

void foo() {
     {
         char[2] buf;
         string result = push_stuff(buf, 1);
         assert(buf.ptr != result.ptr);
     }

     {
         char[2] buf;
         string result = push_stuff(buf, 0);
         assert(buf.ptr == result.ptr); // <-- this assert fails
     }
}


Appender is able to do this:

import std.array;

void main()
{
    char[2] buf;
    auto app = appender(buf[]);
    app.clear; // resets the buffer so it can be used again.
    app ~= 'A';
    app ~= 'B';
    assert(buf[] == "AB");
    assert(app.data.ptr == buf.ptr);
    app ~= 'C';
    assert(app.data.ptr != buf.ptr);
}

-Steve

Reply via email to