public static int unitCount(byte[] utf8, int offset, int length) {
        int units = 0;
        int barrier = offset + length;
        int i = offset;
        while (i < barrier) {
            byte firstByte = utf8[i];
            if (firstByte >= -16) {
                if (firstByte >= 0) {
                    ++units;
                    ++i;
                } else {
                    units += 2;
                    i += 4;
                }
            } else {
                if (firstByte >= -32) {
                    ++units;
                    i += 3;
                } else {
                    ++units;
                    i += 2;
                }
            }
        }
        return units;
    }
Count the number of Unicode code units ("UTF-16 characters") needed to
represent a given array of UTF-8 characters. This method is written to be
cheap to invoke.

Note: It is strongly assumed the sequence is valid.

@param utf8
           raw data
@param offset
           index of first byte of UTF-8 sequence to check
@param length
           number of bytes in the UTF-8 sequence to check
vespajlib/src/main/java/com/yahoo/text/Utf8.java