When creating a string from a ubyte[], I have an invalid length and string.strip() doesn't strip off all whitespace. I'm new to the language. Is this a compiler issue?

import std.string : strip;
import std.stdio  : writefln;

int main()
{
   const string ATA_STR = " ATA ";

   // this works fine
   {
      ubyte[] buffer = [' ', 'A', 'T', 'A', ' ' ];
      string test = strip(cast(string)(buffer));
      assert(test == strip(ATA_STR));
   }

   // This is where things breaks
   {
      ubyte[] buff = new ubyte[16];
      buff[0..ATA_STR.length] = cast(ubyte[])(ATA_STR);

// read the string back from the buffer, stripping whitespace
      string stringFromBuffer = strip(cast(string)(buff[0..16]));
      // this shows strip() doesn't remove all whitespace
writefln("StrFromBuff is '%s'; length %d", stringFromBuffer, stringFromBuffer.length);

      // !! FAILS. stringFromBuffer is length 15, not 3.
      assert(stringFromBuffer.length == strip(ATA_STR).length);

   }

   return 0;
}

Reply via email to