ref: bef630973910df30c0647daa2c6c8cbae4c6a50e
dir: /lib/std/slurp.myr/
use "alloc.use" use "die.use" use "result.use" use "extremum.use" use "syswrap.use" use "errno.use" use "types.use" pkg std = const slurp : (path : byte[:] -> result(byte[:], errno)) const fslurp : (path : fd -> result(byte[:], errno)) ;; const Bufstart = 4096 const slurp = {path var sl match open(path, Ordonly) | `Fail e: -> `Fail e | `Ok fd: sl = fslurp(fd) close(fd) -> sl ;; } const fslurp = {fd var len, bufsz var buf len = 0 bufsz = Bufstart buf = slalloc(bufsz) while true match read(fd, buf[len:]) | `Ok 0: -> `Ok buf[:len] | `Ok n: len += n if len + 2*n >= bufsz bufsz *= 2 buf = slgrow(buf, bufsz) ;; | `Fail e: -> `Fail e ;; ;; die("unreachable") }