read string literals into a dynamically resized transient buffer

This commit is contained in:
Jesse D. McDonald 2020-10-24 11:07:38 -05:00
parent 46211d5662
commit 89c2f4df3d
1 changed files with 18 additions and 9 deletions

View File

@ -1734,19 +1734,29 @@ CREATE TIB-LEFTOVER-BYTES 0 ,
ENDCASE
THEN ;
2VARIABLE STRING-BUFFER
0 0 STRING-BUFFER 2!
\ Read a literal character string up to the next double-quote character
\ Unlike WORD the string is stored in contiguous *allocated* data space
\ The string is stored in a transient buffer and will become invalid when
\ the next string is read; both the address and content may change
\ The delimiting double-quote character is removed from the input buffer
\ Double-quote and backslash characters can be escaped with a backslash
: READSTRING ( "ccc<doublequote>" -- c-addr u )
HERE
STRING-BUFFER 2@ 0
( S: addr length index )
BEGIN
PEEK-CHAR [CHAR] " <>
WHILE
ESCAPED-CHAR C,
2DUP <= IF
\ Grow the buffer by at least 50% + 16 bytes
\ Store the actual allocated object size, not the requested size
-ROT DUP 2/ + 16 + RESIZE DUP OBJECT-SIZE
2DUP STRING-BUFFER 2! ROT
THEN
ROT 2DUP + ESCAPED-CHAR SWAP C! -ROT 1+
REPEAT
SKIP-CHAR
HERE OVER - ;
SKIP-CHAR NIP ;
: PARSENUMBER ( c-addr u -- n TRUE | c-addr u FALSE )
DUP 0= IF FALSE EXIT THEN
@ -1776,12 +1786,11 @@ CREATE TIB-LEFTOVER-BYTES 0 ,
WHILE
PEEK-CHAR [CHAR] " = IF
SKIP-CHAR
READSTRING
STATE @ IF
POSTPONE LITSTRING
HERE 0 C,
READSTRING NIP SWAP C! ALIGN
ELSE
READSTRING
255 UMIN DUP C,
HERE SWAP DUP ALLOT CMOVE ALIGN
THEN
ELSE
WORD