aboutsummaryrefslogtreecommitdiff
path: root/src/RichContent/RichString.kt
blob: f09e4715ba487cb98204de928e60e30868f14bae (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.jetbrains.dokka

public class RichString {
    private val sliceList = arrayListOf<RichStringSlice>()
    public val slices: List<RichStringSlice> get() = sliceList

    public fun addSlice(slice: RichStringSlice) {
        sliceList.add(slice)
    }

    public fun addSlice(text: String, style: RichStringStyle) {
        // TODO: decide on semantics
        // empty slices makes it hard to compare rich strings
        if (text.length > 0)
            sliceList.add(RichStringSlice(text, style))
    }

    public fun isEmpty(): Boolean = sliceList.isEmpty()

    public fun length(): Int = sliceList.fold(0) {(acc, value) -> return acc + value.text.length }

    public override fun toString(): String {
        return sliceList.joinToString("", "&")
    }

    override fun equals(other: Any?): Boolean {
        if (other !is RichString)
            return false
        if (sliceList.size != other.sliceList.size)
            return false
        for (index in sliceList.indices)
            if (!sliceList[index].equals(other.sliceList[index]))
                return false

        return true
    }

    override fun hashCode(): Int {
        return sliceList.map { it.hashCode() }.sum()
    }

    class object {
        public val empty: RichString = RichString()
    }
}

public data class RichStringSlice(public val text: String, public val style: RichStringStyle) {
    public override fun toString(): String {
        return text
    }
}

public trait RichStringStyle
public object NormalStyle : RichStringStyle
public object BoldStyle : RichStringStyle
public object CodeStyle : RichStringStyle
public data class LinkStyle(val link: String) : RichStringStyle

public fun RichString.splitBy(delimiter: String): Pair<RichString, RichString> {
    var index = 0
    while (index < slices.size && !slices[index].text.contains(delimiter)) index++
    if (index == slices.size)
        return Pair(this, RichString.empty)

    val first = RichString()
    val second = RichString()

    for (i in 0..index - 1) {
        first.addSlice(slices[i])
    }

    val splitSlice = slices[index]
    val firstText = splitSlice.text.substringBefore(delimiter)
    val secondText = splitSlice.text.substringAfter(delimiter)
    first.addSlice(firstText, splitSlice.style)
    second.addSlice(secondText, splitSlice.style)

    for (i in index + 1..slices.size - 1) {
        second.addSlice(slices[i])
    }

    return Pair(first, second)
}