Description
The current approach uses polymorphism to make RopeString objects. This is pretty inefficient for the typical small string use-case. To efficiently construct a C-style string in the current framework, one makes the current output stream a memio object and then prints to it. The general pattern I've used is to write a print_whatever
function and then wrap it in a whatever
function that returns a string using print_to_string
. Should we stick with this pattern? It has the advantage of allowing the printing version to be very efficient, but it's kind of awkward to write. Should we figure out a different pattern? Something like C#'s StringBuilder
pattern?
Perhaps it suffices to make strcat
check the size and encodings of its arguments and use print_to_string
approach to concatenate them into a copied string where appropriate — namely when the arguments are of compatible encodings (e.g. any mixture of ASCIIString
and UTF8String
), and if concatenated they would be below some size threshold. For larger strings, we should continue to use the RopeString
approach. Also, string slices should copy their contents as well unless the resulting string is above the "large string" threshold, in which case, they can continue to use the current SubString
with the known issue that this pins the superstring in memory.