优化
This commit is contained in:
@@ -2,14 +2,20 @@ package io.legado.app.utils
|
|||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.text.TextUtils.isEmpty
|
import android.text.TextUtils.isEmpty
|
||||||
|
import android.util.Base64
|
||||||
|
import java.io.ByteArrayInputStream
|
||||||
|
import java.io.ByteArrayOutputStream
|
||||||
|
import java.io.IOException
|
||||||
import java.text.DecimalFormat
|
import java.text.DecimalFormat
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.regex.Matcher
|
import java.util.regex.Matcher
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
import java.util.zip.GZIPInputStream
|
||||||
|
import java.util.zip.GZIPOutputStream
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
|
|
||||||
|
|
||||||
@Suppress("unused", "MemberVisibilityCanBePrivate")
|
@Suppress("unused", "MemberVisibilityCanBePrivate")
|
||||||
object StringUtils {
|
object StringUtils {
|
||||||
private const val HOUR_OF_DAY = 24
|
private const val HOUR_OF_DAY = 24
|
||||||
@@ -167,17 +173,20 @@ object StringUtils {
|
|||||||
result = 0
|
result = 0
|
||||||
tmp = 0
|
tmp = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpNum == 10000 -> {
|
tmpNum == 10000 -> {
|
||||||
result += tmp
|
result += tmp
|
||||||
result *= tmpNum
|
result *= tmpNum
|
||||||
tmp = 0
|
tmp = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
tmpNum >= 10 -> {
|
tmpNum >= 10 -> {
|
||||||
if (tmp == 0)
|
if (tmp == 0)
|
||||||
tmp = 1
|
tmp = 1
|
||||||
result += tmpNum * tmp
|
result += tmpNum * tmp
|
||||||
tmp = 0
|
tmp = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
tmp = if (i >= 2 && i == cn.size - 1 && ChnMap[cn[i - 1]]!! > 10)
|
tmp = if (i >= 2 && i == cn.size - 1 && ChnMap[cn[i - 1]]!! > 10)
|
||||||
tmpNum * ChnMap[cn[i - 1]]!! / 10
|
tmpNum * ChnMap[cn[i - 1]]!! / 10
|
||||||
@@ -256,7 +265,7 @@ object StringUtils {
|
|||||||
while (start < end && (s[end].code <= 0x20 || s[end] == ' ')) {
|
while (start < end && (s[end].code <= 0x20 || s[end] == ' ')) {
|
||||||
--end
|
--end
|
||||||
}
|
}
|
||||||
if (end < len) ++end
|
++end
|
||||||
return if (start > 0 || end < len) s.substring(start, end) else s
|
return if (start > 0 || end < len) s.substring(start, end) else s
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -287,4 +296,58 @@ object StringUtils {
|
|||||||
return buf.toString()
|
return buf.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 压缩字符串
|
||||||
|
*/
|
||||||
|
fun compress(str: String): Result<String> {
|
||||||
|
return kotlin.runCatching {
|
||||||
|
if (str.isEmpty()) {
|
||||||
|
return@runCatching str
|
||||||
|
}
|
||||||
|
val out = ByteArrayOutputStream()
|
||||||
|
var gzip: GZIPOutputStream? = null
|
||||||
|
return@runCatching try {
|
||||||
|
gzip = GZIPOutputStream(out)
|
||||||
|
gzip.write(str.toByteArray())
|
||||||
|
Base64.encodeToString(out.toByteArray(), Base64.NO_WRAP)
|
||||||
|
} finally {
|
||||||
|
gzip?.runCatching {
|
||||||
|
close()
|
||||||
|
}
|
||||||
|
out.runCatching {
|
||||||
|
close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解压字符串
|
||||||
|
*/
|
||||||
|
@Throws(IOException::class)
|
||||||
|
fun unCompress(str: String): Result<String> {
|
||||||
|
return kotlin.runCatching {
|
||||||
|
val outputStream = ByteArrayOutputStream()
|
||||||
|
var inputStream: ByteArrayInputStream? = null
|
||||||
|
var ginZip: GZIPInputStream? = null
|
||||||
|
return@runCatching try {
|
||||||
|
val compressed = Base64.decode(str, Base64.NO_WRAP)
|
||||||
|
inputStream = ByteArrayInputStream(compressed)
|
||||||
|
ginZip = GZIPInputStream(inputStream)
|
||||||
|
ginZip.copyTo(outputStream)
|
||||||
|
outputStream.toString()
|
||||||
|
} finally {
|
||||||
|
ginZip?.runCatching {
|
||||||
|
close()
|
||||||
|
}
|
||||||
|
inputStream?.runCatching {
|
||||||
|
close()
|
||||||
|
}
|
||||||
|
outputStream.runCatching {
|
||||||
|
close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user