]> granicus.if.org Git - php/commitdiff
Unicode support for str_shuffle().
authorAndrei Zmievski <andrei@php.net>
Wed, 9 Aug 2006 20:19:06 +0000 (20:19 +0000)
committerAndrei Zmievski <andrei@php.net>
Wed, 9 Aug 2006 20:19:06 +0000 (20:19 +0000)
ext/standard/string.c
unicode-progress.txt

index aad6c81b7ad013b0ebee42e53f56e4ea26f92c6f..a5e600ad5c199c2ec60a4289379177f574d75f8a 100644 (file)
@@ -6821,46 +6821,55 @@ PHP_FUNCTION(str_rot13)
 /* }}} */
 
 
-static void php_string_shuffle(char *str, long len TSRMLS_DC)
+static void php_string_shuffle(zstr str, int len, zend_uchar str_type TSRMLS_DC)
 {
-       long n_elems, rnd_idx, n_left;
+       int rnd_idx, n_left;
        char temp;
+       UChar u_temp;
        /* The implementation is stolen from array_data_shuffle       */
        /* Thus the characteristics of the randomization are the same */
-       n_elems = len;
 
-       if (n_elems <= 1) {
+       if (len <= 1) {
                return;
        }
 
-       n_left = n_elems;
+       n_left = len;
 
        while (--n_left) {
                rnd_idx = php_rand(TSRMLS_C);
                RAND_RANGE(rnd_idx, 0, n_left, PHP_RAND_MAX);
-               if (rnd_idx != n_left) {
-                       temp = str[n_left];
-                       str[n_left] = str[rnd_idx];
-                       str[rnd_idx] = temp;
+               if (str_type == IS_UNICODE) {
+                       if (rnd_idx != n_left) {
+                               u_temp = str.u[n_left];
+                               str.u[n_left] = str.u[rnd_idx];
+                               str.u[rnd_idx] = u_temp;
+                       }
+               } else {
+                       if (rnd_idx != n_left) {
+                               temp = str.s[n_left];
+                               str.s[n_left] = str.s[rnd_idx];
+                               str.s[rnd_idx] = temp;
+                       }
                }
        }
 }
 
 
-/* {{{ proto void str_shuffle(string str)
+/* {{{ proto void str_shuffle(string str) U
    Shuffles string. One permutation of all possible is created */
 PHP_FUNCTION(str_shuffle)
 {
-       zval **arg;
+       zstr str;
+       int str_len;
+       zend_uchar str_type;
 
-       if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg)) {
-               WRONG_PARAM_COUNT;
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &str, &str_len, &str_type) == FAILURE) {
+               return;
        }
 
-       convert_to_string_ex(arg);
-       RETVAL_ZVAL(*arg, 1, 0);
-       if (Z_STRLEN_P(return_value) > 1) {
-               php_string_shuffle(Z_STRVAL_P(return_value), (long) Z_STRLEN_P(return_value) TSRMLS_CC);
+       RETVAL_ZSTRL(str, str_len, str_type, 1);
+       if (Z_UNILEN_P(return_value) > 1) {
+               php_string_shuffle(Z_UNIVAL_P(return_value), Z_UNILEN_P(return_value), Z_TYPE_P(return_value) TSRMLS_CC);
        }
 }
 /* }}} */
index 748739fff056a104d31f7f1d9338137cf90fcb22..6c5118ed2a30b081a3a865ac631bd6e8d10592e3 100644 (file)
@@ -52,9 +52,6 @@ ext/standard
         Params API, IS_UNICODE upgrade. Case-folding should be handled
         similar to stristr().
 
-    str_shuffle()
-        Params API, IS_UNICODE support
-
     str_split()
         IS_UNICODE support, split on codepoint level.
 
@@ -194,6 +191,7 @@ ext/standard
     str_pad()
     str_repeat()
     str_rot13()
+    str_shuffle()
     strcspn()
     strip_tags()
     stripcslashes()