php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch reset_end_with_offset.patch.txt for Arrays related Bug #31375

Patch version 2011-08-23 16:14 UTC

Return to Bug #31375 | Download this patch
Patch Revisions:

Developer: datibbaw@php.net

Index: basic_functions.c
===================================================================
--- basic_functions.c	(revision 315368)
+++ basic_functions.c	(working copy)
@@ -277,8 +277,9 @@
 	ZEND_ARG_INFO(0, cmp_function)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO(arginfo_end, 0)
+ZEND_BEGIN_ARG_INFO_EX(arginfo_end, 0, 0, 1)
 	ZEND_ARG_INFO(1, arg)
+	ZEND_ARG_INFO(0, offset)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO(arginfo_prev, 0)
@@ -289,8 +290,9 @@
 	ZEND_ARG_INFO(1, arg)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO(arginfo_reset, 0)
+ZEND_BEGIN_ARG_INFO_EX(arginfo_reset, 0, 0, 1)
 	ZEND_ARG_INFO(1, arg)
+	ZEND_ARG_INFO(0, offset)
 ZEND_END_ARG_INFO()
 
 ZEND_BEGIN_ARG_INFO(arginfo_current, ZEND_SEND_PREFER_REF)
Index: array.c
===================================================================
--- array.c	(revision 315368)
+++ array.c	(working copy)
@@ -807,18 +807,26 @@
 /* }}} */
 
 /* {{{ proto mixed end(array array_arg)
-   Advances array argument's internal pointer to the last element and return it */
+   Advances array argument's internal pointer to the last element (or N elements from the end) and returns it */
 PHP_FUNCTION(end)
 {
 	HashTable *array;
 	zval **entry;
+	long offset = 0;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|l", &array, &offset) == FAILURE) {
 		return;
 	}
 
 	zend_hash_internal_pointer_end(array);
 
+	if (offset >= 0) {
+		while (offset && zend_hash_get_current_data(array, (void **) &entry) == SUCCESS) {
+			--offset;
+			zend_hash_move_backwards(array);
+		}
+	}
+
 	if (return_value_used) {
 		if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
 			RETURN_FALSE;
@@ -876,18 +884,26 @@
 /* }}} */
 
 /* {{{ proto mixed reset(array array_arg)
-   Set array argument's internal pointer to the first element and return it */
+   Set array argument's internal pointer to the first element (or N elements from the start) and return it */
 PHP_FUNCTION(reset)
 {
 	HashTable *array;
 	zval **entry;
+	long offset = 0;
 
-	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|l", &array, &offset) == FAILURE) {
 		return;
 	}
 
 	zend_hash_internal_pointer_reset(array);
 
+	if (offset >= 0) {
+		while (offset && zend_hash_get_current_data(array, (void **) &entry) == SUCCESS) {
+			--offset;
+			zend_hash_move_forward(array);
+		}
+	}
+
 	if (return_value_used) {
 		if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
 			RETURN_FALSE;
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 18:01:29 2024 UTC