feat(strictness): subs validates bounds; assoc! bounds-checks vector index

- subs requires a string and validates 0<=start<=end<=count (no Janet
  from-end/clamping); negative/out-of-range/nil indices throw
- assoc! on a transient vector bounds-checks the index (0..count)

subs 11-fail -> 24/5 (5 remaining are byte-vs-codepoint Unicode, platform);
assoc_bang 32/6 -> 35/3. clojure-test-suite pass 3889->3898.
spec: string/subs-strictness (7), transient/assoc!-bounds (4). jpm test green.
This commit is contained in:
Yogthos 2026-06-05 14:07:14 -04:00
parent 6544d8ef44
commit 740b50aef3
4 changed files with 35 additions and 6 deletions

View file

@ -1614,10 +1614,20 @@
(def core-subs
(fn [& args]
(case (length args)
2 (string/slice (args 0) (args 1))
3 (string/slice (args 0) (args 1) (args 2))
(error "Wrong number of args passed to: subs"))))
(when (not (or (= 2 (length args)) (= 3 (length args))))
(error "Wrong number of args passed to: subs"))
(let [s (args 0)
start (get args 1)]
(when (not (string? s)) (error (string "subs requires a string, got " (type s))))
(let [len (length s)
end (if (= 3 (length args)) (args 2) len)]
# Clojure validates bounds (no negative/from-end/clamping like Janet):
# 0 <= start <= end <= (count s).
(when (not (and (number? start) (number? end)
(= start (math/floor start)) (= end (math/floor end))
(>= start 0) (<= start end) (<= end len)))
(error "String index out of range"))
(string/slice s start end)))))
# ============================================================
# I/O — minimal wrappers
@ -3346,7 +3356,10 @@
(defn- tr-assoc! [t k v]
(tr-check-active! t)
(case (t :kind)
:vector (let [a (t :arr)] (if (= k (length a)) (array/push a v) (put a k v)))
:vector (let [a (t :arr)]
(when (not (and (number? k) (= k (math/floor k)) (>= k 0) (<= k (length a))))
(error (string "Index " k " out of bounds for assoc! on a transient vector of length " (length a))))
(if (= k (length a)) (array/push a v) (put a k v)))
:map (put (t :tbl) (canon-key k) @[k v])
(error "assoc! expects a transient vector or map"))
t)