优化代码
This commit is contained in:
1 parent
6d19b26b1f
commit
cb5108933e
1 file changed
+15
-5
@@ -278,8 +278,10 @@ class TextChapterLayout(
|
|||||||
val matcher = AppPattern.imgPattern.matcher(text)
|
val matcher = AppPattern.imgPattern.matcher(text)
|
||||||
val ssb = StringBuffer()
|
val ssb = StringBuffer()
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
|
if (matcher.group(1) != null) {
|
||||||
matcher.appendReplacement(ssb, srcReplaceChar)
|
matcher.appendReplacement(ssb, srcReplaceChar)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
matcher.appendTail(ssb)
|
matcher.appendTail(ssb)
|
||||||
fullTextBuilder.append(ssb.toString())
|
fullTextBuilder.append(ssb.toString())
|
||||||
} else {
|
} else {
|
||||||
@@ -1394,9 +1396,11 @@ class TextChapterLayout(
|
|||||||
var hasMatch = false
|
var hasMatch = false
|
||||||
for (rule in rules) {
|
for (rule in rules) {
|
||||||
try {
|
try {
|
||||||
val regex = regexCache.getOrPut(rule.pattern) {
|
val regex = synchronized(regexCache) {
|
||||||
|
regexCache.getOrPut(rule.pattern) {
|
||||||
Regex(rule.pattern, RegexOption.DOT_MATCHES_ALL)
|
Regex(rule.pattern, RegexOption.DOT_MATCHES_ALL)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
regex.findAll(fullText).forEach { match ->
|
regex.findAll(fullText).forEach { match ->
|
||||||
hasMatch = true
|
hasMatch = true
|
||||||
for (i in match.range) {
|
for (i in match.range) {
|
||||||
@@ -1415,23 +1419,29 @@ class TextChapterLayout(
|
|||||||
private fun applyRegexColorRules(text: String, offset: Int): RegexMatchResult? {
|
private fun applyRegexColorRules(text: String, offset: Int): RegexMatchResult? {
|
||||||
val globalResult = globalRegexResult ?: return null
|
val globalResult = globalRegexResult ?: return null
|
||||||
if (offset < 0) return null
|
if (offset < 0) return null
|
||||||
|
val endIdx = minOf(offset + text.length, globalResult.colorArray.size)
|
||||||
|
var hasMatch = false
|
||||||
|
for (globalIdx in offset until endIdx) {
|
||||||
|
if (globalResult.colorArray[globalIdx] != -1 || globalResult.fontPathArray[globalIdx] != null) {
|
||||||
|
hasMatch = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!hasMatch) return null
|
||||||
val colorArray = IntArray(text.length) { -1 }
|
val colorArray = IntArray(text.length) { -1 }
|
||||||
val fontPathArray = arrayOfNulls<String>(text.length)
|
val fontPathArray = arrayOfNulls<String>(text.length)
|
||||||
var hasMatch = false
|
|
||||||
for (i in text.indices) {
|
for (i in text.indices) {
|
||||||
val globalIdx = offset + i
|
val globalIdx = offset + i
|
||||||
if (globalIdx >= 0 && globalIdx < globalResult.colorArray.size) {
|
if (globalIdx >= 0 && globalIdx < globalResult.colorArray.size) {
|
||||||
if (globalResult.colorArray[globalIdx] != -1) {
|
if (globalResult.colorArray[globalIdx] != -1) {
|
||||||
colorArray[i] = globalResult.colorArray[globalIdx]
|
colorArray[i] = globalResult.colorArray[globalIdx]
|
||||||
hasMatch = true
|
|
||||||
}
|
}
|
||||||
if (globalResult.fontPathArray[globalIdx] != null) {
|
if (globalResult.fontPathArray[globalIdx] != null) {
|
||||||
fontPathArray[i] = globalResult.fontPathArray[globalIdx]
|
fontPathArray[i] = globalResult.fontPathArray[globalIdx]
|
||||||
hasMatch = true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return if (hasMatch) RegexMatchResult(colorArray, fontPathArray) else null
|
return RegexMatchResult(colorArray, fontPathArray)
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class WordStyle(
|
private data class WordStyle(
|
||||||
|
|||||||
Reference in new issue
Block a user