about summary refs log tree commit diff stats
path: root/066stream.mu
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-06-19 12:28:36 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-06-19 12:28:36 -0700
commit8fb0e672c2f5431141d58128f623d1cc02d67bb8 (patch)
treea12ea4a27d24fa537123d71127ba24b5be1e8728 /066stream.mu
parent2a2dc5cf6837871737c0b0d3c5e46a1152ae1d13 (diff)
downloadmu-8fb0e672c2f5431141d58128f623d1cc02d67bb8.tar.gz
1597 - port more helpers from arc version
Diffstat (limited to '066stream.mu')
-rw-r--r--066stream.mu44
1 files changed, 44 insertions, 0 deletions
diff --git a/066stream.mu b/066stream.mu
new file mode 100644
index 00000000..ab86efab
--- /dev/null
+++ b/066stream.mu
@@ -0,0 +1,44 @@
+# new type to help incrementally read strings
+container stream [
+  index:number
+  data:address:array:character
+]
+
+recipe new-stream [
+  default-space:address:array:location <- new location:type, 30:literal
+  result:address:stream <- new stream:type
+  i:address:number <- get-address result:address:stream/deref, index:offset
+  i:address:number/deref <- copy 0:literal
+  d:address:address:array:character <- get-address result:address:stream/deref, data:offset
+  d:address:address:array:character/deref <- next-ingredient
+  reply result:address:stream
+]
+
+recipe rewind-stream [
+  default-space:address:array:location <- new location:type, 30:literal
+  in:address:stream <- next-ingredient
+  x:address:number <- get-address in:address:stream/deref, index:offset
+  x:address:number/deref <- copy 0:literal
+  reply in:address:stream/same-as-arg:0
+]
+
+recipe read-line [
+  default-space:address:array:location <- new location:type, 30:literal
+  in:address:stream <- next-ingredient
+  idx:address:number <- get-address in:address:stream/deref, index:offset
+  s:address:array:character <- get in:address:stream/deref, data:offset
+  next-idx:number <- find-next s:address:array:character, 10:literal/newline, idx:address:number/deref
+  result:address:array:character <- string-copy s:address:array:character, idx:address:number/deref, next-idx:number
+  idx:address:number/deref <- add next-idx:number, 1:literal  # skip newline
+  reply result:address:array:character
+]
+
+recipe end-of-stream? [
+  default-space:address:array:location <- new location:type, 30:literal
+  in:address:stream <- next-ingredient
+  idx:number <- get in:address:stream/deref, index:offset
+  s:address:array:character <- get in:address:stream/deref, data:offset
+  len:number <- length s:address:array:character/deref
+  result:boolean <- greater-or-equal idx:number, len:number
+  reply result:boolean
+]