Anonymous user
Strip comments from a string: Difference between revisions
m
→{{header|Forth}}
Line 623:
Tested with GForth on Windows
<LANG FORTH>\ Rosetta Code Strip Comment
: LASTCHAR ( addr len -- addr len c) 2DUP + C@ ;▼
▲: LASTCHAR ( addr len -- addr len c) 2DUP + C@ ;
: COMMENT? ( char -- ? ) S" #;" ROT SCAN NIP ; \
: -COMMENT ( addr len -- addr len') \ removes # or ; comments
1-
BEGIN
LASTCHAR COMMENT? 0=
WHILE \ while
1- \ reduce length by 1
REPEAT
1- ; \ remove 1 more (the comment char)
: -TRAILING ( adr len --
1-
BEGIN
LASTCHAR BL =
WHILE
1-
REPEAT
1+ ;
▲\ added SKIP to remove leading space characters
▲: COMMENT-STRIP ( addr len -- addr 'len) -COMMENT -TRAILING 2DUP BL SKIP ;</LANG>
Tested at the Forth console
|