Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
\ ============================================================================== \ \ RubyFORTH -- Copyright (C) 2007-8, Marc Simpson (GPL). \ \ Convert IPs between integer and octet representation. \ \ ============================================================================== \ --[ IP Strings ]-------------------------------------------------------------- prim ip-split pop.split(".").each{|octet|push(octet.to_i)} \ --[ Printing ]---------------------------------------------------------------- prim (.) print pop \ --[ Shifting ]---------------------------------------------------------------- : byte* ( n1 -- n2 ) 256 * ; : bytes* ( n1 n2 -- n3 ) 0 do byte* loop ; : byte/ ( n1 -- n2 ) 256 / ; : bytes/ ( n1 n2 -- n3 ) 0 do byte/ loop ; \ --[ Encoding ]---------------------------------------------------------------- : as-integer ( w x y z -- n ) 0 ( sum ) 4 0 do swap i bytes* + loop ; : as-integer" ( "ipaddr" -- n ) char " parse ip-split as-integer ; \ --[ Decoding ]---------------------------------------------------------------- : as-octet ( n -- z y x w ) 4 0 do dup 255 and swap byte/ loop drop ; : .as-octet ( n -- ) as-octet 3 0 do (.) char . emit loop . ; \ ------------------------------------------------------------------------------