/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.http

import io.ktor.http.parsing.*
import io.ktor.http.parsing.regex.*
import kotlin.native.concurrent.*

/**
 * Check if [host] is IPv4 or IPv6 address.
 */
public fun hostIsIp(host: String): Boolean = IP_PARSER.match(host)

@SharedImmutable
private val IPv4address = digits then "." then digits then "." then digits then "." then digits

@SharedImmutable
private val IPv6address = "[" then atLeastOne(hex or ":") then "]"

@SharedImmutable
private val IP_PARSER = (IPv4address or IPv6address).buildRegexParser()
