andrey Wed Sep 25 14:06:06 2002 EDT
Modified files:
/php4/ext/standard basic_functions.c php_string.h string.c
Log:
str_shuffle() function added. Like shuffle() for arrays - however the
algorithm for creating the permutation is quite simple. More like
the implementation of shuffle() for 4.2.1 .
Index: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.511
php4/ext/standard/basic_functions.c:1.512
--- php4/ext/standard/basic_functions.c:1.511 Wed Sep 25 11:25:11 2002
+++ php4/ext/standard/basic_functions.c Wed Sep 25 14:06:05 2002
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: basic_functions.c,v 1.511 2002/09/25 15:25:11 wez Exp $ */
+/* $Id: basic_functions.c,v 1.512 2002/09/25 18:06:05 andrey Exp $ */
#include "php.h"
#include "php_streams.h"
@@ -329,6 +329,7 @@
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: php4/ext/standard/php_string.h
diff -u php4/ext/standard/php_string.h:1.60 php4/ext/standard/php_string.h:1.61
--- php4/ext/standard/php_string.h:1.60 Tue Aug 20 16:47:47 2002
+++ php4/ext/standard/php_string.h Wed Sep 25 14:06:05 2002
@@ -17,7 +17,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: php_string.h,v 1.60 2002/08/20 20:47:47 wez Exp $ */
+/* $Id: php_string.h,v 1.61 2002/09/25 18:06:05 andrey Exp $ */
/* Synced with php 3.0 revision 1.43 1999-06-16 [ssb] */
@@ -82,6 +82,7 @@
PHP_FUNCTION(substr_count);
PHP_FUNCTION(str_pad);
PHP_FUNCTION(sscanf);
+PHP_FUNCTION(str_shuffle);
#ifdef HAVE_STRCOLL
PHP_FUNCTION(strcoll);
#endif
Index: php4/ext/standard/string.c
diff -u php4/ext/standard/string.c:1.297 php4/ext/standard/string.c:1.298
--- php4/ext/standard/string.c:1.297 Mon Sep 23 10:20:02 2002
+++ php4/ext/standard/string.c Wed Sep 25 14:06:05 2002
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: string.c,v 1.297 2002/09/23 14:20:02 sebastian Exp $ */
+/* $Id: string.c,v 1.298 2002/09/25 18:06:05 andrey Exp $ */
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
@@ -3929,6 +3929,39 @@
zval_copy_ctor(return_value);
php_strtr(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), rot13_from,
rot13_to, 52);
+}
+/* }}} */
+
+
+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);
}
/* }}} */
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php