add DUPLICATE to ALLOCATE and copy a string or other data

This commit is contained in:
Jesse D. McDonald 2020-10-24 11:14:33 -05:00
parent 89c2f4df3d
commit 8dbc2939fc
3 changed files with 15 additions and 1 deletions

View File

@ -1421,6 +1421,11 @@ VARIABLE TOTAL
( S: obj-addr1 obj-addr2 copy-size R: obj-addr1 obj-addr2 )
CMOVE R> R> FREE ;
\ Allocate memory to hold a copy of the data describe by c-addr and u
\ This can be used to duplicate strings
: DUPLICATE ( c-addr u -- obj-addr u )
DUP ALLOCATE >R >R 2R@ CMOVE 2R> ;
>>SYSTEM
\ Execute the closure captured at a-addr

View File

@ -30,6 +30,12 @@
0 ALLOCATE "Result: " TYPE U. EOL
"Free a NULL pointer" HEADING
0 FREE ;
0 FREE
"Duplicate a string" HEADING
"test string" 2DUP DUPLICATE
"original (" TYPE 2SWAP DUP U. " chars): " TYPE TYPE EOL
"duplicate (" TYPE DUP U. " chars): " TYPE 2DUP TYPE EOL
DROP FREE ;
TEST

View File

@ -5,4 +5,7 @@
* Allocate 0 bytes
Result: 0
* Free a NULL pointer
* Duplicate a string
original (11 chars): test string
duplicate (11 chars): test string
exit-code: 0