Challenge: write a really really small front() for UTF8

dnspies dspies at ualberta.ca
Mon Mar 24 01:06:52 PDT 2014


On Sunday, 23 March 2014 at 21:23:18 UTC, Andrei Alexandrescu 
wrote:
> Here's a baseline: http://goo.gl/91vIGc. Destroy!
>
> Andrei

Here's mine (the loop gets unrolled):

dchar front(const char[] s) {
   assert(s.length > 0);
   byte c = s[0];
   dchar res = cast(ubyte)c;
   if(c >= 0) {
     return res;
   }
   c <<= 1;
   assert(c < 0);
   for(int i = 1; i < 4; i++) {
     assert(i < s.length);
     ubyte d = s[i];
     assert((d >> 6) == 0b10);
     res = (res << 8) | d;
     c <<= 1;
     if(c >= 0)
       return res;
   }
   assert(false);
}


More information about the Digitalmars-d mailing list