summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcus Boerger <helly@php.net>2004-10-07 23:08:19 +0000
committerMarcus Boerger <helly@php.net>2004-10-07 23:08:19 +0000
commit6349f46e38643259e2de48a2df90c1d4ab7ab55b (patch)
treecc151a5ee4ccf098ca2b02d1057a3a5d97b0b643
parent940c5b393d92f04fab49b59e605364f17372e7dc (diff)
downloadphp-git-6349f46e38643259e2de48a2df90c1d4ab7ab55b.tar.gz
- Added iterator_to_array() and iterator_count()
-rwxr-xr-xext/spl/php_spl.c4
-rwxr-xr-xext/spl/spl_iterators.c64
-rwxr-xr-xext/spl/spl_iterators.h3
-rwxr-xr-xext/spl/tests/spl_001.phpt34
4 files changed, 105 insertions, 0 deletions
diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c
index 34597b6afb..c33a49041b 100755
--- a/ext/spl/php_spl.c
+++ b/ext/spl/php_spl.c
@@ -45,6 +45,10 @@ function_entry spl_functions[] = {
PHP_FE(spl_classes, NULL)
PHP_FE(class_parents, NULL)
PHP_FE(class_implements, NULL)
+#ifdef SPL_ITERATORS_H
+ PHP_FE(iterator_to_array, NULL)
+ PHP_FE(iterator_count, NULL)
+#endif /* SPL_ITERATORS_H */
{NULL, NULL, NULL}
};
/* }}} */
diff --git a/ext/spl/spl_iterators.c b/ext/spl/spl_iterators.c
index 3d09a7c463..2b911559c6 100755
--- a/ext/spl/spl_iterators.c
+++ b/ext/spl/spl_iterators.c
@@ -1322,6 +1322,70 @@ static zend_function_entry spl_funcs_CachingRecursiveIterator[] = {
{NULL, NULL, NULL}
};
+/* {{{ array iterator_to_array(IteratorAggregate $it)
+ Copy the iterator into an array */
+PHP_FUNCTION(iterator_to_array)
+{
+ zval *obj, **data;
+ zend_object_iterator *iter;
+ char *str_key;
+ uint str_key_len;
+ ulong int_key;
+ int key_type;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, zend_ce_aggregate) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ array_init(return_value);
+
+ iter = Z_OBJCE_P(obj)->get_iterator(Z_OBJCE_P(obj), obj TSRMLS_CC);
+
+ iter->funcs->rewind(iter TSRMLS_CC);
+ while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
+ key_type = iter->funcs->get_current_key(iter, &str_key, &str_key_len, &int_key TSRMLS_CC);
+ iter->funcs->get_current_data(iter, &data TSRMLS_CC);
+ (*data)->refcount++;
+ switch(key_type) {
+ case HASH_KEY_IS_STRING:
+ add_assoc_zval_ex(return_value, str_key, str_key_len, *data);
+ efree(str_key);
+ break;
+ case HASH_KEY_IS_LONG:
+ add_index_zval(return_value, int_key, *data);
+ break;
+ }
+ iter->funcs->move_forward(iter TSRMLS_CC);
+ }
+ iter->funcs->dtor(iter TSRMLS_CC);
+}
+/* }}} */
+
+/* {{{ int iterator_count(IteratorAggregate $it)
+ Count the elements in an iterator */
+PHP_FUNCTION(iterator_count)
+{
+ zval *obj;
+ zend_object_iterator *iter;
+ long count = 0;
+
+ if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &obj, zend_ce_aggregate) == FAILURE) {
+ RETURN_FALSE;
+ }
+
+ iter = Z_OBJCE_P(obj)->get_iterator(Z_OBJCE_P(obj), obj TSRMLS_CC);
+
+ iter->funcs->rewind(iter TSRMLS_CC);
+ while (iter->funcs->valid(iter TSRMLS_CC) == SUCCESS) {
+ count++;
+ iter->funcs->move_forward(iter TSRMLS_CC);
+ }
+ iter->funcs->dtor(iter TSRMLS_CC);
+
+ RETURN_LONG(count);
+}
+/* }}} */
+
/* {{{ PHP_MINIT_FUNCTION(spl_iterators)
*/
PHP_MINIT_FUNCTION(spl_iterators)
diff --git a/ext/spl/spl_iterators.h b/ext/spl/spl_iterators.h
index 4f57715ee0..cdec65c047 100755
--- a/ext/spl/spl_iterators.h
+++ b/ext/spl/spl_iterators.h
@@ -35,6 +35,9 @@ extern zend_class_entry *spl_ce_CachingRecursiveIterator;
PHP_MINIT_FUNCTION(spl_iterators);
+PHP_FUNCTION(iterator_to_array);
+PHP_FUNCTION(iterator_count);
+
typedef enum {
DIT_Default = 0,
DIT_LimitIterator,
diff --git a/ext/spl/tests/spl_001.phpt b/ext/spl/tests/spl_001.phpt
new file mode 100755
index 0000000000..e101272a84
--- /dev/null
+++ b/ext/spl/tests/spl_001.phpt
@@ -0,0 +1,34 @@
+--TEST--
+SPL: iterator_to_array() and iterator_count()
+--FILE--
+<?php
+
+$it = new ArrayObject(array("x"=>1, 1=>2, 3=>3, 4, "1"=>5));
+
+$ar = iterator_to_array($it);
+
+var_dump(iterator_count($it));
+
+print_r($ar);
+
+foreach($ar as $v)
+{
+ var_dump($v);
+}
+
+?>
+===DONE===
+--EXPECT--
+int(4)
+Array
+(
+ [x] => 1
+ [1] => 5
+ [3] => 3
+ [4] => 4
+)
+int(1)
+int(5)
+int(3)
+int(4)
+===DONE===