Append string fast by CopyMemoryString()

Share your advanced PureBasic knowledge/code with the community.
humble
New User
New User
Posts: 3
Joined: Thu May 29, 2014 5:36 pm

Append string fast by CopyMemoryString()

Post by humble »

Here is a procedure to put CopyMemoryString() to work in handy way (though not as good as using '+') for appending string speedily. :lol: Glory be to God and Mother Mary! :lol:

Procedure AppendString2Memory(AddString.s)

Code: Select all

Procedure AppendString2Memory(AddString.s, reset=0)
;  Append string  *MasterString is the address of the first string whilst AddString is the Next string appending afterward
;  Returned is the address of the string concatenation
; To close a new concatenation, AppendString2Memory("",1)  ; clean up static variables in AppendString2Memory() for fresh start
;  Created on Dec26,2015
;   e.g.
;       Define *pString = AppendString2Memory("1st",1)  ; new string collection start
;       AppendString2Memory(", 2nd")
;       AppendString2Memory(", final")
;       Debug PeekS(*pString) --> "1st, 2nd, final"
    Static *MasterString
    Static LastSize ; number of bytes for memory area
    If Len(AddString)=0 : ProcedureReturn : EndIf  ; no processing for empty string
    If reset : If *MasterString : FreeMemory(*MasterString) : *MasterString=0 : LastSize=0 : EndIf : EndIf
    
    Static *TrailingAddress   ; last address kept by CopyMemoryString()
    
    Protected *MemNr
    LastSize + StringByteLength(AddString)   ; memory area increased with new string input
    If *MasterString=0
        *MasterString = AllocateMemory(LastSize, #PB_Memory_NoClear)    ; Starting address of the collective string
        *TrailingAddress=*MasterString              ; Keep address *MasterString intact
        CopyMemoryString(AddString, @*TrailingAddress)    ; *TrailingAddress is going to be move up after string appended
    Else
        *MemNr = ReAllocateMemory(*MasterString, LastSize, #PB_Memory_NoClear)  [i][color=#FF0000]; When LastSize runs upto 6131, received will be an error of "The specified '*MemoryID' is not valid.[/color][/i]
        If *MemNr
            *MasterString = *MemNr
            CopyMemoryString(AddString)   ; string is appended at the address of last string ended by an null-character
        Else
            MessageRequester("AppendString2Memory() error", "ReAllocateMemory() failed to expand memory area")
        EndIf
    EndIf
    ProcedureReturn *MasterString
EndProcedure

How to use it >>>
Define *pString = [color=#4040FF]AppendString2Memory[/color]("1st",1) ; start a new string collection with reset=1
AppendString2Memory(", 2nd")
AppendString2Memory(", 3rd")
AppendString2Memory(", really")
AppendString2Memory(". God wins!")

Debug PeekS(*pString)
There is still a technical problem on ReAllocateMemory(*MasterString, LastSize, #PB_Memory_NoClear). Your reply with a solution is very appreciated. God bless.
Last edited by flaith on Sun Dec 27, 2015 4:50 am, edited 2 times in total.
Reason: Added the [code][/code]