Canonicalize CIDR: Difference between revisions

add RPL
(Added XPL0 example.)
(add RPL)
Line 1,711:
canonicalized address: 184.232.128.0/18
</pre>
=={{header|RPL}}==
RPL has hundreds of built-in functions, but strangely a few limited ones for bit handling. There is no <code>>></code> operator, so shifting left an integer by n bits shall be done through a loop: <code> 1 n '''START''' SL '''NEXT'''</code>
{| class="wikitable"
! RPL code
! Comment
|-
|
DUP SIZE "" 1 ROT '''FOR''' j
OVER j j SUB DUP "0" ≥ OVER "9" ≤ AND SWAP " "
IFTE + '''NEXT'''
SWAP DROP STR→ → nbits
≪ SWAP ROT 4 ROLL
32 STWS 1 3 '''START''' #100h * + '''NEXT'''
#FFFFFFFF 1 32 nbits - '''START''' SL '''NEXT''' AND
1 3 '''START''' DUP SRB SWAP OVER SLB - SWAP '''NEXT'''
B→R →STR 1 3 '''START''' "." + SWAP B→R →STR + '''NEXT'''
"/" + nbits →STR +
≫ ≫ ‘<span style="color:blue">CANON</span>’ STO
|
<span style="color:blue">CANON</span> ''( "nn.nn.nn.nn/bits" → "nn.nn.nn.nn/nbits" )
Scan input string
Replace non-numerical chars by spaces
Drop input string, parse addresses to stack - except # bits
Reverse stack order
Consolidate numbers into a 32-bit number
Set and apply mask
Break 32-bit number into 4 in the stack
Make a IP address with the 4 numbers
add # bits
|}
≪ { "36.18.154.103/12" "62.62.197.11/29" "67.137.119.181/4" "161.214.74.21/24" "184.232.176.184/18" } → t
≪ 1 5 '''FOR''' j t j GET <span style="color:blue">CANON</span> '''NEXT'''
≫ ≫ ‘<span style="color:blue">TASK</span>’ STO
{{out}}
<pre>
5: "36.16.0.0/12"
4: "62.62.197.8/29"
3: "64.0.0.0/4"
2: "161.214.74.0/24"
1: "184.232.128.0/18"
</pre>
 
=={{header|Ruby}}==
{{trans|Python}}
Line 1,764 ⟶ 1,809:
161.214.74.0/24
184.232.128.0/18</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::net::Ipv4Addr;
1,150

edits