]> gitweb.factorcode.org Git - factor.git/commitdiff
Make <inet4> and <ipv4> use ip-parser:parse-ipv4 1542/head
authorBenjamin Pollack <benjamin@bitquabit.com>
Sat, 5 Mar 2016 14:11:56 +0000 (09:11 -0500)
committerBenjamin Pollack <benjamin@bitquabit.com>
Sat, 5 Mar 2016 14:11:56 +0000 (09:11 -0500)
Fixes #1209

basis/io/sockets/sockets.factor
basis/ip-parser/authors.txt [new file with mode: 0644]
basis/ip-parser/ip-parser-docs.factor [new file with mode: 0644]
basis/ip-parser/ip-parser-tests.factor [new file with mode: 0644]
basis/ip-parser/ip-parser.factor [new file with mode: 0644]
extra/ip-parser/authors.txt [deleted file]
extra/ip-parser/ip-parser-docs.factor [deleted file]
extra/ip-parser/ip-parser-tests.factor [deleted file]
extra/ip-parser/ip-parser.factor [deleted file]

index 49935e194ab3bd262ff53d3ad990c6e1792a4b7f..0061f8c5cf1726f55dc324df9d303230e9b405e2 100644 (file)
@@ -8,7 +8,7 @@ grouping init io.backend io.binary io.encodings.ascii
 io.encodings.binary io.pathnames io.ports io.streams.duplex
 kernel locals math math.parser memoize namespaces present
 sequences sequences.private splitting strings summary system
-vocabs vocabs.parser ;
+vocabs vocabs.parser ip-parser ;
 IN: io.sockets
 
 << {
@@ -76,7 +76,7 @@ ERROR: malformed-ipv4 sequence ;
 
 ERROR: bad-ipv4-component string ;
 
-: parse-ipv4 ( string -- seq )
+: ipv4>bytes ( string -- seq )
     [ f ] [
         "." split dup length 4 = [ malformed-ipv4 ] unless
         [ dup string>number [ ] [ bad-ipv4-component ] ?if ] B{ } map-as
@@ -93,7 +93,7 @@ M: ipv4 inet-ntop ( data addrspec -- str )
     drop 4 memory>byte-array [ number>string ] { } map-as "." join ;
 
 M: ipv4 inet-pton ( str addrspec -- data )
-    drop [ parse-ipv4 ] [ invalid-ipv4 ] recover ;
+    drop [ parse-ipv4 ipv4>bytes ] [ invalid-ipv4 ] recover ;
 
 M: ipv4 address-size drop 4 ;
 
@@ -154,7 +154,7 @@ ERROR: more-than-8-components ;
     [ f ] [
         ":" split CHAR: . over last member? [
             unclip-last
-            [ parse-ipv6-component ] [ parse-ipv4 ] bi* append
+            [ parse-ipv6-component ] [ ipv4>bytes ] bi* append
         ] [
             parse-ipv6-component
         ] if
diff --git a/basis/ip-parser/authors.txt b/basis/ip-parser/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/basis/ip-parser/ip-parser-docs.factor b/basis/ip-parser/ip-parser-docs.factor
new file mode 100644 (file)
index 0000000..f9eae1b
--- /dev/null
@@ -0,0 +1,15 @@
+USING: help.markup help.syntax strings ;
+IN: ip-parser
+
+HELP: parse-ipv4
+{ $values { "str" string } { "ip" string } }
+{ $description "Parses an IP string that may not have all four address components specified, following these rules:" $nl
+    { $table
+        { { $snippet "A" } { $snippet "0.0.0.A" } }
+        { { $snippet "A.D" } { $snippet "A.0.0.D" } }
+        { { $snippet "A.B.D" } { $snippet "A.B.0.D" } }
+        { { $snippet "A.B.C.D" } { $snippet "A.B.C.D" } }
+    }
+    $nl
+    "In addition, this supports components specified as decimal, octal, hexadecimal, and mixed representations, as well as components specified larger than 255 by carry propagation."
+} ;
diff --git a/basis/ip-parser/ip-parser-tests.factor b/basis/ip-parser/ip-parser-tests.factor
new file mode 100644 (file)
index 0000000..a1a0841
--- /dev/null
@@ -0,0 +1,26 @@
+
+USING: kernel sequences tools.test ;
+
+IN: ip-parser
+
+{ "0.0.0.1" } [ "1" parse-ipv4 ] unit-test
+{ "1.0.0.2" } [ "1.2" parse-ipv4 ] unit-test
+{ "1.2.0.3" } [ "1.2.3" parse-ipv4 ] unit-test
+{ "1.2.3.4" } [ "1.2.3.4" parse-ipv4 ] unit-test
+[ "1.2.3.4.5" parse-ipv4 ] must-fail
+{ "0.0.0.255" } [ "255" parse-ipv4 ] unit-test
+{ "0.0.1.0" } [ "256" parse-ipv4 ] unit-test
+
+{ t } [
+    {
+        "1249763844" ! flat decimal
+        "0112.0175.0342.0004" ! dotted octal
+        "011237361004" ! flat octal
+        "0x4A.0x7D.0xE2.0x04" ! dotted hex
+        "0x4A7DE204" ! flat hex
+        "74.0175.0xe2.4"
+    } [ parse-ipv4 "74.125.226.4" = ] all?
+] unit-test
+
+{ "74.125.226.4" } [ 1249763844 ipv4-ntoa ] unit-test
+{ 1249763844 } [ "74.125.226.4" ipv4-aton ] unit-test
diff --git a/basis/ip-parser/ip-parser.factor b/basis/ip-parser/ip-parser.factor
new file mode 100644 (file)
index 0000000..5dc03fa
--- /dev/null
@@ -0,0 +1,43 @@
+! Copyright (C) 2012-2014 John Benediktsson
+! See http://factorcode.org/license.txt for BSD license
+USING: combinators combinators.short-circuit kernel math
+math.bitwise math.parser math.vectors sequences splitting ;
+
+IN: ip-parser
+
+ERROR: invalid-ipv4 str ;
+
+<PRIVATE
+
+: cleanup-octal ( str -- str )
+    dup { [ "0" = not ] [ "0" head? ] [ "0x" head? not ] } 1&&
+    [ rest "0o" prepend ] when ;
+
+: split-components ( str -- array )
+    "." split [ cleanup-octal string>number ] map ;
+
+: bubble ( array -- newarray )
+    reverse 0 swap [ + 256 /mod ] map reverse nip ;
+
+: join-components ( array -- str )
+    [ number>string ] map "." join ;
+
+: (parse-ipv4) ( str -- array )
+    dup split-components dup length {
+        { 1 [ { 0 0 0 } prepend ] }
+        { 2 [ 1 cut { 0 0 } glue ] }
+        { 3 [ 2 cut { 0 } glue ] }
+        { 4 [ ] }
+        [ drop invalid-ipv4 ]
+    } case bubble nip ; inline
+
+PRIVATE>
+
+: parse-ipv4 ( str -- ip )
+    (parse-ipv4) join-components ;
+
+: ipv4-ntoa ( integer -- ip )
+    { -24 -16 -8 0 } [ 8 shift-mod ] with map join-components ;
+
+: ipv4-aton ( ip -- integer )
+    (parse-ipv4) { 24 16 8 0 } [ shift ] [ + ] 2map-reduce ;
diff --git a/extra/ip-parser/authors.txt b/extra/ip-parser/authors.txt
deleted file mode 100644 (file)
index e091bb8..0000000
+++ /dev/null
@@ -1 +0,0 @@
-John Benediktsson
diff --git a/extra/ip-parser/ip-parser-docs.factor b/extra/ip-parser/ip-parser-docs.factor
deleted file mode 100644 (file)
index f9eae1b..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-USING: help.markup help.syntax strings ;
-IN: ip-parser
-
-HELP: parse-ipv4
-{ $values { "str" string } { "ip" string } }
-{ $description "Parses an IP string that may not have all four address components specified, following these rules:" $nl
-    { $table
-        { { $snippet "A" } { $snippet "0.0.0.A" } }
-        { { $snippet "A.D" } { $snippet "A.0.0.D" } }
-        { { $snippet "A.B.D" } { $snippet "A.B.0.D" } }
-        { { $snippet "A.B.C.D" } { $snippet "A.B.C.D" } }
-    }
-    $nl
-    "In addition, this supports components specified as decimal, octal, hexadecimal, and mixed representations, as well as components specified larger than 255 by carry propagation."
-} ;
diff --git a/extra/ip-parser/ip-parser-tests.factor b/extra/ip-parser/ip-parser-tests.factor
deleted file mode 100644 (file)
index a1a0841..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-
-USING: kernel sequences tools.test ;
-
-IN: ip-parser
-
-{ "0.0.0.1" } [ "1" parse-ipv4 ] unit-test
-{ "1.0.0.2" } [ "1.2" parse-ipv4 ] unit-test
-{ "1.2.0.3" } [ "1.2.3" parse-ipv4 ] unit-test
-{ "1.2.3.4" } [ "1.2.3.4" parse-ipv4 ] unit-test
-[ "1.2.3.4.5" parse-ipv4 ] must-fail
-{ "0.0.0.255" } [ "255" parse-ipv4 ] unit-test
-{ "0.0.1.0" } [ "256" parse-ipv4 ] unit-test
-
-{ t } [
-    {
-        "1249763844" ! flat decimal
-        "0112.0175.0342.0004" ! dotted octal
-        "011237361004" ! flat octal
-        "0x4A.0x7D.0xE2.0x04" ! dotted hex
-        "0x4A7DE204" ! flat hex
-        "74.0175.0xe2.4"
-    } [ parse-ipv4 "74.125.226.4" = ] all?
-] unit-test
-
-{ "74.125.226.4" } [ 1249763844 ipv4-ntoa ] unit-test
-{ 1249763844 } [ "74.125.226.4" ipv4-aton ] unit-test
diff --git a/extra/ip-parser/ip-parser.factor b/extra/ip-parser/ip-parser.factor
deleted file mode 100644 (file)
index 5dc03fa..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-! Copyright (C) 2012-2014 John Benediktsson
-! See http://factorcode.org/license.txt for BSD license
-USING: combinators combinators.short-circuit kernel math
-math.bitwise math.parser math.vectors sequences splitting ;
-
-IN: ip-parser
-
-ERROR: invalid-ipv4 str ;
-
-<PRIVATE
-
-: cleanup-octal ( str -- str )
-    dup { [ "0" = not ] [ "0" head? ] [ "0x" head? not ] } 1&&
-    [ rest "0o" prepend ] when ;
-
-: split-components ( str -- array )
-    "." split [ cleanup-octal string>number ] map ;
-
-: bubble ( array -- newarray )
-    reverse 0 swap [ + 256 /mod ] map reverse nip ;
-
-: join-components ( array -- str )
-    [ number>string ] map "." join ;
-
-: (parse-ipv4) ( str -- array )
-    dup split-components dup length {
-        { 1 [ { 0 0 0 } prepend ] }
-        { 2 [ 1 cut { 0 0 } glue ] }
-        { 3 [ 2 cut { 0 } glue ] }
-        { 4 [ ] }
-        [ drop invalid-ipv4 ]
-    } case bubble nip ; inline
-
-PRIVATE>
-
-: parse-ipv4 ( str -- ip )
-    (parse-ipv4) join-components ;
-
-: ipv4-ntoa ( integer -- ip )
-    { -24 -16 -8 0 } [ 8 shift-mod ] with map join-components ;
-
-: ipv4-aton ( ip -- integer )
-    (parse-ipv4) { 24 16 8 0 } [ shift ] [ + ] 2map-reduce ;