summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2023-11-17 15:23:09 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2023-11-17 15:23:09 +0100
commit80be8d3b7f7f4ec9eba31e75859febb0cb993293 (patch)
treecc9ec83a7348488cdb2f46aae293a831339fc1f5
parentd6fea03e6576f6ddc48d6562abd84ba26d146f81 (diff)
downloadprolog-firewall-80be8d3b7f7f4ec9eba31e75859febb0cb993293.tar.gz
prolog-firewall-80be8d3b7f7f4ec9eba31e75859febb0cb993293.tar.bz2
prolog-firewall-80be8d3b7f7f4ec9eba31e75859febb0cb993293.tar.xz
prolog-firewall-80be8d3b7f7f4ec9eba31e75859febb0cb993293.zip
wip
-rw-r--r--ip.pl23
1 files changed, 20 insertions, 3 deletions
diff --git a/ip.pl b/ip.pl
index c27d020..504caf4 100644
--- a/ip.pl
+++ b/ip.pl
@@ -22,7 +22,7 @@ ip_format(ip4_range(ip4(A, B, C, D, _), Range), Str) :-
ip4_g(A, B, C, D) --> ip4_num(A), ".", ip4_num(B), ".", ip4_num(C), ".", ip4_num(D).
ip4_num(D) --> integer(D), { D >= 0, D =< 255 }.
-ip_parse(Str, Obj) :-
+ip4_parse(Str, Obj) :-
string_codes(Str, Codes),
phrase(ip4_g(A, B, C, D), Codes),
Obj = ip4(A, B, C, D).
@@ -43,6 +43,15 @@ ip6_parse(Str, Obj) :-
phrase(ip6_g(A, B, C, D, E, F, G, H), Codes),
Obj = ip6(A, B, C, D, E, F, G, H).
+ip_parse(Str, Obj) :-
+ string_codes(Str, Codes),
+ (
+ phrase(ip4_g(A, B, C, D), Codes)
+ -> Obj = ip4(A, B, C, D)
+ ; phrase(ip6_g(A, B, C, D, E, F, G, H), Codes),
+ Obj = ip6(A, B, C, D, E, F, G, H)
+ ).
+
:- begin_tests(lists).
:- use_module(library(lists)).
@@ -59,8 +68,8 @@ test(ip_format) :-
ip_format(Ip, Str),
assertion(Str == "192.168.0.0/24").
-test(ip_parse) :-
- ip_parse("1.2.3.4", Ip),
+test(ip4_parse) :-
+ ip4_parse("1.2.3.4", Ip),
assertion(Ip == ip4(1, 2, 3, 4)).
test(ip6_parse) :-
@@ -71,4 +80,12 @@ test(ip6_parse) :-
ip6_parse("0:2:3:4:5:6:a:b", Ip),
assertion(Ip == ip6(0, 2, 3, 4, 5, 6, 10, 11)).
+test(ip_parse) :-
+ ip_parse("0:2:3:4:5:6:a:b", Ip),
+ assertion(Ip == ip6(0, 2, 3, 4, 5, 6, 10, 11)).
+
+test(ip_parse) :-
+ ip_parse("127.0.0.1", Ip),
+ assertion(Ip == ip4(127, 0, 0, 1)).
+
:- end_tests(lists).