diff --git a/domain/src/main/java/tachiyomi/domain/chapter/service/ChapterRecognition.kt b/domain/src/main/java/tachiyomi/domain/chapter/service/ChapterRecognition.kt index b2c3f6b44b..3190e0456a 100644 --- a/domain/src/main/java/tachiyomi/domain/chapter/service/ChapterRecognition.kt +++ b/domain/src/main/java/tachiyomi/domain/chapter/service/ChapterRecognition.kt @@ -41,27 +41,35 @@ object ChapterRecognition { } // Get chapter title with lower case - var name = chapterName.lowercase() - - // Remove manga title from chapter title. - name = name.replace(mangaTitle.lowercase(), "").trim() - - // Remove comma's or hyphens. - name = name.replace(',', '.').replace('-', '.') - - // Remove unwanted white spaces. - name = unwantedWhiteSpace.replace(name, "") - - // Remove unwanted tags. - name = unwanted.replace(name, "") - - // Check base case ch.xx - basic.find(name)?.let { return getChapterNumberFromMatch(it) } - - // Take the first number encountered. - number.find(name)?.let { return getChapterNumberFromMatch(it) } + val cleanChapterName = chapterName.lowercase() + // Remove manga title from chapter title. + .replace(mangaTitle.lowercase(), "").trim() + // Remove comma's or hyphens. + .replace(',', '.') + .replace('-', '.') + // Remove unwanted white spaces. + .replace(unwantedWhiteSpace, "") + + val numberMatch = number.findAll(cleanChapterName) + + when { + numberMatch.none() -> { + return chapterNumber ?: -1.0 + } + numberMatch.count() > 1 -> { + // Remove unwanted tags. + unwanted.replace(cleanChapterName, "").let { name -> + // Check base case ch.xx + basic.find(name)?.let { return getChapterNumberFromMatch(it) } + + // need to find again first number might already removed + number.find(name)?.let { return getChapterNumberFromMatch(it) } + } + } + } - return chapterNumber ?: -1.0 + // return the first number encountered + return getChapterNumberFromMatch(numberMatch.first()) } /** diff --git a/domain/src/test/java/tachiyomi/domain/chapter/service/ChapterRecognitionTest.kt b/domain/src/test/java/tachiyomi/domain/chapter/service/ChapterRecognitionTest.kt index 90ee3a4864..8369bfa5cd 100644 --- a/domain/src/test/java/tachiyomi/domain/chapter/service/ChapterRecognitionTest.kt +++ b/domain/src/test/java/tachiyomi/domain/chapter/service/ChapterRecognitionTest.kt @@ -171,6 +171,17 @@ class ChapterRecognitionTest { assertChapter(mangaTitle, "Tokyo ESP 027: Part 002: Chapter 001", 027.0) } + /** + * Case where the chapter title contains the unwanted tag + * But follow by chapter number. + */ + @Test + fun `Number after unwanted tag`() { + val mangaTitle = "One-punch Man" + + assertChapter(mangaTitle, "Mag Version 195.5", 195.5) + } + @Test fun `Unparseable chapter`() { val mangaTitle = "random"