All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.likethesalad.stem.modules.resolveStrings.resolver.TemplateContainerFinder.kt Maven / Gradle / Ivy

package com.likethesalad.stem.modules.resolveStrings.resolver

import com.likethesalad.android.templates.common.utils.CommonConstants

class TemplateContainerFinder(private val templateNames: List) {

    private val templatesPattern: Regex by lazy {
        Regex(templateNames.fold("") { accumulated, current ->
            val placeholder = toPlaceholderFormat(current)
            if (accumulated.isEmpty()) placeholder else "$accumulated|$placeholder"
        })
    }

    fun containsTemplates(text: String): Boolean {
        return templatesPattern.containsMatchIn(text)
    }

    private fun toPlaceholderFormat(text: String): String {
        return "\\\$\\{$text}"
    }

    fun getTemplateNamesFrom(text: String): List {
        val matches = templatesPattern.findAll(text).toList().map { it.value }

        return matches.toSet().map { CommonConstants.PLACEHOLDER_REGEX.matchEntire(it)!!.groupValues[1] }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy