mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-24 06:50:08 +08:00
Simplify
This commit is contained in:
parent
111d9d0dbc
commit
941fcb3fdd
@ -2,101 +2,50 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.contact
|
package net.mamoe.mirai.contact
|
||||||
|
|
||||||
fun GroupId.toInternalId(): GroupInternalId {//求你别出错
|
import kotlin.math.pow
|
||||||
val left: Long = this.value.toString().let {
|
|
||||||
if (it.length < 6) {
|
|
||||||
|
@Suppress("ObjectPropertyName")
|
||||||
|
private val `10EXP6` = 10.0.pow(6).toUInt()
|
||||||
|
|
||||||
|
|
||||||
|
fun GroupId.toInternalId(): GroupInternalId {
|
||||||
|
if (this.value <= `10EXP6`) {
|
||||||
return GroupInternalId(this.value)
|
return GroupInternalId(this.value)
|
||||||
}
|
}
|
||||||
it.substring(0, it.length - 6).toLong()
|
val left: Long = this.value.toString().dropLast(6).toLong()
|
||||||
}
|
val right: Long = this.value.toString().takeLast(6).toLong()
|
||||||
val right: Long = this.value.toString().let {
|
|
||||||
it.substring(it.length - 6).toLong()
|
|
||||||
}
|
|
||||||
|
|
||||||
return GroupInternalId(
|
return GroupInternalId(
|
||||||
when (left) {
|
when (left) {
|
||||||
in 1..10 -> {
|
in 1..10 -> ((left + 202).toString() + right.toString()).toUInt()
|
||||||
((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 11..19 -> {
|
in 67..156 -> ((left + 1943).toString() + right.toString()).toUInt()
|
||||||
((left + 469).toString() + right.toString()).toUInt()
|
in 157..209 -> ((left + 199).toString() + right.toString()).toUInt()
|
||||||
}
|
in 210..309 -> ((left + 389).toString() + right.toString()).toUInt()
|
||||||
in 20..66 -> {
|
in 310..499 -> ((left + 349).toString() + right.toString()).toUInt()
|
||||||
((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
|
else -> this.value
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun GroupInternalId.toId(): GroupId = with(value) {
|
fun GroupInternalId.toId(): GroupId = with(value.toString()) {
|
||||||
//求你别出错
|
if (value < `10EXP6`) {
|
||||||
var left: UInt = this.toString().let {
|
|
||||||
if (it.length < 6) {
|
|
||||||
return GroupId(value)
|
return GroupId(value)
|
||||||
}
|
}
|
||||||
it.substring(0 until it.length - 6).toUInt()
|
val left: UInt = this.dropLast(6).toUInt()
|
||||||
}
|
|
||||||
|
|
||||||
return GroupId(when (left.toInt()) {
|
return GroupId(
|
||||||
in 203..212 -> {
|
when (left.toInt()) {
|
||||||
val right: UInt = this.toString().let {
|
in 203..212 -> ((left - 202u).toString() + this.takeLast(6).toInt().toString()).toUInt()
|
||||||
it.substring(it.length - 6).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()
|
||||||
((left - 202u).toString() + right.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 480..488 -> {
|
in 4100..4199 -> ((left.toString().take(3).toUInt() - 389u).toString() + this.takeLast(7).toInt().toString()).toUInt()
|
||||||
val right: UInt = this.toString().let {
|
in 3800..3989 -> ((left.toString().take(3).toUInt() - 349u).toString() + this.takeLast(7).toInt().toString()).toUInt()
|
||||||
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
|
else -> value
|
||||||
})
|
}
|
||||||
|
)
|
||||||
}
|
}
|
@ -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
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user