Repository: micronaut-projects/micronaut-core
File: core/src/main/java/io/micronaut/core/util/StringUtils.java (Java)

Code before:
                count++;
            }
        }
        List<String> result = new ArrayList<>(count + 1);
        SplitOmitEmptyIterator iterator = new SplitOmitEmptyIterator(sequence, splitCharacter);
        while (iterator.hasNext()) {
            result.add(iterator.next());
        }
        return result;
    }

    /**
     * Fast split by one character iterator. Implementation omits empty strings.
     * This should be a fast alternative to {@link String#split(String)} where it's used with one character.
     *
     * @param sequence The sequence to split
     * @param splitCharacter The split character
     * @since 2.5.0
     * @return The iterator of possible sequences
     */
    public static Iterator<String> splitOmitEmptyStringsIterator(final CharSequence sequence, final char splitCharacter) {
        Objects.requireNonNull(sequence);
        return new SplitOmitEmptyIterator(sequence, splitCharacter);
    }

    public static int byteCount(@NonNull String str) {
        return str.getBytes(StandardCharsets.UTF_8).length;
    }

    /**
     * Fast split by one character iterator.
     *
     * @author Denis Stepanov
     * @since 2.5.0
     */
    private static final class SplitOmitEmptyIterator implements Iterator<String>, Iterable<String> {

        private final CharSequence sequence;
        private final char splitCharacter;
        private final int length;
        private int index = 0;
        private int fromIndex = 0;
        private int toIndex = 0;
        private boolean end = false;
        private boolean hasNext = true;
        private boolean adjust = true;

        private SplitOmitEmptyIterator(CharSequence sequence, char splitCharacter) {
            this.sequence = sequence;
            this.splitCharacter = splitCharacter;
            this.length = sequence.length();

Review comment (suggestion):
You can copy netty `utf8ByteCount`.

Even better would be a specialized check that the size doesnt exceed the limit. e.g. if `str.length() * 3 < limit`, we don't need to check every byte, and if `str.length() * 2 > limit` we also don't need to

Code after:
        List<String> result = new ArrayList<>(count + 1);
        SplitOmitEmptyIterator iterator = new SplitOmitEmptyIterator(sequence, splitCharacter);
        while (iterator.hasNext()) {
            result.add(iterator.next());
        }
        return result;
    }

    /**
     * Fast split by one character iterator. Implementation omits empty strings.
     * This should be a fast alternative to {@link String#split(String)} where it's used with one character.
     *
     * @param sequence The sequence to split
     * @param splitCharacter The split character
     * @since 2.5.0
     * @return The iterator of possible sequences
     */
    public static Iterator<String> splitOmitEmptyStringsIterator(final CharSequence sequence, final char splitCharacter) {
        Objects.requireNonNull(sequence);
        return new SplitOmitEmptyIterator(sequence, splitCharacter);
    }

    /**
     * Port from Netty StringUtil.
     * @see <a href="https://github.com/netty/netty/blob/4.2/common/src/main/java/io/netty/util/internal/StringUtil.java">Netty StringUtil</a>
     * @param seq String for which you wish to calculate the number of bytes for UTF-8
     * @return  the exact bytes length of UTF8 character sequence.
     */
    public static int utf8Bytes(final CharSequence seq) {
        return utf8ByteCount(seq, 0, seq.length());
    }

    /**
     * Port from Netty StringUtil.
     * @see <a href="https://github.com/netty/netty/blob/4.2/common/src/main/java/io/netty/util/internal/StringUtil.java">Netty StringUtil</a>
     */
    private static int utf8ByteCount(final CharSequence seq, int start, int end) {
        int i = start;
        // ASCII fast path
        while (i < end && seq.charAt(i) < 0x80) {
            ++i;
        }
        // !ASCII is packed in a separate method to let the ASCII case be smaller
        return i < end ? (i - start) + utf8BytesNonAscii(seq, i, end) : i - start;
    }

    /**
     * Determine if {@code c} lies within the range of values defined for.
     * <a href="https://unicode.org/glossary/#surrogate_code_point">Surrogate Code Point</a>.
     * Port from Netty https://github.com/netty/netty/blob/4.2/common/src/main/java/io/netty/util/internal/StringUtil.java
     * @param c the character to check.