From a06c5c96e3d1879ab944af08d4cc926948fbd8a8 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Mon, 11 Aug 2025 22:48:55 +0300 Subject: [PATCH] chore(purity): IdChecker --- build.gradle.kts | 14 +++++++------- core/src/com/unciv/logic/IdChecker.kt | 13 +++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 1d85163831..748031476d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -49,10 +49,10 @@ allprojects { apply(plugin = "io.github.yairm210.purity-plugin") configure{ wellKnownPureFunctions = setOf( - "java.util.regex.Pattern.matcher", - "java.util.regex.Matcher.find", - "java.util.regex.Matcher.replaceAll", - "kotlin.collections.linkedMapOf", + "java.util.regex.Pattern.matcher", // moved + "java.util.regex.Matcher.find", // moved + "java.util.regex.Matcher.replaceAll", // moved + "kotlin.collections.linkedMapOf", // moved ) wellKnownReadonlyFunctions = setOf( "com.badlogic.gdx.math.Vector2.len", @@ -66,11 +66,11 @@ allprojects { "com.badlogic.gdx.files.FileHandle.isFile", "com.badlogic.gdx.files.FileHandle.name", - "kotlin.collections.sortBy", - "kotlin.Throwable.getStackTrace", - "java.lang.StackTraceElement.getClassName", + "kotlin.collections.sortBy", // moved + "kotlin.Throwable.getStackTrace", // moved ) wellKnownPureClasses = setOf( + "java.lang.StackTraceElement" // moved ) wellKnownInternalStateClasses = setOf( "com.badlogic.gdx.math.Vector2", diff --git a/core/src/com/unciv/logic/IdChecker.kt b/core/src/com/unciv/logic/IdChecker.kt index 9ea1fbbfda..fa6d534157 100644 --- a/core/src/com/unciv/logic/IdChecker.kt +++ b/core/src/com/unciv/logic/IdChecker.kt @@ -1,6 +1,6 @@ package com.unciv.logic -import yairm210.purity.annotations.Readonly +import yairm210.purity.annotations.Pure import java.util.Locale import kotlin.math.abs @@ -24,14 +24,17 @@ import kotlin.math.abs */ object IdChecker { + @Pure fun checkAndReturnPlayerUuid(playerId: String): String? { return checkAndReturnUuiId(playerId, "P") } + @Pure fun checkAndReturnGameUuid(gameId: String): String? { return checkAndReturnUuiId(gameId, "G") } + @Pure private fun checkAndReturnUuiId(id: String, prefix: String): String? { val trimmedPlayerId = id.trim() if (trimmedPlayerId.length == 40) { // length of a UUID (36) with pre- and postfix @@ -56,7 +59,7 @@ object IdChecker { /** * Adapted from https://wiki.openmrs.org/display/docs/Check+Digit+Algorithm */ - @Readonly + @Pure fun getCheckDigit(uuid: String): Int? { // allowable characters within identifier @Suppress("SpellCheckingInspection") @@ -69,8 +72,7 @@ object IdChecker { var sum = 0 // loop through digits from right to left - for (i in idWithoutCheckdigit.indices) { - + idWithoutCheckdigit.indices.forEach { i -> //set ch to "current" character to be processed val ch = idWithoutCheckdigit[idWithoutCheckdigit.length - i - 1] @@ -82,7 +84,7 @@ object IdChecker { // weight will be the current digit's contribution to // the running total - var weight: Int + val weight: Int if (i % 2 == 0) { // for alternating digits starting with the rightmost, we @@ -102,7 +104,6 @@ object IdChecker { } // keep a running total of weights sum += weight - } // avoid sum less than 10 (if characters below "0" allowed, // this could happen)