133 lines
2.5 KiB
Plaintext
133 lines
2.5 KiB
Plaintext
fn is-ipv4 [arg]{
|
|
use re
|
|
if (not (re:match '^([0-9]{1,3}\.){3}[0-9]{1,3}$' $arg)) {
|
|
put $false
|
|
} else {
|
|
use ./list
|
|
not (splits '.' $arg | list:contains [p]{ > $p 255 })
|
|
}
|
|
}
|
|
|
|
fn is-ipv6 [arg]{
|
|
use re
|
|
local:p = '[0-9a-fA-F]{1,4}'
|
|
local:g1 local:g2 = '('$p':)' '(:'$p')'
|
|
local:cases = [
|
|
$g1'{7,7}'$p
|
|
$g1'{1,7}:'
|
|
$g1'{1,6}:'$p
|
|
$g1'{1,5}'$g2'{1,2}'
|
|
$g1'{1,4}'$g2'{1,3}'
|
|
$g1'{1,3}'$g2'{1,4}'
|
|
$g1'{1,2}'$g2'{1,5}'
|
|
$p':'$g2'{1,6}'
|
|
':'$g2'{1,7}'
|
|
'::'
|
|
]
|
|
local:r = '^('(joins '|' $cases)')$'
|
|
put (re:match $r $arg)
|
|
}
|
|
|
|
fn -long-part6 [p]{
|
|
use str
|
|
p = (str:to-lower $p)
|
|
c = (- 4 (count $p))
|
|
put (repeat $c '0' | joins '')$p
|
|
}
|
|
|
|
fn -middle-part6 [p]{
|
|
while (and (> (count $p) 1) (eq $p[0] 0)) {
|
|
p = $p[1:]
|
|
}
|
|
put $p
|
|
}
|
|
|
|
fn -find-max0 [parts]{
|
|
use ./list
|
|
use ./condition
|
|
local:idx local:s = -1 1
|
|
local:ci local:cs local:f = -1 0 $false
|
|
list:loop [i p]{
|
|
if (eq $p 0) {
|
|
condition:call $f { cs = (+ $cs 1) } { ci cs f = $i 1 $true }
|
|
} elif $f {
|
|
f = $false
|
|
if (> $cs $s) {
|
|
idx s = $ci $cs
|
|
}
|
|
}
|
|
} $parts
|
|
if (and $f (> $cs $s)) {
|
|
idx s = $ci $cs
|
|
}
|
|
put $idx $s
|
|
}
|
|
|
|
fn long6 [ip]{
|
|
if (not (is-ipv6 $ip)) {
|
|
fail 'Not an IPv6'
|
|
}
|
|
if (eq $ip '::') {
|
|
repeat 8 '0000' | joins ':'
|
|
return
|
|
}
|
|
use str
|
|
local:c = (- 7 (str:count $ip ':'))
|
|
if (> $c 0) {
|
|
local:i = (str:index $ip '::')
|
|
local:z = (repeat $c ':' | joins '')
|
|
ip = (joins '' [$ip[:$i] $z $ip[{$i}:]])
|
|
}
|
|
splits ':' $ip | each $-long-part6~ | joins ':'
|
|
}
|
|
|
|
fn middle6 [ip]{
|
|
splits ':' (long6 $ip) | each $-middle-part6~ | joins ':'
|
|
}
|
|
|
|
fn short6 [ip]{
|
|
local:parts = [(splits ':' (middle6 $ip))]
|
|
local:i local:s = (-find-max0 $parts)
|
|
if (>= $i 0) {
|
|
local:left local:right = $parts[:$i] $parts[(+ $i $s):]
|
|
if (== (count $left) 0) {
|
|
left = ['']
|
|
}
|
|
if (== (count $right) 0) {
|
|
right = ['']
|
|
}
|
|
parts = [$@left '' $@right]
|
|
}
|
|
joins ':' $parts
|
|
}
|
|
|
|
fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) }
|
|
|
|
fn -cmp [e1 e2]{
|
|
use ./list
|
|
use ./condition
|
|
local:c = 0
|
|
list:loop [i p1]{
|
|
local:p2 = $e2[$i]
|
|
c = (condition:set (< $p1 $p2) -1 (condition:set (> $p1 $p2) 1 0))
|
|
if (!= $c 0) {
|
|
break
|
|
}
|
|
} $e1
|
|
put $c
|
|
}
|
|
|
|
fn cmp4 [ip1 ip2]{
|
|
if (or (not (is-ipv4 $ip1)) (not (is-ipv4 $ip2))) {
|
|
fail 'Not an IPv4'
|
|
}
|
|
-cmp [(splits . $ip1)] [(splits . $ip2)]
|
|
}
|
|
|
|
fn cmp6 [ip1 ip2]{
|
|
if (or (not (is-ipv6 $ip1)) (not (is-ipv6 $ip2))) {
|
|
fail 'Not an IPv6'
|
|
}
|
|
-cmp [(splits : (middle6 $ip1))] [(splits : (middle6 $ip2))]
|
|
}
|