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

io.javalin.core.routing.RoutingRegexes.kt Maven / Gradle / Ivy

package io.javalin.core.routing

internal fun constructRegexList(
    matchEverySubPath: Boolean,
    segments: List,
    regexSuffix: String,
    regexOptions: Set = emptySet(),
    mapper: (PathSegment) -> String
): List {
    fun addRegexForExtraWildcard(): List {
        return if (matchEverySubPath) {
            listOf(constructRegex(segments + PathSegment.Wildcard, regexSuffix, regexOptions, mapper))
        } else {
            emptyList()
        }
    }

    return listOf(constructRegex(segments, regexSuffix, regexOptions, mapper)) + addRegexForExtraWildcard()
}

internal fun constructRegex(
    segments: List,
    regexSuffix: String,
    regexOptions: Set = emptySet(),
    mapper: (PathSegment) -> String
): Regex {
    return buildString {
        append("^/")
        append(segments.joinToString(separator = "/", transform = mapper))
        append(regexSuffix)
        append("$")
    }.toRegex(regexOptions)
}

// Match and group values, then drop first element (the input string)
internal fun values(regex: Regex, url: String) = regex.matchEntire(url)?.groupValues?.drop(1) ?: emptyList()




© 2015 - 2024 Weber Informatics LLC | Privacy Policy