diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-06-23 14:02:12 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-06-23 14:02:12 -0700 |
commit | dbe124108b7a3529feeeba91339928c4ac737072 (patch) | |
tree | 6da7f748b04ec70cf5f778863ae4f86403943eb2 /html/060string.mu.html | |
parent | fedebaf5dbc1b39347ad3e3a1580fa0190c320b6 (diff) | |
download | mu-dbe124108b7a3529feeeba91339928c4ac737072.tar.gz |
1631 - update html versions
Html is a little more readable thanks to feedback from J David Eisenberg (https://news.ycombinator.com/item?id=9766330), in particular the suggestion to use https://addons.mozilla.org/En-us/firefox/addon/wcag-contrast-checker.
Diffstat (limited to 'html/060string.mu.html')
-rw-r--r-- | html/060string.mu.html | 580 |
1 files changed, 570 insertions, 10 deletions
diff --git a/html/060string.mu.html b/html/060string.mu.html index 43ecdd77..06164424 100644 --- a/html/060string.mu.html +++ b/html/060string.mu.html @@ -12,14 +12,14 @@ <!-- pre { white-space: pre-wrap; font-family: monospace; color: #eeeeee; background-color: #080808; } body { font-family: monospace; color: #eeeeee; background-color: #080808; } -* { font-size: 1em; } +* { font-size: 1.05em; } .muScenario { color: #00af00; } -.Delimiter { color: #c000c0; } -.Comment { color: #8080ff; } -.Constant { color: #008080; } +.Delimiter { color: #a04060; } +.Comment { color: #9090ff; } +.Constant { color: #00a0a0; } .Special { color: #ff6060; } -.CommentedCode { color: #6c6c6c; } -.muControl { color: #804000; } +.Comment { color: #9090ff; } +.muControl { color: #c0a020; } .muRecipe { color: #ff8700; } --> </style> @@ -137,7 +137,7 @@ container buffer [ data:address:array:character ] -<span class="muRecipe">recipe</span> init-buffer [ +<span class="muRecipe">recipe</span> new-buffer [ <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> <span class="CommentedCode">#? $print default-space:address:array:location, [</span> <span class="CommentedCode">#? ]</span> @@ -216,7 +216,7 @@ container buffer [ <span class="muScenario">scenario</span> buffer-append-works [ run [ <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> - x:address:buffer<span class="Special"> <- </span>init-buffer <span class="Constant">3:literal</span> + x:address:buffer<span class="Special"> <- </span>new-buffer <span class="Constant">3:literal</span> s1:address:array:character<span class="Special"> <- </span>get x:address:buffer/deref, data:offset x:address:buffer<span class="Special"> <- </span>buffer-append x:address:buffer, <span class="Constant">97:literal</span> <span class="Comment"># 'a'</span> x:address:buffer<span class="Special"> <- </span>buffer-append x:address:buffer, <span class="Constant">98:literal</span> <span class="Comment"># 'b'</span> @@ -268,7 +268,7 @@ container buffer [ <span class="muScenario">scenario</span> buffer-append-handles-backspace [ run [ <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> - x:address:buffer<span class="Special"> <- </span>init-buffer <span class="Constant">3:literal</span> + x:address:buffer<span class="Special"> <- </span>new-buffer <span class="Constant">3:literal</span> x:address:buffer<span class="Special"> <- </span>buffer-append x:address:buffer, <span class="Constant">97:literal</span> <span class="Comment"># 'a'</span> x:address:buffer<span class="Special"> <- </span>buffer-append x:address:buffer, <span class="Constant">98:literal</span> <span class="Comment"># 'b'</span> x:address:buffer<span class="Special"> <- </span>buffer-append x:address:buffer, <span class="Constant">8:literal/backspace</span> @@ -301,7 +301,7 @@ container buffer [ n:number<span class="Special"> <- </span>multiply n:number, <span class="Constant">-1:literal</span> <span class="Delimiter">}</span> <span class="Comment"># add digits from right to left into intermediate buffer</span> - tmp:address:buffer<span class="Special"> <- </span>init-buffer <span class="Constant">30:literal</span> + tmp:address:buffer<span class="Special"> <- </span>new-buffer <span class="Constant">30:literal</span> digit-base:number<span class="Special"> <- </span>copy <span class="Constant">48:literal</span> <span class="Comment"># '0'</span> <span class="Delimiter">{</span> done?:boolean<span class="Special"> <- </span>equal n:number, <span class="Constant">0:literal</span> @@ -756,6 +756,566 @@ container buffer [ 3:string<span class="Special"> <- </span><span class="Constant">[abc]</span> ] ] + +<span class="Comment"># next-index:number <- find-next text:address:array:character, pattern:character</span> +<span class="muRecipe">recipe</span> find-next [ + <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> + text:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + pattern:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + idx:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + len:number<span class="Special"> <- </span>length text:address:array:character/deref + <span class="Delimiter">{</span> + eof?:boolean<span class="Special"> <- </span>greater-or-equal idx:number, len:number + <span class="muControl">break-if</span> eof?:boolean + curr:character<span class="Special"> <- </span>index text:address:array:character/deref, idx:number + found?:boolean<span class="Special"> <- </span>equal curr:character, pattern:character + <span class="muControl">break-if</span> found?:boolean + idx:number<span class="Special"> <- </span>add idx:number, <span class="Constant">1:literal</span> + <span class="muControl">loop</span> + <span class="Delimiter">}</span> + <span class="muControl">reply</span> idx:number +] + +<span class="muScenario">scenario</span> string-find-next [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[a/b]</span> + 2:number<span class="Special"> <- </span>find-next 1:address:array:character, <span class="Constant">47:literal/slash</span>, <span class="Constant">0:literal/start-index</span> + ] + memory-should-contain [ + 2<span class="Special"> <- </span>1 + ] +] + +<span class="muScenario">scenario</span> string-find-next-empty [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[]</span> + 2:number<span class="Special"> <- </span>find-next 1:address:array:character, <span class="Constant">47:literal/slash</span>, <span class="Constant">0:literal/start-index</span> + ] + memory-should-contain [ + 2<span class="Special"> <- </span>0 + ] +] + +<span class="muScenario">scenario</span> string-find-next-initial [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[/abc]</span> + 2:number<span class="Special"> <- </span>find-next 1:address:array:character, <span class="Constant">47:literal/slash</span>, <span class="Constant">0:literal/start-index</span> + ] + memory-should-contain [ + 2<span class="Special"> <- </span>0 <span class="Comment"># prefix match</span> + ] +] + +<span class="muScenario">scenario</span> string-find-next-final [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc/]</span> + 2:number<span class="Special"> <- </span>find-next 1:address:array:character, <span class="Constant">47:literal/slash</span>, <span class="Constant">0:literal/start-index</span> + ] + memory-should-contain [ + 2<span class="Special"> <- </span>3 <span class="Comment"># suffix match</span> + ] +] + +<span class="muScenario">scenario</span> string-find-next-missing [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:number<span class="Special"> <- </span>find-next 1:address:array:character, <span class="Constant">47:literal/slash</span>, <span class="Constant">0:literal/start-index</span> + ] + memory-should-contain [ + 2<span class="Special"> <- </span>3 <span class="Comment"># no match</span> + ] +] + +<span class="muScenario">scenario</span> string-find-next-invalid-index [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:number<span class="Special"> <- </span>find-next 1:address:array:character, <span class="Constant">47:literal/slash</span>, <span class="Constant">4:literal/start-index</span> + ] + memory-should-contain [ + 2<span class="Special"> <- </span>4 <span class="Comment"># no change</span> + ] +] + +<span class="muScenario">scenario</span> string-find-next-first [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[ab/c/]</span> + 2:number<span class="Special"> <- </span>find-next 1:address:array:character, <span class="Constant">47:literal/slash</span>, <span class="Constant">0:literal/start-index</span> + ] + memory-should-contain [ + 2<span class="Special"> <- </span>2 <span class="Comment"># first '/' of multiple</span> + ] +] + +<span class="muScenario">scenario</span> string-find-next-second [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[ab/c/]</span> + 2:number<span class="Special"> <- </span>find-next 1:address:array:character, <span class="Constant">47:literal/slash</span>, <span class="Constant">3:literal/start-index</span> + ] + memory-should-contain [ + 2<span class="Special"> <- </span>4 <span class="Comment"># second '/' of multiple</span> + ] +] + +<span class="Comment"># like find-next, but searches for multiple characters</span> +<span class="Comment"># fairly dumb algorithm</span> +<span class="muRecipe">recipe</span> find-substring [ + <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> + text:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + pattern:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + idx:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + first:character<span class="Special"> <- </span>index pattern:address:array:character/deref, <span class="Constant">0:literal</span> + <span class="Comment"># repeatedly check for match at current idx</span> + len:number<span class="Special"> <- </span>length text:address:array:character/deref + <span class="Delimiter">{</span> + <span class="Comment"># does some unnecessary work checking for substrings even when there isn't enough of text left</span> + done?:boolean<span class="Special"> <- </span>greater-or-equal idx:number, len:number + <span class="muControl">break-if</span> done?:boolean + found?:boolean<span class="Special"> <- </span>match-at text:address:array:character pattern:address:array:character, idx:number + <span class="muControl">break-if</span> found?:boolean + idx:number<span class="Special"> <- </span>add idx:number, <span class="Constant">1:literal</span> + <span class="Comment"># optimization: skip past indices that definitely won't match</span> + idx:number<span class="Special"> <- </span>find-next text:address:array:character, first:character, idx:number + <span class="muControl">loop</span> + <span class="Delimiter">}</span> + <span class="muControl">reply</span> idx:number +] + +<span class="muScenario">scenario</span> find-substring-1 [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[bc]</span> + 3:number<span class="Special"> <- </span>find-substring 1:address:array:character, 2:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>1 + ] +] + +<span class="muScenario">scenario</span> find-substring-2 [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abcd]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[bc]</span> + 3:number<span class="Special"> <- </span>find-substring 1:address:array:character, 2:address:array:character, <span class="Constant">1:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>1 + ] +] + +<span class="muScenario">scenario</span> find-substring-no-match [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[bd]</span> + 3:number<span class="Special"> <- </span>find-substring 1:address:array:character, 2:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>3 <span class="Comment"># not found</span> + ] +] + +<span class="muScenario">scenario</span> find-substring-suffix-match [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abcd]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[cd]</span> + 3:number<span class="Special"> <- </span>find-substring 1:address:array:character, 2:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>2 + ] +] + +<span class="muScenario">scenario</span> find-substring-suffix-match-2 [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abcd]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[cde]</span> + 3:number<span class="Special"> <- </span>find-substring 1:address:array:character, 2:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>4 <span class="Comment"># not found</span> + ] +] + +<span class="Comment"># result:boolean <- match-at text:address:array:character, pattern:address:array:character, idx:number</span> +<span class="Comment"># checks if substring matches at index 'idx'</span> +<span class="muRecipe">recipe</span> match-at [ + <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> + text:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + pattern:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + idx:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + pattern-len:number<span class="Special"> <- </span>length pattern:address:array:character/deref + <span class="Comment"># check that there's space left for the pattern</span> + <span class="Delimiter">{</span> + x:number<span class="Special"> <- </span>length text:address:array:character/deref + x:number<span class="Special"> <- </span>subtract x:number, pattern-len:number + enough-room?:boolean<span class="Special"> <- </span>lesser-or-equal idx:number, x:number + <span class="muControl">break-if</span> enough-room?:boolean + <span class="muControl">reply</span> <span class="Constant">0:literal/not-found</span> + <span class="Delimiter">}</span> + <span class="Comment"># check each character of pattern</span> + pattern-idx:number<span class="Special"> <- </span>copy <span class="Constant">0:literal</span> + <span class="Delimiter">{</span> + done?:boolean<span class="Special"> <- </span>greater-or-equal pattern-idx:number, pattern-len:number + <span class="muControl">break-if</span> done?:boolean + c:character<span class="Special"> <- </span>index text:address:array:character/deref, idx:number + exp:character<span class="Special"> <- </span>index pattern:address:array:character/deref, pattern-idx:number + <span class="Delimiter">{</span> + match?:boolean<span class="Special"> <- </span>equal c:character, exp:character + <span class="muControl">break-if</span> match?:boolean + <span class="muControl">reply</span> <span class="Constant">0:literal/not-found</span> + <span class="Delimiter">}</span> + idx:number<span class="Special"> <- </span>add idx:number, <span class="Constant">1:literal</span> + pattern-idx:number<span class="Special"> <- </span>add pattern-idx:number, <span class="Constant">1:literal</span> + <span class="muControl">loop</span> + <span class="Delimiter">}</span> + <span class="muControl">reply</span> <span class="Constant">1:literal/found</span> +] + +<span class="muScenario">scenario</span> match-at-checks-substring-at-index [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[ab]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 2:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>1 <span class="Comment"># match found</span> + ] +] + +<span class="muScenario">scenario</span> match-at-reflexive [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 1:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>1 <span class="Comment"># match found</span> + ] +] + +<span class="muScenario">scenario</span> match-at-outside-bounds [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[a]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 2:address:array:character, <span class="Constant">4:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>0 <span class="Comment"># never matches</span> + ] +] + +<span class="muScenario">scenario</span> match-at-empty-pattern [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 2:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>1 <span class="Comment"># always matches empty pattern given a valid index</span> + ] +] + +<span class="muScenario">scenario</span> match-at-empty-pattern-outside-bound [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 2:address:array:character, <span class="Constant">4:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>0 <span class="Comment"># no match</span> + ] +] + +<span class="muScenario">scenario</span> match-at-empty-text [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 2:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>0 <span class="Comment"># no match</span> + ] +] + +<span class="muScenario">scenario</span> match-at-empty-against-empty [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 1:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>1 <span class="Comment"># matches because pattern is also empty</span> + ] +] + +<span class="muScenario">scenario</span> match-at-inside-bounds [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[bc]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 2:address:array:character, <span class="Constant">1:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>1 <span class="Comment"># matches inner substring</span> + ] +] + +<span class="muScenario">scenario</span> match-at-inside-bounds-2 [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>new <span class="Constant">[bc]</span> + 3:boolean<span class="Special"> <- </span>match-at 1:address:array:character, 2:address:array:character, <span class="Constant">0:literal</span> + ] + memory-should-contain [ + 3<span class="Special"> <- </span>0 <span class="Comment"># no match</span> + ] +] + +<span class="Comment"># result:address:array:address:array:character <- split s:address:array:character, delim:character</span> +<span class="muRecipe">recipe</span> split [ + <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> + s:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + delim:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + <span class="Comment"># empty string? return empty array</span> + len:number<span class="Special"> <- </span>length s:address:array:character/deref + <span class="Delimiter">{</span> + empty?:boolean<span class="Special"> <- </span>equal len:number, <span class="Constant">0:literal</span> + <span class="muControl">break-unless</span> empty?:boolean + result:address:array:address:array:character<span class="Special"> <- </span>new location:type, <span class="Constant">0:literal</span> + <span class="muControl">reply</span> result:address:array:address:array:character + <span class="Delimiter">}</span> + <span class="Comment"># count #pieces we need room for</span> + count:number<span class="Special"> <- </span>copy <span class="Constant">1:literal</span> <span class="Comment"># n delimiters = n+1 pieces</span> + idx:number<span class="Special"> <- </span>copy <span class="Constant">0:literal</span> + <span class="Delimiter">{</span> + idx:number<span class="Special"> <- </span>find-next s:address:array:character, delim:character, idx:number + done?:boolean<span class="Special"> <- </span>greater-or-equal idx:number, len:number + <span class="muControl">break-if</span> done?:boolean + idx:number<span class="Special"> <- </span>add idx:number, <span class="Constant">1:literal</span> + count:number<span class="Special"> <- </span>add count:number, <span class="Constant">1:literal</span> + <span class="muControl">loop</span> + <span class="Delimiter">}</span> + <span class="Comment"># allocate space</span> + result:address:array:address:array:character<span class="Special"> <- </span>new location:type, count:number + <span class="Comment"># repeatedly copy slices start..end until delimiter into result[curr-result]</span> + curr-result:number<span class="Special"> <- </span>copy <span class="Constant">0:literal</span> + start:number<span class="Special"> <- </span>copy <span class="Constant">0:literal</span> + <span class="Delimiter">{</span> + <span class="Comment"># while next delim exists</span> + done?:boolean<span class="Special"> <- </span>greater-or-equal start:number, len:number + <span class="muControl">break-if</span> done?:boolean + end:number<span class="Special"> <- </span>find-next s:address:array:character, delim:character, start:number + <span class="Comment"># copy start..end into result[curr-result]</span> + dest:address:address:array:character<span class="Special"> <- </span>index-address result:address:array:address:array:character/deref, curr-result:number + dest:address:address:array:character/deref<span class="Special"> <- </span>string-copy s:address:array:character, start:number, end:number + <span class="Comment"># slide over to next slice</span> + start:number<span class="Special"> <- </span>add end:number, <span class="Constant">1:literal</span> + curr-result:number<span class="Special"> <- </span>add curr-result:number, <span class="Constant">1:literal</span> + <span class="muControl">loop</span> + <span class="Delimiter">}</span> + <span class="muControl">reply</span> result:address:array:address:array:character +] + +<span class="muScenario">scenario</span> string-split-1 [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[a/b]</span> + 2:address:array:address:array:character<span class="Special"> <- </span>split 1:address:array:character, <span class="Constant">47:literal/slash</span> + 3:number<span class="Special"> <- </span>length 2:address:array:address:array:character/deref + 4:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">0:literal</span> + 5:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">1:literal</span> + 10:array:character<span class="Special"> <- </span>copy 4:address:array:character/deref + 20:array:character<span class="Special"> <- </span>copy 5:address:array:character/deref + ] + memory-should-contain [ + 3<span class="Special"> <- </span>2 <span class="Comment"># length of result</span> + 10:string<span class="Special"> <- </span><span class="Constant">[a]</span> + 20:string<span class="Special"> <- </span><span class="Constant">[b]</span> + ] +] + +<span class="muScenario">scenario</span> string-split-2 [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[a/b/c]</span> + 2:address:array:address:array:character<span class="Special"> <- </span>split 1:address:array:character, <span class="Constant">47:literal/slash</span> + 3:number<span class="Special"> <- </span>length 2:address:array:address:array:character/deref + 4:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">0:literal</span> + 5:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">1:literal</span> + 6:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">2:literal</span> + 10:array:character<span class="Special"> <- </span>copy 4:address:array:character/deref + 20:array:character<span class="Special"> <- </span>copy 5:address:array:character/deref + 30:array:character<span class="Special"> <- </span>copy 6:address:array:character/deref + ] + memory-should-contain [ + 3<span class="Special"> <- </span>3 <span class="Comment"># length of result</span> + 10:string<span class="Special"> <- </span><span class="Constant">[a]</span> + 20:string<span class="Special"> <- </span><span class="Constant">[b]</span> + 30:string<span class="Special"> <- </span><span class="Constant">[c]</span> + ] +] + +<span class="muScenario">scenario</span> string-split-missing [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:address:array:character<span class="Special"> <- </span>split 1:address:array:character, <span class="Constant">47:literal/slash</span> + 3:number<span class="Special"> <- </span>length 2:address:array:address:array:character/deref + 4:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">0:literal</span> + 10:array:character<span class="Special"> <- </span>copy 4:address:array:character/deref + ] + memory-should-contain [ + 3<span class="Special"> <- </span>1 <span class="Comment"># length of result</span> + 10:string<span class="Special"> <- </span><span class="Constant">[abc]</span> + ] +] + +<span class="muScenario">scenario</span> string-split-empty [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[]</span> + 2:address:array:address:array:character<span class="Special"> <- </span>split 1:address:array:character, <span class="Constant">47:literal/slash</span> + 3:number<span class="Special"> <- </span>length 2:address:array:address:array:character/deref + ] + memory-should-contain [ + 3<span class="Special"> <- </span>0 <span class="Comment"># empty result</span> + ] +] + +<span class="muScenario">scenario</span> string-split-empty-piece [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[a/b//c]</span> + 2:address:array:address:array:character<span class="Special"> <- </span>split 1:address:array:character, <span class="Constant">47:literal/slash</span> + 3:number<span class="Special"> <- </span>length 2:address:array:address:array:character/deref + 4:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">0:literal</span> + 5:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">1:literal</span> + 6:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">2:literal</span> + 7:address:array:character<span class="Special"> <- </span>index 2:address:array:address:array:character/deref, <span class="Constant">3:literal</span> + 10:array:character<span class="Special"> <- </span>copy 4:address:array:character/deref + 20:array:character<span class="Special"> <- </span>copy 5:address:array:character/deref + 30:array:character<span class="Special"> <- </span>copy 6:address:array:character/deref + 40:array:character<span class="Special"> <- </span>copy 7:address:array:character/deref + ] + memory-should-contain [ + 3<span class="Special"> <- </span>4 <span class="Comment"># length of result</span> + 10:string<span class="Special"> <- </span><span class="Constant">[a]</span> + 20:string<span class="Special"> <- </span><span class="Constant">[b]</span> + 30:string<span class="Special"> <- </span><span class="Constant">[]</span> + 40:string<span class="Special"> <- </span><span class="Constant">[c]</span> + ] +] + +<span class="Comment"># x:address:array:character, y:address:array:character <- split-first text:address:array:character, delim:character</span> +<span class="muRecipe">recipe</span> split-first [ + <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> + text:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + delim:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + <span class="Comment"># empty string? return empty strings</span> + len:number<span class="Special"> <- </span>length text:address:array:character/deref + <span class="Delimiter">{</span> + empty?:boolean<span class="Special"> <- </span>equal len:number, <span class="Constant">0:literal</span> + <span class="muControl">break-unless</span> empty?:boolean + x:address:array:character<span class="Special"> <- </span>new <span class="Constant">[]</span> + y:address:array:character<span class="Special"> <- </span>new <span class="Constant">[]</span> + <span class="muControl">reply</span> x:address:array:character, y:address:array:character + <span class="Delimiter">}</span> + idx:number<span class="Special"> <- </span>find-next text:address:array:character, delim:character, <span class="Constant">0:literal</span> + x:address:array:character<span class="Special"> <- </span>string-copy text:address:array:character, <span class="Constant">0:literal</span>, idx:number + idx:number<span class="Special"> <- </span>add idx:number, <span class="Constant">1:literal</span> + y:address:array:character<span class="Special"> <- </span>string-copy text:address:array:character, idx:number, len:number + <span class="muControl">reply</span> x:address:array:character, y:address:array:character +] + +<span class="muScenario">scenario</span> string-split-first [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[a/b]</span> + 2:address:array:character, 3:address:array:character<span class="Special"> <- </span>split-first 1:address:array:character, <span class="Constant">47:literal/slash</span> + 10:array:character<span class="Special"> <- </span>copy 2:address:array:character/deref + 20:array:character<span class="Special"> <- </span>copy 3:address:array:character/deref + ] + memory-should-contain [ + 10:string<span class="Special"> <- </span><span class="Constant">[a]</span> + 20:string<span class="Special"> <- </span><span class="Constant">[b]</span> + ] +] + +<span class="Comment"># result:address:array:character <- string-copy buf:address:array:character, start:number, end:number</span> +<span class="Comment"># todo: make this generic</span> +<span class="muRecipe">recipe</span> string-copy [ + <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> + buf:address:array:character<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + start:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + end:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + <span class="Comment"># if end is out of bounds, trim it</span> + len:number<span class="Special"> <- </span>length buf:address:array:character/deref + end:number<span class="Special"> <- </span>min len:number, end:number + <span class="Comment"># allocate space for result</span> + len:number<span class="Special"> <- </span>subtract end:number, start:number + result:address:array:character<span class="Special"> <- </span>new character:type, len:number + <span class="Comment"># copy start..end into result[curr-result]</span> + src-idx:number<span class="Special"> <- </span>copy start:number + dest-idx:number<span class="Special"> <- </span>copy <span class="Constant">0:literal</span> + <span class="Delimiter">{</span> + done?:boolean<span class="Special"> <- </span>greater-or-equal src-idx:number, end:number + <span class="muControl">break-if</span> done?:boolean + src:character<span class="Special"> <- </span>index buf:address:array:character/deref, src-idx:number + dest:address:character<span class="Special"> <- </span>index-address result:address:array:character/deref, dest-idx:number + dest:address:character/deref<span class="Special"> <- </span>copy src:character + src-idx:number<span class="Special"> <- </span>add src-idx:number, <span class="Constant">1:literal</span> + dest-idx:number<span class="Special"> <- </span>add dest-idx:number, <span class="Constant">1:literal</span> + <span class="muControl">loop</span> + <span class="Delimiter">}</span> + <span class="muControl">reply</span> result:address:array:character +] + +<span class="muScenario">scenario</span> string-copy-copies-substring [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>string-copy 1:address:array:character, <span class="Constant">1:literal</span>, <span class="Constant">3:literal</span> + 3:array:character<span class="Special"> <- </span>copy 2:address:array:character/deref + ] + memory-should-contain [ + 3:string<span class="Special"> <- </span><span class="Constant">[bc]</span> + ] +] + +<span class="muScenario">scenario</span> string-copy-out-of-bounds [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>string-copy 1:address:array:character, <span class="Constant">2:literal</span>, <span class="Constant">4:literal</span> + 3:array:character<span class="Special"> <- </span>copy 2:address:array:character/deref + ] + memory-should-contain [ + 3:string<span class="Special"> <- </span><span class="Constant">[c]</span> + ] +] + +<span class="muScenario">scenario</span> string-copy-out-of-bounds-2 [ + run [ + 1:address:array:character<span class="Special"> <- </span>new <span class="Constant">[abc]</span> + 2:address:array:character<span class="Special"> <- </span>string-copy 1:address:array:character, <span class="Constant">3:literal</span>, <span class="Constant">3:literal</span> + 3:array:character<span class="Special"> <- </span>copy 2:address:array:character/deref + ] + memory-should-contain [ + 3:string<span class="Special"> <- </span><span class="Constant">[]</span> + ] +] + +<span class="muRecipe">recipe</span> min [ + <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> + x:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + y:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + <span class="Delimiter">{</span> + return-x?:boolean<span class="Special"> <- </span>lesser-than x:number, y:number + <span class="muControl">break-if</span> return-x?:boolean + <span class="muControl">reply</span> y:number + <span class="Delimiter">}</span> + <span class="muControl">reply</span> x:number +] + +<span class="muRecipe">recipe</span> max [ + <span class="Constant">default-space</span>:address:array:location<span class="Special"> <- </span>new location:type, <span class="Constant">30:literal</span> + x:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + y:number<span class="Special"> <- </span><span class="Constant">next-ingredient</span> + <span class="Delimiter">{</span> + return-x?:boolean<span class="Special"> <- </span>greater-than x:number, y:number + <span class="muControl">break-if</span> return-x?:boolean + <span class="muControl">reply</span> y:number + <span class="Delimiter">}</span> + <span class="muControl">reply</span> x:number +] </pre> </body> </html> |