From: David ‘Bombe’ Roden Date: Wed, 18 Oct 2017 04:49:51 +0000 (+0200) Subject: Refactor finding needles to be better readable X-Git-Tag: 0.9.8^2~11 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=e85adea97e44a7088a400dc28a8ffec46bc176d9;p=Sone.git Refactor finding needles to be better readable --- diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt index 02d63c5..69b7c85 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SearchPage.kt @@ -106,15 +106,12 @@ class SearchPage @JvmOverloads constructor(template: Template, webInterface: Web return requiredHits * 3 + optionalHits + (requiredHits - requiredPhrases) * 5 - (forbiddenHits * 2) } - private fun String.findAll(needle: String): List { - var nextIndex = indexOf(needle) - val positions = mutableListOf() - while (nextIndex != -1) { - positions += nextIndex - nextIndex = indexOf(needle, nextIndex + 1) - } - return positions - } + private fun String.findAll(needle: String) = + generateSequence(indexOf(needle).takeIf { it > -1 }) { lastPosition -> + lastPosition + .let { indexOf(needle, it + 1) } + .takeIf { it > -1 } + }.toList() private fun String.parse() = StringEscaper.parseLine(this)