From a654e4ecace2d506d1b10f1dde2c287ebe84ef37 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 26 Mar 2016 23:59:59 -0700 Subject: 2812 --- html/077stream.mu.html | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 html/077stream.mu.html (limited to 'html/077stream.mu.html') diff --git a/html/077stream.mu.html b/html/077stream.mu.html new file mode 100644 index 00000000..5bf82f66 --- /dev/null +++ b/html/077stream.mu.html @@ -0,0 +1,76 @@ + + + + +Mu - 077stream.mu + + + + + + + + + + +
+# new type to help incrementally read texts (arrays of characters)
+container stream [
+  index:number
+  data:address:shared:array:character
+]
+
+def new-stream s:address:shared:array:character -> result:address:shared:stream [
+  local-scope
+  load-ingredients
+  result <- new stream:type
+  i:address:number <- get-address *result, index:offset
+  *i <- copy 0
+  d:address:address:shared:array:character <- get-address *result, data:offset
+  *d <- copy s
+]
+
+def rewind-stream in:address:shared:stream -> in:address:shared:stream [
+  local-scope
+  load-ingredients
+  x:address:number <- get-address *in, index:offset
+  *x <- copy 0
+]
+
+def read-line in:address:shared:stream -> result:address:shared:array:character, in:address:shared:stream [
+  local-scope
+  load-ingredients
+  idx:address:number <- get-address *in, index:offset
+  s:address:shared:array:character <- get *in, data:offset
+  next-idx:number <- find-next s, 10/newline, *idx
+  result <- copy-range s, *idx, next-idx
+  *idx <- add next-idx, 1  # skip newline
+]
+
+def end-of-stream? in:address:shared:stream -> result:boolean [
+  local-scope
+  load-ingredients
+  idx:number <- get *in, index:offset
+  s:address:shared:array:character <- get *in, data:offset
+  len:number <- length *s
+  result <- greater-or-equal idx, len
+]
+
+ + + -- cgit 1.4.1-2-gfad0