diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/util/filter/IteratorFilterSet.kt | 38 | ||||
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/util/textutil.kt | 26 |
2 files changed, 62 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/filter/IteratorFilterSet.kt b/src/main/kotlin/moe/nea/firmament/util/filter/IteratorFilterSet.kt new file mode 100644 index 0000000..61d6524 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/filter/IteratorFilterSet.kt @@ -0,0 +1,38 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util.filter + +abstract class IteratorFilterSet<K>(val original: java.util.Set<K>) : java.util.Set<K> by original { + abstract fun shouldKeepElement(element: K): Boolean + + override fun iterator(): MutableIterator<K> { + val parentIterator = original.iterator() + return object : MutableIterator<K> { + var lastEntry: K? = null + override fun hasNext(): Boolean { + while (lastEntry == null) { + if (!parentIterator.hasNext()) + break + val element = parentIterator.next() + if (!shouldKeepElement(element)) continue + lastEntry = element + } + return lastEntry != null + } + + override fun next(): K { + if (!hasNext()) throw NoSuchElementException() + return lastEntry ?: throw NoSuchElementException() + } + + override fun remove() { + TODO("Not yet implemented") + } + } + } +} + diff --git a/src/main/kotlin/moe/nea/firmament/util/textutil.kt b/src/main/kotlin/moe/nea/firmament/util/textutil.kt index f811bd8..1d61332 100644 --- a/src/main/kotlin/moe/nea/firmament/util/textutil.kt +++ b/src/main/kotlin/moe/nea/firmament/util/textutil.kt @@ -1,5 +1,6 @@ /* * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe> + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> * * SPDX-License-Identifier: GPL-3.0-or-later */ @@ -69,9 +70,30 @@ class TextMatcher(text: Text) { } } +val formattingChars = "kmolnrKMOLNR".toSet() +fun CharSequence.removeColorCodes(keepNonColorCodes: Boolean = false): String { + var nextParagraph = indexOf('§') + if (nextParagraph < 0) return this.toString() + val stringBuffer = StringBuilder(this.length) + var readIndex = 0 + while (nextParagraph >= 0) { + stringBuffer.append(this, readIndex, nextParagraph) + if (keepNonColorCodes && nextParagraph + 1 < length && this[nextParagraph + 1] in formattingChars) { + readIndex = nextParagraph + nextParagraph = indexOf('§', startIndex = readIndex + 1) + } else { + readIndex = nextParagraph + 2 + nextParagraph = indexOf('§', startIndex = readIndex) + } + if (readIndex > this.length) + readIndex = this.length + } + stringBuffer.append(this, readIndex, this.length) + return stringBuffer.toString() +} -val Text.unformattedString - get() = string.replace("§.".toRegex(), "") +val Text.unformattedString: String + get() = string.removeColorCodes().toString() fun Text.transformEachRecursively(function: (Text) -> Text): Text { |