From 5cbaf55dd622982db07a0447d7e260df2750f13d Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 21 Nov 2021 12:48:13 +0000 Subject: [PATCH] Add hash function used by pre-JS5 caches Signed-off-by: Graham --- util/src/main/kotlin/org/openrs2/util/StringUtils.kt | 8 ++++++++ util/src/test/kotlin/org/openrs2/util/StringUtilsTest.kt | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/util/src/main/kotlin/org/openrs2/util/StringUtils.kt b/util/src/main/kotlin/org/openrs2/util/StringUtils.kt index 42ae8c86..07f27e4e 100644 --- a/util/src/main/kotlin/org/openrs2/util/StringUtils.kt +++ b/util/src/main/kotlin/org/openrs2/util/StringUtils.kt @@ -19,6 +19,14 @@ public fun CharSequence.krHashCode(): Int { return hash } +public fun CharSequence.jagHashCode(): Int { + var hash = 0 + for (c in this) { + hash = (hash * 61) + (c.code - 32) + } + return hash +} + public fun String.capitalize(): String { return replaceFirstChar { it.titlecase() } } diff --git a/util/src/test/kotlin/org/openrs2/util/StringUtilsTest.kt b/util/src/test/kotlin/org/openrs2/util/StringUtilsTest.kt index 11c7abde..4e0dfa97 100644 --- a/util/src/test/kotlin/org/openrs2/util/StringUtilsTest.kt +++ b/util/src/test/kotlin/org/openrs2/util/StringUtilsTest.kt @@ -21,6 +21,12 @@ class StringUtilsTest { assertEquals(92340183, "h€llo".krHashCode()) } + @Test + fun testJagHashCode() { + assertEquals(0, "".jagHashCode()) + assertEquals(1012849752, "hello".jagHashCode()) + } + @Test fun testCapitalize() { assertEquals("Hello", "hello".capitalize())