Update HtmlFormatter.kt

精简图片格式化程序,提供执行效率,改重复匹配为依次查找
pull/1060/head
bushixuanqi 4 years ago committed by GitHub
parent 8282386f62
commit 92597a82d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      app/src/main/java/io/legado/app/utils/HtmlFormatter.kt

@ -24,25 +24,18 @@ object HtmlFormatter {
html ?: return ""
var formatHtml = formatKeepImg(html)
val sb = StringBuffer()
var appendPos = 0
while (appendPos < formatHtml.length) {
val matcher = AppPattern.imgPattern.matcher(formatHtml)
if(matcher.find()) {
val urlArray = matcher.group(1)!!.split(AnalyzeUrl.splitUrlRegex)
var url = NetworkUtils.getAbsoluteURL(redirectUrl, urlArray[0])
if (urlArray.size > 1) {
url = "$url,${urlArray[1]}"
}
sb.append(formatHtml.substring(appendPos, matcher.start()))
sb.append("<img src=\"$url\" >")
appendPos = matcher.end()
formatHtml = formatHtml.substring(appendPos, formatHtml.length)
appendPos = 0
} else {
sb.append(formatHtml)
appendPos = formatHtml.length
val matcher = AppPattern.imgPattern.matcher(formatHtml)
while (matcher.find()) {
val urlArray = matcher.group(1)!!.split(AnalyzeUrl.splitUrlRegex)
var url = NetworkUtils.getAbsoluteURL(redirectUrl, urlArray[0])
if (urlArray.size > 1) {
url = "$url,${urlArray[1]}"
}
//将Matcher上次匹配结尾到本次匹配结尾这段字符串序列追加到sb中,且是先将其中匹配到的部分替换后再追加
matcher.appendReplacement(sb, "<img src=\"$url\" >")
}
//将Matcher最后那个匹配之后的字串匹配到追加到sb中
matcher.appendTail(sb)
return sb.toString()
}

Loading…
Cancel
Save