From 0a2026a6bdb43568422f368846130685b46a8074 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 19 Aug 2015 22:13:15 -0700 Subject: 2039 - warn on unbalanced '[' --- 060string.mu | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) (limited to '060string.mu') diff --git a/060string.mu b/060string.mu index c7c8bf71..df82ee23 100644 --- a/060string.mu +++ b/060string.mu @@ -404,6 +404,78 @@ scenario string-append-1 [ ] ] +scenario replace-character-in-string [ + run [ + 1:address:array:character/raw <- new [abc] + 1:address:array:character/raw <- string-replace 1:address:array:character/raw, 98/b, 122/z + 2:array:character/raw <- copy *1:address:array:character/raw + ] + memory-should-contain [ + 2:string <- [azc] + ] +] + +recipe string-replace [ + local-scope + s:address:array:character <- next-ingredient + oldc:character <- next-ingredient + newc:character <- next-ingredient + from:number <- next-ingredient + len:number <- length *s + i:number <- find-next s, oldc, from + done?:boolean <- greater-or-equal i, len + reply-if done?, s/same-as-ingredient:0 + dest:address:character <- index-address *s, i + *dest <- copy newc + i <- add i, 1 + s <- string-replace s, oldc, newc, i + reply s/same-as-ingredient:0 +] + +scenario replace-character-at-start [ + run [ + 1:address:array:character/raw <- new [abc] + 1:address:array:character/raw <- string-replace 1:address:array:character/raw, 97/a, 122/z + 2:array:character/raw <- copy *1:address:array:character/raw + ] + memory-should-contain [ + 2:string <- [zbc] + ] +] + +scenario replace-character-at-end [ + run [ + 1:address:array:character/raw <- new [abc] + 1:address:array:character/raw <- string-replace 1:address:array:character/raw, 99/c, 122/z + 2:array:character/raw <- copy *1:address:array:character/raw + ] + memory-should-contain [ + 2:string <- [abz] + ] +] + +scenario replace-character-missing [ + run [ + 1:address:array:character/raw <- new [abc] + 1:address:array:character/raw <- string-replace 1:address:array:character/raw, 100/d, 122/z + 2:array:character/raw <- copy *1:address:array:character/raw + ] + memory-should-contain [ + 2:string <- [abc] + ] +] + +scenario replace-all-characters [ + run [ + 1:address:array:character/raw <- new [banana] + 1:address:array:character/raw <- string-replace 1:address:array:character/raw, 97/a, 122/z + 2:array:character/raw <- copy *1:address:array:character/raw + ] + memory-should-contain [ + 2:string <- [bznznz] + ] +] + # replace underscores in first with remaining args # result:address:array:character <- interpolate template:address:array:character, ... recipe interpolate [ -- cgit 1.4.1-2-gfad0