diff options
-rw-r--r-- | ip.pl | 23 |
1 files changed, 20 insertions, 3 deletions
@@ -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). |