Просмотр исходного кода

refactor: apply code review feedback for performance and deprecated API

a328929 4 месяцев назад
Родитель
Сommit
248782e2e5

+ 1 - 2
.github/scripts/tg_bot.py

@@ -7,8 +7,7 @@ import telebot
 
 # 日志打印
 def log(msg):
-    utc_time = datetime.datetime.utcnow()
-    china_time = utc_time + datetime.timedelta(hours=8)
+    china_time = datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=8)))
     print(f"[{china_time.strftime('%Y.%m.%d %H:%M:%S')}] {msg}")
 
 

+ 13 - 4
app/src/main/java/io/legado/app/ui/book/read/page/provider/TextChapterLayout.kt

@@ -1482,17 +1482,20 @@ class TextChapterLayout(
         var styles: Array<CharStyle?>? = null
         for (compiled in rules) {
             if (!compiled.rule.appliesTo(isTitle)) continue
-            val charStyle = compiled.rule.toCharStyle()
+            var charStyle: CharStyle? = null
             compiled.regex.findAll(text).forEach { match ->
                 val start = match.range.first.coerceAtLeast(0)
                 val end = match.range.last.coerceAtMost(text.lastIndex)
                 if (start > end) return@forEach
+                val activeStyle = charStyle ?: compiled.rule.toCharStyle().also {
+                    charStyle = it
+                }
                 val activeStyles = styles ?: arrayOfNulls<CharStyle>(text.length).also {
                     styles = it
                 }
                 for (i in start..end) {
                     // 后来的规则覆盖先前的(与 Legado_Max 行为一致)
-                    activeStyles[i] = charStyle
+                    activeStyles[i] = activeStyle
                 }
             }
         }
@@ -1526,14 +1529,20 @@ class TextChapterLayout(
             val copyLength = length.coerceAtMost(source.size - offset)
             if (copyLength <= 0) return null
             var hasStyle = false
+            for (i in 0 until copyLength) {
+                if (source[offset + i] != null) {
+                    hasStyle = true
+                    break
+                }
+            }
+            if (!hasStyle) return null
             val result = arrayOfNulls<CharStyle>(length)
             for (i in 0 until copyLength) {
                 source[offset + i]?.let { style ->
                     result[i] = style
-                    hasStyle = true
                 }
             }
-            return if (hasStyle) result else null
+            return result
         }
     }