diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-02-07 21:31:35 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-02-07 21:31:35 -0800 |
commit | 50ec8698320239deb0a5a24f9efc2bcdae425a58 (patch) | |
tree | 77cd798b295080418f5a36556c60c5c2770a6f4e | |
parent | 7962e0fd085e408e24201ca3951c48e17aec7411 (diff) | |
download | mu-50ec8698320239deb0a5a24f9efc2bcdae425a58.tar.gz |
712
-rw-r--r-- | mu.arc | 20 | ||||
-rw-r--r-- | mu.arc.t | 14 |
2 files changed, 34 insertions, 0 deletions
diff --git a/mu.arc b/mu.arc index a78f80c9..5d81a7b8 100644 --- a/mu.arc +++ b/mu.arc @@ -2140,6 +2140,26 @@ (reply result:string-address-array-address) ) +(init-fn split-first ; string, character -> string, string + (default-space:space-address <- new space:literal 30:literal) + (s:string-address <- next-input) + (delim:character <- next-input) ; todo: unicode chars + ; empty string? return empty array + (len:integer <- length s:string-address/deref) + { begin + (empty?:boolean <- equal len:integer 0:literal) + (break-unless empty?:boolean) + (x:string-address <- new "") + (y:string-address <- new "") + (reply x:string-address y:string-address) + } + (idx:integer <- find-next s:string-address delim:character 0:literal) + (x:string-address <- string-copy s:string-address 0:literal idx:integer) + (idx:integer <- add idx:integer 1:literal) + (y:string-address <- string-copy s:string-address idx:integer len:integer) + (reply x:string-address y:string-address) +) + ; todo: make this generic (init-fn string-copy ; buf start end -> address of new array (default-space:space-address <- new space:literal 30:literal) diff --git a/mu.arc.t b/mu.arc.t index df439e13..622ea7a9 100644 --- a/mu.arc.t +++ b/mu.arc.t @@ -4357,6 +4357,20 @@ ;? (quit) ;? 1 (reset) +(new-trace "string-split-first") +(add-code + '((function main [ + (1:string-address <- new "a/b") + (2:string-address 3:string-address <- split-first 1:string-address ((#\/ literal))) + ]))) +(run 'main) +(each routine completed-routines* + (aif rep.routine!error (prn "error - " it))) +(when (or (~memory-contains-array memory*.2 "a") + (~memory-contains-array memory*.3 "b")) + (prn "F - 'split-first' cuts string at first occurrence of delimiter")) + +(reset) (new-trace "integer-to-decimal-string") (add-code '((function main [ |