What to do when I hit max String size?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CalmTurtle

I have a dialog box (just a RichTextLabel) that I would like to output continuous bbcode text to. As another line of text comes in, the log starts a newline /n and adds the next line.

It works fine. I continuously load the separate strings into an array. Whenever a new string item is added to the array, I concatenate each string into one string (we will call it log) and update the label.

I am worried when I have a few dozen or a few hundred lines of text, if that final concatenated string (log) will be able to store all the information. I am not sure the string size limitation on a 64bit device, but regardless of the max, I know it is there.

The only solution I can imagine is to chop the log at size x by popping off the earliest array entries one by one.

Thanks for the additional insight/ideas.

:bust_in_silhouette: Reply From: wombatstampede

The limit is 32-Bit at the least. That would be 2GB when using signed ints.
That would be 25 million lines if each line has 80 characters.
…and I’m wondering if the length limit isn’t actually 64bit…

Your problem when working with that big strings will be performance, not length.
I’d say: Yes, crop the string at a much lower limit. You can also use somestring.find("\n", (some offset)) to cut whole lines. Then use somestring.right(offset+1) to get the new shortened part. You’ll have to reassign the string to the rich text control.

To make sure it works I’d debug/test with a much lower limit (i.e. 300 characters or so).
Then I’d also implement a threshold. So i.e. when the string exceeds 3MB length, cut back to 2MB length. (depending on how much text you want to allow the user to scroll back.)

Probably the question of how much text you want to retain will more depend on how much the user will comfortably be able to manage with keyboard or scrollbar than limitations on memory/string length or performance.

Thank you for the answer and the scientific explanation. Took me a minute to wrap my head around a 2GB string but I got it. I will experiment with the performance side and see how it goes.

CalmTurtle | 2019-03-28 13:33