Magisk/app/src/main/java/com/topjohnwu/magisk/ktx/XString.kt
2021-04-09 20:01:32 -07:00

49 lines
1.4 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.topjohnwu.magisk.ktx
import android.content.res.Resources
val specialChars = arrayOf('!', '@', '#', '$', '%', '&', '?')
val fullSpecialChars = arrayOf('', '', '', '', '', '', '')
fun String.isCJK(): Boolean {
for (i in 0 until length)
if (isCJK(codePointAt(i)))
return true
return false
}
fun isCJK(codepoint: Int) = Character.isIdeographic(codepoint)
fun String.replaceRandomWithSpecial(passes: Int): String {
var string = this
repeat(passes) {
string = string.replaceRandomWithSpecial()
}
return string
}
fun String.replaceRandomWithSpecial(): String {
val sp = if (isCJK()) fullSpecialChars else specialChars
var random: Char
do {
random = random()
} while (random == '.')
return replace(random, sp.random())
}
fun StringBuilder.appendIf(condition: Boolean, builder: StringBuilder.() -> Unit) =
if (condition) apply(builder) else this
fun Int.res(vararg args: Any): String {
val resources: Resources by inject()
return resources.getString(this, *args)
}
fun String.trimEmptyToNull(): String? = if (isBlank()) null else this
fun String.legalFilename() = replace(" ", "_").replace("'", "").replace("\"", "")
.replace("$", "").replace("`", "").replace("*", "").replace("/", "_")
.replace("#", "").replace("@", "").replace("\\", "_")
fun String.isEmptyInternal() = isNullOrBlank()