From 80be8d3b7f7f4ec9eba31e75859febb0cb993293 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 17 Nov 2023 15:23:09 +0100 Subject: wip --- ip.pl | 23 ++++++++++++++++++++--- 1 file 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). -- cgit v1.2.3