Canonicalize CIDR: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: fixed the default IPv4 address.)
(→‎{{header|REXX}}: used a more generic approach.)
Line 304: Line 304:
parse arg z . /*obtain optional argument from the CL.*/
parse arg z . /*obtain optional argument from the CL.*/
if z=='' | z=="," then z= '87.70.141.1/22' /*Not specified? Then use the default.*/
if z=='' | z=="," then z= '87.70.141.1/22' /*Not specified? Then use the default.*/
parse var z # '/' -0 mask /*get the address nodes & optional mask*/
parse var z # '/' -0 mask /*get the address nodes & netaddr mask.*/
size= d2b( substr(mask, 2) ) /*size of binary network mask in bytes.*/
shift= length(size % 2**8) /*compute the size of the binary shift.*/
#= translate(#, , .) /*elide periods from IPv4 address nodes*/
#= translate(#, , .) /*elide periods from IPv4 address nodes*/
$= /*initialize the to-be reassembled IPv4*/
$= /*initialize the to-be reassembled IPv4*/
do j=1 for words(#); x= word(#, j) /*process each of the address nodes. */
do j=1 for max( words(#), 4) /*process each of IP node, a min of 4. */
if j>1 & x//2 then x= x - 1 /*Is low─order bit on? Then remove it.*/
x= word( word(#, j) 0, 1) /*use a zero for omitted IP nodes. */
if j>shift then do /*possibly elide some low─order bits. */
_= right( d2b(x), 2**5, 0) /*convert X to binary, pad with zeros.*/
x= b2d( (left(_, length(_)-shift) || left('', shift, 0) ) )
end /* [↑] "zero─out" some low─order bits*/
$= $ x /*reconstruct (by abutment) IPv4 nodes.*/
$= $ x /*reconstruct (by abutment) IPv4 nodes.*/
end /*j*/ /*(REXX stores it's numbers in decimal)*/
end /*j*/ /*(REXX stores it's numbers in decimal)*/

/*stick a fork in it, we're all done. */
say ' original IPv4 address: ' z /*display the original IPv4 address. */
say ' original IPv4 address: ' z /*display the original IPv4 address. */
say ' canonicalized address: ' translate( space($), ., " ") || mask /*canonicalized.*/</lang>
say ' canonicalized address: ' translate( space($), ., " ")mask /*canonicalized.*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
b2d: return x2d( b2x( arg(1) ) ) + 0 /*convert binary ───► decimal number.*/
d2b: return x2b( d2x( arg(1) ) ) + 0 /* " decimal ───► binary " */</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>