From f91973d60948d449cc45a4add901e6fe43aebd62 Mon Sep 17 00:00:00 2001 From: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> Date: Fri, 16 Feb 2024 21:21:43 +1100 Subject: Moved many regex patterns in the repo and code cleanup. #871 --- .../at/hannibal2/skyhanni/utils/StringUtils.kt | 44 +++++----------------- 1 file changed, 9 insertions(+), 35 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt') 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 = "(?.*?)(?:§[f7r])*: .*".toPattern() - private val chatUsernamePattern = - "^(?:§\\w\\[§\\w\\d+§\\w] )?(?:(?:§\\w)+\\S )?(?(?:§\\w\\[\\w.+] )?(?:§\\w)?(?\\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 subMapWithKeysThatAreSuffixes(prefix: String, map: NavigableMap): Map? { - 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 Pattern.matchMatcher(text: String, consumer: Matcher.() -> T) = matcher(text).let { if (it.matches()) consumer(it) else null } + inline fun 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) } } -- cgit