Any way to get TStringList.CommaText to not escape commas with quotes?

Mason Wheeler picture Mason Wheeler · Jun 10, 2010 · Viewed 7.3k times · Source

I'm doing some work with code generation, and one of the things I need to do is create a function call where one of the parameters is a function call, like so:

result := Func1(x, y, Func2(a, b, c));

TStringList.CommaText is very useful for generating the parameter lists, but when I traverse the tree to build the outer function call, what I end up with looks like this:

result := Func1(x, y, "Func2(a, b, c)");

It's quoting the third argument because it contains commas, and that produced invalid code. But I can't do something simplistic like StringReplace all double quotes with empty strings, because it's quite possible that a function argument could be a string with double quotes inside. Is there any way to make it just not escape the lines that contain commas?

Answer

Rob Kennedy picture Rob Kennedy · Jun 10, 2010

You could set QuoteChar to be a space, and you'd merely get some extra spaces in the output, which is generally OK since generated code isn't usually expected to look pretty. String literals would be affected, though; they would have extra spaces inserted, changing the value of the string.

Free Pascal's TStrings class uses StrictDelimiter to control whether quoting occurs when reading the DelimitedText property. When it's true, quoting does not occur at all. Perhaps Delphi treats that property the same way.