Canonicalize CIDR: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Removed an unnecessary space.) |
(→{{header|Go}}: Changed to cope with the extra examples.) |
||
Line 203: | Line 203: | ||
"fmt" |
"fmt" |
||
"log" |
"log" |
||
⚫ | |||
"strconv" |
"strconv" |
||
"strings" |
"strings" |
||
) |
) |
||
func check(err error) { |
func check(err error) { |
||
if err != nil { |
if err != nil { |
||
log.Fatal(err) |
log.Fatal(err) |
||
} |
} |
||
} |
} |
||
⚫ | |||
⚫ | |||
func canonicalize(cidr string) string { |
|||
⚫ | |||
⚫ | |||
if len(os.Args) > 1 { |
|||
cidr = os.Args[1] |
|||
⚫ | |||
log.Fatal("Please pass the CIDR to be canonicalized.") |
|||
} |
|||
// dotted-decimal / bits in network part |
// dotted-decimal / bits in network part |
||
split := strings.Split(cidr, "/") |
split := strings.Split(cidr, "/") |
||
Line 249: | Line 241: | ||
} |
} |
||
// and |
// and return |
||
return strings.Join(canon, ".") + "/" + split[1] |
|||
} |
|||
⚫ | |||
⚫ | |||
"87.70.141.1/22", |
|||
"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", |
|||
⚫ | |||
for _, test := range tests { |
|||
fmt.Printf("%-18s -> %s\n", test, canonicalize(test)) |
|||
⚫ | |||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
87.70.141.1/22 -> 87.70.140.0/22 |
|||
36.18.154.103/12 -> 36.16.0.0/12 |
|||
87.70.140.0/22 |
|||
62.62.197.11/29 -> 62.62.197.8/29 |
|||
67.137.119.181/4 -> 64.0.0.0/4 |
|||
161.214.74.21/24 -> 161.214.74.0/24 |
|||
184.232.176.184/18 -> 184.232.128.0/18 |
|||
</pre> |
</pre> |
||