Currently, im using the & operator:
# start with an empty string
var buffer = ""
# do some process that outputs lots of small strings
if someCondition:
buffer &= foo
for text in someIterable:
if someOtherCondition:
buffer &= text
# read the resulting buffer at the end
echo buffer
It works, but i would like to know if it is reallocating the string with every write, and if there is a fastest way in Nim (looking for something like C++ stringstream, or Java StringBuilder).
Nim strings are mutable (as opposed to Java's strings) so they already behave like Java's StringBuilder.
If you append to a string, it won't allocate a new string (but may increase the underlying byte buffer). To avoid buffer reallocations, you can use 'newStringOfCap' to create a new string with sufficient capacity to avoid reallocation.
Also keep in mind that Nim copies strings on assignment to prevent certain classes of errors. See 'A note on string assignment' in this doc: https://gist.github.com/Varriount/c3ba438533497bc636da