cvs: php-src /ext/standard string.c var.c

From: Date: Mon, 04 Dec 2006 18:55:40 +0000
Subject: cvs: php-src /ext/standard string.c var.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-41885@lists.php.net to get a copy of this message
andrei Mon Dec 4 18:55:40 2006 UTC Modified files: /php-src/ext/standard string.c var.c Log: Unicode support in var_export(). http://cvs.php.net/viewvc.cgi/php-src/ext/standard/string.c?r1=1.616&r2=1.617&diff_format=u Index: php-src/ext/standard/string.c diff -u php-src/ext/standard/string.c:1.616 php-src/ext/standard/string.c:1.617 --- php-src/ext/standard/string.c:1.616 Thu Nov 30 21:46:54 2006 +++ php-src/ext/standard/string.c Mon Dec 4 18:55:40 2006 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: string.c,v 1.616 2006/11/30 21:46:54 andrei Exp $ */ +/* $Id: string.c,v 1.617 2006/12/04 18:55:40 andrei Exp $ */ /* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */ @@ -4799,7 +4799,7 @@ *(buf+buf_len) = (UChar)0x30; buf_len++; /* 0 */ break; case '\'': - case '\"': + case '\"': case '\\': *(buf+buf_len) = (UChar)0x5C; buf_len++; /* \ */ /* break is missing *intentionally* */ http://cvs.php.net/viewvc.cgi/php-src/ext/standard/var.c?r1=1.248&r2=1.249&diff_format=u Index: php-src/ext/standard/var.c diff -u php-src/ext/standard/var.c:1.248 php-src/ext/standard/var.c:1.249 --- php-src/ext/standard/var.c:1.248 Fri Dec 1 19:25:10 2006 +++ php-src/ext/standard/var.c Mon Dec 4 18:55:40 2006 @@ -18,7 +18,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: var.c,v 1.248 2006/12/01 19:25:10 andrei Exp $ */ +/* $Id: var.c,v 1.249 2006/12/04 18:55:40 andrei Exp $ */ @@ -521,6 +521,47 @@ return 0; } +static void php_unicode_export(UChar *ustr, int ustr_len TSRMLS_DC) +{ + UChar32 cp; + int i = 0; + char buf[10]; + int buf_len; + + /* + * We export all codepoints > 128 in escaped form to avoid encoding issues + * in case the result is used in a script. + */ + while (i < ustr_len) { + U16_NEXT(ustr, i, ustr_len, cp); + switch (cp) { + case 0x0: /* '\0' */ + PHPWRITE("\\0", 2); + break; + + case 0x27: /* '\'' */ + PHPWRITE("\\'", 2); + break; + + case 0x5c: /* '\\' */ + PHPWRITE("\\\\", 2); + break; + + default: + if ((uint32_t)cp < 128) { + buf[0] = (char) (short) cp; + buf_len = 1; + } else if (U_IS_BMP(cp)) { + buf_len = snprintf(buf, sizeof(buf), "\\u%04X", cp); + } else { + buf_len = snprintf(buf, sizeof(buf), "\\u%06X", cp); + } + PHPWRITE(buf, buf_len); + break; + } + } +} + PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC) { HashTable *myht; @@ -550,7 +591,9 @@ efree (tmp_str); break; case IS_UNICODE: - php_var_dump_unicode(Z_USTRVAL_PP(struc), Z_USTRLEN_PP(struc), 0, "'", 1 TSRMLS_CC); + PUTS ("'"); + php_unicode_export(Z_USTRVAL_PP(struc), Z_USTRLEN_PP(struc) TSRMLS_CC); + PUTS ("'"); break; case IS_ARRAY: myht = Z_ARRVAL_PP(struc); @@ -589,7 +632,7 @@ /* }}} */ -/* {{{ proto mixed var_export(mixed var [, bool return]) +/* {{{ proto mixed var_export(mixed var [, bool return]) U Outputs or returns a string representation of a variable */ PHP_FUNCTION(var_export) {

« previous php.cvs (#41885) next »