Solving the strcat() woes
haneefmubarak.com
haneefmubarak.com
Really, it's unfortunate that null-termination was used at all. The "unlimited length" feature did not have to be implemented in such an expensive way.
As an example, I think it would be possible to use leading bytes to encode strings in one of three ways, using a Pascal-style string as the base case but limited to 253 characters:
[0] [1...253] (short strings)
length data
[0] [1...254] [end+1] (concat. string chains in large contiguous block)
254 len+data start of next
[0] [1...8] [9...255] (concat. string chains in non-contiguous blocks)
255 next ptr. segment len+data
In other words, the values 254 and 255 could act as special values to dictate how to chain together multiple string fragments, and any shorter complete string would degrade to a Pascal-style string.For a multi-segment setup the length of the entire string would be found by summing the lengths of each segment. This is still "fast" because you would have to, at most, test one byte per segment to find the next length byte, to find the total string length.
I'll try to roll something together into another blog post in the next few days about the general topic.