From 602112724d8236c1ec6671e1893128862c9f5815 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Thu, 22 Feb 2024 00:30:05 +0100 Subject: Add custom model predicates Add regex support Add and and or predicates --- .../nea/firmament/util/filter/IteratorFilterSet.kt | 38 ++++++++++++++++++++++ src/main/kotlin/moe/nea/firmament/util/textutil.kt | 26 +++++++++++++-- 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/util/filter/IteratorFilterSet.kt (limited to 'src/main/kotlin/moe/nea/firmament/util') 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 + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util.filter + +abstract class IteratorFilterSet(val original: java.util.Set) : java.util.Set by original { + abstract fun shouldKeepElement(element: K): Boolean + + override fun iterator(): MutableIterator { + val parentIterator = original.iterator() + return object : MutableIterator { + 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 + * SPDX-FileCopyrightText: 2024 Linnea Gräf * * 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 { -- cgit