xref: /PHP-8.0/ext/spl/spl_engine.c (revision a3d0d947)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | http://www.php.net/license/3_01.txt                                  |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Marcus Boerger <helly@php.net>                              |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20 
21 #include "php.h"
22 #include "php_ini.h"
23 #include "ext/standard/info.h"
24 #include "zend_interfaces.h"
25 
26 #include "php_spl.h"
27 #include "spl_functions.h"
28 #include "spl_engine.h"
29 
30 #include "spl_array.h"
31 
spl_offset_convert_to_long(zval * offset)32 PHPAPI zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */
33 {
34 	zend_ulong idx;
35 
36 try_again:
37 	switch (Z_TYPE_P(offset)) {
38 	case IS_STRING:
39 		if (ZEND_HANDLE_NUMERIC(Z_STR_P(offset), idx)) {
40 			return idx;
41 		}
42 		break;
43 	case IS_DOUBLE:
44 		return zend_dval_to_lval(Z_DVAL_P(offset));
45 	case IS_LONG:
46 		return Z_LVAL_P(offset);
47 	case IS_FALSE:
48 		return 0;
49 	case IS_TRUE:
50 		return 1;
51 	case IS_REFERENCE:
52 		offset = Z_REFVAL_P(offset);
53 		goto try_again;
54 	case IS_RESOURCE:
55 		return Z_RES_HANDLE_P(offset);
56 	}
57 	return -1;
58 }
59 /* }}} */
60