lua: string.byte(s,i,j) supports ranges, returns multi-values
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled

This commit is contained in:
2026-04-25 01:10:30 +00:00
parent 702e7c8eac
commit fad44ca097
4 changed files with 53 additions and 34 deletions

View File

@@ -990,10 +990,28 @@
lua-string-byte
(fn (&rest args)
(let ((s (first args))
(i (if (> (len args) 1) (nth args 1) 1)))
(cond
((or (< i 1) (> i (len s))) nil)
(else (char-code (char-at s (- i 1))))))))
(i-raw (if (> (len args) 1) (nth args 1) nil))
(j-raw (if (> (len args) 2) (nth args 2) nil)))
(let ((i (if (= i-raw nil) 1 i-raw)))
(let ((j (if (= j-raw nil) i j-raw)))
(let ((slen (len s)))
(let ((ni (cond ((< i 0) (+ slen i 1)) ((= i 0) 1) (else i)))
(nj (cond ((< j 0) (+ slen j 1)) (else j))))
(cond
((or (< ni 1) (> ni slen) (< nj ni)) nil)
((= ni nj) (char-code (char-at s (- ni 1))))
(else
(let ((out (list (quote lua-multi))))
(begin
(define
b-loop
(fn (k)
(when (and (<= k nj) (<= k slen))
(begin
(set! out (append out (list (char-code (char-at s (- k 1))))))
(b-loop (+ k 1))))))
(b-loop ni)
out)))))))))))
(define
lua-char-one