[phc-general] Single Quoting strings

Dan Libby dan at osc.co.cr
Thu Aug 3 02:11:21 CEST 2006


Oops, I missed the obvious case that one should not single-quote a string with 
embedded single-quotes.  This line fixes it.

if( !strpbrk( in->source_rep->c_str(), "\n\r\t\\$'" ) ) {

On Wednesday 02 August 2006 16:33, Dan Libby wrote:
> Hi,
>
> It is commonly known that singly-quoted strings take less time for php to
> parse than doubly-quoted strings.
>
> As such, a utility for converting doubly-quoted strings to singly-quoted
> should be useful for optimization purposes.  I was about to write a script
> to do this, but decided that it could be better to use a "real" parser for
> the task, and phc seemed well suited.
>
> However, I quickly discovered that "phc --dump-php" dumps all strings as
> doubly-quoted ( even if originally quoted or in a here-doc ).  To work
> around this, I added a little hack that checks for the presence of special
> characters in the string and uses double-quotes if found, else single
> quotes.
>
> Here's the patch.   regards.
>
> void PHP_unparser::children_string(Token_string* in)
> {
> 	if(
> 		*in->source_rep == "__FILE__" ||
> 		*in->source_rep == "__CLASS__" ||
> 		*in->source_rep == "__METHOD__" ||
> 		*in->source_rep == "__FUNCTION__"
> 		)
> 	{
> 		echo(in->source_rep);
> 	}
> 	else
> 	{
> 		echo("\"");
> 		echo_escaped(in->source_rep);
> 		echo("\"");
> 	}
> }
>
> ========== MODIFIED CODE ============
>
> void PHP_unparser::children_string(Token_string* in)
> {
> 	if(
> 		*in->source_rep == "__FILE__" ||
> 		*in->source_rep == "__CLASS__" ||
> 		*in->source_rep == "__METHOD__" ||
> 		*in->source_rep == "__FUNCTION__"
> 		)
> 	{
> 		echo(in->source_rep);
> 	}
> 	else
> 	{
> 		if( !strpbrk( in->source_rep->c_str(), "\n\r\t\\$" ) ) {
> 			echo("'");
> 			echo_escaped(in->source_rep);
> 			echo("'");
> 		}
> 		else {
> 			echo("\"");
> 			echo_escaped(in->source_rep);
> 			echo("\"");
> 		}
> 	}
> }

-- 
Dan Libby

Open Source Consulting
San Jose, Costa Rica
http://osc.co.cr
phone: 011 506 223 7382
Fax: 011 506 223 7359



More information about the phc-general mailing list