\ ********************************************************************* 
\ STRINGS management for FlashForth 
\    Filename:      strings.txt 
\    Date:          12/10/2020 
\    Updated:       04/12/2020 
\    File Version:  1.0 
\    MCU:           ARDUINO all models 
\    Copyright:     Marc PETREMANN 
\    Author:        Marc PETREMANN 
\    GNU General Public License 
\ ********************************************************************* 
 
-strings 
marker -strings 
 
\ compare two strings 
\ n = 0 if strings are equals | otherwise n <> 0 
: compare ( str1 str2 --- n) 
    drop swap >r 
    begin 
        r> 1- >r 
        r@ 
    while 
        over r@ + c@   
        over r@ + c@ 
       <> 
        if 
            2drop  
            1  
            exit 
        then 
    repeat 
    r> drop 
    2drop 
    0 
  ; 
 
: $= ( addr1 len1 addr2 len2 --- fl) 
    compare 
    0 = 
    if      true 
    else    false   then 
  ; 
 
\ define a strvar 
: string  ( comp: n --- names_strvar | exec: --- addr len ) 
    flash 
    create 
        dup ic,         \ n 
        ram here i,     \ n 
        0 c,     0 c,   \ n 
        allot 
    does> 
        dup             \ addrf addrf 
        c@              \ addrf maxlen 
        over            \ addrf maxlen addrf 
        1+              \ addrf maxlen addrf+1 
        @               \ addrf maxlen addrm 
        c!              \ addrf 
        1+              \ addrf+1 
        @               \ addrm 
        2 +             \ addrm+2 
        dup             \ addrm+2 addrm+2 
        1-              \ addrm+2 addrm+1 
        c@              \ addrm+2 currlen 
; 
 
\ define a strvar - for other FORTH version 
\ : string  ( comp: n --- names_strvar | exec: --- addr len ) 
\     create 
\         dup 
\         c,      \ n is maxlength 
\         0 c,    \ 0 is real length 
\         allot 
\     does> 
\         2 + 
\         dup 1 - c@ 
\   ; 
 
\ store str into strvar 
: $!  ( addr1 len1 addrvar lenvar ---) 
    drop 
    dup 2- c@       \ get maxlength of strvar 
    rot min         \ keep min length 
    2dup swap 1- c! \ store real length 
    cmove           \ copy string 
  ; 
 
 
\ used for shell 
\ eof 
 
*** Example: ********** 
: constStr 
    s" this is constant string" ; 
 
\ create variable varStr 
32 string varStr 
 
\ copy constStr into varStr 
constStr varStr $! 
 
varStr type \ display: this is constant string