zPos function

int zPos(
  1. int x
)

Determines the number of bytes needed to represent a Unicode character in UTF-8. Returns the number of continuation bytes (so total bytes = return value + 1)

Implementation

int zPos(int x) {
  if ((x & 0x80) == 0) return 0; // 0xxxxxxx = ASCII (0 continuation bytes)
  if ((x & 0xE0) == 0xC0) return 1; // 110xxxxx = 2-byte (1 continuation byte)
  if ((x & 0xF0) == 0xE0) return 2; // 1110xxxx = 3-byte (2 continuation bytes)
  if ((x & 0xF8) == 0xF0) return 3; // 11110xxx = 4-byte (3 continuation bytes)
  return 4; // Invalid or unsupported
}