mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-05 07:30:09 +08:00
Generalize chineseLength
to mirai-core-utils
This commit is contained in:
parent
3955546868
commit
a9516b401b
@ -1,10 +1,10 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
* Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("EXPOSED_SUPER_CLASS")
|
||||
@ -22,6 +22,7 @@ import net.mamoe.mirai.console.permission.RootPermission
|
||||
import net.mamoe.mirai.contact.*
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import net.mamoe.mirai.utils.MiraiExperimentalApi
|
||||
import net.mamoe.mirai.utils.chineseLength
|
||||
import java.time.temporal.TemporalAccessor
|
||||
|
||||
|
||||
@ -570,20 +571,11 @@ internal fun Double.toDecimalPlace(n: Int): String = "%.${n}f".format(this)
|
||||
internal fun String.truncate(lengthLimit: Int, replacement: String = "..."): String = buildString {
|
||||
var lengthSum = 0
|
||||
for (char in this@truncate) {
|
||||
lengthSum += char.chineseLength()
|
||||
lengthSum += char.chineseLength
|
||||
if (lengthSum > lengthLimit) {
|
||||
append(replacement)
|
||||
return toString()
|
||||
} else append(char)
|
||||
}
|
||||
return toString()
|
||||
}
|
||||
|
||||
internal fun Char.chineseLength(): Int {
|
||||
return when (this) {
|
||||
in '\u0000'..'\u007F' -> 1
|
||||
in '\u0080'..'\u07FF' -> 2
|
||||
in '\u0800'..'\uFFFF' -> 2
|
||||
else -> 2
|
||||
}
|
||||
}
|
@ -29,3 +29,28 @@ public fun String.dropEmoji(): String {
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
public fun CharSequence.chineseLength(upTo: Int = Int.MAX_VALUE): Int {
|
||||
return this.sumUpTo(upTo) { it.chineseLength }
|
||||
}
|
||||
|
||||
public val Char.chineseLength: Int
|
||||
get() {
|
||||
return when (this) {
|
||||
in '\u0000'..'\u007F' -> 1
|
||||
in '\u0080'..'\u07FF' -> 2
|
||||
in '\u0800'..'\uFFFF' -> 3
|
||||
else -> 4
|
||||
}
|
||||
}
|
||||
|
||||
public inline fun CharSequence.sumUpTo(upTo: Int, selector: (Char) -> Int): Int {
|
||||
var sum = 0
|
||||
for (element in this) {
|
||||
sum += selector(element)
|
||||
if (sum >= upTo) {
|
||||
return sum
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
/*
|
||||
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||
* Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.internal.utils
|
||||
@ -12,6 +12,7 @@ package net.mamoe.mirai.internal.utils
|
||||
import net.mamoe.mirai.contact.ContactOrBot
|
||||
import net.mamoe.mirai.internal.message.ForwardMessageInternal
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import net.mamoe.mirai.utils.chineseLength
|
||||
import net.mamoe.mirai.utils.toInt
|
||||
import net.mamoe.mirai.utils.toLongUnsigned
|
||||
import java.net.Inet4Address
|
||||
@ -43,17 +44,6 @@ internal fun String.toIpV4Long(): Long {
|
||||
}
|
||||
}
|
||||
|
||||
internal fun String.chineseLength(upTo: Int): Int {
|
||||
return this.sumUpTo(upTo) {
|
||||
when (it) {
|
||||
in '\u0000'..'\u007F' -> 1
|
||||
in '\u0080'..'\u07FF' -> 2
|
||||
in '\u0800'..'\uFFFF' -> 3
|
||||
else -> 4
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun MessageChain.estimateLength(target: ContactOrBot, upTo: Int): Int =
|
||||
sumUpTo(upTo) { it, up ->
|
||||
it.estimateLength(target, up)
|
||||
@ -82,13 +72,3 @@ internal inline fun <T> Iterable<T>.sumUpTo(upTo: Int, selector: (T, remaining:
|
||||
return sum
|
||||
}
|
||||
|
||||
internal inline fun CharSequence.sumUpTo(upTo: Int, selector: (Char) -> Int): Int {
|
||||
var sum = 0
|
||||
for (element in this) {
|
||||
sum += selector(element)
|
||||
if (sum >= upTo) {
|
||||
return sum
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user