chores: slightly improvement in util package

This commit is contained in:
NanamiNakano 2023-10-29 21:40:43 +08:00 committed by purofle
parent 7c4a965e40
commit da751d00fd
2 changed files with 11 additions and 9 deletions

View File

@ -10,6 +10,10 @@ import java.io.File
// SagerNet Class // SagerNet Class
const val KB = 1024L
const val MB = KB * 1024
const val GB = MB * 1024
fun SagerNet.cleanWebview() { fun SagerNet.cleanWebview() {
var pathToClean = "app_webview" var pathToClean = "app_webview"
if (isBgProcess) pathToClean += "_$process" if (isBgProcess) pathToClean += "_$process"
@ -44,13 +48,11 @@ fun Context.getDrawableByName(name: String?): Drawable? {
// Traffic display // Traffic display
fun Long.toBytesString(): String { fun Long.toBytesString(): String {
val size = this.toDouble()
return when { return when {
this > 1024 * 1024 * 1024 -> String.format( this >= GB -> String.format("%.2f GiB", size / GB)
"%.2f GiB", (this.toDouble() / 1024 / 1024 / 1024) this >= MB -> String.format("%.2f MiB", size / MB)
) this >= KB -> String.format("%.2f KiB", size / KB)
this > 1024 * 1024 -> String.format("%.2f MiB", (this.toDouble() / 1024 / 1024))
this > 1024 -> String.format("%.2f KiB", (this.toDouble() / 1024))
else -> "$this Bytes" else -> "$this Bytes"
} }
} }
@ -58,5 +60,5 @@ fun Long.toBytesString(): String {
// List // List
fun String.listByLineOrComma(): List<String> { fun String.listByLineOrComma(): List<String> {
return this.replace(",", "\n").split("\n") return this.split(",","\n").map { it.trim() }.filter { it.isNotEmpty() }
} }

View File

@ -21,7 +21,7 @@ object Util {
*/ */
fun getSubString(text: String, left: String?, right: String?): String { fun getSubString(text: String, left: String?, right: String?): String {
var zLen: Int var zLen: Int
if (left == null || left.isEmpty()) { if (left.isNullOrEmpty()) {
zLen = 0 zLen = 0
} else { } else {
zLen = text.indexOf(left) zLen = text.indexOf(left)
@ -32,7 +32,7 @@ object Util {
} }
} }
var yLen = if (right == null) -1 else text.indexOf(right, zLen) var yLen = if (right == null) -1 else text.indexOf(right, zLen)
if (yLen < 0 || right == null || right.isEmpty()) { if (yLen < 0 || right.isNullOrEmpty()) {
yLen = text.length yLen = text.length
} }
return text.substring(zLen, yLen) return text.substring(zLen, yLen)