about summary refs log tree commit diff stats
path: root/076stream.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-11-13 10:08:57 -0800
committerKartik K. Agaram <vc@akkartik.com>2015-11-13 10:08:57 -0800
commitc603cd6cef43100aa83a62e15f96fd54c9fb987e (patch)
treed8631b1964a0200d170a996314f6e023686ff8de /076stream.mu
parent05d3592047a76db4cc8d77102508c21ca1b86e7b (diff)
downloadmu-c603cd6cef43100aa83a62e15f96fd54c9fb987e.tar.gz
2430 - make room for more transforms
Diffstat (limited to '076stream.mu')
-rw-r--r--076stream.mu44
1 files changed, 44 insertions, 0 deletions
diff --git a/076stream.mu b/076stream.mu
new file mode 100644
index 00000000..0ee3e2c1
--- /dev/null
+++ b/076stream.mu
@@ -0,0 +1,44 @@
+# new type to help incrementally read strings
+container stream [
+  index:number
+  data:address:array:character
+]
+
+recipe new-stream [
+  local-scope
+  result:address:stream <- new stream:type
+  i:address:number <- get-address *result, index:offset
+  *i <- copy 0
+  d:address:address:array:character <- get-address *result, data:offset
+  *d <- next-ingredient
+  reply result
+]
+
+recipe rewind-stream [
+  local-scope
+  in:address:stream <- next-ingredient
+  x:address:number <- get-address *in, index:offset
+  *x <- copy 0
+  reply in/same-as-arg:0
+]
+
+recipe read-line [
+  local-scope
+  in:address:stream <- next-ingredient
+  idx:address:number <- get-address *in, index:offset
+  s:address:array:character <- get *in, data:offset
+  next-idx:number <- find-next s, 10/newline, *idx
+  result:address:array:character <- string-copy s, *idx, next-idx
+  *idx <- add next-idx, 1  # skip newline
+  reply result
+]
+
+recipe end-of-stream? [
+  local-scope
+  in:address:stream <- next-ingredient
+  idx:number <- get *in, index:offset
+  s:address:array:character <- get *in, data:offset
+  len:number <- length *s
+  result:boolean <- greater-or-equal idx, len
+  reply result
+]