Fixed the broken tests.

Signed-off-by: Adam Rocska <adam.rocska@adams.solutions>
This commit is contained in:
Adam Rocska 2020-05-07 18:18:49 +02:00
parent b5dd2f8693
commit a51a554958
1 changed files with 10 additions and 6 deletions

View File

@ -45,17 +45,21 @@ public enum IpAddress: Equatable {
}
static func v6(_ string: String) -> IpAddress {
let inputRawChunks = string.split(separator: ":")
if string == "::1" {
print(string)
}
let inputRawChunks = string.split(separator: ":", omittingEmptySubsequences: false)
precondition(inputRawChunks.count <= 8, "IPv6 strings must have at most 8 bytes defined.")
var prefixBytes: [UInt8] = []
var suffixBytes: [UInt8] = []
var shouldProcessPrefix = true
var emptyChunksFound = 0
for chunk in inputRawChunks {
precondition(chunk.count <= 4, "Invalid hexadectet provided : \"\(chunk)\".")
if chunk.isEmpty {
if !shouldProcessPrefix {
if emptyChunksFound >= 2 {
preconditionFailure("Invalid IPv6 format provided.")
}
shouldProcessPrefix = false
emptyChunksFound += 1
continue
}
let hexadectet = String(repeating: "0", count: 4 - chunk.count) + chunk
@ -64,11 +68,11 @@ public enum IpAddress: Equatable {
guard let byte1 = UInt8(hexadectet[..<separationIndex], radix: 16) else {
preconditionFailure("Invalid hexadectet defined: \(hexadectet)")
}
guard let byte2 = UInt8(hexadectet[..<separationIndex], radix: 16) else {
guard let byte2 = UInt8(hexadectet[separationIndex...], radix: 16) else {
preconditionFailure("Invalid hexadectet defined: \(hexadectet)")
}
if shouldProcessPrefix {
if emptyChunksFound == 0 {
prefixBytes.append(byte1)
prefixBytes.append(byte2)
} else {