/**
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.
IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).
However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.
Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.
Note: You may assume there is no extra space or special characters in the input string.
Example 1:
Input: "172.16.254.1"
Output: "IPv4"
Explanation: This is a valid IPv4 address, return "IPv4".
Example 2:
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334"
Output: "IPv6"
Explanation: This is a valid IPv6 address, return "IPv6".
Example 3:
Input: "256.256.256.256"
Output: "Neither"
Explanation: This is neither a IPv4 address nor a IPv6 address.
*/
/**
Discuss:
这个题目里面应该尽量考虑二分的思维:
1. 整个内容需要考虑两种可用状态,一种IPV4, 一种IPV6,
然后IPV4里面需要满足分割的数组里面每一个token再满足条件。
2. 所以整个解决的思路是
IPV4 Valid ==> Splited by ".", Every Token Valid
Valid ==>
IPV6 Valid ==> Splited by ":", Every Token Valid
另外用枚举的实现更符合Swift的编码语意
//Todo
还有改进的地方,是不论验证v4,v6,v12,都是符合柯里化寓意的,使用函数变成思想更合理
*/
import UIKit
enum Ip : String {
static let v4SplitCount: Int = 4
static let v6SplitCount: Int = 8
case IpV4 = "IPv4"
case IpV6 = "IPv6"
case Neither = "Neither"
static func checkIp(_ ipCheckStr: String) -> String {
//MARK: V4
//v4每个分割内容是否满足
//01 失败,长度超过3位少于1位失败,大于255失败
//以.结束失败
//包含..失败
func validV4Token(_ token: String) -> Bool {
//长度超过3位失败
guard token.lengthOfBytes(using: .ascii) <= 3, token.lengthOfBytes(using: .ascii) >= 1
else {
return false
}
if token.contains("-") || token.contains("+") || token.contains("..") {
return false
}
let tokenInt = UInt(token)
if let tokenInt = tokenInt{
//01 失败
if tokenInt > 0, token.hasPrefix("0") {
return false
}
//大于255失败
else if tokenInt > 255 {
return false
}
else {
return true
}
}
else {
return false
}
}
//是否满足IPV4
func isIpV4(_ v4Str: String) -> Bool {
if v4Str.hasSuffix(".") || v4Str.contains("::") {
return false
}
let array = v4Str.characters.split(separator: ".")
if array.count == Ip.v4SplitCount {
//分割的第一个token, 以0开头,则直接失败
if String(array[0]).hasPrefix("0") {
return false
}
for token in array {
if !validV4Token(String(token)) {
return false
}
}
}
else {
return false
}
return true
}
//MARK: V6
//v6每个分割内容是否满足
//超过4位不足一位失败
//以:结尾失败
//token非16进制数失败,这里如果是"-0"也会出现正确,所以要去掉- 和 +
//包含::失败
func validV6Token(_ token: String) -> Bool {
guard token.lengthOfBytes(using: .ascii) <= 4, token.lengthOfBytes(using: .ascii) >= 1
else {
return false
}
if token.contains("-") || token.contains("+") {
return false
}
//转换为16进制, 需要使用UInt, 而不是UInt8,会超出
if let _ = UInt(token, radix: 16) {
return true
}
return false
}
//是否满足IPV6
func isIpV6(_ v6Str: String) -> Bool {
if v6Str.hasSuffix(":") || v6Str.contains("::") {
return false
}
let array = v6Str.characters.split(separator: ":")
//分割数目需要满足条件,才能继续
if array.count == Ip.v6SplitCount {
//分割的第一个token, 以0开头,则直接失败, 01开头满足IPv6
// if String(array[0]).hasPrefix("0") {
// return false
// }
for token in array {
if !validV6Token(String(token)) {
return false
}
}
}
else {
return false
}
return true
}
guard ipCheckStr.lengthOfBytes(using: .ascii) > 1 else {
return Ip.Neither.rawValue
}
if isIpV4(ipCheckStr) {
return Ip.IpV4.rawValue
}
else if isIpV6(ipCheckStr) {
return Ip.IpV6.rawValue
}
else {
return Ip.Neither.rawValue
}
}
}
//TestCase
print(Ip.checkIp("192"))
print(Ip.checkIp("172.16.254.1"))
print(Ip.checkIp("2001:0db8:85a3:0:0:8A2E:0370:7334"))
print(Ip.checkIp("172.16.254.256"))
print(Ip.checkIp("172.16..1"))
print(Ip.checkIp("02001:0db8:85a3:0:0:8A2E:0370:7334"))
print(Ip.checkIp("2001:0db8::0:0:8A2E:0370:7334"))
print(Ip.checkIp("2001:0db8:85a3:0:0:0:0370:7334"))
print(Ip.checkIp("2001:0db8:85a3:0:0:0:0370:7334"))
print(Ip.checkIp("2001:db8:85a3:0:0::8a2E:0370:7334"))
468. Validate IP Address
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Write a function to check whether an input string is a va...
- 关于IP地址 IP address Boost.Asio中IP地址的表示 IP地址分为IPv4和IPv6两种,在B...