pull/1319/head
gedoor 3 years ago
parent 00054857d4
commit 11f5e5f611
  1. 11
      app/src/main/java/io/legado/app/help/ContentProcessor.kt
  2. 60
      app/src/main/java/io/legado/app/utils/StringUtils.kt

@ -6,6 +6,7 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.utils.StringUtils
import io.legado.app.utils.toastOnUi
import splitties.init.appCtx
import java.lang.ref.WeakReference
@ -16,8 +17,6 @@ class ContentProcessor private constructor(
) {
companion object {
private val fbsArr =
arrayOf("\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|")
private val processors = hashMapOf<String, WeakReference<ContentProcessor>>()
fun get(bookName: String, bookOrigin: String): ContentProcessor {
@ -67,13 +66,9 @@ class ContentProcessor private constructor(
var mContent = content
if (includeTitle) {
//去除重复标题
var name = book.name
var title = chapter.title
try {
fbsArr.forEach {
name = name.replace(it, "\\" + it)
title = title.replace(it, "\\" + it)
}
val name = StringUtils.escapeToRegex(book.name)
val title = StringUtils.escapeToRegex(chapter.title)
val titleRegex = "^(\\s|\\p{P}|${name})*${title}(\\s|\\p{P})+".toRegex()
mContent = mContent.replace(titleRegex, "")
} catch (e: Exception) {

@ -41,7 +41,9 @@ object StringUtils {
return map
}
//将时间转换成日期
/**
* 将时间转换成日期
*/
fun dateConvert(time: Long, pattern: String): String {
val date = Date(time)
@ -50,7 +52,9 @@ object StringUtils {
return format.format(date)
}
//将日期转换成昨天、今天、明天
/**
* 将日期转换成昨天今天明天
*/
fun dateConvert(source: String, pattern: String): String {
@SuppressLint("SimpleDateFormat")
val format = SimpleDateFormat(pattern)
@ -69,12 +73,8 @@ object StringUtils {
if (oldHour == 0) {
//比日期:昨天今天和明天
return when {
difDate == 0L -> {
"今天"
}
difDate < DAY_OF_YESTERDAY -> {
"昨天"
}
difDate == 0L -> "今天"
difDate < DAY_OF_YESTERDAY -> "昨天"
else -> {
@SuppressLint("SimpleDateFormat")
val convertFormat = SimpleDateFormat("yyyy-MM-dd")
@ -97,10 +97,12 @@ object StringUtils {
}.onFailure {
it.printOnDebug()
}
return ""
}
/**
* 单位转换
*/
fun toSize(length: Long): String {
if (length <= 0) return "0"
val units = arrayOf("b", "kb", "M", "G", "T")
@ -112,6 +114,9 @@ object StringUtils {
.format(length / 1024.0.pow(digitGroups.toDouble())) + " " + units[digitGroups]
}
/**
* 首字母大写
*/
@SuppressLint("DefaultLocale")
fun toFirstCapital(str: String): String {
return str.substring(0, 1).uppercase(Locale.getDefault()) + str.substring(1)
@ -140,7 +145,9 @@ object StringUtils {
return String(c)
}
//功能:字符串全角转换为半角
/**
* 字符串全角转换为半角
*/
fun fullToHalf(input: String): String {
val c = input.toCharArray()
for (i in c.indices) {
@ -157,6 +164,9 @@ object StringUtils {
return String(c)
}
/**
* 中文大写数字转数字
*/
fun chineseNumToInt(chNum: String): Int {
var result = 0
var tmp = 0
@ -207,6 +217,9 @@ object StringUtils {
}.getOrDefault(-1)
}
/**
* 字符串转数字
*/
fun stringToInt(str: String?): Int {
if (str != null) {
val num = fullToHalf(str).replace("\\s+".toRegex(), "")
@ -219,12 +232,18 @@ object StringUtils {
return -1
}
/**
* 是否包含数字
*/
fun isContainNumber(company: String): Boolean {
val p = Pattern.compile("[0-9]+")
val m = p.matcher(company)
return m.find()
}
/**
* 是否数字
*/
fun isNumeric(str: String): Boolean {
val pattern = Pattern.compile("[0-9]+")
val isNum = pattern.matcher(str)
@ -249,7 +268,9 @@ object StringUtils {
return wordsS
}
// 移除字符串首尾空字符的高效方法(利用ASCII值判断,包括全角空格)
/**
* 移除字符串首尾空字符的高效方法(利用ASCII值判断,包括全角空格)
*/
fun trim(s: String): String {
if (isEmpty(s)) return ""
var start = 0
@ -265,6 +286,9 @@ object StringUtils {
return if (start > 0 || end < len) s.substring(start, end) else s
}
/**
* 重复字符串
*/
fun repeat(str: String, n: Int): String {
val stringBuilder = StringBuilder()
for (i in 0 until n) {
@ -273,6 +297,9 @@ object StringUtils {
return stringBuilder.toString()
}
/**
* 移除UTF头
*/
fun removeUTFCharacters(data: String?): String? {
if (data == null) return null
val p = Pattern.compile("\\\\u(\\p{XDigit}{4})")
@ -286,6 +313,17 @@ object StringUtils {
return buf.toString()
}
private val regexNeedEscaped =
arrayOf("\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|")
fun escapeToRegex(text: String): String {
var str = text
regexNeedEscaped.forEach {
str = str.replace(it, "\\" + it)
}
return str
}
fun byteToHexString(bytes: ByteArray?): String {
if (bytes == null) return ""
val sb = StringBuilder(bytes.size * 2)

Loading…
Cancel
Save