"2001:db8::ff00:42:8329"
} [ parse-ipv6 { 8193 3512 0 0 0 65280 66 33577 } = ] all?
] unit-test
+
+{ 1 } [ "::1" ipv6-aton ] unit-test
+{ "::1" } [ 1 ipv6-ntoa ] unit-test
+{ 0x10000000000000000000000000000 } [ "1::" ipv6-aton ] unit-test
+{ "1::" } [ 0x10000000000000000000000000000 ipv6-ntoa ] unit-test
+{ 0x10002000000000000000000030004 } [ "1:2::3:4" ipv6-aton ] unit-test
+{ "1:2::3:4" } [ 0x10002000000000000000000030004 ipv6-ntoa ] unit-test
! Copyright (C) 2012-2014 John Benediktsson
! See https://factorcode.org/license.txt for BSD license
-USING: byte-arrays combinators combinators.short-circuit kernel
-math math.bitwise math.parser sequences splitting ;
+
+USING: arrays byte-arrays combinators combinators.short-circuit
+endian grouping kernel math math.bitwise math.parser regexp
+sequences splitting ;
IN: ip-parser
ERROR: malformed-ipv4 string ;
+ERROR: malformed-ipv6 string ;
+
ERROR: bad-ipv4-component string ;
<PRIVATE
: parse-ipv6 ( string -- seq )
"::" split1 [ [ f ] [ split-ipv6 ] if-empty ] bi@ pad-ipv6 ;
+
+: ipv6-ntoa ( integer -- ip )
+ 16 >be bytes>hex-string 4 <groups>
+ [ [ CHAR: 0 = ] trim-head ] map ":" join
+ R/ [:][:]+/ "::" re-replace ;
+
+: ipv6-aton ( ip -- integer )
+ parse-ipv6 0 [ [ 16 shift ] [ + ] bi* ] reduce ;