From 8d953e611d88378074a8d17df631211c987c9846 Mon Sep 17 00:00:00 2001 From: Andrei Zmievski Date: Wed, 9 Aug 2006 20:19:06 +0000 Subject: [PATCH] Unicode support for str_shuffle(). --- ext/standard/string.c | 43 ++++++++++++++++++++++++++----------------- unicode-progress.txt | 4 +--- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index aad6c81b7a..a5e600ad5c 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -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); } } /* }}} */ diff --git a/unicode-progress.txt b/unicode-progress.txt index 748739fff0..6c5118ed2a 100644 --- a/unicode-progress.txt +++ b/unicode-progress.txt @@ -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() -- 2.40.0