dos_compilers/DX-FORTH v430/STRINGS.SCR

1 line
5.0 KiB
Plaintext
Raw Normal View History

2024-07-09 18:07:02 +02:00
\ Information A simple string handling package SPLIT split string at character C+STRING append char to end of string C/STRING extract char from start of string STRING/ return right-most characters S= test two strings for equality S+ concatenate two strings; result in temp buffer These only work with string variables: STRING create string variable (255 chars max) S! store string to string variable \ Strings - Load screen forth definitions decimal application cr .( loading Strings ) 2 #screens 1- thru \ Strings - SPLIT STRING+C C/STRING STRING/ \ Split string at character leaving first on top : SPLIT ( a u char -- a2 u2 a3 u3 ) >r 2dup r> scan 2swap 2 pick - ; \ Append character to end of string : C+STRING ( char a u -- a2 u2 ) 2dup 2r> + c! 2r> 1+ ; \ Extract character from start of string : C/STRING ( a u -- a2 u2 char ) over >r 1 /string r> c@ ; \ Return u right-most characters of string : STRING/ ( a1 u1 u -- a2 u2 ) over umin over swap - /string ; \ Compare two strings for equality : S= ( a1 u1 a2 u2 -- flag ) compare 0= ; \ Strings - S= S+ STRING S! 255 ( buffer size ) -? create SB dup , allot \ Concatenate two strings placing result in temp buffer : S+ ( a1 u1 a2 u2 -- a3 u3 ) 2>r sb @ umin sb cell+ 0 +string sb @ over - 2r> rot min 2swap +string ; behead sb sb \ Define string variable with max length u -? : STRING create ( u -- ) 255 min dup c, 0 c, allot does> ( -- sa su ) 1+ count ; \ Store string a u to string variable : S! ( a u sa su -- ) drop 1- dup >r 1- c@ min r> place ;