public static int limitUtf8Index()

in gflog-core/src/main/java/com/epam/deltix/gflog/core/util/Util.java [213:247]


    public static int limitUtf8Index(final CharSequence value, int start, final int end, int limit) {
        while (start < end) {
            final char c = value.charAt(start);
            boolean surrogate = false;

            if (c <= 0x007F) {
                limit -= 1;
            } else if (c <= 0x07FF) {
                limit -= 2;
            } else if (!Character.isSurrogate(c)) {
                limit -= 3;
            } else {
                final int next = start + 1;

                if (next == end) {
                    throw new IndexOutOfBoundsException("no space for low surrogate");
                }

                if (!Character.isSurrogatePair(c, value.charAt(next))) {
                    throw new IllegalArgumentException("not a surrogate pair");
                }

                surrogate = true;
                limit -= 4;
            }

            if (limit < 0) {
                break;
            }

            start += surrogate ? 2 : 1;
        }

        return start;
    }