aboutsummaryrefslogtreecommitdiff
path: root/src/texturePacks/java/moe/nea/firmament/features/texturepack/StringMatcher.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/texturePacks/java/moe/nea/firmament/features/texturepack/StringMatcher.kt')
-rw-r--r--src/texturePacks/java/moe/nea/firmament/features/texturepack/StringMatcher.kt47
1 files changed, 45 insertions, 2 deletions
diff --git a/src/texturePacks/java/moe/nea/firmament/features/texturepack/StringMatcher.kt b/src/texturePacks/java/moe/nea/firmament/features/texturepack/StringMatcher.kt
index 7de84d2..863ca3b 100644
--- a/src/texturePacks/java/moe/nea/firmament/features/texturepack/StringMatcher.kt
+++ b/src/texturePacks/java/moe/nea/firmament/features/texturepack/StringMatcher.kt
@@ -23,6 +23,10 @@ interface StringMatcher {
return matches(text.string)
}
+ val asRegex: java.util.regex.Pattern
+
+ fun matchWithGroups(string: String): MatchNamedGroupCollection?
+
fun matches(nbt: NbtString): Boolean {
val string = nbt.value
val jsonStart = string.indexOf('{')
@@ -36,20 +40,59 @@ interface StringMatcher {
}
class Equals(input: String, val stripColorCodes: Boolean) : StringMatcher {
+ override val asRegex by lazy(LazyThreadSafetyMode.PUBLICATION) { input.toPattern(java.util.regex.Pattern.LITERAL) }
private val expected = if (stripColorCodes) input.removeColorCodes() else input
override fun matches(string: String): Boolean {
return expected == (if (stripColorCodes) string.removeColorCodes() else string)
}
+ override fun matchWithGroups(string: String): MatchNamedGroupCollection? {
+ if (matches(string))
+ return object : MatchNamedGroupCollection {
+ override fun get(name: String): MatchGroup? {
+ return null
+ }
+
+ override fun get(index: Int): MatchGroup? {
+ return null
+ }
+
+ override val size: Int
+ get() = 0
+
+ override fun isEmpty(): Boolean {
+ return true
+ }
+
+ override fun contains(element: MatchGroup?): Boolean {
+ return false
+ }
+
+ override fun iterator(): Iterator<MatchGroup?> {
+ return emptyList<MatchGroup>().iterator()
+ }
+
+ override fun containsAll(elements: Collection<MatchGroup?>): Boolean {
+ return elements.isEmpty()
+ }
+ }
+ return null
+ }
+
override fun toString(): String {
return "Equals($expected, stripColorCodes = $stripColorCodes)"
}
}
class Pattern(val patternWithColorCodes: String, val stripColorCodes: Boolean) : StringMatcher {
- private val regex: Predicate<String> = patternWithColorCodes.toPattern().asMatchPredicate()
+ private val pattern = patternWithColorCodes.toRegex()
+ override val asRegex = pattern.toPattern()
override fun matches(string: String): Boolean {
- return regex.test(if (stripColorCodes) string.removeColorCodes() else string)
+ return pattern.matches(if (stripColorCodes) string.removeColorCodes() else string)
+ }
+
+ override fun matchWithGroups(string: String): MatchNamedGroupCollection? {
+ return pattern.matchEntire(if (stripColorCodes) string.removeColorCodes() else string)?.groups as MatchNamedGroupCollection?
}
override fun toString(): String {