netip.bend checks
raw source on the hub · import 0x2997b34519267605d9e11af48ae387c7/netip.bend as Netip
IPv4 and IPv6 address values: strict parsing, RFC 5952 text, and CIDR prefixes. Source: https://github.com/paymog/bend-kit/tree/main/netip
1 import
import Base
Types
type Addr source · line 5 · raw
Data
V4: the address as one big-endian word. V6: four network-order words, a first.
V4@word:U32 -> Addr
V6@a:U32 -> @b:U32 -> @c:U32 -> @d:U32 -> Addr
type Walk source · line 107 · raw
Data
Groups read so far, each list newest first. Gap: "::" was seen; post holds the groups after it.
NoGap@pre:List<&2, U32> -> Walk
Gap@pre:List<&2, U32> -> @post:List<&2, U32> -> Walk
type Prefix source · line 353 · raw
Data
Prefix@addr:Addr -> @bits:U32 -> Prefix
Definitions
def hexval source · line 10 · raw
@+c:U32 -> U32
A hex digit's value, or 16 when c is not one.
def hex.step source · line 15 · raw
@+d:U32 -> @acc:Maybe<&2, U32> -> Maybe<&2, U32>
def hex.go source · line 22 · raw
@s:String -> @acc:Maybe<&2, U32> -> Maybe<&2, U32>
def hextet.ok source · line 29 · raw
@ok:Bool -> @s:String -> Maybe<&2, U32>
def hextet source · line 37 · raw
@+s:String -> Maybe<&2, U32>
One IPv6 group: 1 to 4 hex digits, either case.
def dec.step source · line 41 · raw
@+d:U32 -> @acc:Maybe<&2, U32> -> Maybe<&2, U32>
A non-digit wraps past 9 in (c - 48), so it fails the digit test.
def dec.go source · line 48 · raw
@s:String -> @acc:Maybe<&2, U32> -> Maybe<&2, U32>
def octet.lead source · line 56 · raw
@s:String -> Bool
1 to 3 characters, and no leading zero unless the octet is "0".
def octet.max source · line 63 · raw
@m:Maybe<&2, U32> -> Maybe<&2, U32>
def octet.ok source · line 70 · raw
@ok:Bool -> @s:String -> Maybe<&2, U32>
def octet source · line 77 · raw
@+s:String -> Maybe<&2, U32>
def v4.parts source · line 80 · raw
@ps:List<&2, String> -> Maybe<&2, U32>
def v4.word source · line 93 · raw
@s:String -> Maybe<&2, U32>
Dotted-decimal IPv4 as its big-endian word.
def v4.wrap source · line 96 · raw
@m:Maybe<&2, U32> -> Maybe<&2, Addr>
def v4 source · line 103 · raw
@s:String -> Maybe<&2, Addr>
def walk.push source · line 111 · raw
@st:Walk -> @+g:U32 -> Walk
def v6.hex source · line 118 · raw
@w:Walk -> @m:Maybe<&2, U32> -> Maybe<&2, Walk>
def v6.gap source · line 126 · raw
@w:Walk -> Maybe<&2, Walk>
An empty inner segment is the one "::".
def v6.mid.seg source · line 133 · raw
@h:String -> @w:Walk -> Maybe<&2, Walk>
def v6.mid source · line 140 · raw
@st:Maybe<&2, Walk> -> @h:String -> Maybe<&2, Walk>
def v6.trail source · line 148 · raw
@w:Walk -> Maybe<&2, Walk>
An empty last segment is the second colon of a trailing "::".
def v6.dot source · line 156 · raw
@w:Walk -> @m:Maybe<&2, U32> -> Maybe<&2, Walk>
Embedded dotted IPv4 fills the last two groups.
def v6.last.kind source · line 163 · raw
@dot:Bool -> @w:Walk -> @s:String -> Maybe<&2, Walk>
def v6.last.text source · line 170 · raw
@w:Walk -> @+s:String -> Maybe<&2, Walk>
def v6.last.seg source · line 173 · raw
@h:String -> @w:Walk -> Maybe<&2, Walk>
def v6.last source · line 180 · raw
@st:Maybe<&2, Walk> -> @h:String -> Maybe<&2, Walk>
def v6.go source · line 188 · raw
@t:List<&2, String> -> @h:String -> @st:Maybe<&2, Walk> -> Maybe<&2, Walk>
h: the current segment; t: the segments after it.
def v6.words source · line 195 · raw
@gs:List<&2, U32> -> Maybe<&2, Addr>
def v6.pad source · line 203 · raw
@ok:Bool -> @pre:List<&2, U32> -> @post:List<&2, U32> -> @n:Nat -> Maybe<&2, Addr>
"::" stands for at least one zero group.
def v6.fill source · line 210 · raw
@w:Walk -> Maybe<&2, Addr>
def v6.fin source · line 218 · raw
@m:Maybe<&2, Walk> -> Maybe<&2, Addr>
def v6 source · line 226 · raw
@ps:List<&2, String> -> Maybe<&2, Addr>
The ":"-split segments. Only a leading "::" may start with an empty segment.
def parse.fam source · line 237 · raw
@colon:Bool -> @s:String -> Maybe<&2, Addr>
def parse source · line 246 · raw
@+s:String -> Maybe<&2, Addr>
Strict address text: 4 dotted-decimal octets, or RFC 4291 IPv6 with at most one "::" and an optional trailing dotted IPv4. No brackets, zones, ports, or surrounding space.
def hex.char source · line 250 · raw
@+n:U32 -> String
Hex fields contain no leading zeros.
def hex.two source · line 253 · raw
@+n:U32 -> String
def hex.three source · line 256 · raw
@+n:U32 -> String
def hex.four source · line 259 · raw
@+n:U32 -> String
def hex.many source · line 262 · raw
@n:U32 -> @small:Bool -> String
def hex.more source · line 269 · raw
@+n:U32 -> @small:Bool -> String
def hex.group.small source · line 276 · raw
@+n:U32 -> @small:Bool -> String
def hex.group source · line 283 · raw
@+n:U32 -> String
def groups source · line 286 · raw
@+a:U32 -> @+b:U32 -> @+c:U32 -> @+d:U32 -> List<&2, U32>
def zeros.best source · line 291 · raw
@xs:List<&2, U32> -> @+i:U32 -> @+run:U32 -> @+start:U32 -> @+best:U32 -> @+length:U32 -> Pair(U32, U32)
Keep the first run when two longest zero runs tie.
def v6.field.if source · line 302 · raw
@inside:Bool -> @n:U32 -> @+i:U32 -> @start:U32 -> @end:U32 -> String
def v6.field source · line 309 · raw
@n:U32 -> @+i:U32 -> @+start:U32 -> @+length:U32 -> String
def v6.render source · line 313 · raw
@xs:List<&2, U32> -> @+i:U32 -> @+start:U32 -> @+length:U32 -> String
def v6.show.run source · line 320 · raw
@+a:U32 -> @+b:U32 -> @+c:U32 -> @+d:U32 -> @run:Pair(U32, U32) -> String
def v4.show source · line 325 · raw
@+w:U32 -> String
def v6.show.mapped source · line 329 · raw
@mapped:Bool -> @+a:U32 -> @+b:U32 -> @+c:U32 -> @+d:U32 -> String
def show source · line 337 · raw
@addr:Addr -> String
RFC 5952: lowercase, shortest hex groups, longest leftmost "::" run.
def eq source · line 344 · raw
@x:Addr -> @y:Addr -> Bool
def prefix.digits source · line 356 · raw
@s:String -> Bool
def prefix.limit source · line 363 · raw
@addr:Addr -> @+bits:U32 -> Maybe<&2, Prefix>
def prefix.valid source · line 370 · raw
@addr:Addr -> @bits:U32 -> @valid:Bool -> Maybe<&2, Prefix>
def prefix.number source · line 377 · raw
@addr:Addr -> @+text:String -> @parsed:Maybe<&2, U32> -> Maybe<&2, Prefix>
def prefix.addr source · line 384 · raw
@text:String -> @+suffix:String -> @parsed:Maybe<&2, Addr> -> Maybe<&2, Prefix>
def prefix.parts source · line 391 · raw
@parts:List<&2, String> -> Maybe<&2, Prefix>
def prefix.parse source · line 398 · raw
@s:String -> Maybe<&2, Prefix>
def prefix.show source · line 401 · raw
@p:Prefix -> String
def mask source · line 405 · raw
@bits:U32 -> U32
def leading source · line 408 · raw
@+bits:U32 -> @+offset:U32 -> U32
def word.contains source · line 411 · raw
@+a:U32 -> @+b:U32 -> @bits:U32 -> Bool
def contains source · line 416 · raw
@p:Prefix -> @addr:Addr -> Bool
Address and prefix families must match. The supplied prefix address keeps its host bits.