This also improves randomness by avoiding to reinitialize the random number generator repeatedly from a seed based on the current time. Typically, if the number generator is reinitialized repeatedly at non-random times (like multiple times in a row), then generated numbers have a higher chance of repeating. The Kotlin Random object is only initialized once, using the best seed available for the current Android version.
63 lines
1.4 KiB
Kotlin
63 lines
1.4 KiB
Kotlin
@file:JvmName("StringUtils")
|
|
|
|
package com.keylesspalace.tusky.util
|
|
|
|
import android.text.Spanned
|
|
import kotlin.random.Random
|
|
|
|
private const val POSSIBLE_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
fun randomAlphanumericString(count: Int): String {
|
|
val chars = CharArray(count)
|
|
for (i in 0 until count) {
|
|
chars[i] = POSSIBLE_CHARS[Random.nextInt(POSSIBLE_CHARS.length)]
|
|
}
|
|
return String(chars)
|
|
}
|
|
|
|
/**
|
|
* A < B (strictly) by length and then by content.
|
|
* Examples:
|
|
* "abc" < "bcd"
|
|
* "ab" < "abc"
|
|
* "cb" < "abc"
|
|
* not: "ab" < "ab"
|
|
* not: "abc" > "cb"
|
|
*/
|
|
fun String.isLessThan(other: String): Boolean {
|
|
return when {
|
|
this.length < other.length -> true
|
|
this.length > other.length -> false
|
|
else -> this < other
|
|
}
|
|
}
|
|
|
|
/**
|
|
* A <= B (strictly) by length and then by content.
|
|
* Examples:
|
|
* "abc" <= "bcd"
|
|
* "ab" <= "abc"
|
|
* "cb" <= "abc"
|
|
* "ab" <= "ab"
|
|
* not: "abc" > "cb"
|
|
*/
|
|
fun String.isLessThanOrEqual(other: String): Boolean {
|
|
return this == other || isLessThan(other)
|
|
}
|
|
|
|
fun Spanned.trimTrailingWhitespace(): Spanned {
|
|
var i = length
|
|
do {
|
|
i--
|
|
} while (i >= 0 && get(i).isWhitespace())
|
|
return subSequence(0, i + 1) as Spanned
|
|
}
|
|
|
|
/**
|
|
* BidiFormatter.unicodeWrap is insufficient in some cases (see #1921)
|
|
* So we force isolation manually
|
|
* https://unicode.org/reports/tr9/#Explicit_Directional_Isolates
|
|
*/
|
|
fun CharSequence.unicodeWrap(): String {
|
|
return "\u2068${this}\u2069"
|
|
}
|