ip-parser: minor cleanup.

db4
John Benediktsson 2012-10-18 17:01:49 -07:00
parent 6776388c8f
commit 8cf23c71ac
1 changed files with 7 additions and 22 deletions

View File

@ -21,30 +21,15 @@ IN: ip-parser
: join-components ( array -- str ) : join-components ( array -- str )
bubble [ number>string ] map "." join ; bubble [ number>string ] map "." join ;
: components ( str -- n )
[ CHAR: . = ] count ;
: parse-1 ( str -- ip )
split-components { 0 0 0 } prepend ;
: parse-2 ( str -- ip )
split-components first2 [| A D | { A 0 0 D } ] call ;
: parse-3 ( str -- ip )
split-components first3 [| A B D | { A B 0 D } ] call ;
: parse-4 ( str -- ip )
split-components ;
PRIVATE> PRIVATE>
ERROR: invalid-ipv4 str ; ERROR: invalid-ipv4 str ;
: parse-ipv4 ( str -- ip ) : parse-ipv4 ( str -- ip )
dup components { dup split-components dup length {
{ 0 [ parse-1 ] } { 1 [ { 0 0 0 } prepend ] }
{ 1 [ parse-2 ] } { 2 [ first2 [| A D | { A 0 0 D } ] call ] }
{ 2 [ parse-3 ] } { 3 [ first3 [| A B D | { A B 0 D } ] call ] }
{ 3 [ parse-4 ] } { 4 [ ] }
[ invalid-ipv4 ] [ drop invalid-ipv4 ]
} case join-components ; } case join-components nip ;