summaryrefslogtreecommitdiff
path: root/lib/std/netaddr.myr
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-06-07 09:54:09 -0700
committerOri Bernstein <ori@eigenstate.org>2017-06-07 09:54:09 -0700
commit5518b9657c07d4ee93ac1db4c97b6d4ec164d42b (patch)
treefbd276632a4e0a231694f3601dde3e515a0d8388 /lib/std/netaddr.myr
parent2698fe288642465ec636dca1c8f4ff88a22c5afe (diff)
downloadmc-5518b9657c07d4ee93ac1db4c97b6d4ec164d42b.tar.gz
Add address builder function.
Nice for building addresses with defaults.
Diffstat (limited to 'lib/std/netaddr.myr')
-rw-r--r--lib/std/netaddr.myr38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/std/netaddr.myr b/lib/std/netaddr.myr
new file mode 100644
index 0000000..432dce9
--- /dev/null
+++ b/lib/std/netaddr.myr
@@ -0,0 +1,38 @@
+use "alloc"
+use "die"
+use "memops"
+use "endian"
+use "fmt"
+use "option"
+use "result"
+use "sldup"
+use "strfind"
+use "utf"
+
+pkg std =
+ const netaddr : (addr : byte[:], net : byte[:], port : byte[:] -> byte[:])
+;;
+
+const netaddr = {addr, net, port
+ match strfind(addr, "!")
+ | `Some idx:
+ match strfind(addr[idx+1:], "!")
+ | `Some _: -> sldup(addr)
+ | `None: /* fall through */
+ ;;
+ if port.len > 0
+ -> fmt("{}!{}", addr, port)
+ elif net.len > 0
+ -> fmt("{}!{}", net, addr)
+ else
+ -> sldup(addr)
+ ;;
+ | `None:
+ match (net.len, port.len)
+ | (0, 0): -> sldup(addr)
+ | (0, _): -> fmt("net!{}!{}", addr, port)
+ | (_, 0): -> fmt("{}!{}", net, addr)
+ | (_, _): -> fmt("{}!{}!{}", net, addr, port)
+ ;;
+ ;;
+}