If you look at the Str
code closely, you might notice that all
allocation of Str::data
is done by calling resize()
. This
is done for the following reasons:
resize()
function correctly handles the
STR_ALLOCATED
, STR_TIME_EFFICIENT
and
STR_EXTRA_BUFFER
bits in the data structure.
resize()
also correctly handles the case where
Str::data == 0
resize()
puts the logic of when to resize the
buffer in one place, making it easy to change.
resize()
puts the malloc()
call in one
place. This makes it easier to change to a different allocation
method.