From fcc7761320f37ca195c35fa2f8a4512ae0475746 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 24 Mar 2020 20:26:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/localBook/AnalyzeTxtFile.kt | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt index 6720168f0..c1b498a7b 100644 --- a/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt +++ b/app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt @@ -68,31 +68,24 @@ object AnalyzeTxtFile { //如果存在相应章节 while (matcher.find()) { //获取匹配到的字符在字符串中的起始位置 val chapterStart = matcher.start() + //获取章节内容 + val chapterContent = blockContent.substring(seekPos, chapterStart) //如果 seekPos == 0 && nextChapterPos != 0 表示当前block处前面有一段内容 //第一种情况一定是序章 第二种情况可能是上一个章节的内容 if (seekPos == 0 && chapterStart != 0) { //获取当前章节的内容 - val chapterContent = blockContent.substring(seekPos, chapterStart) - //设置指针偏移 - seekPos += chapterContent.length - //获取上一章节 - val lastChapter = toc.lastOrNull() - ?: BookChapter().apply { - toc.add(this) - start = 0 - title = "前言" - } - //将当前段落添加上一章去 - lastChapter.end = - lastChapter.end!! + chapterContent.toByteArray(charset).size + val chapter = BookChapter() + chapter.title = "前言" + chapter.start = 0 + chapter.end = chapterContent.toByteArray(charset).size.toLong() + toc.add(chapter) //创建当前章节 val curChapter = BookChapter() curChapter.title = matcher.group() - curChapter.start = lastChapter.end + curChapter.start = chapter.end + curChapter.end = chapter.end toc.add(curChapter) - } else { //是否存在章节 - if (toc.size != 0) { //获取章节内容 - val chapterContent = blockContent.substring(seekPos, matcher.start()) - seekPos += chapterContent.length + } else { + if (toc.size != 0) { //获取上一章节 val lastChapter = toc.last() lastChapter.end = @@ -110,6 +103,8 @@ object AnalyzeTxtFile { toc.add(curChapter) } } + //设置指针偏移 + seekPos += chapterContent.length } } else { //进行本地虚拟分章 //章节在buffer的偏移量