Skip to content

Commit

Permalink
chores: slightly improvement in util package
Browse files Browse the repository at this point in the history
  • Loading branch information
NanamiNakano authored and purofle committed Oct 30, 2023
1 parent 7c4a965 commit da751d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions app/src/main/java/moe/matsuri/nb4a/utils/KotlinUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import java.io.File

// SagerNet Class

const val KB = 1024L
const val MB = KB * 1024
const val GB = MB * 1024

fun SagerNet.cleanWebview() {
var pathToClean = "app_webview"
if (isBgProcess) pathToClean += "_$process"
Expand Down Expand Up @@ -44,19 +48,17 @@ fun Context.getDrawableByName(name: String?): Drawable? {
// Traffic display

fun Long.toBytesString(): String {
val size = this.toDouble()
return when {
this > 1024 * 1024 * 1024 -> String.format(
"%.2f GiB", (this.toDouble() / 1024 / 1024 / 1024)
)

this > 1024 * 1024 -> String.format("%.2f MiB", (this.toDouble() / 1024 / 1024))
this > 1024 -> String.format("%.2f KiB", (this.toDouble() / 1024))
this >= GB -> String.format("%.2f GiB", size / GB)
this >= MB -> String.format("%.2f MiB", size / MB)
this >= KB -> String.format("%.2f KiB", size / KB)
else -> "$this Bytes"
}
}

// List

fun String.listByLineOrComma(): List<String> {
return this.replace(",", "\n").split("\n")
return this.split(",","\n").map { it.trim() }.filter { it.isNotEmpty() }
}
4 changes: 2 additions & 2 deletions app/src/main/java/moe/matsuri/nb4a/utils/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object Util {
*/
fun getSubString(text: String, left: String?, right: String?): String {
var zLen: Int
if (left == null || left.isEmpty()) {
if (left.isNullOrEmpty()) {
zLen = 0
} else {
zLen = text.indexOf(left)
Expand All @@ -32,7 +32,7 @@ object Util {
}
}
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
}
return text.substring(zLen, yLen)
Expand Down

0 comments on commit da751d0

Please sign in to comment.