StringBuilder

Building a long string piece by piece, without copying it again at every step.

Compatible with PureBasic 6.50.

CreateStringBuilder ✅, FreeStringBuilder ✅, IsStringBuilder ✅

Result = CreateStringBuilder(#StringBuilder [, IncrementLength])
FreeStringBuilder(#StringBuilder)
Result = IsStringBuilder(#StringBuilder)

IncrementLength is how many characters the buffer grows by when it's full (8192 unless given). x grows it by at least half its size each time, so even a very long string is built quickly. FreeStringBuilder(#PB_All) frees all of them.

AppendStringBuilderString ✅, AppendStringBuilderStringN ✅

Result = AppendStringBuilderString(#StringBuilder, String$)
Result = AppendStringBuilderStringN(#StringBuilder, String$)

Adds the string, and with the N version a line end too: #CRLF$ on Windows, #LF$ on Linux and macOS. Returns the length of the whole string so far (0 if the string builder doesn't exist).

GetStringBuilderString ✅, ResetStringBuilder ✅

Result$ = GetStringBuilderString(#StringBuilder)
Result = ResetStringBuilder(#StringBuilder)

GetStringBuilderString returns the string built so far; more can still be added afterwards. ResetStringBuilder empties it.

OpenConsole()
CreateStringBuilder(0)
For i = 1 To 5
  AppendStringBuilderString(0, Str(i))
  If i < 5 : AppendStringBuilderString(0, ", ") : EndIf
Next
PrintN(GetStringBuilderString(0))
ResetStringBuilder(0)
PrintN("length now: " + Str(AppendStringBuilderString(0, "abc")))
FreeStringBuilder(0)

Output:

1, 2, 3, 4, 5
length now: 3

eXperimental is an independent implementation: no proprietary PureBasic source code or implementation code is incorporated into it. PureBasic is a product of Fantaisie Software. eXperimental is neither affiliated with nor endorsed by Fantaisie Software.