From ced133e40290c30809e6d632cdf1e6f749ea9dd5 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 16 Sep 2016 16:04:11 -0700 Subject: 3371 --- html/061text.mu.html | 99 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 41 deletions(-) (limited to 'html/061text.mu.html') diff --git a/html/061text.mu.html b/html/061text.mu.html index e1a99fc8..0a0e41bf 100644 --- a/html/061text.mu.html +++ b/html/061text.mu.html @@ -151,15 +151,15 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color return result ] -def grow-buffer in:address:buffer -> in:address:buffer [ +def grow-buffer buf:address:buffer -> buf:address:buffer [ local-scope load-ingredients # double buffer size - olddata:text <- get *in, data:offset + olddata:text <- get *buf, data:offset oldlen:number <- length *olddata newlen:number <- multiply oldlen, 2 newdata:text <- new character:type, newlen - *in <- put *in, data:offset, newdata + *buf <- put *buf, data:offset, newdata # copy old contents i:number <- copy 0 { @@ -198,10 +198,10 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color } ] -def append in:address:buffer, c:character -> in:address:buffer [ +def append buf:address:buffer, c:character -> buf:address:buffer [ local-scope load-ingredients - len:number <- get *in, length:offset + len:number <- get *buf, length:offset { # backspace? just drop last character if it exists and return backspace?:boolean <- equal c, 8/backspace @@ -209,19 +209,34 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color empty?:boolean <- lesser-or-equal len, 0 return-if empty? len <- subtract len, 1 - *in <- put *in, length:offset, len + *buf <- put *buf, length:offset, len return } { # grow buffer if necessary - full?:boolean <- buffer-full? in + full?:boolean <- buffer-full? buf break-unless full? - in <- grow-buffer in + buf <- grow-buffer buf } - s:text <- get *in, data:offset + s:text <- get *buf, data:offset *s <- put-index *s, len, c len <- add len, 1 - *in <- put *in, length:offset, len + *buf <- put *buf, length:offset, len +] + +def append buf:address:buffer, t:text -> buf:address:buffer [ + local-scope + load-ingredients + len:number <- length *t + i:number <- copy 0 + { + done?:boolean <- greater-or-equal i, len + break-if done? + c:character <- index *t, i + buf <- append buf, c + i <- add i, 1 + loop + } ] scenario buffer-append-works [ @@ -318,44 +333,32 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color } ] -def append a:text, b:text -> result:text [ +# Append any number of texts together. +# A later layer also translates calls to this to implicitly call to-text, so +# append to string becomes effectively dynamically typed. +# +# Beware though: this hack restricts how much 'append' can be overridden. Any +# new variants that match: +# append _:text, ___ +# will never ever get used. +def append first:text -> result:text [ local-scope load-ingredients - # handle null addresses - return-unless a, b - return-unless b, a - # result = new character[a.length + b.length] - a-len:number <- length *a - b-len:number <- length *b - result-len:number <- add a-len, b-len - result <- new character:type, result-len - # copy a into result - result-idx:number <- copy 0 - i:number <- copy 0 + buf:address:buffer <- new-buffer 30 + # append first ingredient { - # while i < a.length - a-done?:boolean <- greater-or-equal i, a-len - break-if a-done? - # result[result-idx] = a[i] - in:character <- index *a, i - *result <- put-index *result, result-idx, in - i <- add i, 1 - result-idx <- add result-idx, 1 - loop + break-unless first + buf <- append buf, first } - # copy b into result - i <- copy 0 + # append remaining ingredients { - # while i < b.length - b-done?:boolean <- greater-or-equal i, b-len - break-if b-done? - # result[result-idx] = a[i] - in:character <- index *b, i - *result <- put-index *result, result-idx, in - i <- add i, 1 - result-idx <- add result-idx, 1 + arg:text, arg-found?:boolean <- next-ingredient + break-unless arg-found? + loop-unless arg + buf <- append buf, arg loop } + result <- buffer-to-array buf ] scenario text-append-1 [ @@ -397,6 +400,20 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color ] ] +scenario text-append-multiary [ + run [ + local-scope + x:text <- new [hello, ] + y:text <- new [world] + z:text <- new [!] + z:text <- append x, y, z + 10:array:character/raw <- copy *z + ] + memory-should-contain [ + 10:array:character <- [hello, world!] + ] +] + scenario replace-character-in-text [ run [ local-scope -- cgit 1.4.1-2-gfad0