From f45ea0cb5c4109bc573da1d0b2289b451a29b068 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 19 Dec 2014 18:57:11 -0800 Subject: [PATCH] 441 - string 'find-next' for characters --- mu.arc | 16 +++++++++++ mu.arc.t | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/mu.arc b/mu.arc index 72d47469..bb7cd6f2 100644 --- a/mu.arc +++ b/mu.arc @@ -1456,6 +1456,22 @@ } (reply result:string-address)) +(init-fn find-next ; string, character, index -> next index + (s:string-address <- next-input) + (needle:character <- next-input) ; todo: unicode chars + (idx:integer <- next-input) + (len:integer <- length s:string-address/deref) + { begin + (eof?:boolean <- greater-or-equal idx:integer len:integer) + (break-if eof?:boolean) + (curr:byte <- index s:string-address/deref idx:integer) + (found?:boolean <- equal curr:byte needle:character) + (break-if found?:boolean) + (idx:integer <- add idx:integer 1:literal) + (loop) + } + (reply idx:integer)) + ) ; section 100 for system software ;; load all provided files and start at 'main' diff --git a/mu.arc.t b/mu.arc.t index 1c5cc132..2e284b7d 100644 --- a/mu.arc.t +++ b/mu.arc.t @@ -3321,6 +3321,94 @@ (if (~memory-contains-array memory*.5 "hello, abc, def, and ghi!") (prn "F - 'interpolate' splices in any number of strings")) +(reset) +(new-trace "string-find-next") +(add-code '((function main [ + (1:string-address <- new "a/b") + (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal) + ]))) +(run 'main) +(if (~is memory*.2 1) + (prn "F - 'find-next' finds first location of a character")) + +(reset) +(new-trace "string-find-next-empty") +(add-code '((function main [ + (1:string-address <- new "") + (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal) + ]))) +(run 'main) +(each routine completed-routines* + (aif rep.routine!error (prn "error - " it))) +(if (~is memory*.2 0) + (prn "F - 'find-next' finds first location of a character")) + +(reset) +(new-trace "string-find-next-initial") +(add-code '((function main [ + (1:string-address <- new "/abc") + (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal) + ]))) +(run 'main) +(if (~is memory*.2 0) + (prn "F - 'find-next' handles prefix match")) + +(reset) +(new-trace "string-find-next-final") +(add-code '((function main [ + (1:string-address <- new "abc/") + (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal) + ]))) +(run 'main) +;? (prn memory*.2) +(if (~is memory*.2 3) + (prn "F - 'find-next' handles suffix match")) + +(reset) +(new-trace "string-find-next-missing") +(add-code '((function main [ + (1:string-address <- new "abc") + (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal) + ]))) +(run 'main) +;? (prn memory*.2) +(if (~is memory*.2 3) + (prn "F - 'find-next' handles no match")) + +(reset) +(new-trace "string-find-next-invalid-index") +(add-code '((function main [ + (1:string-address <- new "abc") + (2:integer <- find-next 1:string-address ((#\/ literal)) 4:literal) + ]))) +;? (= dump-trace* (obj whitelist '("run"))) +(run 'main) +(each routine completed-routines* + (aif rep.routine!error (prn "error - " it))) +;? (prn memory*.2) +(if (~is memory*.2 4) + (prn "F - 'find-next' skips invalid index (past end of string)")) + +(reset) +(new-trace "string-find-next-first") +(add-code '((function main [ + (1:string-address <- new "ab/c/") + (2:integer <- find-next 1:string-address ((#\/ literal)) 0:literal) + ]))) +(run 'main) +(if (~is memory*.2 2) + (prn "F - 'find-next' finds first of multiple options")) + +(reset) +(new-trace "string-find-next-second") +(add-code '((function main [ + (1:string-address <- new "ab/c/") + (2:integer <- find-next 1:string-address ((#\/ literal)) 3:literal) + ]))) +(run 'main) +(if (~is memory*.2 4) + (prn "F - 'find-next' finds second of multiple options")) + ) ; section 100 for string utilities (reset)