diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2015-02-04 23:28:50 -0800 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2015-02-04 23:28:50 -0800 |
commit | ee50fd157c110c9febdd7676ae3e615a7598579d (patch) | |
tree | c5ce808acf0f97af8f03793f81c65545bced6d66 | |
parent | 22a3a0073c0c8ebecf7607a6cbbb14729edb5c51 (diff) | |
download | mu-ee50fd157c110c9febdd7676ae3e615a7598579d.tar.gz |
706
-rw-r--r-- | mu.arc | 30 | ||||
-rw-r--r-- | mu.arc.t | 83 |
2 files changed, 113 insertions, 0 deletions
diff --git a/mu.arc b/mu.arc index 236024b5..a1d7c2f0 100644 --- a/mu.arc +++ b/mu.arc @@ -1891,6 +1891,36 @@ (result:boolean <- equal full:integer curr:integer) (reply result:boolean)) +(init-fn string-equal + (default-space:space-address <- new space:literal 30:literal) + (a:string-address <- next-input) + (a-len:integer <- length a:string-address/deref) + (b:string-address <- next-input) + (b-len:integer <- length b:string-address/deref) + ; compare lengths + { begin + (length-equal?:boolean <- equal a-len:integer b-len:integer) + (break-if length-equal?:boolean) + (reply nil:literal) + } + ; compare each corresponding byte + (i:integer <- copy 0:literal) + { begin + (done?:boolean <- greater-or-equal i:integer a-len:integer) + (break-if done?:boolean) + (a-char:byte <- index a:string-address/deref i:integer) + (b-char:byte <- index b:string-address/deref i:integer) + { begin + (chars-match?:boolean <- equal a-char:byte b-char:byte) + (break-if chars-match?:boolean) + (reply nil:literal) + } + (i:integer <- add i:integer 1:literal) + (loop) + } + (reply t:literal) +) + (init-fn strcat (default-space:space-address <- new space:literal 30:literal) ; result = new string[a.length + b.length] diff --git a/mu.arc.t b/mu.arc.t index a1fd7b3f..f529f72e 100644 --- a/mu.arc.t +++ b/mu.arc.t @@ -4000,6 +4000,89 @@ (prn "F - 'new' initializes allocated memory to string literal")))) (reset) +(new-trace "string-equal") +(add-code + '((function main [ + (1:string-address <- new "hello") + (2:string-address <- new "hello") + (3:boolean <- string-equal 1:string-address 2:string-address) + ]))) +(run 'main) +(when (~iso memory*.3 t) + (prn "F - 'string-equal'")) + +(reset) +(new-trace "string-equal-empty") +(add-code + '((function main [ + (1:string-address <- new "") + (2:string-address <- new "") + (3:boolean <- string-equal 1:string-address 2:string-address) + ]))) +(run 'main) +(when (~iso memory*.3 t) + (prn "F - 'string-equal' works on empty strings")) + +(reset) +(new-trace "string-equal-compare-with-empty") +(add-code + '((function main [ + (1:string-address <- new "a") + (2:string-address <- new "") + (3:boolean <- string-equal 1:string-address 2:string-address) + ]))) +(run 'main) +(when (~iso memory*.3 nil) + (prn "F - 'string-equal' compares correctly with empty strings")) + +(reset) +(new-trace "string-equal-compares-length") +(add-code + '((function main [ + (1:string-address <- new "a") + (2:string-address <- new "ab") + (3:boolean <- string-equal 1:string-address 2:string-address) + ]))) +(run 'main) +(when (~iso memory*.3 nil) + (prn "F - 'string-equal' handles differing lengths")) + +(reset) +(new-trace "string-equal-compares-initial-element") +(add-code + '((function main [ + (1:string-address <- new "aa") + (2:string-address <- new "ba") + (3:boolean <- string-equal 1:string-address 2:string-address) + ]))) +(run 'main) +(when (~iso memory*.3 nil) + (prn "F - 'string-equal' handles inequal final byte")) + +(reset) +(new-trace "string-equal-compares-final-element") +(add-code + '((function main [ + (1:string-address <- new "ab") + (2:string-address <- new "aa") + (3:boolean <- string-equal 1:string-address 2:string-address) + ]))) +(run 'main) +(when (~iso memory*.3 nil) + (prn "F - 'string-equal' handles inequal final byte")) + +(reset) +(new-trace "string-equal-reflexive") +(add-code + '((function main [ + (1:string-address <- new "ab") + (3:boolean <- string-equal 1:string-address 1:string-address) + ]))) +(run 'main) +(when (~iso memory*.3 t) + (prn "F - 'string-equal' handles identical pointer")) + +(reset) (new-trace "strcat") (add-code '((function main [ |