441 - string 'find-next' for characters
This commit is contained in:
parent
4fc54b3d98
commit
f45ea0cb5c
16
mu.arc
16
mu.arc
|
@ -1456,6 +1456,22 @@
|
||||||
}
|
}
|
||||||
(reply result:string-address))
|
(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
|
) ; section 100 for system software
|
||||||
|
|
||||||
;; load all provided files and start at 'main'
|
;; load all provided files and start at 'main'
|
||||||
|
|
88
mu.arc.t
88
mu.arc.t
|
@ -3321,6 +3321,94 @@
|
||||||
(if (~memory-contains-array memory*.5 "hello, abc, def, and ghi!")
|
(if (~memory-contains-array memory*.5 "hello, abc, def, and ghi!")
|
||||||
(prn "F - 'interpolate' splices in any number of strings"))
|
(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
|
) ; section 100 for string utilities
|
||||||
|
|
||||||
(reset)
|
(reset)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user