From: the.beez.speaks@gmail.com   
      
   On 28-04-2025 13:52, albert@spenarnc.xs4all.nl wrote:   
   > In article <03d13185898a1ee00bea4f420aa1a3721b0585f2@i2pn2.org>,   
   > dxf wrote:   
   >> On 27/04/2025 8:28 pm, Hans Bezemer wrote:   
   >>> ...   
   >>> Another handy word is >STRING that converts a "raw" string to a fully   
   qualified string ( a1 n1 -- a2) that can handle a COUNT (you have the carnal   
   knowledge - you take care   
   >> of the "how"). E.g. for counted strings it might be:   
   >>>   
   >>> : >STRING swap over over dup char+ rot cmove c! ;   
   >>   
   >> For forths that have PACK ( a u a2 -- a2 )   
   >>   
   >> : >STRING ( a u -- a ) over pack ;   
   >>   
   > Store a string in allocated memory.   
   > sc (string constant: a u)   
   > : $!A DUP ALLOCATE THROW >R   
   > R@ SWAP CMOVE   
   > R> ;   
   >   
   > It helps to have SIZE in the memory wordset:   
   > sva (string variable stored ALLOCATE d)   
   > : $@A DUP SIZE ;   
   >   
   > The proper mindset is that there are string variables,   
   > (string areas where a string could be stored and changed)   
   > and string constants, sc n where you are no supposed   
   > to change the content of the string.   
   >   
   > Groetjes Albert   
      
   Allocating a string on the heap is essentially what my dynamic string   
   library does. However, it's advised to treat this string as "immutable".   
   So, you may look at it, but don't touch it.. Just like mummy told you!   
      
   What is does is make a tiny allocation at creation, so the variable   
   always points to an allocated string. The trick of the equivalent PLACE   
   and +PLACE words is that it will keep the previous string alive until   
   the very last moment - when all assignments have been done.   
      
   So, if you want to +PLACE a dynamic string onto itself - you can. You   
   can also manage an entire array of these strings (which makes releasing   
   it a whole lot easier).   
      
   That's how 4tH's uBASIC interpreter allocates and handles its strings.   
   Of course, since that's in a sense an application program, the   
   immutability is strictly enforced. Virtually every string operation   
   creates a new string.   
      
   BTW, 4tH's heap management API has ALLOCATED - which returns the size of   
   an allocation. So, no problems there.   
      
   Hans Bezemer   
      
   --- SoupGate-DOS v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|