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
<< {
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
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 ;
[ 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
--- /dev/null
+John Benediktsson
--- /dev/null
+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."
+} ;
--- /dev/null
+
+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
--- /dev/null
+! 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 ;
+++ /dev/null
-John Benediktsson
+++ /dev/null
-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."
-} ;
+++ /dev/null
-
-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
+++ /dev/null
-! 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 ;