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

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon Mar 24 09:26:01 PDT 2014


On 3/24/14, 1:56 AM, dnspies wrote:
> 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

That turned out quite large. -- Andrei



More information about the Digitalmars-d mailing list