diff --git a/startup.4th b/startup.4th index c23492e..d6db7dc 100644 --- a/startup.4th +++ b/startup.4th @@ -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 diff --git a/test/allocate.4th b/test/allocate.4th index cb1a370..017c2e3 100644 --- a/test/allocate.4th +++ b/test/allocate.4th @@ -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 diff --git a/test/allocate.exp b/test/allocate.exp index 0508090..6a465c0 100644 --- a/test/allocate.exp +++ b/test/allocate.exp @@ -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