andrei Fri Oct 6 20:00:37 2006 UTC Modified files: /php-src/ext/standard uniqid.c Log: Unicode support in uniqid(). http://cvs.php.net/viewvc.cgi/php-src/ext/standard/uniqid.c?r1=1.45&r2=1.46&diff_format=u Index: php-src/ext/standard/uniqid.c diff -u php-src/ext/standard/uniqid.c:1.45 php-src/ext/standard/uniqid.c:1.46 --- php-src/ext/standard/uniqid.c:1.45 Thu Apr 6 20:05:43 2006 +++ php-src/ext/standard/uniqid.c Fri Oct 6 20:00:37 2006 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: uniqid.c,v 1.45 2006/04/06 20:05:43 pollita Exp $ */ +/* $Id: uniqid.c,v 1.46 2006/10/06 20:00:37 andrei Exp $ */ #include "php.h" @@ -38,23 +38,26 @@ #include "php_lcg.h" #include "uniqid.h" -/* {{{ proto string uniqid([string prefix , bool more_entropy]) +/* {{{ proto string uniqid([string prefix , bool more_entropy]) U Generates a unique ID */ #ifdef HAVE_GETTIMEOFDAY PHP_FUNCTION(uniqid) { - char *prefix = ""; + zstr prefix = EMPTY_ZSTR; + int prefix_len = 0; + zend_uchar str_type; #if defined(__CYGWIN__) zend_bool more_entropy = 1; #else zend_bool more_entropy = 0; #endif char *uniqid; - int sec, usec, prefix_len = 0; + UChar *u_uniqid; + int sec, usec; struct timeval tv; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sb", &prefix, &prefix_len, - &more_entropy)) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|tb", &prefix, + &prefix_len, &str_type, &more_entropy) == FAILURE) { return; } @@ -75,13 +78,28 @@ /* The max value usec can have is 0xF423F, so we use only five hex * digits for usecs. */ - if (more_entropy) { - spprintf(&uniqid, 0, "%s%08x%05x%.8f", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10); + if (str_type == IS_UNICODE) { + /* prefix + 8 (sec) + 5 (usec) + 10 (lcg) */ + int buf_len = prefix_len + 8 + 5 + 10 + 1; + int written; + + u_uniqid = eumalloc(buf_len + 1); + if (more_entropy) { + written = u_sprintf(u_uniqid, "%S%08x%05x%.8f", prefix.u, sec, usec, php_combined_lcg(TSRMLS_C) * 10); + } else { + written = u_sprintf(u_uniqid, "%S%08x%05x", prefix.u, sec, usec); + } + u_uniqid[written] = 0; + RETURN_UNICODEL(u_uniqid, written, 0); } else { - spprintf(&uniqid, 0, "%s%08x%05x", prefix, sec, usec); - } + if (more_entropy) { + spprintf(&uniqid, 0, "%s%08x%05x%.8f", prefix.s, sec, usec, php_combined_lcg(TSRMLS_C) * 10); + } else { + spprintf(&uniqid, 0, "%s%08x%05x", prefix.s, sec, usec); + } - RETURN_RT_STRING(uniqid, ZSTR_AUTOFREE); + RETURN_STRING(uniqid, 0); + } } #endif /* }}} */
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php