]> granicus.if.org Git - php/commitdiff
Unicode support in var_export().
authorAndrei Zmievski <andrei@php.net>
Mon, 4 Dec 2006 18:55:40 +0000 (18:55 +0000)
committerAndrei Zmievski <andrei@php.net>
Mon, 4 Dec 2006 18:55:40 +0000 (18:55 +0000)
ext/standard/string.c
ext/standard/var.c

index 363de4cec036de88c6150c5e460f12f8120a0457..ab173a39e71e807fe9607f3a56f4947fc374c8c3 100644 (file)
@@ -4799,7 +4799,7 @@ PHPAPI UChar *php_u_addslashes_ex(UChar *str, int length, int *new_length, int s
                                *(buf+buf_len) = (UChar)0x30; buf_len++; /* 0 */
                                break;
                        case '\'':
-                               case '\"':
+                       case '\"':
                        case '\\':
                                        *(buf+buf_len) = (UChar)0x5C; buf_len++; /* \ */
                                        /* break is missing *intentionally* */
index d36f3f133dc82f16a306cd259ce094da6ff6e195..3d46250d624fb3715e49f7c180fdaa939ef28351 100644 (file)
@@ -521,6 +521,47 @@ static int php_object_element_export(zval **zv, int num_args, va_list args, zend
        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 @@ PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC)
                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 @@ PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC)
 /* }}} */
 
 
-/* {{{ 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)
 {