44 lines
1.2 KiB
Kotlin
44 lines
1.2 KiB
Kotlin
package be.simplenotes.app.utils
|
|
|
|
import be.simplenotes.search.SearchTerms
|
|
|
|
private fun innerRegex(name: String) =
|
|
"""$name:['"](.*?)['"]""".toRegex()
|
|
private fun outerRegex(name: String) =
|
|
"""($name:['"].*?['"])""".toRegex()
|
|
|
|
private val titleRe = innerRegex("title")
|
|
private val outerTitleRe = outerRegex("title")
|
|
|
|
private val tagRe = innerRegex("tag")
|
|
private val outerTagRe = outerRegex("tag")
|
|
|
|
private val contentRe = innerRegex("content")
|
|
private val outerContentRe = outerRegex("content")
|
|
|
|
fun parseSearchTerms(input: String): SearchTerms {
|
|
var c: String = input
|
|
|
|
fun extract(innerRegex: Regex, outerRegex: Regex): String? {
|
|
val match = innerRegex.find(input)?.groups?.get(1)?.value
|
|
if (match != null) {
|
|
val group = outerRegex.find(input)?.groups?.get(1)?.value
|
|
group?.let { c = c.replace(it, "") }
|
|
}
|
|
return match
|
|
}
|
|
|
|
val title: String? = extract(titleRe, outerTitleRe)
|
|
val tag: String? = extract(tagRe, outerTagRe)
|
|
val content: String? = extract(contentRe, outerContentRe)
|
|
|
|
val all = c.trim().ifEmpty { null }
|
|
|
|
return SearchTerms(
|
|
title = title,
|
|
tag = tag,
|
|
content = content,
|
|
all = all
|
|
)
|
|
}
|