On Saturday, 15 October 2016 at 18:40:11 UTC, Uplink_Coder wrote:
It can also be written like this producing smaller code.
But it the cost of slower decoding.

dchar myFront(ref char[] str) pure
{
    dchar c = cast(dchar) str.ptr[0];
    if (c & 128)
    {
        if (c & 64)
        {
            int idx = 0;
            int l = charWidthTab.ptr[c - 192];
            if (str.length < l)
                goto Linvalid;
            c = 0;
          l--;
            while(l) {
              l--;
               c |= str.ptr[idx++];
               c <<= 6;
            }
            c |= str.ptr[idx];

       }
        else
    Linvalid : throw new Exception("yadayada");

    }
    return c;
}

Just a question. Do encoding errors not have to be detected or is validity of the string guaranteed? Wrong continuation bytes or overlong encodings are not detected by this routine.

Reply via email to