]> granicus.if.org Git - php/commitdiff
str_shuffle() function added. Like shuffle() for arrays - however the
authorAndrey Hristov <andrey@php.net>
Wed, 25 Sep 2002 18:06:05 +0000 (18:06 +0000)
committerAndrey Hristov <andrey@php.net>
Wed, 25 Sep 2002 18:06:05 +0000 (18:06 +0000)
algorithm for creating the permutation is quite simple. More like
the implementation of shuffle() for 4.2.1 .

ext/standard/basic_functions.c
ext/standard/php_string.h
ext/standard/string.c

index cfa03d6afbe86ca03157dcaf884c116f86a9205c..43548224e146ffe9f2860a78860188899806294c 100644 (file)
@@ -329,6 +329,7 @@ function_entry basic_functions[] = {
        PHP_FE(strstr,                                                                                                                  NULL)
        PHP_FE(stristr,                                                                                                                 NULL)
        PHP_FE(strrchr,                                                                                                                 NULL)
+       PHP_FE(str_shuffle,                                                                                                                     NULL)
 
 #ifdef HAVE_STRCOLL
        PHP_FE(strcoll,                                                                                                                 NULL)
index 6fbc57bd4257b38f7f7de7060351af25919993a8..4b10700762d27c4c7c6cff1201270e85ed55e839 100644 (file)
@@ -82,6 +82,7 @@ PHP_FUNCTION(strnatcasecmp);
 PHP_FUNCTION(substr_count);
 PHP_FUNCTION(str_pad);
 PHP_FUNCTION(sscanf);
+PHP_FUNCTION(str_shuffle);
 #ifdef HAVE_STRCOLL
 PHP_FUNCTION(strcoll);
 #endif
index 09d6f71d82e0bc60e75be4c2a9231f01acdc3035..5f2c0538cdaf757ec4ec527d293a5f10db7b0f2d 100644 (file)
@@ -3933,6 +3933,39 @@ PHP_FUNCTION(str_rot13)
 /* }}} */
 
 
+static int php_string_shuffle(const void *a, const void *b TSRMLS_DC)
+{
+       long rnd;
+       rnd = php_rand(TSRMLS_C);
+       if (rnd % 3)
+               return 1;
+       else if (rnd % 5)
+               return 0;
+       else 
+               return -1;
+}
+
+/* {{{ proto string str_shuffle(string str)
+   Shuffles string. One permutation of all possible is created */
+PHP_FUNCTION(str_shuffle)
+{
+       /* Note : by using current php_string_shuffle for string  */
+       /* with 6 chars (6! permutations) about 2/3 of them are   */
+       /* computed for 6! calls for the function. So it isn't so */
+       /* unique. The ratio is the same for other lengths.       */
+       char *str;
+       int i, str_len;
+       
+       i = 0;
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
+               RETURN_FALSE;
+       }
+       zend_qsort((void *)str, str_len, sizeof(char), php_string_shuffle TSRMLS_CC);
+       RETURN_STRINGL(str, str_len, 1);
+}
+/* }}} */
+
+
 #if HAVE_STRFMON
 /* {{{ proto string money_format(string format , float value)
    Convert monetary value(s) to string */