Fix multiple cond/slice bugs revealed by LotusRonin's new find testcase

This commit is contained in:
Nathan Braswell
2022-04-24 20:39:51 -04:00
parent 8a876a7b29
commit 8b3cab7a2f
2 changed files with 42 additions and 21 deletions

13
find.kp
View File

@@ -83,17 +83,16 @@
_find (rec-lambda _find (str sub i) (let (
len1 (len str)
len2 (len sub)
) (cond (> len2 len1) -1
(= len2 0) i
(and (> len1 0) (> len2 0))
(cond (compare_substr str sub) i
true (_find (cdr str) sub (+ i 1)))
true -1
) (cond (> len2 len1) -1
(= len2 0) i
(and (> len1 0) (> len2 0)) (cond (compare_substr str sub) i
true (_find (cdr str) sub (+ i 1)))
true -1
)))
; find the index of a substr in a str
; check if a substr is in a str
find (lambda (str sub) (_find str sub 0))
contains(lambda (str sub) (!= (find str sub) -1))
contains (lambda (str sub) (!= (find str sub) -1))
monad (array 'write 1 "testing find funcs: \n" (vau (written code)
(array 'write 1 "find in \"foobar\" the string \"oba\"\n" (vau (written code)