-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* change preimage size to data count * update * update ignore * update * no message * update Cli * update cli * update regex * update regexs * update regexs * update test * update regexs * udpate regex * revert submodule change
- Loading branch information
Showing
4 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Foundation | ||
|
||
public enum RegexsError: Error { | ||
case invalidFormat | ||
case invalidPort | ||
} | ||
|
||
public enum Regexs { | ||
// Combined regex pattern for IP address with port | ||
public static func parseAddress(_ address: String) throws -> (ip: String, port: Int) { | ||
let ipv4Pattern: String = [ | ||
"(?:", | ||
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", | ||
"\\.", | ||
"){3}", | ||
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", | ||
].reduce("", +) | ||
|
||
let ipv6Pattern = [ | ||
"(?:", | ||
"(?:(?:[0-9A-Fa-f]{1,4}:){6}", | ||
"|::(?:[0-9A-Fa-f]{1,4}:){5}", | ||
"|(?:[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}", | ||
"|(?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}", | ||
"|(?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}", | ||
"|(?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}:", | ||
"|(?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::)", | ||
"(?:", | ||
"[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}", | ||
"|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}", | ||
"(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", | ||
")", | ||
"|(?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}", | ||
"|(?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::", | ||
")", | ||
].reduce("", +) | ||
let ipAddressWithPortPattern = #"(?:(\#(ipv4Pattern))|\[(\#(ipv6Pattern))\]):(\d{1,5})"# | ||
|
||
let regex = try NSRegularExpression(pattern: ipAddressWithPortPattern, options: []) | ||
let range = NSRange(location: 0, length: address.utf16.count) | ||
|
||
if let match = regex.firstMatch(in: address, options: [], range: range) { | ||
let ipRange: Range<String.Index> | ||
if let ipv4Range = Range(match.range(at: 1), in: address) { | ||
ipRange = ipv4Range | ||
} else if let ipv6Range = Range(match.range(at: 2), in: address) { | ||
ipRange = ipv6Range | ||
} else { | ||
throw RegexsError.invalidFormat | ||
} | ||
|
||
let portRange = Range(match.range(at: 3), in: address)! | ||
|
||
let ip = String(address[ipRange]) | ||
let portString = String(address[portRange]) | ||
|
||
if let port = Int(portString), (0 ... 65535).contains(port) { | ||
return (ip, port) | ||
} else { | ||
throw RegexsError.invalidPort | ||
} | ||
} else { | ||
throw RegexsError.invalidFormat | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import Foundation | ||
import Testing | ||
|
||
@testable import Utils | ||
|
||
struct RegexsTests { | ||
@Test func parseAddress() throws { | ||
// Correct IPv4 address | ||
#expect(try Regexs.parseAddress("127.0.0.1:9955") == ("127.0.0.1", 9955)) | ||
|
||
// Correct IPv6 addresses | ||
#expect(try Regexs | ||
.parseAddress("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:8080") == ("2001:0db8:85a3:0000:0000:8a2e:0370:7334", 8080)) | ||
#expect(try Regexs.parseAddress("[2001:db8:85a3::8a2e:370:7334]:8080") == ("2001:db8:85a3::8a2e:370:7334", 8080)) | ||
#expect(try Regexs.parseAddress("[::1]:8080") == ("::1", 8080)) | ||
|
||
// // Exception case: Missing port | ||
#expect(throws: RegexsError.invalidFormat) { try Regexs.parseAddress("127.0.0.1") } | ||
#expect(throws: RegexsError.invalidFormat) { try Regexs.parseAddress("abcd:::") } | ||
// Exception case: Invalid port | ||
#expect(throws: RegexsError.invalidPort) { try Regexs.parseAddress("127.0.0.1:75535") } | ||
#expect(throws: RegexsError.invalidPort) { try Regexs.parseAddress("[2001:db8::1]:75535") } | ||
|
||
// Exception case: Invalid IPv4 format | ||
#expect(throws: RegexsError.invalidFormat) { try Regexs.parseAddress("256.256.256.256:8080") } | ||
|
||
// Exception case: Invalid IPv6 format | ||
#expect(throws: RegexsError.invalidFormat) { try Regexs.parseAddress("[2001:db8:::1]:8080") } | ||
} | ||
} |