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

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


On Monday, 24 March 2014 at 08:06:53 UTC, dnspies wrote:
> 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);
> }

Sorry, I misunderstood.  We only want the x's in the output.  
Here's my revised solution

http://goo.gl/PL729J

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


More information about the Digitalmars-d mailing list