aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/Model/CodeNode.kt
blob: 17b5062760a5935a4b97e866043043edc7330307 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.jetbrains.dokka.Model

import org.jsoup.helper.StringUtil.isWhitespace
import org.jsoup.nodes.TextNode

class CodeNode(text: String, baseUri: String): TextNode(text, baseUri) {

    override fun text(): String {
        return normaliseInitialWhitespace(wholeText.removePrefix("<code>")
            .removeSuffix("</code>"))
    }

    private fun normaliseInitialWhitespace(text: String): String {
        val sb = StringBuilder(text.length)
        removeInitialWhitespace(sb, text)
        return sb.toString()
    }

    /**
     * Remove initial whitespace.
     * @param accum builder to append to
     * @param string string to remove the initial whitespace
     */
    private fun removeInitialWhitespace(accum: StringBuilder, string: String) {
        var reachedNonWhite = false

        val len = string.length
        var c: Int
        var i = 0
        while (i < len) {
            c = string.codePointAt(i)
            if (isWhitespace(c) && !reachedNonWhite) {
                i += Character.charCount(c)
                continue
            } else {
                accum.appendCodePoint(c)
                reachedNonWhite = true
            }
            i += Character.charCount(c)
        }
    }
}