class Varchars {

    public static int byteCount(Slice slice, int offset, int length, int codePointCount)
    {
        requireNonNull(slice, "slice is null");
        if (length < 0) {
            throw new IllegalArgumentException("length must be greater than or equal to zero");
        }
        if (offset < 0 || offset + length > slice.length()) {
            throw new IllegalArgumentException("invalid offset/length");
        }
        if (codePointCount < 0) {
            throw new IllegalArgumentException("codePointsCount must be greater than or equal to zero");
        }
        if (codePointCount == 0) {
            return 0;
        }

        // min codepoint size is 1 byte.
        // if size in bytes is less than the max length
        // we don't need to decode codepoints
        if (codePointCount > length) {
            return length;
        }

        // get the end index with respect to code point count
        int endIndex = offsetOfCodePoint(slice, offset, codePointCount);
        if (endIndex < 0) {
            // end index runs over the slice's length
            return length;
        }
        if (offset > endIndex) {
            throw new AssertionError("offset cannot be smaller than or equal to endIndex");
        }

        // end index could run over length because of large code points (e.g., 4-byte code points)
        // or within length because of small code points (e.g., 1-byte code points)
        return min(endIndex - offset, length);
    }

    private  Varchars();

}