HS pick runtime: guard nil inputs so pick first/last/items/match/matches don't hang
- hs-pick-first/last/random/items/slice: short-circuit nil or non-list (strings flow through unchanged). - New hs-pick-match / hs-pick-matches wrappers around regex-match / regex-find-all, also nil-safe; compiler routes pick-match / pick-matches through them. Unblocks 'pick first from null returns null' and 'pick match from null returns null' which previously looped past step_limit.
This commit is contained in:
@@ -818,7 +818,7 @@
|
||||
(quote set!)
|
||||
(quote it)
|
||||
(list
|
||||
(quote regex-match)
|
||||
(quote hs-pick-match)
|
||||
(hs-to-sx (nth ast 1))
|
||||
(hs-to-sx (nth ast 2)))))
|
||||
((= head (quote pick-matches))
|
||||
@@ -826,7 +826,7 @@
|
||||
(quote set!)
|
||||
(quote it)
|
||||
(list
|
||||
(quote regex-find-all)
|
||||
(quote hs-pick-matches)
|
||||
(hs-to-sx (nth ast 1))
|
||||
(hs-to-sx (nth ast 2)))))
|
||||
((= head (quote prop-is))
|
||||
|
||||
@@ -1767,37 +1767,67 @@
|
||||
hs-slice
|
||||
(fn
|
||||
(col start end)
|
||||
(let
|
||||
((s (if (nil? start) 0 start))
|
||||
(e (if (nil? end) (len col) (+ end 1))))
|
||||
(slice col s e))))
|
||||
(cond
|
||||
((nil? col) nil)
|
||||
((not (list? col)) col)
|
||||
(true
|
||||
(let
|
||||
((s (if (nil? start) 0 start))
|
||||
(e (if (nil? end) (len col) (+ end 1))))
|
||||
(slice col s e))))))
|
||||
|
||||
(define
|
||||
hs-pick-first
|
||||
(fn
|
||||
(col n)
|
||||
(let ((m (if (< n (len col)) n (len col)))) (slice col 0 m))))
|
||||
(cond
|
||||
((nil? col) nil)
|
||||
((not (list? col)) col)
|
||||
(true
|
||||
(let ((m (if (< n (len col)) n (len col)))) (slice col 0 m))))))
|
||||
|
||||
(define
|
||||
hs-pick-last
|
||||
(fn
|
||||
(col n)
|
||||
(let
|
||||
((total (len col)))
|
||||
(let
|
||||
((start (if (< n total) (- total n) 0)))
|
||||
(slice col start total)))))
|
||||
(cond
|
||||
((nil? col) nil)
|
||||
((not (list? col)) col)
|
||||
(true
|
||||
(let
|
||||
((total (len col)))
|
||||
(let
|
||||
((start (if (< n total) (- total n) 0)))
|
||||
(slice col start total)))))))
|
||||
|
||||
(define
|
||||
hs-pick-random
|
||||
(fn
|
||||
(col n)
|
||||
(if
|
||||
(nil? n)
|
||||
(first col)
|
||||
(let ((m (if (< n (len col)) n (len col)))) (slice col 0 m)))))
|
||||
(cond
|
||||
((nil? col) nil)
|
||||
((not (list? col)) col)
|
||||
((nil? n) (first col))
|
||||
(true
|
||||
(let ((m (if (< n (len col)) n (len col)))) (slice col 0 m))))))
|
||||
|
||||
(define hs-pick-items (fn (col start end) (slice col start end)))
|
||||
(define
|
||||
hs-pick-items
|
||||
(fn
|
||||
(col start end)
|
||||
(cond
|
||||
((nil? col) nil)
|
||||
((not (list? col)) col)
|
||||
(true (slice col start end)))))
|
||||
|
||||
(define
|
||||
hs-pick-match
|
||||
(fn
|
||||
(regex haystack)
|
||||
(cond
|
||||
((nil? haystack) nil)
|
||||
((nil? regex) nil)
|
||||
(true (regex-match regex haystack)))))
|
||||
|
||||
(define
|
||||
hs-sorted-by
|
||||
|
||||
Reference in New Issue
Block a user