fun offsetByCodePoints()

in src/commonMain/kotlin/org/intellij/markdown/lexer/Compat.kt [30:66]


    fun offsetByCodePoints(seq: CharSequence, index: Int,
                           codePointOffset: Int): Int {
        val length = seq.length
        if (index < 0 || index > length) {
            throw IndexOutOfBoundsException()
        }

        var x = index
        if (codePointOffset >= 0) {
            var i: Int
            i = 0
            while (x < length && i < codePointOffset) {
                if (seq[x++].isHighSurrogate() && x < length &&
                        seq[x].isLowSurrogate()) {
                    x++
                }
                i++
            }
            if (i < codePointOffset) {
                throw IndexOutOfBoundsException()
            }
        } else {
            var i: Int
            i = codePointOffset
            while (x > 0 && i < 0) {
                if (seq[--x].isLowSurrogate() && x > 0 &&
                        seq[x - 1].isHighSurrogate()) {
                    x--
                }
                i++
            }
            if (i < 0) {
                throw IndexOutOfBoundsException()
            }
        }
        return x
    }