XC!+?

( xchar xc-addr1 u1 -- xc-addr2 u2 flag )

Stores the xchar into the string buffer specified by xc-addr1 u1. xc-addr2 u2 is the remaining string buffer. If the xchar did fit into the buffer, flag is true, otherwise flag is false, and xc-addr2 u2 equal xc-addr1 u1. XC!+? is safe for buffer overflows.

Implementation:

: XC!+? ( xchar xc-addr u -- xc-addr' u' flag )
   >R OVER XC-SIZE R@ OVER U< IF ( xchar xc-addr1 len r: u1 )
     \ not enough space
     DROP NIP R> FALSE
   ELSE
     >R XC!+ R> R> SWAP - TRUE
   THEN ;

Testing:

T{ $ffff PAD 4 XC!+? -> PAD 3 + 1 <TRUE> }T

ContributeContributions