Challenge: write a really really small front() for UTF8
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Mon Mar 24 09:23:55 PDT 2014
On 3/24/14, 1:06 AM, 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);
> }
http://goo.gl/HBSv5T - looks nice! For minimum size replace the
assert(false) with one inside the loop: http://goo.gl/eWs0ft
Andrei
More information about the Digitalmars-d
mailing list