flan/test/programs/string-traps.flan

26 lines
1.1 KiB
Plaintext

;;;; What a String refuses at run time, one per run: the argument chooses.
;;;; Bytes that are not UTF-8, reached through a str, stop the program at the
;;;; append; so does a code point with no encoding; a character position past
;;;; the end signals BoundsError counted in characters; and a text builder
;;;; given bytes that are not UTF-8 stops at the call that asked for it, or,
;;;; called through a function value, at its own check in the prelude. The
;;;; test asserts the line and column of each.
(defonce bad [2 u8])
(defn main [args [str]] i32
(let [which (if (> (length args) 1) (i32 (bytes->i64 (bytes-view (at args 1)))) 0)
s (string-new "日本")]
(set (at bad 0) 0xc3)
(set (at bad 1) 0x28)
(println "before")
(cond
(= which 0) (append s (str (slice bad)))
(= which 1) (append s (+ 0xd800 which -1))
(= which 2) (insert s 3 "x")
(= which 3) (println (remove s 2))
(= which 5) (println (to-lower (slice bad)))
(= which 6) (let [f to-lower] (println (f (slice bad))))
:else (append s (bytes->string (let [v (vec-new u8)] (push v 0xff) v))))
(println s))
0)