xref: /PHP-8.1/ext/spl/spl_iterators.h (revision d7eea8e1)
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    | https://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 #ifndef SPL_ITERATORS_H
18 #define SPL_ITERATORS_H
19 
20 #include "php.h"
21 #include "php_spl.h"
22 #include "ext/pcre/php_pcre.h"
23 
24 extern PHPAPI zend_class_entry *spl_ce_AppendIterator;
25 extern PHPAPI zend_class_entry *spl_ce_CachingIterator;
26 extern PHPAPI zend_class_entry *spl_ce_CallbackFilterIterator;
27 extern PHPAPI zend_class_entry *spl_ce_EmptyIterator;
28 extern PHPAPI zend_class_entry *spl_ce_FilterIterator;
29 extern PHPAPI zend_class_entry *spl_ce_InfiniteIterator;
30 extern PHPAPI zend_class_entry *spl_ce_IteratorIterator;
31 extern PHPAPI zend_class_entry *spl_ce_LimitIterator;
32 extern PHPAPI zend_class_entry *spl_ce_NoRewindIterator;
33 extern PHPAPI zend_class_entry *spl_ce_OuterIterator;
34 extern PHPAPI zend_class_entry *spl_ce_ParentIterator;
35 extern PHPAPI zend_class_entry *spl_ce_RecursiveCachingIterator;
36 extern PHPAPI zend_class_entry *spl_ce_RecursiveCallbackFilterIterator;
37 extern PHPAPI zend_class_entry *spl_ce_RecursiveFilterIterator;
38 extern PHPAPI zend_class_entry *spl_ce_RecursiveIterator;
39 extern PHPAPI zend_class_entry *spl_ce_RecursiveIteratorIterator;
40 extern PHPAPI zend_class_entry *spl_ce_RecursiveRegexIterator;
41 extern PHPAPI zend_class_entry *spl_ce_RecursiveTreeIterator;
42 extern PHPAPI zend_class_entry *spl_ce_RegexIterator;
43 extern PHPAPI zend_class_entry *spl_ce_SeekableIterator;
44 
45 PHP_MINIT_FUNCTION(spl_iterators);
46 
47 PHP_FUNCTION(iterator_apply);
48 PHP_FUNCTION(iterator_count);
49 PHP_FUNCTION(iterator_to_array);
50 
51 typedef enum {
52 	DIT_Default = 0,
53 	DIT_FilterIterator = DIT_Default,
54 	DIT_RecursiveFilterIterator = DIT_Default,
55 	DIT_ParentIterator = DIT_Default,
56 	DIT_LimitIterator,
57 	DIT_CachingIterator,
58 	DIT_RecursiveCachingIterator,
59 	DIT_IteratorIterator,
60 	DIT_NoRewindIterator,
61 	DIT_InfiniteIterator,
62 	DIT_AppendIterator,
63 	DIT_RegexIterator,
64 	DIT_RecursiveRegexIterator,
65 	DIT_CallbackFilterIterator,
66 	DIT_RecursiveCallbackFilterIterator,
67 	DIT_Unknown = ~0
68 } dual_it_type;
69 
70 typedef enum {
71 	RIT_Default = 0,
72 	RIT_RecursiveIteratorIterator = RIT_Default,
73 	RIT_RecursiveTreeIterator,
74 	RIT_Unknow = ~0
75 } recursive_it_it_type;
76 
77 enum {
78 	/* public */
79 	CIT_CALL_TOSTRING        = 0x00000001,
80 	CIT_TOSTRING_USE_KEY     = 0x00000002,
81 	CIT_TOSTRING_USE_CURRENT = 0x00000004,
82 	CIT_TOSTRING_USE_INNER   = 0x00000008,
83 	CIT_CATCH_GET_CHILD      = 0x00000010,
84 	CIT_FULL_CACHE           = 0x00000100,
85 	CIT_PUBLIC               = 0x0000FFFF,
86 	/* private */
87 	CIT_VALID                = 0x00010000,
88 	CIT_HAS_CHILDREN         = 0x00020000
89 };
90 
91 enum {
92 	/* public */
93 	REGIT_USE_KEY            = 0x00000001,
94 	REGIT_INVERTED           = 0x00000002
95 };
96 
97 typedef enum {
98 	REGIT_MODE_MATCH,
99 	REGIT_MODE_GET_MATCH,
100 	REGIT_MODE_ALL_MATCHES,
101 	REGIT_MODE_SPLIT,
102 	REGIT_MODE_REPLACE,
103 	REGIT_MODE_MAX
104 } regex_mode;
105 
106 typedef struct _spl_cbfilter_it_intern {
107 	zend_fcall_info       fci;
108 	zend_fcall_info_cache fcc;
109 	zend_object           *object;
110 } _spl_cbfilter_it_intern;
111 
112 typedef struct _spl_dual_it_object {
113 	struct {
114 		zval                 zobject;
115 		zend_class_entry     *ce;
116 		zend_object          *object;
117 		zend_object_iterator *iterator;
118 	} inner;
119 	struct {
120 		zval                 data;
121 		zval                 key;
122 		zend_long            pos;
123 	} current;
124 	dual_it_type             dit_type;
125 	union {
126 		struct {
127 			zend_long             offset;
128 			zend_long             count;
129 		} limit;
130 		struct {
131 			zend_long             flags; /* CIT_* */
132 			zend_string          *zstr;
133 			zval             zchildren;
134 			zval             zcache;
135 		} caching;
136 		struct {
137 			zval                  zarrayit;
138 			zend_object_iterator *iterator;
139 		} append;
140 		struct {
141 			zend_long        flags;
142 			zend_long        preg_flags;
143 			pcre_cache_entry *pce;
144 			zend_string      *regex;
145 			regex_mode       mode;
146 			int              use_flags;
147 		} regex;
148 		_spl_cbfilter_it_intern *cbfilter;
149 	} u;
150 	zend_object              std;
151 } spl_dual_it_object;
152 
spl_dual_it_from_obj(zend_object * obj)153 static inline spl_dual_it_object *spl_dual_it_from_obj(zend_object *obj) /* {{{ */ {
154 	return (spl_dual_it_object*)((char*)(obj) - XtOffsetOf(spl_dual_it_object, std));
155 } /* }}} */
156 
157 #define Z_SPLDUAL_IT_P(zv)  spl_dual_it_from_obj(Z_OBJ_P((zv)))
158 
159 typedef int (*spl_iterator_apply_func_t)(zend_object_iterator *iter, void *puser);
160 
161 PHPAPI int spl_iterator_apply(zval *obj, spl_iterator_apply_func_t apply_func, void *puser);
162 
163 #endif /* SPL_ITERATORS_H */
164