Previously, concatenation was treated as a binary
operator, taking two values from the stack and
concatenating them. Where an expression involves more
than one concatenation, this involved creating
temporary strings, requiring separate allocation, which
would only be discarded later, for each application of the
binary concatenation. To make matters worse,
concatenation would rescan each string to determine its
length using strlen().
This modification changes the way the concatenation
operator is handled. Instead of treating it as a binary
operator, it sees it as a list operator, rather like the
comma in an argument list. Thus a concatenation
expression is treated as a counted sequence of
subexpressions. The count is coded into the "bytecode"
to tell the concat() function (in interpret.c) how many
expression values to pull off the stack. The function
then exploits the fact that string DataValue structures
now include the strings' lengths to work out the total
allocation length required for the whole result. (This is
based on the previous implementation of
I have also added static functions longAsStr() and
lenLongAsStr() to generate a string representation of a
number, or just to work out its length. This allows us to
avoid calling sprintf() with "%d" all the time. The new
AllocStringOfNumber() exploits this, and is used in
various places in interpret.c. Note that longAsStr()
returns the string value in a static buffer which must be
used/copied immediately to avoid a second call
overwriting its content.
NOTE: I have not regenerated parse_noyacc.c!
Log in to post a comment.