aboutsummaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
Diffstat (limited to 'src/base')
-rw-r--r--src/base/arg.c71
-rw-r--r--src/base/bufio/dump.c66
-rw-r--r--src/base/bufio/get.c17
-rw-r--r--src/base/bufio/internal.h4
-rw-r--r--src/base/bufio/read.c36
-rw-r--r--src/base/bufio/reader.c28
-rw-r--r--src/base/bufio/refill.h28
-rw-r--r--src/base/bufio/rules.mk5
-rw-r--r--src/base/bufio/unget.c18
-rw-r--r--src/base/coro/coro.c43
-rw-r--r--src/base/coro/internal.h15
-rw-r--r--src/base/coro/rules.mk3
-rw-r--r--src/base/coro/unix_x64.s113
-rw-r--r--src/base/error/errorf.c13
-rw-r--r--src/base/error/exits.c11
-rw-r--r--src/base/error/internal.h3
-rw-r--r--src/base/error/panicf.c16
-rw-r--r--src/base/error/rules.mk6
-rw-r--r--src/base/error/verrorf.c9
-rw-r--r--src/base/error/vpanicf.c11
-rw-r--r--src/base/flate/internal.h39
-rw-r--r--src/base/flate/read.c41
-rw-r--r--src/base/flate/reader.c59
-rw-r--r--src/base/flate/rules.mk6
-rw-r--r--src/base/flate/write.c48
-rw-r--r--src/base/flate/writer.c57
-rw-r--r--src/base/fs/internal.h18
-rw-r--r--src/base/fs/rules.mk3
-rw-r--r--src/base/fs/walk.c119
-rw-r--r--src/base/fs/walker.c39
-rw-r--r--src/base/gz/flush.c7
-rw-r--r--src/base/gz/get.c17
-rw-r--r--src/base/gz/interface.c12
-rw-r--r--src/base/gz/internal.h6
-rw-r--r--src/base/gz/open.c13
-rw-r--r--src/base/gz/printf.c15
-rw-r--r--src/base/gz/put.c7
-rw-r--r--src/base/gz/putstring.c8
-rw-r--r--src/base/gz/read.c16
-rw-r--r--src/base/gz/rules.mk11
-rw-r--r--src/base/gz/seek.c7
-rw-r--r--src/base/gz/write.c7
-rw-r--r--src/base/io/fd.c7
-rw-r--r--src/base/io/flush.c7
-rw-r--r--src/base/io/get.c7
-rw-r--r--src/base/io/interface.c70
-rw-r--r--src/base/io/internal.h4
-rw-r--r--src/base/io/open.c13
-rw-r--r--src/base/io/putbyte.c7
-rw-r--r--src/base/io/putstring.c7
-rw-r--r--src/base/io/read.c7
-rw-r--r--src/base/io/readln.c12
-rw-r--r--src/base/io/rules.mk14
-rw-r--r--src/base/io/seek.c7
-rw-r--r--src/base/io/stat.c7
-rw-r--r--src/base/io/tell.c7
-rw-r--r--src/base/io/unget.c7
-rw-r--r--src/base/io/write.c7
-rw-r--r--src/base/mem/arena.c119
-rw-r--r--src/base/mem/buffer.c45
-rw-r--r--src/base/mem/interface.c36
-rw-r--r--src/base/mem/internal.h4
-rw-r--r--src/base/mem/rules.mk5
-rw-r--r--src/base/mem/set64.c13
-rw-r--r--src/base/mmap/internal.h5
-rw-r--r--src/base/mmap/mmap.c39
-rw-r--r--src/base/mmap/rules.mk2
-rw-r--r--src/base/os/basename.c10
-rw-r--r--src/base/os/exists.c7
-rw-r--r--src/base/os/internal.h4
-rw-r--r--src/base/os/rules.mk4
-rw-r--r--src/base/os/sep.c14
-rw-r--r--src/base/rng/base.c24
-rw-r--r--src/base/rng/bernoulli.c7
-rw-r--r--src/base/rng/exponential.c11
-rw-r--r--src/base/rng/internal.h19
-rw-r--r--src/base/rng/normal.c77
-rw-r--r--src/base/rng/poisson.c126
-rw-r--r--src/base/rng/random.c33
-rw-r--r--src/base/rng/rules.mk7
-rw-r--r--src/base/rules.mk37
-rw-r--r--src/base/sort/double.c12
-rw-r--r--src/base/sort/float.c12
-rw-r--r--src/base/sort/int.c12
-rw-r--r--src/base/sort/int16.c12
-rw-r--r--src/base/sort/int32.c12
-rw-r--r--src/base/sort/int64.c12
-rw-r--r--src/base/sort/int8.c12
-rw-r--r--src/base/sort/internal.h5
-rw-r--r--src/base/sort/rules.mk14
-rw-r--r--src/base/sort/string.c12
-rw-r--r--src/base/sort/uint.c12
-rw-r--r--src/base/sort/uint16.c12
-rw-r--r--src/base/sort/uint32.c12
-rw-r--r--src/base/sort/uint64.c12
-rw-r--r--src/base/sort/uint8.c12
-rw-r--r--src/base/string/append.c53
-rw-r--r--src/base/string/appendf.c31
-rw-r--r--src/base/string/clear.c9
-rw-r--r--src/base/string/copyn.c11
-rw-r--r--src/base/string/equals.c12
-rw-r--r--src/base/string/find.c11
-rw-r--r--src/base/string/fit.c20
-rw-r--r--src/base/string/free.c8
-rw-r--r--src/base/string/grow.c33
-rw-r--r--src/base/string/internal.h12
-rw-r--r--src/base/string/join.c16
-rw-r--r--src/base/string/len.c17
-rw-r--r--src/base/string/lower.c12
-rw-r--r--src/base/string/make.c53
-rw-r--r--src/base/string/makef.c25
-rw-r--r--src/base/string/read.c12
-rw-r--r--src/base/string/replace.c26
-rw-r--r--src/base/string/rules.mk19
-rw-r--r--src/base/string/split.c39
-rw-r--r--src/base/string/upper.c12
-rw-r--r--src/base/test.c170
117 files changed, 2685 insertions, 0 deletions
diff --git a/src/base/arg.c b/src/base/arg.c
new file mode 100644
index 0000000..269043e
--- /dev/null
+++ b/src/base/arg.c
@@ -0,0 +1,71 @@
+#include <u.h>
+#include <base.h>
+
+// NOTE: this utf8 bit is copied from libunicode to remove the hard dependency just for ARG_BEGIN.
+
+#define UTFmax 4
+#define RuneSync 0x80u
+#define RuneSelf 0x80u
+#define RuneErr 0xFFFDu
+#define RuneMax 0x10FFFFu
+#define RuneMask 0x1FFFFFu
+
+#define Bit(i) (7-(i))
+/* N 0's preceded by i 1's e.g. T(Bit(2)) is 1100 0000 */
+#define Tbyte(i) (((1 << (Bit(i)+1))-1) ^ 0xFF)
+/* 0000 0000 0000 0111 1111 1111 */
+#define RuneX(i) ((1 << (Bit(i) + ((i)-1)*Bitx))-1)
+enum
+{
+ Bitx = Bit(1),
+ Tx = Tbyte(1),
+ Rune1 = (1 << (Bit(0)+0*Bitx)) - 1,
+
+ Maskx = (1 << Bitx) - 1, /* 0011 1111 */
+ Testx = Maskx ^ 0xff, /* 1100 0000 */
+
+ SurrogateMin = 0xD800,
+ SurrogateMax = 0xDFFF,
+ Bad = RuneErr,
+};
+
+
+int
+arg·bytetorune(uint32* r, byte* s)
+{
+ int c[4], i;
+ uint32 l;
+
+ c[0] = *(ubyte*)(s);
+ if(c[0] < Tx) {
+ *r = c[0];
+ return 1;
+ }
+
+ l = c[0];
+ for(i = 1; i < UTFmax; i++) {
+ c[i] = *(ubyte*)(s+i);
+ c[i] ^= Tx;
+ if (c[i] & Testx) goto bad;
+
+ l = (l << Bitx) | c[i];
+ if(c[0] < Tbyte(i + 2)) {
+ l &= RuneX(i + 1);
+ if (i == 1) {
+ if (c[0] < Tbyte(2) || l <= Rune1)
+ goto bad;
+ } else if (l <= RuneX(i) || l > RuneMax)
+ goto bad;
+ if (i == 2 && SurrogateMin <= l && l <= SurrogateMax)
+ goto bad;
+
+ *r = l;
+ return i + 1;
+ }
+ }
+bad:
+ *r = RuneErr;
+ return 1;
+}
+
+char *argv0;
diff --git a/src/base/bufio/dump.c b/src/base/bufio/dump.c
new file mode 100644
index 0000000..0b527e2
--- /dev/null
+++ b/src/base/bufio/dump.c
@@ -0,0 +1,66 @@
+// -----------------------------------------------------------------------
+// reader
+
+#if 0
+rune
+bufio·getrune(io·Buffer *buf)
+{
+ ubyte b;
+ int i;
+ byte str[UTFmax+1];
+ rune r;
+
+ // NOTE: I'm worried about the sign here...
+ b = bufio·getbyte(buf);
+ if (b < RuneSelf) {
+ buf->runesize = 1;
+ return b;
+ }
+
+ i = 0;
+ str[i++] = b;
+
+nextbyte:
+ b = bufio·getbyte(buf);
+ if (b < 0) return b;
+ if (i >= arrlen(str)) return RuneErr;
+ str[i++] = b;
+ if (!utf8·fullrune(str, i))
+ goto nextbyte;
+
+ buf->runesize = utf8·bytetorune(&r, str);
+ if (r == RuneErr && b == 1) {
+ errorf("illegal UTF-8 sequence");
+ for (; i >= 0; i--)
+ errorf("%s%.2x", i > 0 ? " " : "", *(ubyte*)(str+i));
+ errorf("\n");
+
+ buf->runesize = 0;
+ } else
+ for (; i > buf->runesize; i--)
+ bufio·ungetbyte(buf, str[i]);
+
+ return r;
+}
+
+// TODO: Check that we are given the correct rune!
+error
+bufio·ungetrune(io·Buffer *buf, rune r)
+{
+ if (buf->state & bufio·rdr) {
+ errorf("attempted to unget on non-active reader");
+ return bufio·err;
+ }
+
+ if (buf->pos == buf->buf) {
+ errorf("attempted to unget past end of buffer");
+ return bufio·err;
+ }
+
+ buf->pos -= buf->runesize;
+ return 0;
+}
+#endif
+
+// -----------------------------------------------------------------------
+// writer
diff --git a/src/base/bufio/get.c b/src/base/bufio/get.c
new file mode 100644
index 0000000..9f10c88
--- /dev/null
+++ b/src/base/bufio/get.c
@@ -0,0 +1,17 @@
+#include "internal.h"
+#include "refill.h"
+
+int
+bufio·getbyte(io·Buffer *buf)
+{
+getbyte:
+ if(buf->pos < buf->end)
+ return *buf->pos++;
+
+ memmove(buf->buf, buf->end - bufio·ungets, bufio·ungets);
+
+ if(refill(buf) <= 0)
+ return bufio·eof;
+
+ goto getbyte;
+}
diff --git a/src/base/bufio/internal.h b/src/base/bufio/internal.h
new file mode 100644
index 0000000..302c035
--- /dev/null
+++ b/src/base/bufio/internal.h
@@ -0,0 +1,4 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
diff --git a/src/base/bufio/read.c b/src/base/bufio/read.c
new file mode 100644
index 0000000..09a9f83
--- /dev/null
+++ b/src/base/bufio/read.c
@@ -0,0 +1,36 @@
+#include "internal.h"
+#include "refill.h"
+
+int
+bufio·read(io·Buffer *buf, int sz, int n, void *out)
+{
+ byte *wtr;
+ int nr, rem, diff;
+
+ if(n == 0 || buf->state & bufio·end)
+ return bufio·err;
+
+ assert(buf->state & bufio·rdr);
+
+ wtr = out;
+ rem = n*sz;
+
+ while(rem > 0){
+ diff = buf->end - buf->pos;
+ nr = MIN(diff, rem);
+ if(!nr){
+ if(buf->state & bufio·end)
+ break;
+ if(refill(buf) <= 0)
+ break;
+
+ continue;
+ }
+ memmove(wtr, buf->pos, nr);
+ wtr += nr;
+ buf->pos += nr;
+ rem -= nr;
+ }
+
+ return n - rem/sz;
+}
diff --git a/src/base/bufio/reader.c b/src/base/bufio/reader.c
new file mode 100644
index 0000000..afdaf60
--- /dev/null
+++ b/src/base/bufio/reader.c
@@ -0,0 +1,28 @@
+#include "internal.h"
+
+error
+bufio·initreader(io·Buffer *buf, io·Reader rdr, void *h)
+{
+ if (buf->state) {
+ errorf("attemped to initialize an active buffer, state is '%d'", buf->state);
+ return bufio·err;
+ }
+ buf->state = bufio·rdr;
+ buf->runesize = 0;
+ buf->h = h;
+ buf->rdr = rdr;
+ buf->beg = buf->buf + bufio·ungets;
+ buf->pos = buf->beg;
+ buf->end = buf->pos;
+ buf->size = bufio·size - bufio·ungets;
+
+ return 0;
+}
+
+void
+bufio·finireader(io·Buffer *buf)
+{
+ buf->state = bufio·nil;
+ buf->runesize = 0;
+ buf->rdr = (io·Reader){ .read = nil };
+}
diff --git a/src/base/bufio/refill.h b/src/base/bufio/refill.h
new file mode 100644
index 0000000..41e357e
--- /dev/null
+++ b/src/base/bufio/refill.h
@@ -0,0 +1,28 @@
+int
+refill(io·Buffer *buf)
+{
+ int n;
+
+ if(buf->state & bufio·end)
+ return bufio·err;
+
+ memcpy(buf->buf, buf->pos - bufio·ungets, bufio·ungets);
+
+ n = buf->rdr.read(buf->h, 1, buf->size, buf->beg);
+ if(n < 0)
+ return bufio·err;
+ if(n == 0){
+ buf->state |= bufio·end;
+ return 0;
+ }
+
+ buf->pos = buf->beg;
+ buf->end = buf->pos + n;
+
+ // TEST: put a physical EOF byte at the end
+ // this would allow for an unget operation
+ if(n < buf->size)
+ *buf->end++ = EOF;
+
+ return n;
+}
diff --git a/src/base/bufio/rules.mk b/src/base/bufio/rules.mk
new file mode 100644
index 0000000..84f283f
--- /dev/null
+++ b/src/base/bufio/rules.mk
@@ -0,0 +1,5 @@
+SRCS_$(d)+=\
+ $(d)/bufio/get.c\
+ $(d)/bufio/read.c\
+ $(d)/bufio/reader.c\
+ $(d)/bufio/unget.c\
diff --git a/src/base/bufio/unget.c b/src/base/bufio/unget.c
new file mode 100644
index 0000000..3fd16de
--- /dev/null
+++ b/src/base/bufio/unget.c
@@ -0,0 +1,18 @@
+#include "internal.h"
+
+error
+bufio·ungetbyte(io·Buffer *buf, byte c)
+{
+ if(!(buf->state & bufio·rdr)) {
+ errorf("attempted to unget on non-active reader");
+ return bufio·err;
+ }
+
+ if(buf->pos == buf->buf) {
+ errorf("attempted to unget past end of buffer");
+ return bufio·err;
+ }
+
+ buf->pos--;
+ return 0;
+}
diff --git a/src/base/coro/coro.c b/src/base/coro/coro.c
new file mode 100644
index 0000000..2255c99
--- /dev/null
+++ b/src/base/coro/coro.c
@@ -0,0 +1,43 @@
+#include "internal.h"
+
+/* Co-routine context */
+Coro*
+coro·make(uintptr stk, uintptr (*func)(Coro*, uintptr))
+{
+ if (!func) return nil;
+ if (stk == 0) stk = 8192;
+
+ byte *block = malloc(stk);
+ Coro *co = (Coro*)&block[stk - sizeof(Coro)];
+ co->bp = block;
+ co->size = stk;
+
+ _newcoro(co, func, co);
+ return co;
+}
+
+error
+coro·free(Coro *co)
+{
+ enum
+ {
+ NIL,
+ GOOD,
+ EMPTY,
+ LOST,
+ };
+
+ if (!co) return NIL;
+ if (!co->bp) return LOST;
+ if (co->size == 0) return EMPTY;
+
+ free(co->bp);
+
+ return GOOD;
+}
+
+uintptr
+coro·yield(Coro *c, uintptr arg)
+{
+ return _coroyield(c, arg);
+}
diff --git a/src/base/coro/internal.h b/src/base/coro/internal.h
new file mode 100644
index 0000000..f57d27b
--- /dev/null
+++ b/src/base/coro/internal.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
+
+extern void _newcoro(Coro *co, uintptr (*func)(Coro*, uintptr), void *stk);
+extern uintptr _coroyield(Coro *co, uintptr arg);
+
+struct Coro
+{
+ void *sp;
+ void *bp;
+ uintptr size;
+ void *user;
+};
diff --git a/src/base/coro/rules.mk b/src/base/coro/rules.mk
new file mode 100644
index 0000000..c2ee89f
--- /dev/null
+++ b/src/base/coro/rules.mk
@@ -0,0 +1,3 @@
+SRCS_$(d)+=\
+ $(d)/coro/coro.c\
+ $(d)/coro/unix_x64.s\
diff --git a/src/base/coro/unix_x64.s b/src/base/coro/unix_x64.s
new file mode 100644
index 0000000..d7de2a2
--- /dev/null
+++ b/src/base/coro/unix_x64.s
@@ -0,0 +1,113 @@
+; Nicholas Noll 2019
+;
+; ===================================================================
+%use altreg
+
+ bits 64
+ default rel
+ global _newcoro
+ global _coroyield
+
+; ===================================================================
+ section .text
+; -------------------------------------------------------------------
+
+%assign L.coro -8
+%assign L.func -16
+
+coroinit:
+ mov R7, [RBP + L.coro]
+ mov R6, R0
+ call [RBP + L.func]
+
+rerun:
+ mov R7, [RBP + L.coro]
+ mov R6, R0
+ call _coroyield
+ jmp rerun
+
+; -------------------------------------------------------------------
+; # Register Mapping
+;
+; R0 R1 R2 R3 R4 R5 R6 R7 R8 ...
+; RAX RCX RDX RBX RSP RBP RSI RDI R8 ...
+;
+; # Sys V calling convention
+; func(R7, R6, R2, R1, R8, R9, Z0-7): R0
+;
+; # Stack layout of an in-flight coro
+; *coro
+; *func
+; *bp (base pointer of stack)
+; ....... STACK .........
+; Saved Clobbers
+;
+; ###
+; Stack layout of an init coro
+; Stores the func pointer to init
+; Stores the clobber registers.
+;
+; L.coro [8]
+; L.func [7]
+; coroinit [6]
+; RBP [5]
+; R3 [4]
+; R12 [3]
+; R13 [2]
+; R14 [1]
+; R15 [0]
+
+%define WORDSZ 8
+%define NSAVES 9
+
+; coro *coro·new(co *coro, fn func, bp *stack)
+_newcoro:
+ lea R0, [coroinit] ; Store address of init function
+ lea R1, [R2 - NSAVES*WORDSZ] ; Store offset address of stack
+
+ mov [R1 + 8*WORDSZ], R7 ; Store context pointer
+ mov [R1 + 7*WORDSZ], R6 ; Store function pointer
+ mov [R1 + 6*WORDSZ], R0 ; Store initializer pointer
+ mov [R1 + 5*WORDSZ], R2 ; Store stack base pointer
+
+ xor R0, R0
+
+ ; Start of mutable stack
+ ; Blank out the clobbers
+ mov [R1 + 4*WORDSZ], R0 ; R3
+ mov [R1 + 3*WORDSZ], R0 ; R12
+ mov [R1 + 2*WORDSZ], R0 ; R13
+ mov [R1 + 1*WORDSZ], R0 ; R14
+ mov [R1 + 0*WORDSZ], R0 ; R15
+
+ mov [R7], R1
+ ret
+
+; Saves register state
+%macro pushclobs 0
+ push RBP
+ push R3
+ push R12
+ push R13
+ push R14
+ push R15
+%endmacro
+
+; Restores register state
+%macro popclobs 0
+ pop R15
+ pop R14
+ pop R13
+ pop R12
+ pop R3
+ pop RBP
+%endmacro
+
+; uintptr coro.yield(co *coro, data uintptr)
+_coroyield:
+ pushclobs
+ mov R0, R6 ; Move return value into return register.
+ xchg RSP, [R7] ; Atomically swap the stack pointer with the yieldee.
+ popclobs
+
+ ret
diff --git a/src/base/error/errorf.c b/src/base/error/errorf.c
new file mode 100644
index 0000000..193dd9d
--- /dev/null
+++ b/src/base/error/errorf.c
@@ -0,0 +1,13 @@
+#include "internal.h"
+
+void
+errorf(byte* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+
+ fprintf(stderr, "error: ");
+ vfprintf(stderr, fmt, args);
+
+ va_end(args);
+}
diff --git a/src/base/error/exits.c b/src/base/error/exits.c
new file mode 100644
index 0000000..6be7d3b
--- /dev/null
+++ b/src/base/error/exits.c
@@ -0,0 +1,11 @@
+#include "internal.h"
+
+void
+exits(char *s)
+{
+ if(s == nil || *s == 0)
+ exit(0);
+
+ fputs(s, stderr);
+ exit(1);
+}
diff --git a/src/base/error/internal.h b/src/base/error/internal.h
new file mode 100644
index 0000000..88a8895
--- /dev/null
+++ b/src/base/error/internal.h
@@ -0,0 +1,3 @@
+#include <u.h>
+#include <base.h>
+
diff --git a/src/base/error/panicf.c b/src/base/error/panicf.c
new file mode 100644
index 0000000..d698576
--- /dev/null
+++ b/src/base/error/panicf.c
@@ -0,0 +1,16 @@
+#include "internal.h"
+
+void
+panicf(byte* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+
+ printf("panic: ");
+ vprintf(fmt, args);
+ printf("\n");
+
+ va_end(args);
+
+ exit(1);
+}
diff --git a/src/base/error/rules.mk b/src/base/error/rules.mk
new file mode 100644
index 0000000..e3a9ce0
--- /dev/null
+++ b/src/base/error/rules.mk
@@ -0,0 +1,6 @@
+SRCS_$(d)+=\
+ $(d)/error/exits.c \
+ $(d)/error/errorf.c \
+ $(d)/error/panicf.c \
+ $(d)/error/verrorf.c \
+ $(d)/error/vpanicf.c \
diff --git a/src/base/error/verrorf.c b/src/base/error/verrorf.c
new file mode 100644
index 0000000..15af064
--- /dev/null
+++ b/src/base/error/verrorf.c
@@ -0,0 +1,9 @@
+#include "internal.h"
+
+void
+verrorf(byte* fmt, va_list args)
+{
+ printf("error: ");
+ vprintf(fmt, args);
+ printf("\n");
+}
diff --git a/src/base/error/vpanicf.c b/src/base/error/vpanicf.c
new file mode 100644
index 0000000..bea97ac
--- /dev/null
+++ b/src/base/error/vpanicf.c
@@ -0,0 +1,11 @@
+#include "internal.h"
+
+void
+vpanicf(byte* fmt, va_list args)
+{
+ printf("panic: ");
+ vprintf(fmt, args);
+ printf("\n");
+
+ exit(1);
+}
diff --git a/src/base/flate/internal.h b/src/base/flate/internal.h
new file mode 100644
index 0000000..794c7c2
--- /dev/null
+++ b/src/base/flate/internal.h
@@ -0,0 +1,39 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
+
+#include <zlib.h>
+
+typedef struct buffer
+{
+ union {
+ struct z_stream_s;
+ z_stream z;
+ };
+
+ ubyte buf[4098];
+} buffer;
+
+typedef struct flate·Reader
+{
+ io·Reader rdr;
+ void* impl;
+
+ union {
+ struct buffer;
+ buffer b;
+ };
+} flate·Reader;
+
+typedef struct flate·Writer
+{
+ io·Writer wtr;
+ void* impl;
+
+ union {
+ struct buffer;
+ buffer b;
+ };
+} flate·Writer;
+
diff --git a/src/base/flate/read.c b/src/base/flate/read.c
new file mode 100644
index 0000000..9a42070
--- /dev/null
+++ b/src/base/flate/read.c
@@ -0,0 +1,41 @@
+#include "internal.h"
+
+int
+flate·read(flate·Reader *rdr, int sz, int n, void *buf)
+{
+ int r;
+ int err;
+ flate·Reader zrdr;
+
+ zrdr = *rdr;
+ zrdr.next_out = buf;
+ zrdr.avail_out = n*sz;
+
+READ:
+ err = inflate(&zrdr.b.z, Z_STREAM_END);
+ switch (err) {
+ case Z_OK:
+ return n;
+
+ case Z_STREAM_END:
+ r = zrdr.next_out - (ubyte*)buf;
+ n -= r;
+ zrdr.avail_in = zrdr.rdr.read(zrdr.impl, 1, arrlen(zrdr.buf), zrdr.buf);
+ if (!zrdr.avail_in) {
+ return r;
+ }
+ zrdr.next_in = zrdr.buf;
+ goto READ;
+
+ case Z_NEED_DICT:
+ errorf("zlib: need input dictionary");
+ goto ERROR;
+
+ case Z_STREAM_ERROR:
+ errorf("zlib: inconsistent stream structure");
+ goto ERROR;
+ }
+ERROR:
+ flate·closereader(rdr);
+ return -1;
+}
diff --git a/src/base/flate/reader.c b/src/base/flate/reader.c
new file mode 100644
index 0000000..84f0d80
--- /dev/null
+++ b/src/base/flate/reader.c
@@ -0,0 +1,59 @@
+#include "internal.h"
+
+flate·Reader*
+flate·openreader(io·Reader rdr, void* r, mem·Allocator mem, void* m)
+{
+ error err;
+ flate·Reader *zrdr;
+
+ zrdr = mem.alloc(m, 1, sizeof(*zrdr));
+
+ zrdr->zalloc = (void *(*)(void *, unsigned int, unsigned int))mem.alloc;
+ zrdr->zfree = mem.free;
+ zrdr->opaque = m;
+ zrdr->avail_in = rdr.read(r, 1, arrlen(zrdr->buf), zrdr->buf);
+ zrdr->next_in = zrdr->buf;
+
+ err = inflateInit(&zrdr->b.z);
+
+ switch (err) {
+ case Z_OK:
+ return zrdr;
+
+ case Z_MEM_ERROR:
+ errorf("zlib: not enough memory");
+ goto ERROR;
+
+ case Z_VERSION_ERROR:
+ errorf("zlib: incompatible version");
+ goto ERROR;
+
+ case Z_STREAM_ERROR:
+ errorf("zlib: incorrect input parameters");
+ goto ERROR;
+
+ default:
+ errorf("zlib: unrecognized error code");
+ }
+ERROR:
+ errorf("zlib: msg: %s", zrdr->msg);
+ mem.free(m, zrdr);
+ return nil;
+}
+
+error
+flate·closereader(flate·Reader *rdr)
+{
+ int err;
+ flate·Reader zrdr;
+
+ zrdr = *rdr;
+ err = inflateEnd(&zrdr.b.z);
+ if (err != Z_OK) {
+ errorf("zlib: failed to cleanup");
+ return err;
+ }
+ rdr->zfree(rdr->opaque, rdr);
+
+ return 0;
+}
diff --git a/src/base/flate/rules.mk b/src/base/flate/rules.mk
new file mode 100644
index 0000000..54d8c14
--- /dev/null
+++ b/src/base/flate/rules.mk
@@ -0,0 +1,6 @@
+SRCS_$(d)+=\
+ $(d)/flate/read.c\
+ $(d)/flate/reader.c\
+ $(d)/flate/write.c\
+ $(d)/flate/writer.c\
+ $(d)/flate/writer.c\
diff --git a/src/base/flate/write.c b/src/base/flate/write.c
new file mode 100644
index 0000000..3f07b94
--- /dev/null
+++ b/src/base/flate/write.c
@@ -0,0 +1,48 @@
+#include "internal.h"
+
+int
+flate·write(flate·Writer *wtr, int sz, int n, void *buf)
+{
+ int r;
+ int err;
+ flate·Writer zwtr;
+
+ zwtr = *wtr;
+ zwtr.next_out = buf;
+DEFLATE:
+ zwtr.avail_out = n*sz;
+ err = deflate(&zwtr.z, Z_NO_FLUSH);
+
+ switch (err) {
+ case Z_STREAM_END:
+ return n;
+
+ case Z_OK:
+ r = (zwtr.next_out - (ubyte*)buf)/sz;
+ n -= r;
+ if (!n) {
+ return r;
+ }
+ buf += n;
+ goto DEFLATE;
+
+ case Z_STREAM_ERROR:
+ errorf("zlib: bad input");
+ goto ERROR;
+
+ case Z_BUF_ERROR:
+ if (!zwtr.avail_in) {
+ zwtr.avail_in += zwtr.wtr.write(zwtr.impl, 1, arrlen(zwtr.buf), buf);
+ if (!zwtr.avail_in) {
+ errorf("reader: failed read");
+ goto ERROR;
+ }
+ goto DEFLATE;
+ }
+ }
+
+ return 0;
+ERROR:
+ errorf("zlib: %s", zwtr.msg);
+ return -1;
+}
diff --git a/src/base/flate/writer.c b/src/base/flate/writer.c
new file mode 100644
index 0000000..f339ae0
--- /dev/null
+++ b/src/base/flate/writer.c
@@ -0,0 +1,57 @@
+#include "internal.h"
+
+flate·Writer*
+flate·openwriter(io·Writer wtr, void* w, mem·Allocator mem, void* m)
+{
+ error err;
+ flate·Writer *zwtr;
+
+ zwtr = mem.alloc(m, 1, sizeof(*zwtr));
+ zwtr->zalloc = (void *(*)(void *, unsigned int, unsigned int))mem.alloc;
+ zwtr->zfree = mem.free;
+ zwtr->opaque = m;
+ zwtr->avail_in = 0;
+
+ err = deflateInit(&zwtr->b.z, Z_DEFAULT_COMPRESSION);
+
+ switch (err) {
+ case Z_OK:
+ return zwtr;
+
+ case Z_MEM_ERROR:
+ errorf("zlib: not enough memory");
+ goto ERROR;
+
+ case Z_VERSION_ERROR:
+ errorf("zlib: incompatible version");
+ goto ERROR;
+
+ case Z_STREAM_ERROR:
+ errorf("zlib: incorrect compression level");
+ goto ERROR;
+
+ default:
+ errorf("zlib: unrecognized error code");
+ }
+ERROR:
+ errorf("zlib: msg: %s", zwtr->msg);
+ mem.free(m, zwtr);
+ return nil;
+}
+
+error
+flate·closewriter(flate·Writer *wtr)
+{
+ int err;
+ flate·Writer zwtr;
+
+ zwtr = *wtr;
+ err = deflateEnd(&zwtr.b.z);
+ if (err != Z_OK) {
+ errorf("zlib: failed to cleanup");
+ return err;
+ }
+ zwtr.zfree(zwtr.opaque, wtr);
+
+ return 0;
+}
diff --git a/src/base/fs/internal.h b/src/base/fs/internal.h
new file mode 100644
index 0000000..7fde093
--- /dev/null
+++ b/src/base/fs/internal.h
@@ -0,0 +1,18 @@
+#include <u.h>
+#include <base.h>
+#include <base/macro/map.h>
+#include <dirent.h>
+
+/*
+ * path history
+ */
+struct Key
+{
+ ino_t ino;
+ dev_t dev;
+};
+
+struct fs·History
+{
+ SET_STRUCT_BODY(struct Key);
+};
diff --git a/src/base/fs/rules.mk b/src/base/fs/rules.mk
new file mode 100644
index 0000000..3927ae3
--- /dev/null
+++ b/src/base/fs/rules.mk
@@ -0,0 +1,3 @@
+SRCS_$(d)+=\
+ $(d)/fs/walk.c\
+ $(d)/fs/walker.c\
diff --git a/src/base/fs/walk.c b/src/base/fs/walk.c
new file mode 100644
index 0000000..d528896
--- /dev/null
+++ b/src/base/fs/walk.c
@@ -0,0 +1,119 @@
+#include "internal.h"
+
+#define hash(k) ((int32)k.ino ^ (int32)k.dev)
+#define equal(k1, k2) (k1.ino == k2.ino && k1.dev == k2.dev)
+
+static
+int
+morehistory(fs·History *h, int n)
+{
+ SET_GROW(h, struct Key, n, hash, sys·Memory, nil);
+}
+
+static
+int
+addentry(fs·History *h, struct Key key, int *err)
+{
+ SET_PUT(h, key, hash, equal, morehistory, err);
+}
+
+static
+void
+forget(fs·History *h)
+{
+ if (!h)
+ return;
+
+ SET_RESET(h);
+}
+
+void
+fs·walk(fs·Walker *fs)
+{
+ char *e, *b;
+ DIR *dir;
+ int new, fd, ofd, flags;
+ fs·History *h;
+ struct dirent *d;
+ io·Stat cwd;
+ struct fs·Entry *it;
+
+ flags = 0;
+ if(fs->flags & fs·nolinks)
+ flags |= AT_SYMLINK_NOFOLLOW;
+
+ /* get info for base relative to current fd */
+ if(fstatat(fs->fd, fs->base, &cwd, flags) < 0){
+ if(fs->flags & fs·verbose)
+ errorf("stat: %s", fs->path);
+ return;
+ }
+
+ /* if we hit a file, finish! */
+ if(!S_ISDIR(cwd.st_mode)) {
+ fs->func(fs->data, fs->base, fs->path, &cwd);
+ return;
+ }
+
+ /* have we been here before? (cycle detection) */
+ /* if not, add to our path history */
+ if (!(fs->flags & fs·nolinks)) {
+ addentry(fs->hist, (struct Key){.dev=cwd.st_dev, .ino=cwd.st_ino}, &new);
+ if (!new)
+ return;
+ }
+
+ /*
+ * operate on directory first if preorder traversal
+ * truncate recursion if callback returns an error code
+ */
+ if (fs->flags & fs·preorder) {
+ if (fs->func(fs->data, fs->base, fs->path, &cwd))
+ return;
+ }
+
+ /* open directory */
+ if(!fs->max || fs->lev + 1 < fs->max) {
+ fd = openat(fs->fd, fs->base, O_RDONLY | O_CLOEXEC | O_DIRECTORY);
+ if (fd < 0)
+ errorf("open %s:", fs->path);
+
+ if (!(dir=fdopendir(fd))) {
+ if(fs->flags & fs·verbose)
+ errorf("fdopendir: %s", fs->path);
+ return;
+ }
+
+ ofd = fs->fd, fs->fd = fd;
+
+ /* traverse children */
+ e = fs->end, b = fs->base;
+ if (fs->end[-1] != '/')
+ *fs->end++ = '/';
+
+ fs->base = fs->end;
+ while((d = readdir(dir))) {
+ if(*d->d_name == '.')
+ if(d->d_name[1] == 0 || /* . */
+ (d->d_name[1] == '.' && d->d_name[2] == 0)) /* .. */
+ continue;
+
+ fs->end = str·copyn(fs->base, d->d_name, arrend(fs->path) - fs->base);
+
+ fs->lev++;
+ fs·walk(fs);
+ fs->lev--;
+ }
+ *e = 0;
+ fs->fd = ofd;
+ fs->end = e, fs->base = b;
+ closedir(dir);
+ }
+
+ /* operate on directory if postorder (default) traversal */
+ if (!(fs->flags & fs·preorder))
+ fs->func(fs->data, fs->base, fs->path, &cwd);
+
+ if (!fs->lev)
+ forget(fs->hist);
+}
diff --git a/src/base/fs/walker.c b/src/base/fs/walker.c
new file mode 100644
index 0000000..65ff391
--- /dev/null
+++ b/src/base/fs/walker.c
@@ -0,0 +1,39 @@
+#include "internal.h"
+
+static
+void
+delete(fs·History *h)
+{
+ SET_FREE(h, sys·Memory, nil);
+}
+
+int
+fs·init(fs·Walker *fs, char *path)
+{
+ fs->base = fs->end = fs->path;
+
+ if(!path || !path[0]){
+ path = getcwd(fs->path, arrlen(fs->path));
+ if (!path)
+ return 1;
+ fs->end += strlen(path);
+ }else
+ fs->end = str·copyn(fs->base, path, arrlen(fs->path));
+
+ if(fs->path[0] != '/')
+ fs->fd = AT_FDCWD;
+
+ if(!fs->hist && !(fs->flags & fs·nolinks))
+ fs->hist = calloc(1, sizeof(*fs->hist));
+
+ return 0;
+}
+
+void
+fs·fini(fs·Walker *fs)
+{
+ if(fs->hist){
+ delete(fs->hist);
+ free(fs->hist);
+ }
+}
diff --git a/src/base/gz/flush.c b/src/base/gz/flush.c
new file mode 100644
index 0000000..011a3ab
--- /dev/null
+++ b/src/base/gz/flush.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+error
+gz·flush(gz·Stream *s)
+{
+ return gzflush(s, Z_FINISH);
+}
diff --git a/src/base/gz/get.c b/src/base/gz/get.c
new file mode 100644
index 0000000..24ba23a
--- /dev/null
+++ b/src/base/gz/get.c
@@ -0,0 +1,17 @@
+#include "internal.h"
+
+byte
+gz·getbyte(gz·Stream *s)
+{
+ // NOTE: Can't call macro
+ byte b[2];
+ gzread(s, b, 1);
+
+ return b[0];
+}
+
+error
+gz·ungetbyte(gz·Stream *s, byte c)
+{
+ return gzungetc(c, s);
+}
diff --git a/src/base/gz/interface.c b/src/base/gz/interface.c
new file mode 100644
index 0000000..15b8f10
--- /dev/null
+++ b/src/base/gz/interface.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+io·Reader gz·Reader = (io·Reader){ gz·read };
+io·Peeker gz·Peeker = (io·Peeker){ gz·getbyte, gz·ungetbyte };
+io·Seeker gz·Seeker = (io·Seeker){ gz·seek, gz·tell };
+io·PeekReader gz·Peekreader = (io·PeekReader){ gz·read, gz·getbyte, gz·ungetbyte };
+
+io·Writer gz·Writer = (io·Writer){ gz·write };
+io·Putter gz·Putter = (io·Putter){ gz·putbyte, gz·putstring };
+io·PutWriter gz·PutWriter = (io·PutWriter){ gz·write, gz·putbyte, gz·putstring };
+
+io·ReadWriter gz·ReadWriter = (io·ReadWriter){ gz·read, gz·write };
diff --git a/src/base/gz/internal.h b/src/base/gz/internal.h
new file mode 100644
index 0000000..6a268c4
--- /dev/null
+++ b/src/base/gz/internal.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
+
+#include <zlib.h>
diff --git a/src/base/gz/open.c b/src/base/gz/open.c
new file mode 100644
index 0000000..c84ce5e
--- /dev/null
+++ b/src/base/gz/open.c
@@ -0,0 +1,13 @@
+#include "internal.h"
+
+gz·Stream*
+gz·open(byte *path, byte *mode)
+{
+ return gzopen(path, mode);
+}
+
+error
+gz·close(gz·Stream* s)
+{
+ return gzclose(s);
+}
diff --git a/src/base/gz/printf.c b/src/base/gz/printf.c
new file mode 100644
index 0000000..d7f75cf
--- /dev/null
+++ b/src/base/gz/printf.c
@@ -0,0 +1,15 @@
+#include "internal.h"
+
+int
+gz·printf(gz·Stream *s, byte *fmt, ...)
+{
+ error err;
+
+ va_list args;
+ va_start(args, fmt);
+ err = gzprintf(s, fmt, args);
+ va_end(args);
+
+ return err;
+}
+
diff --git a/src/base/gz/put.c b/src/base/gz/put.c
new file mode 100644
index 0000000..fa9807d
--- /dev/null
+++ b/src/base/gz/put.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+error
+gz·putbyte(gz·Stream *s, byte c)
+{
+ return gzputc(s, c);
+}
diff --git a/src/base/gz/putstring.c b/src/base/gz/putstring.c
new file mode 100644
index 0000000..64ff470
--- /dev/null
+++ b/src/base/gz/putstring.c
@@ -0,0 +1,8 @@
+#include "internal.h"
+
+error
+gz·putstring(gz·Stream *s, byte *str)
+{
+ return gzputs(s, str);
+}
+
diff --git a/src/base/gz/read.c b/src/base/gz/read.c
new file mode 100644
index 0000000..112fe4d
--- /dev/null
+++ b/src/base/gz/read.c
@@ -0,0 +1,16 @@
+#include "internal.h"
+
+int
+gz·read(gz·Stream *s, int sz, int n, void* buf)
+{
+ return gzread(s, buf, n*sz);
+}
+
+int
+gz·readln(gz·Stream *s, int n, byte *buf)
+{
+ byte* b;
+ b = gzgets(s, buf, n);
+
+ return strlen(b);
+}
diff --git a/src/base/gz/rules.mk b/src/base/gz/rules.mk
new file mode 100644
index 0000000..a933291
--- /dev/null
+++ b/src/base/gz/rules.mk
@@ -0,0 +1,11 @@
+SRCS_$(d)+=\
+ $(d)/gz/flush.c\
+ $(d)/gz/get.c\
+ $(d)/gz/interface.c\
+ $(d)/gz/open.c\
+ $(d)/gz/printf.c\
+ $(d)/gz/put.c\
+ $(d)/gz/putstring.c\
+ $(d)/gz/read.c\
+ $(d)/gz/seek.c\
+ $(d)/gz/write.c\
diff --git a/src/base/gz/seek.c b/src/base/gz/seek.c
new file mode 100644
index 0000000..328886d
--- /dev/null
+++ b/src/base/gz/seek.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+gz·seek(gz·Stream *s, long off, enum SeekPos whence)
+{
+ return gzseek(s, off, whence);
+}
diff --git a/src/base/gz/write.c b/src/base/gz/write.c
new file mode 100644
index 0000000..862d833
--- /dev/null
+++ b/src/base/gz/write.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+gz·write(gz·Stream *s, int sz, int n, void* buf)
+{
+ return gzwrite(s, buf, n*sz);
+}
diff --git a/src/base/io/fd.c b/src/base/io/fd.c
new file mode 100644
index 0000000..ded1b02
--- /dev/null
+++ b/src/base/io/fd.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+io·fd(io·Stream *s)
+{
+ return fileno(s);
+}
diff --git a/src/base/io/flush.c b/src/base/io/flush.c
new file mode 100644
index 0000000..0f1217a
--- /dev/null
+++ b/src/base/io/flush.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+io·flush(io·Stream *s)
+{
+ return fflush(s);
+}
diff --git a/src/base/io/get.c b/src/base/io/get.c
new file mode 100644
index 0000000..d4e52f8
--- /dev/null
+++ b/src/base/io/get.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+byte
+io·getbyte(io·Stream *s)
+{
+ return fgetc(s);
+}
diff --git a/src/base/io/interface.c b/src/base/io/interface.c
new file mode 100644
index 0000000..bead9e1
--- /dev/null
+++ b/src/base/io/interface.c
@@ -0,0 +1,70 @@
+#include "internal.h"
+
+static
+int
+·read(void *rdr, int size, int n, void *buf)
+{
+ return io·read((io·Stream *)rdr, size, n, buf);
+}
+
+static
+byte
+·get(void *rdr)
+{
+ return io·getbyte((io·Stream *)rdr);
+}
+
+static
+error
+·unget(void *rdr, byte c)
+{
+ return io·ungetbyte((io·Stream *)rdr, c);
+}
+
+static
+int
+·write(void *wtr, int sz, int n, void *buf)
+{
+ return io·write((io·Stream *)wtr, sz, n, buf);
+}
+
+static
+error
+·put(void *wtr, byte c)
+{
+ return io·putbyte((io·Stream *)wtr, c);
+}
+
+static
+int
+·puts(void *wtr, string s)
+{
+ return io·putstring((io·Stream *)wtr, s);
+}
+
+static
+int
+·seek(void *skr, long off, enum SeekPos whence)
+{
+ return io·seek((io·Stream *)skr, off, whence);
+}
+
+static
+long
+·tell(void *skr)
+{
+ return io·tell((io·Stream *)skr);
+}
+
+/* actual interfaces */
+io·Reader sys·Reader = (io·Reader){ ·read };
+io·Seeker sys·Seeker = (io·Seeker){ ·seek, ·tell };
+io·Peeker sys·Peeker = (io·Peeker){ ·get, ·unget };
+io·SeekReader sys·SeekReader = (io·SeekReader){ ·seek, ·tell, ·read };
+io·PeekReader sys·PeekReader = (io·PeekReader){ ·read, ·get, ·unget };
+
+io·Writer sys·Writer = (io·Writer){ ·write };
+io·Putter sys·Putter = (io·Putter){ ·put, ·puts };
+io·PutWriter sys·PutWriter = (io·PutWriter){ ·write, ·put, ·puts };
+
+io·ReadWriter sys·ReadWriter = (io·ReadWriter){ ·read, ·write };
diff --git a/src/base/io/internal.h b/src/base/io/internal.h
new file mode 100644
index 0000000..302c035
--- /dev/null
+++ b/src/base/io/internal.h
@@ -0,0 +1,4 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
diff --git a/src/base/io/open.c b/src/base/io/open.c
new file mode 100644
index 0000000..e50e334
--- /dev/null
+++ b/src/base/io/open.c
@@ -0,0 +1,13 @@
+#include "internal.h"
+
+io·Stream*
+io·open(byte *name, byte *mode)
+{
+ return fopen(name, mode);
+}
+
+error
+io·close(io·Stream *s)
+{
+ return fclose(s);
+}
diff --git a/src/base/io/putbyte.c b/src/base/io/putbyte.c
new file mode 100644
index 0000000..2350a8d
--- /dev/null
+++ b/src/base/io/putbyte.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+io·putbyte(io·Stream *s, byte c)
+{
+ return fputc(c, s);
+}
diff --git a/src/base/io/putstring.c b/src/base/io/putstring.c
new file mode 100644
index 0000000..53fa993
--- /dev/null
+++ b/src/base/io/putstring.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+io·putstring(io·Stream *s, string str)
+{
+ return fputs(str, s);
+}
diff --git a/src/base/io/read.c b/src/base/io/read.c
new file mode 100644
index 0000000..b0ed3d2
--- /dev/null
+++ b/src/base/io/read.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+io·read(io·Stream *s, int sz, int n, void *buf)
+{
+ return fread(buf, sz, n, s);
+}
diff --git a/src/base/io/readln.c b/src/base/io/readln.c
new file mode 100644
index 0000000..283472d
--- /dev/null
+++ b/src/base/io/readln.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+int
+io·readln(io·Stream *s, int n, byte* buf)
+{
+ byte* b;
+ b = fgets(buf, n+1, s);
+ if(b == nil)
+ return -1;
+
+ return strlen(buf);
+}
diff --git a/src/base/io/rules.mk b/src/base/io/rules.mk
new file mode 100644
index 0000000..2e03ca5
--- /dev/null
+++ b/src/base/io/rules.mk
@@ -0,0 +1,14 @@
+SRCS_$(d)+=\
+ $(d)/io/fd.c\
+ $(d)/io/flush.c\
+ $(d)/io/interface.c\
+ $(d)/io/open.c\
+ $(d)/io/putbyte.c\
+ $(d)/io/putstring.c\
+ $(d)/io/read.c\
+ $(d)/io/readln.c\
+ $(d)/io/seek.c\
+ $(d)/io/stat.c\
+ $(d)/io/tell.c\
+ $(d)/io/unget.c\
+ $(d)/io/write.c\
diff --git a/src/base/io/seek.c b/src/base/io/seek.c
new file mode 100644
index 0000000..d0e7488
--- /dev/null
+++ b/src/base/io/seek.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+io·seek(io·Stream *s, long off, enum SeekPos origin)
+{
+ return fseek(s, off, origin);
+}
diff --git a/src/base/io/stat.c b/src/base/io/stat.c
new file mode 100644
index 0000000..d86f1ee
--- /dev/null
+++ b/src/base/io/stat.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+error
+io·stat(io·Stream *s, io·Stat *buf)
+{
+ return fstat(fileno(s), buf);
+}
diff --git a/src/base/io/tell.c b/src/base/io/tell.c
new file mode 100644
index 0000000..1c50439
--- /dev/null
+++ b/src/base/io/tell.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+long
+io·tell(io·Stream *s)
+{
+ return ftell(s);
+}
diff --git a/src/base/io/unget.c b/src/base/io/unget.c
new file mode 100644
index 0000000..5ec3536
--- /dev/null
+++ b/src/base/io/unget.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+error
+io·ungetbyte(io·Stream *s, byte c)
+{
+ return ungetc(c, s);
+}
diff --git a/src/base/io/write.c b/src/base/io/write.c
new file mode 100644
index 0000000..63df664
--- /dev/null
+++ b/src/base/io/write.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+io·write(io·Stream *s, int sz, int n, void *buf)
+{
+ return fwrite(buf, sz, n, s);
+}
diff --git a/src/base/mem/arena.c b/src/base/mem/arena.c
new file mode 100644
index 0000000..b2ce044
--- /dev/null
+++ b/src/base/mem/arena.c
@@ -0,0 +1,119 @@
+#include "internal.h"
+
+#define ARENA_ALIGN 8
+#define ARENA_BLOCK_SIZE 1024 * 1024
+
+#define ALIGN_DOWN(n, a) ((n) & ~((a)-1))
+#define ALIGN_UP(n, a) ALIGN_DOWN((n) + (a)-1, (a))
+#define ALIGN_DOWN_PTR(p, a) ((void*)ALIGN_DOWN((uintptr)(p), (a)))
+#define ALIGN_UP_PTR(p, a) ((void*)ALIGN_UP((uintptr)(p), (a)))
+
+struct Block
+{
+ struct Block *next;
+ byte buf[];
+};
+
+struct mem·Arena
+{
+ void *heap;
+ mem·Allocator mem;
+
+ byte *off;
+ byte *end;
+ struct Block *curr;
+ struct Block first;
+};
+
+static
+void*
+·arenaalloc(void *heap, uint n, ulong size)
+{
+ return mem·arenaalloc(heap, n, size);
+}
+
+static
+void
+·arenafree(void *heap, void *ptr)
+{
+ /* no-op */
+}
+
+mem·Allocator mem·ArenaAllocator = {
+ .alloc = ·arenaalloc,
+ .free = ·arenafree,
+};
+
+
+static
+void
+grow(mem·Arena *a, vlong min)
+{
+ uintptr size;
+ struct Block *blk;
+
+ size = ALIGN_UP(MAX(min, ARENA_BLOCK_SIZE), ARENA_ALIGN);
+ blk = a->mem.alloc(a->heap, 1, sizeof(*blk) + size);
+ a->off = blk->buf;
+ a->end = a->off + size;
+
+ assert(a->curr->next == nil);
+ assert(a->off == ALIGN_DOWN_PTR(a->off, ARENA_ALIGN));
+
+ a->curr->next = blk;
+ a->curr = blk;
+}
+
+mem·Arena*
+mem·makearena(mem·Allocator from, void *impl)
+{
+ mem·Arena *a = from.alloc(impl, 1, sizeof(*a) + ARENA_BLOCK_SIZE);
+ a->mem = from;
+ a->heap = impl;
+ a->off = a->first.buf;
+ a->end = a->first.buf + ARENA_BLOCK_SIZE;
+ a->curr = &a->first;
+ a->first.next = nil;
+
+ return a;
+}
+
+void
+mem·freearena(mem·Arena *a)
+{
+ struct Block *it, *next;
+
+ it = a->first.next;
+ while (it != nil) {
+ next = it->next;
+ a->mem.free(a->heap, it);
+ it = next;
+ }
+
+ a->mem.free(a->heap, a);
+}
+
+void*
+mem·arenaalloc(mem·Arena *a, uint n, ulong size)
+{
+ if(!n) {
+ return nil;
+ }
+
+ void *ptr;
+ // TODO(nnoll): check for overflow
+ size = n * size;
+
+ if (size > (ulong)(a->end - a->off)) {
+ grow(a, size);
+ assert(size <= (uintptr)(a->end - a->off));
+ }
+
+ ptr = a->off;
+ a->off = ALIGN_UP_PTR(a->off + size, ARENA_ALIGN);
+
+ assert(a->off <= a->end);
+ assert(ptr == ALIGN_DOWN_PTR(ptr, ARENA_ALIGN));
+
+ return ptr;
+}
diff --git a/src/base/mem/buffer.c b/src/base/mem/buffer.c
new file mode 100644
index 0000000..b684d35
--- /dev/null
+++ b/src/base/mem/buffer.c
@@ -0,0 +1,45 @@
+#include "internal.h"
+
+/* Grow to particular size */
+void*
+·bufgrow(void* buf, vlong newLen, vlong eltsize)
+{
+ assert(bufcap(buf) <= (SIZE_MAX - 1) / 2);
+
+ vlong newCap = MAX(16, MAX(1 + 2 * bufcap(buf), newLen));
+
+ assert(newLen <= newCap);
+ assert(newCap <= (SIZE_MAX - offsetof(BufHdr, buf)) / eltsize);
+
+ vlong newSize = offsetof(BufHdr, buf) + newCap * eltsize;
+
+ BufHdr* newHdr;
+ if (buf) {
+ newHdr = bufhdr(buf);
+ newHdr = (BufHdr*)realloc((void*)newHdr, newSize);
+ } else {
+ newHdr = (BufHdr*)malloc(newSize);
+ newHdr->len = 0;
+ }
+
+ newHdr->cap = newCap;
+ return (void*)newHdr->buf;
+}
+
+/* Pop out a value */
+void
+·bufdel(void *buf, int i, vlong eltsize)
+{
+ int n;
+ byte *b;
+ byte stk[1024];
+ assert(eltsize < sizeof(stk));
+
+ b = (byte*)buf;
+ if(n = buflen(buf), i < n) {
+ memcpy(stk, b+eltsize*i, eltsize);
+ memcpy(b+eltsize*i, b+eltsize*(i+1), eltsize*(n-i-1));
+ memcpy(b+eltsize*(n-1), stk, eltsize);
+ }
+ bufhdr(buf)->len--;
+}
diff --git a/src/base/mem/interface.c b/src/base/mem/interface.c
new file mode 100644
index 0000000..4d7d1ce
--- /dev/null
+++ b/src/base/mem/interface.c
@@ -0,0 +1,36 @@
+#include "internal.h"
+
+static
+void
+·free(void *_, void *ptr) {
+ return free(ptr);
+}
+
+static
+void *
+·alloc(void *_, uint n, ulong size) {
+ return malloc(n*size);
+}
+
+static
+void *
+·calloc(void *_, uint n, ulong size) {
+ return calloc(n, size);
+}
+
+static
+void *
+·realloc(void *_, void *ptr, uint n, ulong size) {
+ return realloc(ptr, n*size);
+}
+
+mem·Allocator sys·Memory = {
+ .alloc = ·calloc,
+ .free = ·free
+};
+
+mem·Reallocator sys·FullMemory = {
+ .alloc = ·calloc,
+ .realloc = ·realloc,
+ .free = ·free
+};
diff --git a/src/base/mem/internal.h b/src/base/mem/internal.h
new file mode 100644
index 0000000..302c035
--- /dev/null
+++ b/src/base/mem/internal.h
@@ -0,0 +1,4 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
diff --git a/src/base/mem/rules.mk b/src/base/mem/rules.mk
new file mode 100644
index 0000000..b912d0c
--- /dev/null
+++ b/src/base/mem/rules.mk
@@ -0,0 +1,5 @@
+SRCS_$(d)+=\
+ $(d)/mem/arena.c\
+ $(d)/mem/buffer.c\
+ $(d)/mem/interface.c\
+ $(d)/mem/set64.c\
diff --git a/src/base/mem/set64.c b/src/base/mem/set64.c
new file mode 100644
index 0000000..464b3ad
--- /dev/null
+++ b/src/base/mem/set64.c
@@ -0,0 +1,13 @@
+#include "internal.h"
+
+void
+mem·set64(void *dst, uint64 val, uintptr size)
+{
+ intptr i;
+
+ for(i = 0; i < (size & (~7)); i += 8)
+ memcpy((byte*)dst + i, &val, 8);
+
+ for(; i < size; i++)
+ ((byte*)dst)[i] = ((byte*)&val)[i&7];
+}
diff --git a/src/base/mmap/internal.h b/src/base/mmap/internal.h
new file mode 100644
index 0000000..7606c7e
--- /dev/null
+++ b/src/base/mmap/internal.h
@@ -0,0 +1,5 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
+#include <sys/mman.h>
diff --git a/src/base/mmap/mmap.c b/src/base/mmap/mmap.c
new file mode 100644
index 0000000..ce3011c
--- /dev/null
+++ b/src/base/mmap/mmap.c
@@ -0,0 +1,39 @@
+#include "internal.h"
+
+mmap·Reader
+mmap·open(byte *filename)
+{
+ int fd;
+ int err;
+ void *buf;
+ io·Stream *s;
+ io·Stat st;
+
+ s = io·open(filename, "r");
+ fd = io·fd(s);
+ err = io·stat(s, &st);
+ if(err){
+ errorf("file stat: error code %d", err);
+ goto ERROR;
+ }
+
+ buf = mmap(nil, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+ if(!buf){
+ errorf("mmap: failed");
+ goto ERROR;
+ }
+ // NOTE: posix systems require that reference kept to mmap file after fd is closed
+ io·close(s);
+ return (mmap·Reader){.len=st.st_size, .b=buf};
+
+ERROR:
+ io·close(s);
+ return (mmap·Reader){ 0 };
+}
+
+error
+mmap·close(mmap·Reader rdr)
+{
+ munmap(rdr.b, rdr.len);
+ return 0;
+}
diff --git a/src/base/mmap/rules.mk b/src/base/mmap/rules.mk
new file mode 100644
index 0000000..fb3cab5
--- /dev/null
+++ b/src/base/mmap/rules.mk
@@ -0,0 +1,2 @@
+SRCS_$(d)+=\
+ $(d)/mmap/mmap.c\
diff --git a/src/base/os/basename.c b/src/base/os/basename.c
new file mode 100644
index 0000000..b5bb343
--- /dev/null
+++ b/src/base/os/basename.c
@@ -0,0 +1,10 @@
+#include "internal.h"
+
+char*
+os·basename(char *path)
+{
+ char *sep;
+
+ sep = strrchr(path, os·sep());
+ return (sep == nil) ? path : sep+1;
+}
diff --git a/src/base/os/exists.c b/src/base/os/exists.c
new file mode 100644
index 0000000..a3c8935
--- /dev/null
+++ b/src/base/os/exists.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+int
+os·exists(byte *path, int flag)
+{
+ return access(path, flag) == 0;
+}
diff --git a/src/base/os/internal.h b/src/base/os/internal.h
new file mode 100644
index 0000000..302c035
--- /dev/null
+++ b/src/base/os/internal.h
@@ -0,0 +1,4 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
diff --git a/src/base/os/rules.mk b/src/base/os/rules.mk
new file mode 100644
index 0000000..bf1e71d
--- /dev/null
+++ b/src/base/os/rules.mk
@@ -0,0 +1,4 @@
+SRCS_$(d)+=\
+ $(d)/os/basename.c\
+ $(d)/os/exists.c\
+ $(d)/os/sep.c\
diff --git a/src/base/os/sep.c b/src/base/os/sep.c
new file mode 100644
index 0000000..750e627
--- /dev/null
+++ b/src/base/os/sep.c
@@ -0,0 +1,14 @@
+#include "internal.h"
+
+int
+os·sep(void)
+{
+#if defined(UNIX) || defined(__linux__)
+ return '/';
+#elif defined(WIN32)
+ return '\\';
+#else
+ panicf("unrecognized operating system");
+ return '\0';
+#endif
+}
diff --git a/src/base/rng/base.c b/src/base/rng/base.c
new file mode 100644
index 0000000..9ec496e
--- /dev/null
+++ b/src/base/rng/base.c
@@ -0,0 +1,24 @@
+#include "internal.h"
+
+static uint64
+splitmix64(struct Mix *state)
+{
+ uint64 result = state->s;
+
+ state->s = result + 0x9E3779B97f4A7C15;
+ result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9;
+ result = (result ^ (result >> 27)) * 0x94D049BB133111EB;
+ return result ^ (result >> 31);
+}
+
+int
+rng·init(uint64 seed)
+{
+ int i;
+ Mix smstate = {seed};
+
+ for(i=0; i < 4; i++)
+ rng·RNG.s[i] = splitmix64(&smstate);
+
+ return 0;
+}
diff --git a/src/base/rng/bernoulli.c b/src/base/rng/bernoulli.c
new file mode 100644
index 0000000..02f531e
--- /dev/null
+++ b/src/base/rng/bernoulli.c
@@ -0,0 +1,7 @@
+#include "internal.h"
+
+bool
+rng·bernoulli(double f)
+{
+ return rng·random() < f;
+}
diff --git a/src/base/rng/exponential.c b/src/base/rng/exponential.c
new file mode 100644
index 0000000..c07e007
--- /dev/null
+++ b/src/base/rng/exponential.c
@@ -0,0 +1,11 @@
+#include "internal.h"
+
+/* Returns a random float64 between 0 and 1 */
+double
+rng·exponential(double lambda)
+{
+ double f;
+
+ f = rng·random();
+ return -log(1 - f)/lambda;
+}
diff --git a/src/base/rng/internal.h b/src/base/rng/internal.h
new file mode 100644
index 0000000..9cf5f41
--- /dev/null
+++ b/src/base/rng/internal.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
+
+#define rol64(x, k) ((x) << (k) | ((x) >> (64-(k))))
+
+typedef struct Rng
+{
+ uint64 s[4];
+} Rng;
+
+typedef struct Mix
+{
+ uint64 s;
+} Mix;
+
+
+extern Rng rng·RNG;
diff --git a/src/base/rng/normal.c b/src/base/rng/normal.c
new file mode 100644
index 0000000..aab5731
--- /dev/null
+++ b/src/base/rng/normal.c
@@ -0,0 +1,77 @@
+#include "internal.h"
+
+static inline double
+erfinv(double x)
+{
+ /* useful constants */
+ static double
+ a0 = 1.1975323115670912564578e0, a1 = 4.7072688112383978012285e1,
+ a2 = 6.9706266534389598238465e2, a3 = 4.8548868893843886794648e3,
+ a4 = 1.6235862515167575384252e4, a5 = 2.3782041382114385731252e4,
+ a6 = 1.1819493347062294404278e4, a7 = 8.8709406962545514830200e2,
+
+ b0 = 1.0000000000000000000e0, b1 = 4.2313330701600911252e1,
+ b2 = 6.8718700749205790830e2, b3 = 5.3941960214247511077e3,
+ b4 = 2.1213794301586595867e4, b5 = 3.9307895800092710610e4,
+ b6 = 2.8729085735721942674e4, b7 = 5.2264952788528545610e3,
+
+ c0 = 1.42343711074968357734e0, c1 = 4.63033784615654529590e0,
+ c2 = 5.76949722146069140550e0, c3 = 3.64784832476320460504e0,
+ c4 = 1.27045825245236838258e0, c5 = 2.41780725177450611770e-1,
+ c6 = 2.27238449892691845833e-2, c7 = 7.74545014278341407640e-4,
+
+ d0 = 1.4142135623730950488016887e0, d1 = 2.9036514445419946173133295e0,
+ d2 = 2.3707661626024532365971225e0, d3 = 9.7547832001787427186894837e-1,
+ d4 = 2.0945065210512749128288442e-1, d5 = 2.1494160384252876777097297e-2,
+ d6 = 7.7441459065157709165577218e-4, d7 = 1.4859850019840355905497876e-9,
+
+ e0 = 6.65790464350110377720e0, e1 = 5.46378491116411436990e0,
+ e2 = 1.78482653991729133580e0, e3 = 2.96560571828504891230e-1,
+ e4 = 2.65321895265761230930e-2, e5 = 1.24266094738807843860e-3,
+ e6 = 2.71155556874348757815e-5, e7 = 2.01033439929228813265e-7,
+
+ f0 = 1.414213562373095048801689e0, f1 = 8.482908416595164588112026e-1,
+ f2 = 1.936480946950659106176712e-1, f3 = 2.103693768272068968719679e-2,
+ f4 = 1.112800997078859844711555e-3, f5 = 2.611088405080593625138020e-5,
+ f6 = 2.010321207683943062279931e-7, f7 = 2.891024605872965461538222e-15,
+
+ Ln2 = 0.693147180559945309417232121458176568075500134360255254120680009;
+
+ int s;
+ double r, z1, z2;
+
+ if(x < 0) {
+ s = -1;
+ x = -x;
+ } else {
+ s = +1;
+ }
+
+ if(x <= 0.85) {
+ r = 0.180625 - 0.25*x*x;
+ z1 = ((((((a7*r+a6)*r+a5)*r+a4)*r+a3)*r+a2)*r+a1)*r + a0;
+ z2 = ((((((b7*r+b6)*r+b5)*r+b4)*r+b3)*r+b2)*r+b1)*r + b0;
+ return s*(x*z1) / z2;
+ }
+ r = sqrt(Ln2 - log(1.0-x));
+ if(r <= 5.0) {
+ r -= 1.6;
+ z1 = ((((((c7*r+c6)*r+c5)*r+c4)*r+c3)*r+c2)*r+c1)*r + c0;
+ z2 = ((((((d7*r+d6)*r+d5)*r+d4)*r+d3)*r+d2)*r+d1)*r + d0;
+ } else {
+ r -= 5.0;
+ z1 = ((((((e7*r+e6)*r+e5)*r+e4)*r+e3)*r+e2)*r+e1)*r + e0;
+ z2 = ((((((f7*r+f6)*r+f5)*r+f4)*r+f3)*r+f2)*r+f1)*r + f0;
+ }
+
+ return s*z1/z2;
+}
+
+double
+rng·normal(void)
+{
+ double f;
+ f = rng·random();
+
+ return sqrt(2)*erfinv(2*f-1);
+}
diff --git a/src/base/rng/poisson.c b/src/base/rng/poisson.c
new file mode 100644
index 0000000..3ec15c9
--- /dev/null
+++ b/src/base/rng/poisson.c
@@ -0,0 +1,126 @@
+#include "internal.h"
+
+/*
+ * Ahrens, J. H., & Dieter, U. (1982).
+ * Computer Generation of Poisson Deviates from Modified Normal Distributions.
+ */
+static double factorial[10] = {1., 1., 2., 6., 24., 120., 720., 5040., 40320., 362880.};
+static double coeffs[9] = {
+ -.500000000, +.333333333, -.249999856,
+ +.200011780, -.166684875, +.142187833,
+ -.124196313, +.125005956, -.114265030,
+};
+
+static inline
+double
+log1pmx(double x, double off)
+{
+ int i;
+ double r, t;
+
+ if(-0.25 < x && x < 0.25) {
+ r = 0;
+ t = 1;
+ for(i=0;i<arrlen(coeffs);i++) {
+ r += coeffs[i]*t;
+ t *= x;
+ }
+
+ return x*x*r;
+ }
+ return log(1+x) - off;
+}
+
+static inline
+double
+procf(double mu, double s, int64 K, double *px, double *py, double *fx, double *fy)
+{
+ double d, V, X;
+ double w, b1, b2, c1, c2, c3, c0, c;
+
+ w = 0.3989422804014327/s;
+ b1 = 0.041666666666666664/mu;
+ b2 = 0.3*b1*b1;
+ c3 = 0.14285714285714285*b1*b2;
+ c2 = b2 - 15.*c3;
+ c1 = b1 - 6.*b2 + 45.*c3;
+ c0 = 1 - b1 + 3.*b2 - 15.*c3;
+ c = .1069/mu;
+
+ if(K < 10) {
+ *px = -mu;
+ *py = pow(mu,K) / factorial[K];
+ }else{
+ d = 0.08333333333333333/K;
+ d = d - 4.8*d*d*d;
+ V = (mu - K) / K;
+
+ *px = K*log1pmx(V,mu-K) - d;
+ *py = 0.3989422804014327/sqrt(K);
+ }
+
+ X = (K - mu + 0.5)/s;
+ *fx = -0.5*X*X;
+ *fy = w*(((c3*X*X + c2)*X*X + c1)*X*X + c0);
+
+ return c;
+}
+
+static inline
+uint64
+bigpoisson(double mu)
+{
+ int64 L,K;
+ double G,s,d,U,E,T;
+ double px,py,fx,fy,c;
+
+ s = sqrt(mu);
+ d = 6*mu*mu;
+ L = floor(mu - 1.1484);
+
+stepN:
+ G = mu + s*rng·normal();
+ K = floor(G);
+ if(K<0)
+ goto stepP;
+stepI:
+ if(K>=L)
+ return K;
+stepS:
+ U = rng·random();
+ if(d*U >= (mu-K)*(mu-K)*(mu-K))
+ return K;
+stepP:
+ if(G < 0)
+ goto stepE;
+stepQ:
+ c = procf(mu, s, K, &px, &py, &fx, &fy);
+stepE:
+ E = rng·exponential(1.0);
+ U = rng·random();
+ U = U + U - 1;
+ T = 1.8 + copysign(E,U);
+ if(T < 0.6744)
+ goto stepE;
+ K = floor(mu + s*T);
+ c = procf(mu, s, K, &px, &py, &fx, &fy);
+stepH:
+ if(c*fabs(U) > (py*exp(px + E) - fy*exp(fx + E)))
+ goto stepE;
+ return K;
+}
+
+uint64
+rng·poisson(double mean)
+{
+ int64 n;
+ double z;
+
+ if(mean<10.0) {
+ for(n=0, z=rng·exponential(1.0); z<mean; ++n, z+=rng·exponential(1.0))
+ ;
+ return n;
+ }
+
+ return bigpoisson(mean);
+}
diff --git a/src/base/rng/random.c b/src/base/rng/random.c
new file mode 100644
index 0000000..bd1bd6b
--- /dev/null
+++ b/src/base/rng/random.c
@@ -0,0 +1,33 @@
+#include "internal.h"
+
+static uint64
+xoshiro256ss(Rng *state)
+{
+ uint64 *s = state->s;
+ uint64 result = rol64(s[1] * 5, 7) * 9;
+ uint64 t = s[1] << 17;
+
+ s[2] ^= s[0];
+ s[3] ^= s[1];
+ s[1] ^= s[2];
+ s[0] ^= s[3];
+
+ s[2] ^= t;
+ s[3] = rol64(s[3], 45);
+
+ return result;
+}
+
+double
+rng·random(void)
+{
+ uint64 r = xoshiro256ss(&rng·RNG);
+ return (double)r / (double)UINT64_MAX;
+}
+
+uint64
+rng·randi(int max)
+{
+ uint64 r = xoshiro256ss(&rng·RNG);
+ return r % max;
+}
diff --git a/src/base/rng/rules.mk b/src/base/rng/rules.mk
new file mode 100644
index 0000000..407b1bf
--- /dev/null
+++ b/src/base/rng/rules.mk
@@ -0,0 +1,7 @@
+SRCS_$(d)+=\
+ $(d)/rng/base.c\
+ $(d)/rng/bernoulli.c\
+ $(d)/rng/exponential.c\
+ $(d)/rng/normal.c\
+ $(d)/rng/poisson.c\
+ $(d)/rng/random.c\
diff --git a/src/base/rules.mk b/src/base/rules.mk
new file mode 100644
index 0000000..847e4d8
--- /dev/null
+++ b/src/base/rules.mk
@@ -0,0 +1,37 @@
+include share/push.mk
+
+# Iterate through subdirectory tree
+
+# local sources
+SRCS_$(d):=\
+ $(d)/arg.c
+include $(d)/bufio/rules.mk
+include $(d)/coro/rules.mk
+include $(d)/error/rules.mk
+include $(d)/flate/rules.mk
+include $(d)/fs/rules.mk
+include $(d)/gz/rules.mk
+include $(d)/io/rules.mk
+include $(d)/mem/rules.mk
+include $(d)/mmap/rules.mk
+include $(d)/os/rules.mk
+include $(d)/rng/rules.mk
+include $(d)/sort/rules.mk
+include $(d)/string/rules.mk
+CHECK_$(d):=\
+ $(d)/test.c
+
+# outputs
+LIBS_$(d) := $(d)/base.a
+BINS_$(d) :=
+
+include share/paths.mk
+
+$(LIBS_$(d)): $(OBJS_$(d))
+ $(ARCHIVE)
+
+$(TEST_$(d)): TCLIBS := $(LIBS_$(d))
+$(TEST_$(d)): $(UNIT_$(d)) $(LIBS_$(d))
+ $(LINK)
+
+include share/pop.mk
diff --git a/src/base/sort/double.c b/src/base/sort/double.c
new file mode 100644
index 0000000..c3feac2
--- /dev/null
+++ b/src/base/sort/double.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·double(uintptr sz, double arr[])
+{
+ double tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/float.c b/src/base/sort/float.c
new file mode 100644
index 0000000..57bd482
--- /dev/null
+++ b/src/base/sort/float.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·float(uintptr sz, float arr[])
+{
+ float tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/int.c b/src/base/sort/int.c
new file mode 100644
index 0000000..33e1def
--- /dev/null
+++ b/src/base/sort/int.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·int(uintptr sz, int arr[])
+{
+ int tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/int16.c b/src/base/sort/int16.c
new file mode 100644
index 0000000..072a3eb
--- /dev/null
+++ b/src/base/sort/int16.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·int16(uintptr sz, int16 arr[])
+{
+ int16 tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/int32.c b/src/base/sort/int32.c
new file mode 100644
index 0000000..27b3b7b
--- /dev/null
+++ b/src/base/sort/int32.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·int32(uintptr sz, int32 arr[])
+{
+ int32 tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/int64.c b/src/base/sort/int64.c
new file mode 100644
index 0000000..b3fa5d4
--- /dev/null
+++ b/src/base/sort/int64.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·int64(uintptr sz, int64 arr[])
+{
+ int64 tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/int8.c b/src/base/sort/int8.c
new file mode 100644
index 0000000..5848e6e
--- /dev/null
+++ b/src/base/sort/int8.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·int8(uintptr sz, int8 arr[])
+{
+ int8 tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/internal.h b/src/base/sort/internal.h
new file mode 100644
index 0000000..ac569de
--- /dev/null
+++ b/src/base/sort/internal.h
@@ -0,0 +1,5 @@
+#pragma once
+
+#include <u.h>
+#include <base.h>
+#include <base/macro/qsort.h>
diff --git a/src/base/sort/rules.mk b/src/base/sort/rules.mk
new file mode 100644
index 0000000..780d6ea
--- /dev/null
+++ b/src/base/sort/rules.mk
@@ -0,0 +1,14 @@
+SRCS_$(d)+=\
+ $(d)/sort/double.c\
+ $(d)/sort/float.c\
+ $(d)/sort/int.c\
+ $(d)/sort/int16.c\
+ $(d)/sort/int32.c\
+ $(d)/sort/int64.c\
+ $(d)/sort/int8.c\
+ $(d)/sort/string.c\
+ $(d)/sort/uint.c\
+ $(d)/sort/uint16.c\
+ $(d)/sort/uint32.c\
+ $(d)/sort/uint64.c\
+ $(d)/sort/uint8.c\
diff --git a/src/base/sort/string.c b/src/base/sort/string.c
new file mode 100644
index 0000000..b511efa
--- /dev/null
+++ b/src/base/sort/string.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·string(uintptr sz, byte* arr[])
+{
+ byte *tmp;
+#define LESS(i, j) (strcmp(arr[i], arr[j]) < 0)
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/uint.c b/src/base/sort/uint.c
new file mode 100644
index 0000000..5b27330
--- /dev/null
+++ b/src/base/sort/uint.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·uint(uintptr sz, uint arr[])
+{
+ uint tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/uint16.c b/src/base/sort/uint16.c
new file mode 100644
index 0000000..2b635b4
--- /dev/null
+++ b/src/base/sort/uint16.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·uint16(uintptr sz, uint16 arr[])
+{
+ uint16 tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/uint32.c b/src/base/sort/uint32.c
new file mode 100644
index 0000000..99a58cf
--- /dev/null
+++ b/src/base/sort/uint32.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·uint32(uintptr sz, uint32 arr[])
+{
+ uint32 tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/uint64.c b/src/base/sort/uint64.c
new file mode 100644
index 0000000..2769825
--- /dev/null
+++ b/src/base/sort/uint64.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·uint64(uintptr sz, uint64 arr[])
+{
+ uint64 tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/sort/uint8.c b/src/base/sort/uint8.c
new file mode 100644
index 0000000..ff02b3c
--- /dev/null
+++ b/src/base/sort/uint8.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+void
+sort·uint8(uintptr sz, uint8 arr[])
+{
+ uint8 tmp;
+#define LESS(i, j) (arr[i] < arr[j])
+#define SWAP(i, j) (tmp = arr[i], arr[i] = arr[j], arr[j] = tmp)
+ QSORT(sz, LESS, SWAP);
+#undef SWAP
+#undef LESS
+}
diff --git a/src/base/string/append.c b/src/base/string/append.c
new file mode 100644
index 0000000..d4d0396
--- /dev/null
+++ b/src/base/string/append.c
@@ -0,0 +1,53 @@
+#include "internal.h"
+
+// append will append the given null terminated c string to the string data
+// structure. this variant can append a substring of length len of the given
+// string to our buffer. the result is reallocated if not enough room is present
+// in the buffer.
+int
+str·appendlen(string *s, vlong n, const byte* b)
+{
+ /*
+ bl = strlen(b);
+ if (n > bl) panicf("attempted to make a substring longer than string");
+ */
+
+ str·grow(s, n);
+ if(*s == nil)
+ return 0;
+
+ Hdr* h = (Hdr*)(*s - sizeof(Hdr));
+
+ memcpy(*s + str·len(*s), b, n);
+ h->len += n;
+ (*s)[h->len] = '\0';
+
+ return n;
+}
+
+// append will append the given null terminated c string to the string data
+// structure. this variant will append the entire string.
+int
+str·append(string *s, const byte* b)
+{
+ return str·appendlen(s, strlen(b), b);
+}
+
+// appendbyte will append the given byte to our string.
+// NOTE: as the byte is on the stack, it is not null-terminated.
+// can not pass to the above functions.
+int
+str·appendbyte(string *s, const byte b)
+{
+ str·grow(s, 1);
+ if(*s == nil)
+ return 0;
+
+ Hdr* h = (Hdr*)(*s - sizeof(Hdr));
+
+ *(*s + str·len(*s)) = b;
+ h->len++;
+ (*s)[h->len] = '\0'; // NOTE: I don't think an explicit zero is required..?
+
+ return 1;
+}
diff --git a/src/base/string/appendf.c b/src/base/string/appendf.c
new file mode 100644
index 0000000..4b8d76c
--- /dev/null
+++ b/src/base/string/appendf.c
@@ -0,0 +1,31 @@
+#include "internal.h"
+
+/*
+ * appendf will append the given formatted string to our buffer.
+ * returns the newly minted string
+ */
+
+int
+str·appendf(string *s, const byte* fmt, ...)
+{
+ va_list args;
+ va_start(args, fmt);
+ int remain = str·cap(*s) - str·len(*s);
+ int n = vsnprintf(*s + str·len(*s), remain + 1, fmt, args);
+ va_end(args);
+
+ if(n > remain){
+ // If the first write was incomplete, we overwite the data again.
+ str·grow(s, n);
+ va_list args;
+ va_start(args, fmt);
+ n = vsnprintf(*s + str·len(*s), n + 1, fmt, args);
+ assert(n - remain <= str·cap(*s));
+ va_end(args);
+ }
+
+ Hdr* h = (Hdr*)(*s - sizeof(Hdr));
+ h->len += n;
+
+ return n;
+}
diff --git a/src/base/string/clear.c b/src/base/string/clear.c
new file mode 100644
index 0000000..986f809
--- /dev/null
+++ b/src/base/string/clear.c
@@ -0,0 +1,9 @@
+#include "internal.h"
+
+void
+str·clear(string *s)
+{
+ Hdr* h = (Hdr*)(*s - sizeof(Hdr));
+ h->len = 0;
+ *s[0] = '\0';
+}
diff --git a/src/base/string/copyn.c b/src/base/string/copyn.c
new file mode 100644
index 0000000..09c2879
--- /dev/null
+++ b/src/base/string/copyn.c
@@ -0,0 +1,11 @@
+#include "internal.h"
+
+char *
+str·copyn(char *dst, char *src, int n)
+{
+ while(*src && n-- > 0)
+ *dst++ = *src++;
+
+ *dst = 0;
+ return dst;
+}
diff --git a/src/base/string/equals.c b/src/base/string/equals.c
new file mode 100644
index 0000000..a975cf5
--- /dev/null
+++ b/src/base/string/equals.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+// Equals returns true if string s and t are equivalent.
+bool
+str·equals(const string s, const string t)
+{
+ vlong sL = str·len(s);
+ vlong tL = str·len(t);
+ if (sL != tL) return false;
+
+ return memcmp(s, t, sL) == 0;
+}
diff --git a/src/base/string/find.c b/src/base/string/find.c
new file mode 100644
index 0000000..20f990e
--- /dev/null
+++ b/src/base/string/find.c
@@ -0,0 +1,11 @@
+#include "internal.h"
+
+// find will find the first occurence of
+// substr in the string returns -1 if nothing was found.
+int
+str·find(string s, const byte* substr)
+{
+ byte* loc = strstr(s, substr);
+ if (loc == nil) return -1;
+ return (int)(loc - s);
+}
diff --git a/src/base/string/fit.c b/src/base/string/fit.c
new file mode 100644
index 0000000..56ab041
--- /dev/null
+++ b/src/base/string/fit.c
@@ -0,0 +1,20 @@
+#include "internal.h"
+
+// fit reallocates the string such that the buffer is exactly sized for the
+// buffer. if the capacity equals the length, then the function is a noop. the
+// byte array is unchanged.
+void
+str·fit(string *s)
+{
+ Hdr* h;
+ vlong cap = str·cap(*s);
+ vlong len = str·len(*s);
+
+ if (cap == len) return;
+
+ h = (Hdr*)(s - sizeof(Hdr));
+ h = realloc(h, sizeof(*h) + len + 1);
+ h->cap = len;
+
+ *s = h->buf;
+}
diff --git a/src/base/string/free.c b/src/base/string/free.c
new file mode 100644
index 0000000..7b5ee98
--- /dev/null
+++ b/src/base/string/free.c
@@ -0,0 +1,8 @@
+#include "internal.h"
+
+// free returns memory associated to the buffer.
+void
+str·free(string s)
+{
+ free(s - sizeof(Hdr));
+}
diff --git a/src/base/string/grow.c b/src/base/string/grow.c
new file mode 100644
index 0000000..39a9d2f
--- /dev/null
+++ b/src/base/string/grow.c
@@ -0,0 +1,33 @@
+#include "internal.h"
+
+// grow ensures that the string can encompass at least delta bytes.
+// if it already can, this is a no op.
+// if it can't, the string will be reallocated.
+void
+str·grow(string *s, vlong delta)
+{
+ Hdr *h, *newh;
+ vlong cap = str·cap(*s);
+ vlong len = str·len(*s);
+ assert(cap >= len); // To prevent unsigned behavior
+
+ if (cap - len >= delta) return;
+
+ h = (Hdr*)(*s - sizeof(Hdr));
+
+ vlong newCap = cap + delta;
+ assert(newCap >= cap); // To prevent unsigned behavior
+ if (newCap < MAX_STRING_ALLOC) {
+ newCap *= 2;
+ } else
+ newCap += MAX_STRING_ALLOC;
+
+ newh = (Hdr*)realloc(h, sizeof(*h) + newCap + 1);
+ if (newh == nil) return;
+
+ memset(newh->buf + len, '\0', newCap - len);
+ newh->cap = newCap;
+ newh->len = len;
+
+ *s = newh->buf;
+}
diff --git a/src/base/string/internal.h b/src/base/string/internal.h
new file mode 100644
index 0000000..8c16c64
--- /dev/null
+++ b/src/base/string/internal.h
@@ -0,0 +1,12 @@
+#pragma once
+#include <u.h>
+#include <base.h>
+
+#define MAX_STRING_ALLOC 1024 * 1024
+
+typedef struct Hdr
+{
+ vlong len;
+ vlong cap;
+ byte buf[];
+} Hdr;
diff --git a/src/base/string/join.c b/src/base/string/join.c
new file mode 100644
index 0000000..fb97b6c
--- /dev/null
+++ b/src/base/string/join.c
@@ -0,0 +1,16 @@
+#include "internal.h"
+
+string
+str·join(vlong len, byte** fields, const byte* sep)
+{
+ string s = str·makecap("", 0, 10);
+ int j = 0;
+
+ for (j = 0; j < len; j++) {
+ str·append(&s, fields[j]);
+ if (j < len - 1)
+ str·appendlen(&s, 1, sep);
+ }
+
+ return s;
+}
diff --git a/src/base/string/len.c b/src/base/string/len.c
new file mode 100644
index 0000000..5e42919
--- /dev/null
+++ b/src/base/string/len.c
@@ -0,0 +1,17 @@
+#include "internal.h"
+
+// len returns the length of the string.
+int
+str·len(const string s)
+{
+ Hdr* h = (Hdr*)(s - sizeof(Hdr));
+ return h->len;
+}
+
+// cap returns the capacity of the string buffer.
+int
+str·cap(const string s)
+{
+ Hdr* h = (Hdr*)(s - sizeof(Hdr));
+ return h->cap;
+}
diff --git a/src/base/string/lower.c b/src/base/string/lower.c
new file mode 100644
index 0000000..c6935f8
--- /dev/null
+++ b/src/base/string/lower.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+// lower will force all runes in the string to be lowercase
+void
+str·lower(string s)
+{
+ byte *b, *e;
+ b = s;
+ e = b + str·len(s);
+ while (b++ != e)
+ *b = tolower(*b);
+}
diff --git a/src/base/string/make.c b/src/base/string/make.c
new file mode 100644
index 0000000..eb71543
--- /dev/null
+++ b/src/base/string/make.c
@@ -0,0 +1,53 @@
+#include "internal.h"
+
+// new returns a new dynamic string object, initialized from the given c string.
+// len defines the length of the c substring that we will copy into our buffer.
+// the backing buffer will have capacity cap.
+string
+str·makecap(const byte *s, vlong len, vlong cap)
+{
+ struct Hdr* h;
+
+ h = malloc(sizeof(*h) + cap + 1);
+ if (s == nil) memset(h, 0, sizeof(*h));
+
+ if (h == nil) return nil; // Allocation failed.
+
+ h->len = (s == nil) ? 0 : len;
+ h->cap = cap;
+
+ if (cap < h->len) goto cleanup;
+
+ if (s != nil && cap > 0) {
+ memcpy(h->buf, s, h->len);
+ memset(h->buf + h->len, '\0', h->cap - h->len + 1);
+ }
+
+ return h->buf;
+
+cleanup:
+ free(h);
+ panicf("Attempted to create a string with less capacity than length");
+ return nil;
+}
+
+// new returns a new dynamic string object, initialized from the given c string.
+// the backing buffer capacity is equivalent to the string length.
+string
+str·makelen(const byte *s, vlong len)
+{
+ vlong sl = (!s) ? 0 : strlen(s);
+ if (sl < len) panicf("attempted to take a bigger substring than string length");
+
+ vlong cap = (len == 0) ? 1 : len;
+ return str·makecap(s, len, cap);
+}
+
+// new returns a new dynamic string object, initialized from the given c string.
+// the backing buffer capacity is equivalent to the string length.
+string
+str·make(const byte *s)
+{
+ vlong len = (!s) ? 0 : strlen(s);
+ return str·makelen(s, len);
+}
diff --git a/src/base/string/makef.c b/src/base/string/makef.c
new file mode 100644
index 0000000..8fb9c38
--- /dev/null
+++ b/src/base/string/makef.c
@@ -0,0 +1,25 @@
+#include "internal.h"
+
+// Newf returns a new dynamic string object
+string
+str·makef(const byte *fmt, ...)
+{
+ vlong n;
+ string s;
+ va_list args;
+
+ va_start(args, fmt);
+ n = vsnprintf(nil, 0, fmt, args);
+ va_end(args);
+
+ s = str·makecap(nil, 0, n);
+
+ va_start(args, fmt);
+ vsnprintf(s, n + 1, fmt, args);
+ va_end(args);
+
+ Hdr* h = (Hdr*)(s - sizeof(Hdr));
+ h->len = n;
+
+ return s;
+}
diff --git a/src/base/string/read.c b/src/base/string/read.c
new file mode 100644
index 0000000..df2028f
--- /dev/null
+++ b/src/base/string/read.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+int
+str·read(string s, int size, int n, void *buf)
+{
+ int len;
+
+ len = MIN(n * size, str·len(s));
+ memcpy(buf, s, len);
+
+ return len;
+}
diff --git a/src/base/string/replace.c b/src/base/string/replace.c
new file mode 100644
index 0000000..127daed
--- /dev/null
+++ b/src/base/string/replace.c
@@ -0,0 +1,26 @@
+#include "internal.h"
+
+// replace will replace all occurences of the given bytes 'from' to bytes 'to'
+// edits are done in place and modify the string.
+// NOTE: as of now strings from and to must be the same size.
+void
+str·replace(string s, const byte* from, const byte* to)
+{
+ vlong fromL = strlen(from);
+ vlong toL = strlen(to);
+ if (toL != fromL) { panicf("different sized replacement string not supported"); }
+
+ vlong l = str·len(s);
+ vlong i = l;
+ vlong j = l;
+
+ for (i = 0; i < l; i++) {
+ for (j = 0; j < toL; j++) {
+ if (s[i] == from[j]) {
+ s[i] = to[j];
+ break;
+ }
+ }
+ }
+}
+
diff --git a/src/base/string/rules.mk b/src/base/string/rules.mk
new file mode 100644
index 0000000..e517ca5
--- /dev/null
+++ b/src/base/string/rules.mk
@@ -0,0 +1,19 @@
+SRCS_$(d)+=\
+ $(d)/string/append.c\
+ $(d)/string/appendf.c\
+ $(d)/string/clear.c\
+ $(d)/string/copyn.c\
+ $(d)/string/equals.c\
+ $(d)/string/find.c\
+ $(d)/string/fit.c\
+ $(d)/string/free.c\
+ $(d)/string/grow.c\
+ $(d)/string/join.c\
+ $(d)/string/len.c\
+ $(d)/string/lower.c\
+ $(d)/string/make.c\
+ $(d)/string/makef.c\
+ $(d)/string/read.c\
+ $(d)/string/replace.c\
+ $(d)/string/split.c\
+ $(d)/string/upper.c\
diff --git a/src/base/string/split.c b/src/base/string/split.c
new file mode 100644
index 0000000..2aa68b4
--- /dev/null
+++ b/src/base/string/split.c
@@ -0,0 +1,39 @@
+#include "internal.h"
+
+// split will split the string by the given token.
+// returns a stretchy buffer of strings that result from the partition.
+// it is the caller's responsibility to clean the memory.
+string*
+str·split(string s, const byte* tok)
+{
+ string* fields = nil;
+ vlong start = 0;
+
+ vlong sL = str·len(s);
+ vlong tokL = strlen(tok);
+ if (sL == 0 || tokL == 0) return nil;
+
+ buffit(fields, 5);
+
+ for (vlong i = 0; i < sL - tokL; i++) {
+ if ((tokL == 1 && s[i] == tokL) || !memcmp(s + i, tok, tokL)) {
+ bufpush(fields, str·makelen(s + start, i - start));
+ if (fields[buflen(fields) - 1] == nil) goto cleanup;
+
+ start = i + tokL;
+ i += tokL - 1;
+ }
+ }
+
+ bufpush(fields, str·makelen(s + start, sL - start));
+
+ return fields;
+
+cleanup:
+ for (vlong i = 0; i < buflen(fields); i++) {
+ str·free(fields[i]);
+ }
+ buffree(fields);
+ return nil;
+}
+
diff --git a/src/base/string/upper.c b/src/base/string/upper.c
new file mode 100644
index 0000000..ab692c1
--- /dev/null
+++ b/src/base/string/upper.c
@@ -0,0 +1,12 @@
+#include "internal.h"
+
+// Upper will force all runes in the string to be uppercase.
+void
+str·upper(string s)
+{
+ byte *b, *e;
+ b = s;
+ e = b + str·len(s);
+ while (b++ != e)
+ *b = toupper(*b);
+}
diff --git a/src/base/test.c b/src/base/test.c
new file mode 100644
index 0000000..a29be1d
--- /dev/null
+++ b/src/base/test.c
@@ -0,0 +1,170 @@
+#include <u.h>
+#include <base.h>
+#include <base/macro/map.h>
+
+#include <time.h>
+
+uintptr
+printtest(Coro *c, uintptr d)
+{
+ printf("--> Recieved %lu\n", d);
+ d = coro·yield(c, d+10);
+ printf("--> Now %lu\n", d);
+
+ return d;
+}
+
+uintptr
+sequence(Coro *c, uintptr start)
+{
+ int d = start;
+ for (;;) {
+ coro·yield(c, d++);
+ }
+
+ return d;
+}
+
+struct PrimeMsg
+{
+ Coro *seq;
+ int p;
+};
+
+uintptr
+filter(Coro *c, uintptr data)
+{
+ int x, p;
+ Coro *seq;
+ struct PrimeMsg *msg;
+
+ // Need to copy relevant variables onto the local stack
+ // Data is volatile.
+ msg = (struct PrimeMsg*)data;
+ seq = msg->seq;
+ p = msg->p;
+
+ for (;;) {
+ x = coro·yield(seq, x);
+ if (x % p != 0) {
+ x = coro·yield(c, x);
+ }
+ }
+
+ return 0;
+}
+
+error
+test·coro()
+{
+ int i;
+ Coro *c[4];
+ uintptr d;
+
+ printf("Starting singleton test\n");
+
+ for (i = 0; i < arrlen(c); i++) {
+ c[i] = coro·make(0, &printtest);
+ }
+
+ /* Singleton test */
+ d = 0;
+ for (i = 0; i < 10; i++) {
+ d = coro·yield(c[0], d);
+ }
+
+ printf("Starting triplet test\n");
+
+ /* Triplet test */
+ for (i = 0; i < 10; i++) {
+ d = coro·yield(c[1], d);
+ d = coro·yield(c[2], d+100);
+ d = coro·yield(c[3], d+200);
+ }
+
+ for (i = 0; i < arrlen(c); i++) {
+ coro·free(c[i]);
+ }
+
+ /* Prime sieve */
+ printf("Starting prime test\n");
+ uintptr num;
+ Coro *cur, *seq[50];
+
+ num = 2;
+ seq[0] = coro·make(4096, &sequence);
+ cur = *seq;
+
+ num = coro·yield(cur, num);
+ for (i = 1; i < arrlen(seq); i++) {
+ seq[i] = coro·make(4096, &filter);
+ struct PrimeMsg msg = {
+ .seq = cur,
+ .p = num,
+ };
+ cur = seq[i];
+ num = coro·yield(cur, (uintptr)&msg);
+ printf("--> prime number %lu\n", num);
+ }
+ return 0;
+}
+
+int
+less(void* a, void* b)
+{
+ int ai, bi;
+ ai = *(int*)a;
+ bi = *(int*)b;
+
+ return ai - bi;
+}
+
+error
+test·sort()
+{
+ clock_t t;
+ int i, test[10000];
+ for (i = 0; i < arrlen(test); i++) {
+ test[i] = rand();
+ }
+
+ t = clock();
+ sort·int(arrlen(test), test);
+ t = clock() - t;
+ printf("inlined code took %f ms to execute\n", 1000.*t/CLOCKS_PER_SEC);
+
+ for (i = 0; i < arrlen(test); i++) {
+ test[i] = rand();
+ }
+
+ t = clock();
+ qsort(test, arrlen(test), sizeof(int), (int (*)(const void *, const void *))less);
+ t = clock() - t;
+ printf("std qsort code took %f ms to execute\n", 1000.*t/CLOCKS_PER_SEC);
+
+ /*
+ for (i = 1; i < arrlen(test); i++) {
+ if (test[i] >= test[i-1]) {
+ printf("%d is less that %d\n", test[i], test[i-1]);
+ } else {
+ printf("ERROR: %d is NOT less that %d\n", test[i], test[i-1]);
+ }
+ }
+ */
+
+ return 0;
+}
+
+error
+main()
+{
+ error err;
+#if 0
+ if (err = test·coro(), err) {
+ errorf("test fail: coroutine");
+ }
+#endif
+ if (err = test·sort(), err) {
+ errorf("test fail: coroutine");
+ }
+}