On 3/23/14, 9:49 PM, Michel Fortin wrote:

dchar front(char[] s)
{
  if (s[0] < 0b1000000)
    return s[0]; // ASCII

  // pattern     indicator  tailLength
  // 0b1100xxxx  0b00 (0)   1
  // 0b1101xxxx  0b01 (1)   1 == indicator
  // 0b1110xxxx  0b10 (2)   2 == indicator
  // 0b1111xxxx  0b11 (3)   3 == indicator
  // note: undefined result for illegal 0b11111xxx case

  auto indicator = (s[0] >> 4) & 0b11;
  auto tailLength = indicator ? indicator : 1;

  dchar result = s[0] & (0b00111111 >> tailLength);
  foreach (i; 0..tailLength)
      result = (result << 6) | (s[1+i] & 0b00111111);
  return result;
}

http://goo.gl/y9EVFr

Andrei

Reply via email to