CTFE Status

Stefan Koch via Digitalmars-d digitalmars-d at puremagic.com
Wed Nov 9 17:17:48 PST 2016


After the bad news now a bit of good news.
I just finished the strCat algorithm;

The reason this took so much time is because of the 
representation the heap has in the elevator.
it is a uint[] meaning you are getting into trouble with offsets 
if the first string is not a multiple of 4 characters long:)
This is the code for strcat :

uint[] strcat(const uint[] a, const uint[] b, uint aLength = 0, 
uint bLength = 0)
{
   aLength = a[0];
   bLength = b[0];
   uint resultLength = a[0] + b[0];
   uint[] result;
   result.length = neededArraySize(resultLength);
   assert(result.length == (a.length + b.length) - 2);
   result[0] = resultLength;
   auto resultPosition = 1;

   auto aMinusOne = !(aLength & 3);
   foreach(p, ca; a[1 .. $ - aMinusOne])
   {
     result[resultPosition++] = ca;
   }
   // this one is easy we just copy
     auto bMinusOne = !(bLength & 3);

   uint offset = aLength & 3;
   if (offset)
   {
     assert(offset == 2);
     resultPosition--;
     foreach(p, cb; b[1 .. $])
     {
       result[resultPosition++] |= (cb & ((1U << (offset*8)) -1)) 
<< (offset*8);
       if (resultPosition == result.length) break;
       result[resultPosition++] |= (cb & ~((1U << (offset*8)) -1)) 
 >> (offset*8);
       resultPosition--;
     }
   }
   else
   {
     foreach(p, cb; b[1 .. $ - bMinusOne])
     {
       result[resultPosition++] = cb;
     }
   }

   return result;
}

uint neededArraySize(uint size)
{
   return (((size - 1) / 4) + 2) + (!(size & 3));
}



More information about the Digitalmars-d mailing list