From 6209f2b5e5034718db5d3f9acfb2f1a1b75a8328 Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 7 Apr 2024 20:02:39 +0800 Subject: [PATCH] optimize digest --- plugin/digest/digest.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/plugin/digest/digest.go b/plugin/digest/digest.go index ea3e998..f4faad1 100644 --- a/plugin/digest/digest.go +++ b/plugin/digest/digest.go @@ -24,6 +24,14 @@ func Html(content string, limit int) (string, string) { return content, "" } index := quto.FindAllStringIndex(content, -1) + var runeIndex [][]int + if len(index) > 0 { + runeIndex = slice.Map(index, func(t []int) []int { + return slice.Map(t, func(i int) int { + return utf8.RuneCountInString(content[:i]) + }) + }) + } end := 0 ru := []rune(content) tagIn := false @@ -32,13 +40,11 @@ func Html(content string, limit int) (string, string) { i := -1 for { i++ - for len(index) > 0 { - ints := slice.Map(index[0], func(t int) int { - return utf8.RuneCountInString(content[:t]) - }) + for len(runeIndex) > 0 && i >= runeIndex[0][0] { + ints := runeIndex[0] if ints[0] <= i { - i = i + i - ints[0] + ints[1] - ints[0] - index = index[1:] + i = ints[1] + runeIndex = runeIndex[1:] end++ continue } else {