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 ENDCASE
THEN ; THEN ;
2VARIABLE STRING-BUFFER
0 0 STRING-BUFFER 2!
\ Read a literal character string up to the next double-quote character \ 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 \ The delimiting double-quote character is removed from the input buffer
\ Double-quote and backslash characters can be escaped with a backslash \ Double-quote and backslash characters can be escaped with a backslash
: READSTRING ( "ccc<doublequote>" -- c-addr u ) : READSTRING ( "ccc<doublequote>" -- c-addr u )
HERE STRING-BUFFER 2@ 0
( S: addr length index )
BEGIN BEGIN
PEEK-CHAR [CHAR] " <> PEEK-CHAR [CHAR] " <>
WHILE 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 REPEAT
SKIP-CHAR SKIP-CHAR NIP ;
HERE OVER - ;
: PARSENUMBER ( c-addr u -- n TRUE | c-addr u FALSE ) : PARSENUMBER ( c-addr u -- n TRUE | c-addr u FALSE )
DUP 0= IF FALSE EXIT THEN DUP 0= IF FALSE EXIT THEN
@ -1776,12 +1786,11 @@ CREATE TIB-LEFTOVER-BYTES 0 ,
WHILE WHILE
PEEK-CHAR [CHAR] " = IF PEEK-CHAR [CHAR] " = IF
SKIP-CHAR SKIP-CHAR
READSTRING
STATE @ IF STATE @ IF
POSTPONE LITSTRING POSTPONE LITSTRING
HERE 0 C, 255 UMIN DUP C,
READSTRING NIP SWAP C! ALIGN HERE SWAP DUP ALLOT CMOVE ALIGN
ELSE
READSTRING
THEN THEN
ELSE ELSE
WORD WORD