From b2810db1a0a0898b69a8568cc4cc857354a1be63 Mon Sep 17 00:00:00 2001 From: giles Date: Fri, 24 Apr 2026 13:23:08 +0000 Subject: [PATCH] =?UTF-8?q?js-on-sx:=20strip=20leading=20zeros=20from=20ex?= =?UTF-8?q?ponent=20in=20num=E2=86=92string=20(+3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SX's (str 1e-7) gives "1e-07" but JS spec is "1e-7" — no padding, no leading zeros in the exponent (sign stays). We stepped through: mant "e" expraw → mant "e" (sign (strip-zeros body)) Added four small helpers: js-normalize-num-str, js-split-sign, js-strip-leading-zeros, js-strip-zeros-loop. All pure string walkers. Unit 521/522, slice 148/148 unchanged. String 40 → 42, Number 75 → 76 (+3 total). Fixes S9.8.1_A9_T1, fromCharCode/S9.7_A3.1_T1..T2 family. --- lib/js/runtime.sx | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/js/runtime.sx b/lib/js/runtime.sx index ecfd6c87..e7fd4ebc 100644 --- a/lib/js/runtime.sx +++ b/lib/js/runtime.sx @@ -1195,7 +1195,52 @@ ((js-number-is-nan n) "NaN") ((= n (js-infinity-value)) "Infinity") ((= n (- 0 (js-infinity-value))) "-Infinity") - (else (str n))))) + (else (js-normalize-num-str (str n)))))) + +(define + js-normalize-num-str + (fn + (s) + (let + ((ei (js-find-exp-char s))) + (if + (< ei 0) + s + (let + ((mant (js-string-slice s 0 ei)) + (expraw (js-string-slice s (+ ei 1) (len s)))) + (str mant "e" (js-strip-leading-zeros expraw))))))) + +(define + js-strip-leading-zeros + (fn + (s) + (let + ((sign-and-body (js-split-sign s))) + (let + ((sign (nth sign-and-body 0)) (body (nth sign-and-body 1))) + (let + ((stripped (js-strip-zeros-loop body 0 (len body)))) + (if (= stripped "") (str sign "0") (str sign stripped))))))) + +(define + js-split-sign + (fn + (s) + (cond + ((= s "") (list "" "")) + ((= (char-at s 0) "-") (list "-" (js-string-slice s 1 (len s)))) + ((= (char-at s 0) "+") (list "" (js-string-slice s 1 (len s)))) + (else (list "" s))))) + +(define + js-strip-zeros-loop + (fn + (s i n) + (cond + ((>= i n) "") + ((= (char-at s i) "0") (js-strip-zeros-loop s (+ i 1) n)) + (else (js-string-slice s i n))))) (define js-add