Add custom model predicates
Add regex support Add and and or predicates
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user