Is it possible to append to a local buffer without reallocating?

tipdbmp email at example.com
Thu Jan 11 11:16:30 UTC 2018


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
     }
}



More information about the Digitalmars-d-learn mailing list