aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
diff options
context:
space:
mode:
authorCalMWolfs <94038482+CalMWolfs@users.noreply.github.com>2024-02-16 21:21:43 +1100
committerGitHub <noreply@github.com>2024-02-16 11:21:43 +0100
commitf91973d60948d449cc45a4add901e6fe43aebd62 (patch)
tree4c8c77ec4a9585a821651a034ebe5bed3308a6af /src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
parent26fe548fa9a5cfe29b130a0a5585278df3429ee9 (diff)
downloadskyhanni-f91973d60948d449cc45a4add901e6fe43aebd62.tar.gz
skyhanni-f91973d60948d449cc45a4add901e6fe43aebd62.tar.bz2
skyhanni-f91973d60948d449cc45a4add901e6fe43aebd62.zip
Moved many regex patterns in the repo and code cleanup. #871
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt44
1 files changed, 9 insertions, 35 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
index 926efc877..001315baf 100644
--- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
+++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt
@@ -4,31 +4,21 @@ import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.mixins.transformers.AccessorChatComponentText
import at.hannibal2.skyhanni.utils.GuiRenderUtils.darkenColor
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
-import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.client.Minecraft
import net.minecraft.client.gui.GuiUtilRenderComponents
import net.minecraft.util.ChatComponentText
import net.minecraft.util.IChatComponent
import java.util.Base64
-import java.util.NavigableMap
import java.util.UUID
import java.util.function.Predicate
import java.util.regex.Matcher
import java.util.regex.Pattern
object StringUtils {
-
- // TODO USE SH-REPO
- private val playerChatPattern = "(?<important>.*?)(?:§[f7r])*: .*".toPattern()
- private val chatUsernamePattern =
- "^(?:§\\w\\[§\\w\\d+§\\w] )?(?:(?:§\\w)+\\S )?(?<rankedName>(?:§\\w\\[\\w.+] )?(?:§\\w)?(?<username>\\w+))(?: (?:§\\w)?\\[.+?])?".toPattern()
private val whiteSpaceResetPattern = "^(?:\\s|§r)*|(?:\\s|§r)*$".toPattern()
private val whiteSpacePattern = "^\\s*|\\s*$".toPattern()
private val resetPattern = "(?i)§R".toPattern()
- private val isRomanPattern by RepoPattern.pattern(
- "utils.string.isroman",
- "^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})"
- )
+ private val stringColourPattern = "§[0123456789abcdef].*".toPattern()
fun String.trimWhiteSpaceAndResets(): String = whiteSpaceResetPattern.matcher(this).replaceAll("")
fun String.trimWhiteSpace(): String = whiteSpacePattern.matcher(this).replaceAll("")
@@ -68,31 +58,17 @@ object StringUtils {
return builder.toString()
}
- /**
- * From https://stackoverflow.com/questions/10711494/get-values-in-treemap-whose-string-keys-start-with-a-pattern
- */
- fun <T> subMapWithKeysThatAreSuffixes(prefix: String, map: NavigableMap<String?, T>): Map<String?, T>? {
- if ("" == prefix) return map
- val lastKey = nextLexicographicallyStringWithSameLength(prefix)
- return map.subMap(prefix, true, lastKey, false)
- }
-
- fun nextLexicographicallyStringWithSameLength(input: String): String {
- val lastCharPosition = input.length - 1
- val inputWithoutLastChar = input.substring(0, lastCharPosition)
- val lastChar = input[lastCharPosition]
- val incrementedLastChar = (lastChar.code + 1).toChar()
- return inputWithoutLastChar + incrementedLastChar
- }
-
fun UUID.toDashlessUUID(): String {
return toString().replace("-", "")
}
- // TODO find better name for this method
+
inline fun <T> Pattern.matchMatcher(text: String, consumer: Matcher.() -> T) =
matcher(text).let { if (it.matches()) consumer(it) else null }
+ inline fun <T> Pattern.findMatcher(text: String, consumer: Matcher.() -> T) =
+ matcher(text).let { if (it.find()) consumer(it) else null }
+
private fun String.internalCleanPlayerName(): String {
val split = trim().split(" ")
return if (split.size > 1) {
@@ -122,9 +98,7 @@ object StringUtils {
}
fun getColor(string: String, default: Int, darker: Boolean = true): Int {
- val stringPattern = "§[0123456789abcdef].*".toPattern()
-
- val matcher = stringPattern.matcher(string)
+ val matcher = stringColourPattern.matcher(string)
if (matcher.matches()) {
val colorInt = Minecraft.getMinecraft().fontRendererObj.getColorCode(string[1])
return if (darker) {
@@ -265,7 +239,7 @@ object StringUtils {
private fun matchPlayerChatMessage(string: String): Matcher? {
var username = ""
- var matcher = playerChatPattern.matcher(string)
+ var matcher = UtilsPatterns.playerChatPattern.matcher(string)
if (matcher.matches()) {
username = matcher.group("important").removeResets()
}
@@ -282,7 +256,7 @@ object StringUtils {
username = username.removePrefix("§dFrom ")
username = username.removePrefix("§dTo ")
- matcher = chatUsernamePattern.matcher(username)
+ matcher = UtilsPatterns.chatUsernamePattern.matcher(username)
return if (matcher.matches()) matcher else null
}
@@ -298,6 +272,6 @@ object StringUtils {
fun String?.equalsIgnoreColor(string: String?) = this?.let { it.removeColor() == string?.removeColor() } ?: false
fun String.isRoman(): Boolean {
- return isRomanPattern.matches(this)
+ return UtilsPatterns.isRomanPattern.matches(this)
}
}