diff --git a/app/src/main/java/io/legado/app/utils/Encoder.kt b/app/src/main/java/io/legado/app/utils/Encoder.kt index b74266fa3..8f97c4c83 100644 --- a/app/src/main/java/io/legado/app/utils/Encoder.kt +++ b/app/src/main/java/io/legado/app/utils/Encoder.kt @@ -3,26 +3,20 @@ package io.legado.app.utils object Encoder { fun escape(src: String): String { - var i = 0 - var char: Char val tmp = StringBuilder() - tmp.ensureCapacity(src.length * 6) - while (i < src.length) { - char = src[i] - if (Character.isDigit(char) || Character.isLowerCase(char) - || Character.isUpperCase(char) - ) + for (char in src) { + val charCode = char.toInt() + if (charCode in 48..57 || charCode in 65..90 || charCode in 97..122) { tmp.append(char) - else if (char.toInt() < 256) { - tmp.append("%") - if (char.toInt() < 16) - tmp.append("0") - tmp.append(char.toInt().toString(16)) - } else { - tmp.append("%u") - tmp.append(char.toInt().toString(16)) + continue } - i++ + + val prefix = when { + charCode < 16 -> "%0" + charCode < 256 -> "%" + else -> "%u" + } + tmp.append(prefix).append(charCode.toString(16)) } return tmp.toString() } diff --git a/app/src/main/java/io/legado/app/utils/FileUtils.kt b/app/src/main/java/io/legado/app/utils/FileUtils.kt index 8c5ba3823..e64869d11 100644 --- a/app/src/main/java/io/legado/app/utils/FileUtils.kt +++ b/app/src/main/java/io/legado/app/utils/FileUtils.kt @@ -21,20 +21,7 @@ object FileUtils { fun getSdPath() = Environment.getExternalStorageDirectory().absolutePath fun getFileByPath(filePath: String): File? { - return if (isSpace(filePath)) null else File(filePath) - } - - fun isSpace(s: String?): Boolean { - if (s == null) return true - var i = 0 - val len = s.length - while (i < len) { - if (!Character.isWhitespace(s[i])) { - return false - } - ++i - } - return true + return if (filePath.isBlank()) null else File(filePath) } fun getSdCardPath(): String { @@ -101,12 +88,9 @@ object FileUtils { return paths } - @TargetApi(Build.VERSION_CODES.KITKAT) fun getPath(context: Context, uri: Uri): String? { - val isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT - // DocumentProvider - if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { + if (DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider if (isExternalStorageDocument(uri)) { val docId = DocumentsContract.getDocumentId(uri) @@ -119,7 +103,7 @@ object FileUtils { } else if (isDownloadsDocument(uri)) { val id = DocumentsContract.getDocumentId(uri) - val split = id.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + val split = id.split(":").dropLastWhile { it.isEmpty() }.toTypedArray() val type = split[0] if ("raw".equals( type, diff --git a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt index 79ce84ef9..ae2d42d47 100644 --- a/app/src/main/java/io/legado/app/utils/NetworkUtils.kt +++ b/app/src/main/java/io/legado/app/utils/NetworkUtils.kt @@ -8,37 +8,18 @@ object NetworkUtils { private val notNeedEncoding: BitSet by lazy { val bitSet = BitSet(256) - var i: Int = 'a'.toInt() - while (i <= 'z'.toInt()) { + for (i in 'a'.toInt()..'z'.toInt()) { bitSet.set(i) - i++ } - i = 'A'.toInt() - while (i <= 'Z'.toInt()) { + for (i in 'A'.toInt()..'Z'.toInt()) { bitSet.set(i) - i++ } - i = '0'.toInt() - while (i <= '9'.toInt()) { + for (i in '0'.toInt()..'9'.toInt()) { bitSet.set(i) - i++ } - bitSet.set('+'.toInt()) - bitSet.set('-'.toInt()) - bitSet.set('_'.toInt()) - bitSet.set('.'.toInt()) - bitSet.set('$'.toInt()) - bitSet.set(':'.toInt()) - bitSet.set('('.toInt()) - bitSet.set(')'.toInt()) - bitSet.set('!'.toInt()) - bitSet.set('*'.toInt()) - bitSet.set('@'.toInt()) - bitSet.set('&'.toInt()) - bitSet.set('#'.toInt()) - bitSet.set(','.toInt()) - bitSet.set('['.toInt()) - bitSet.set(']'.toInt()) + for (char in "+-_.$:()!*@&#,[]") { + bitSet.set(char.toInt()) + } return@lazy bitSet } @@ -69,7 +50,6 @@ object NetworkUtils { // 其他字符,肯定需要urlEncode needEncode = true break - i++ } return !needEncode @@ -79,7 +59,7 @@ object NetworkUtils { * 判断c是否是16进制的字符 */ private fun isDigit16Char(c: Char): Boolean { - return c >= '0' && c <= '9' || c >= 'A' && c <= 'F' + return c in '0'..'9' || c in 'A'..'F' || c in 'a'..'f' } /** diff --git a/app/src/main/java/io/legado/app/utils/README.md b/app/src/main/java/io/legado/app/utils/README.md deleted file mode 100644 index a31b943b5..000000000 --- a/app/src/main/java/io/legado/app/utils/README.md +++ /dev/null @@ -1 +0,0 @@ -## 放置一些工具类 \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/StringExtensions.kt b/app/src/main/java/io/legado/app/utils/StringExtensions.kt index 0ec34e16e..de179f1da 100644 --- a/app/src/main/java/io/legado/app/utils/StringExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/StringExtensions.kt @@ -17,7 +17,7 @@ fun String?.isJson(): Boolean = this?.run { } ?: false fun String?.htmlFormat(): String = if (this.isNullOrBlank()) "" else - this.replace("(?i)<(br[\\s/]*|/*p.*?|/*div.*?)>".toRegex(), "\n")// 替换特定标签为换行符 + this.replace("(?i)<(br[\\s/]*|/*p\\b.*?|/*div\\b.*?)>".toRegex(), "\n")// 替换特定标签为换行符 .replace("<[script>]*.*?>| ".toRegex(), "")// 删除script标签对和空格转义符 .replace("\\s*\\n+\\s*".toRegex(), "\n  ")// 移除空行,并增加段前缩进2个汉字 .replace("^[\\n\\s]+".toRegex(), "  ")//移除开头空行,并增加段前缩进2个汉字