From 941fcb3fdde4703f7b138c64672c4fde2c69a48b Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 13 Dec 2019 12:11:45 +0800 Subject: [PATCH] Simplify --- .../contact/GroupIdConvertions.kt | 119 +++++---------- .../mirai/contact/GroupIdConversionsKtTest.kt | 139 ++++++++++++++++++ 2 files changed, 173 insertions(+), 85 deletions(-) create mode 100644 mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/contact/GroupIdConversionsKtTest.kt diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/GroupIdConvertions.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/GroupIdConvertions.kt index de6cc8a7c..e383d3bb8 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/GroupIdConvertions.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/GroupIdConvertions.kt @@ -2,101 +2,50 @@ package net.mamoe.mirai.contact -fun GroupId.toInternalId(): GroupInternalId {//求你别出错 - val left: Long = this.value.toString().let { - if (it.length < 6) { - return GroupInternalId(this.value) - } - it.substring(0, it.length - 6).toLong() - } - val right: Long = this.value.toString().let { - it.substring(it.length - 6).toLong() +import kotlin.math.pow + + +@Suppress("ObjectPropertyName") +private val `10EXP6` = 10.0.pow(6).toUInt() + + +fun GroupId.toInternalId(): GroupInternalId { + if (this.value <= `10EXP6`) { + return GroupInternalId(this.value) } + val left: Long = this.value.toString().dropLast(6).toLong() + val right: Long = this.value.toString().takeLast(6).toLong() return GroupInternalId( when (left) { - in 1..10 -> { - ((left + 202).toString() + right.toString()).toUInt() - } - in 11..19 -> { - ((left + 469).toString() + right.toString()).toUInt() - } - in 20..66 -> { - ((left + 208).toString() + right.toString()).toUInt() - } - in 67..156 -> { - ((left + 1943).toString() + right.toString()).toUInt() - } - in 157..209 -> { - ((left + 199).toString() + right.toString()).toUInt() - } - in 210..309 -> { - ((left + 389).toString() + right.toString()).toUInt() - } - in 310..499 -> { - ((left + 349).toString() + right.toString()).toUInt() - } + in 1..10 -> ((left + 202).toString() + right.toString()).toUInt() + in 11..19 -> ((left + 469).toString() + right.toString()).toUInt() + in 20..66 -> ((left + 208).toString() + right.toString()).toUInt() + in 67..156 -> ((left + 1943).toString() + right.toString()).toUInt() + in 157..209 -> ((left + 199).toString() + right.toString()).toUInt() + in 210..309 -> ((left + 389).toString() + right.toString()).toUInt() + in 310..499 -> ((left + 349).toString() + right.toString()).toUInt() else -> this.value } ) } -fun GroupInternalId.toId(): GroupId = with(value) { - //求你别出错 - var left: UInt = this.toString().let { - if (it.length < 6) { - return GroupId(value) - } - it.substring(0 until it.length - 6).toUInt() +fun GroupInternalId.toId(): GroupId = with(value.toString()) { + if (value < `10EXP6`) { + return GroupId(value) } + val left: UInt = this.dropLast(6).toUInt() - return GroupId(when (left.toInt()) { - in 203..212 -> { - val right: UInt = this.toString().let { - it.substring(it.length - 6).toUInt() - } - ((left - 202u).toString() + right.toString()).toUInt() + return GroupId( + when (left.toInt()) { + in 203..212 -> ((left - 202u).toString() + this.takeLast(6).toInt().toString()).toUInt() + in 480..488 -> ((left - 469u).toString() + this.takeLast(6).toInt().toString()).toUInt() + in 2100..2146 -> ((left.toString().take(3).toUInt() - 208u).toString() + this.takeLast(7).toInt().toString()).toUInt() + in 2010..2099 -> ((left - 1943u).toString() + this.takeLast(6).toInt().toString()).toUInt() + in 2147..2199 -> ((left.toString().take(3).toUInt() - 199u).toString() + this.takeLast(7).toInt().toString()).toUInt() + in 4100..4199 -> ((left.toString().take(3).toUInt() - 389u).toString() + this.takeLast(7).toInt().toString()).toUInt() + in 3800..3989 -> ((left.toString().take(3).toUInt() - 349u).toString() + this.takeLast(7).toInt().toString()).toUInt() + else -> value } - in 480..488 -> { - val right: UInt = this.toString().let { - it.substring(it.length - 6).toUInt() - } - ((left - 469u).toString() + right.toString()).toUInt() - } - in 2100..2146 -> { - val right: UInt = this.toString().let { - it.substring(it.length - 7).toUInt() - } - left = left.toString().substring(0 until 3).toUInt() - ((left - 208u).toString() + right.toString()).toUInt() - } - in 2010..2099 -> { - val right: UInt = this.toString().let { - it.substring(it.length - 6).toUInt() - } - ((left - 1943u).toString() + right.toString()).toUInt() - } - in 2147..2199 -> { - val right: UInt = this.toString().let { - it.substring(it.length - 7).toUInt() - } - left = left.toString().substring(0 until 3).toUInt() - ((left - 199u).toString() + right.toString()).toUInt() - } - in 4100..4199 -> { - val right: UInt = this.toString().let { - it.substring(it.length - 7).toUInt() - } - left = left.toString().substring(0 until 3).toUInt() - ((left - 389u).toString() + right.toString()).toUInt() - } - in 3800..3989 -> { - val right: UInt = this.toString().let { - it.substring(it.length - 7).toUInt() - } - left = left.toString().substring(0 until 3).toUInt() - ((left - 349u).toString() + right.toString()).toUInt() - } - else -> value - }) + ) } \ No newline at end of file diff --git a/mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/contact/GroupIdConversionsKtTest.kt b/mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/contact/GroupIdConversionsKtTest.kt new file mode 100644 index 000000000..2c03fae54 --- /dev/null +++ b/mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/contact/GroupIdConversionsKtTest.kt @@ -0,0 +1,139 @@ +package net.mamoe.mirai.contact + +import net.mamoe.mirai.test.shouldBeEqualTo +import org.junit.Test +import kotlin.random.Random + +internal class GroupIdConversionsKtTest { + + @UseExperimental(ExperimentalUnsignedTypes::class) + @Test + fun toInternalId() { + repeat(1000000) { _ -> + val it = Random.nextInt() + try { + GroupId(it.toUInt()).toInternalId() shouldBeEqualTo GroupId(it.toUInt()).toInternalIdOld() + } catch (e: Throwable) { + println(it) + throw e + } + } + } + + @UseExperimental(ExperimentalUnsignedTypes::class) + @Test + fun toId() { + repeat(1000000) { _ -> + val it = Random.nextInt() + try { + GroupInternalId(it.toUInt()).toId() shouldBeEqualTo GroupInternalId(it.toUInt()).toIdOld() + } catch (e: Throwable) { + println(it) + throw e + } + } + } + + +} + +@UseExperimental(ExperimentalUnsignedTypes::class) +private fun GroupId.toInternalIdOld(): GroupInternalId {//求你别出错 + val left: Long = this.value.toString().let { + if (it.length <= 6) { + return GroupInternalId(this.value) + } + it.substring(0, it.length - 6).toLong() + } + val right: Long = this.value.toString().let { + it.substring(it.length - 6).toLong() + } + + return GroupInternalId( + when (left) { + in 1..10 -> { + ((left + 202).toString() + right.toString()).toUInt() + } + in 11..19 -> { + ((left + 469).toString() + right.toString()).toUInt() + } + in 20..66 -> { + ((left + 208).toString() + right.toString()).toUInt() + } + in 67..156 -> { + ((left + 1943).toString() + right.toString()).toUInt() + } + in 157..209 -> { + ((left + 199).toString() + right.toString()).toUInt() + } + in 210..309 -> { + ((left + 389).toString() + right.toString()).toUInt() + } + in 310..499 -> { + ((left + 349).toString() + right.toString()).toUInt() + } + else -> this.value + } + ) +} + +@UseExperimental(ExperimentalUnsignedTypes::class) +private fun GroupInternalId.toIdOld(): GroupId = with(value) { + //求你别出错 + var left: UInt = this.toString().let { + if (it.length <= 6) { + return GroupId(value) + } + it.substring(0 until it.length - 6).toUInt() + } + + return GroupId(when (left.toInt()) { + in 203..212 -> { + val right: UInt = this.toString().let { + it.substring(it.length - 6).toUInt() + } + ((left - 202u).toString() + right.toString()).toUInt() + } + in 480..488 -> { + val right: UInt = this.toString().let { + it.substring(it.length - 6).toUInt() + } + ((left - 469u).toString() + right.toString()).toUInt() + } + in 2100..2146 -> { + val right: UInt = this.toString().let { + it.substring(it.length - 7).toUInt() + } + left = left.toString().substring(0 until 3).toUInt() + ((left - 208u).toString() + right.toString()).toUInt() + } + in 2010..2099 -> { + val right: UInt = this.toString().let { + it.substring(it.length - 6).toUInt() + } + ((left - 1943u).toString() + right.toString()).toUInt() + } + in 2147..2199 -> { + val right: UInt = this.toString().let { + it.substring(it.length - 7).toUInt() + } + left = left.toString().substring(0 until 3).toUInt() + ((left - 199u).toString() + right.toString()).toUInt() + } + in 4100..4199 -> { + val right: UInt = this.toString().let { + it.substring(it.length - 7).toUInt() + } + left = left.toString().substring(0 until 3).toUInt() + ((left - 389u).toString() + right.toString()).toUInt() + } + in 3800..3989 -> { + val right: UInt = this.toString().let { + it.substring(it.length - 7).toUInt() + } + left = left.toString().substring(0 until 3).toUInt() + ((left - 349u).toString() + right.toString()).toUInt() + } + else -> value + }) +} \ No newline at end of file