Fix D<, COMPARE, add -TRAILING — double 27→16, string 17→13

- D< used D- D0< which overflows for extreme signed doubles.
  Replaced with high-cell comparison + unsigned low-cell comparison.
- COMPARE had inverted sign for length difference (u2-u1 vs u1-u2).
- Added -TRAILING (removed during Phase 6 refactoring, never re-added).
This commit is contained in:
2026-04-08 10:52:20 +02:00
parent 4719f50925
commit 9ffbaa5428
+7 -2
View File
@@ -98,7 +98,9 @@
: D= D- D0= ; : D= D- D0= ;
\ D< ( d1 d2 -- flag ) true if d1 < d2 (signed) \ D< ( d1 d2 -- flag ) true if d1 < d2 (signed)
: D< D- D0< ; \ Cannot use D- D0< because subtraction overflows for extreme values.
\ Compare high cells first (signed); if equal, compare low cells unsigned.
: D< ROT 2DUP = IF 2DROP U< ELSE 2SWAP 2DROP > THEN ;
\ D2* ( d -- d*2 ) double-cell shift left \ D2* ( d -- d*2 ) double-cell shift left
: D2* 2DUP D+ ; : D2* 2DUP D+ ;
@@ -255,7 +257,7 @@
\ COMPARE ( addr1 u1 addr2 u2 -- n ) compare two strings lexicographically \ COMPARE ( addr1 u1 addr2 u2 -- n ) compare two strings lexicographically
: COMPARE : COMPARE
ROT 2DUP - >R ROT 2DUP SWAP - >R
MIN 0 ?DO MIN 0 ?DO
OVER I + C@ OVER I + C@
OVER I + C@ OVER I + C@
@@ -270,6 +272,9 @@
0> IF 1 EXIT THEN 0> IF 1 EXIT THEN
0 ; 0 ;
\ -TRAILING ( c-addr u1 -- c-addr u2 ) remove trailing spaces
: -TRAILING BEGIN DUP WHILE 2DUP + 1- C@ BL = WHILE 1- REPEAT THEN ;
\ SEARCH stays as a host function (complex multi-line control flow). \ SEARCH stays as a host function (complex multi-line control flow).
\ --------------------------------------------------------------- \ ---------------------------------------------------------------