php-ext-orng icon indicating copy to clipboard operation
php-ext-orng copied to clipboard

Duplicate code in arrayRand method.

Open zeriyoshi opened this issue 4 years ago • 1 comments
trafficstars

https://github.com/zeriyoshi/php-ext-orng/blob/master/rng/glibcrand.c#L287 https://github.com/zeriyoshi/php-ext-orng/blob/master/rng/mt19937.c#L222 https://github.com/zeriyoshi/php-ext-orng/blob/master/rng/xorshift128plus.c#L176

zeriyoshi avatar Dec 12 '20 05:12 zeriyoshi

For compatibility with PHP 7.3, the internal implementation of arrayRand() is outdated to PHP 8.0. But, it has been verified that the results to correct in tests.

zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
	zval zv;
	/* We can't use zend_hash_index_find()
	 * because the array may have string keys or gaps. */
	ZEND_HASH_FOREACH_KEY(Z_ARRVAL_P(input), num_key, string_key) {
		if (zend_bitset_in(bitset, i) ^ negative_bitset) {
			if (string_key) {
				// ZEND_HASH_FILL_SET_STR_COPY(string_key);
				ZVAL_STR_COPY(&zv, string_key);
			} else {
				// ZEND_HASH_FILL_SET_LONG(num_key);
				ZVAL_LONG(&zv, num_key);
			}
			// ZEND_HASH_FILL_NEXT();
			ZEND_HASH_FILL_ADD(&zv);
		}
		i++;
	} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FILL_END();
free_alloca(bitset, use_heap);

zeriyoshi avatar Dec 12 '20 06:12 zeriyoshi