This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
op.c: Add debugging dump function
[perl5.git] / invlist_inline.h
CommitLineData
b992490d 1/* invlist_inline.h
81e983c1
KW
2 *
3 * Copyright (C) 2012 by Larry Wall and others
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
81e983c1
KW
7 */
8
3dd7db29
JK
9#ifndef PERL_INVLIST_INLINE_H_
10#define PERL_INVLIST_INLINE_H_
11
de9382e0
KW
12#if defined(PERL_IN_UTF8_C) \
13 || defined(PERL_IN_REGCOMP_C) \
14 || defined(PERL_IN_REGEXEC_C) \
15 || defined(PERL_IN_TOKE_C) \
58a0d047
KW
16 || defined(PERL_IN_PP_C) \
17 || defined(PERL_IN_OP_C)
81e983c1 18
9e7f4f43
KW
19/* An element is in an inversion list iff its index is even numbered: 0, 2, 4,
20 * etc */
21#define ELEMENT_RANGE_MATCHES_INVLIST(i) (! ((i) & 1))
22#define PREV_RANGE_MATCHES_INVLIST(i) (! ELEMENT_RANGE_MATCHES_INVLIST(i))
23
4c60406d
KW
24/* This converts to/from our UVs to what the SV code is expecting: bytes. */
25#define TO_INTERNAL_SIZE(x) ((x) * sizeof(UV))
26#define FROM_INTERNAL_SIZE(x) ((x)/ sizeof(UV))
27
510ceaa0
KW
28PERL_STATIC_INLINE bool
29S_is_invlist(SV* const invlist)
30{
465848b5 31 return invlist != NULL && SvTYPE(invlist) == SVt_INVLIST;
510ceaa0
KW
32}
33
4c60406d 34PERL_STATIC_INLINE bool*
dc3bf405 35S_get_invlist_offset_addr(SV* invlist)
9e7f4f43 36{
4c60406d
KW
37 /* Return the address of the field that says whether the inversion list is
38 * offset (it contains 1) or not (contains 0) */
4c60406d 39 PERL_ARGS_ASSERT_GET_INVLIST_OFFSET_ADDR;
9e7f4f43 40
510ceaa0 41 assert(is_invlist(invlist));
f49d8074 42
4c60406d 43 return &(((XINVLIST*) SvANY(invlist))->is_offset);
9e7f4f43
KW
44}
45
46PERL_STATIC_INLINE UV
dc3bf405 47S__invlist_len(SV* const invlist)
9e7f4f43
KW
48{
49 /* Returns the current number of elements stored in the inversion list's
50 * array */
51
52 PERL_ARGS_ASSERT__INVLIST_LEN;
53
510ceaa0 54 assert(is_invlist(invlist));
f49d8074 55
4c60406d
KW
56 return (SvCUR(invlist) == 0)
57 ? 0
58 : FROM_INTERNAL_SIZE(SvCUR(invlist)) - *get_invlist_offset_addr(invlist);
9e7f4f43
KW
59}
60
61PERL_STATIC_INLINE bool
dc3bf405 62S__invlist_contains_cp(SV* const invlist, const UV cp)
9e7f4f43
KW
63{
64 /* Does <invlist> contain code point <cp> as part of the set? */
65
66 IV index = _invlist_search(invlist, cp);
67
68 PERL_ARGS_ASSERT__INVLIST_CONTAINS_CP;
69
70 return index >= 0 && ELEMENT_RANGE_MATCHES_INVLIST(index);
71}
72
551cedb5
KW
73PERL_STATIC_INLINE UV*
74S_invlist_array(SV* const invlist)
75{
76 /* Returns the pointer to the inversion list's array. Every time the
77 * length changes, this needs to be called in case malloc or realloc moved
78 * it */
79
80 PERL_ARGS_ASSERT_INVLIST_ARRAY;
81
82 /* Must not be empty. If these fail, you probably didn't check for <len>
83 * being non-zero before trying to get the array */
84 assert(_invlist_len(invlist));
85
86 /* The very first element always contains zero, The array begins either
87 * there, or if the inversion list is offset, at the element after it.
88 * The offset header field determines which; it contains 0 or 1 to indicate
89 * how much additionally to add */
90 assert(0 == *(SvPVX(invlist)));
91 return ((UV *) SvPVX(invlist) + *get_invlist_offset_addr(invlist));
92}
93
a9269870
KW
94#endif
95#if defined(PERL_IN_REGCOMP_C)
96
97PERL_STATIC_INLINE void
98S_invlist_extend(pTHX_ SV* const invlist, const UV new_max)
99{
100 /* Grow the maximum size of an inversion list */
101
102 PERL_ARGS_ASSERT_INVLIST_EXTEND;
103
104 assert(SvTYPE(invlist) == SVt_INVLIST);
105
106 /* Add one to account for the zero element at the beginning which may not
107 * be counted by the calling parameters */
108 SvGROW((SV *)invlist, TO_INTERNAL_SIZE(new_max + 1));
109}
110
111PERL_STATIC_INLINE void
112S_invlist_set_len(pTHX_ SV* const invlist, const UV len, const bool offset)
113{
114 /* Sets the current number of elements stored in the inversion list.
115 * Updates SvCUR correspondingly */
116 PERL_UNUSED_CONTEXT;
117 PERL_ARGS_ASSERT_INVLIST_SET_LEN;
118
119 assert(SvTYPE(invlist) == SVt_INVLIST);
120
121 SvCUR_set(invlist,
122 (len == 0)
123 ? 0
124 : TO_INTERNAL_SIZE(len + offset));
125 assert(SvLEN(invlist) == 0 || SvCUR(invlist) <= SvLEN(invlist));
126}
127
128PERL_STATIC_INLINE SV*
129S_add_cp_to_invlist(pTHX_ SV* invlist, const UV cp) {
130 return _add_range_to_invlist(invlist, cp, cp);
131}
132
133PERL_STATIC_INLINE UV
134S_invlist_highest(SV* const invlist)
135{
136 /* Returns the highest code point that matches an inversion list. This API
137 * has an ambiguity, as it returns 0 under either the highest is actually
138 * 0, or if the list is empty. If this distinction matters to you, check
139 * for emptiness before calling this function */
140
141 UV len = _invlist_len(invlist);
142 UV *array;
143
144 PERL_ARGS_ASSERT_INVLIST_HIGHEST;
145
146 if (len == 0) {
147 return 0;
148 }
149
150 array = invlist_array(invlist);
151
152 /* The last element in the array in the inversion list always starts a
153 * range that goes to infinity. That range may be for code points that are
154 * matched in the inversion list, or it may be for ones that aren't
155 * matched. In the latter case, the highest code point in the set is one
156 * less than the beginning of this range; otherwise it is the final element
157 * of this range: infinity */
158 return (ELEMENT_RANGE_MATCHES_INVLIST(len - 1))
159 ? UV_MAX
160 : array[len - 1] - 1;
161}
162
163PERL_STATIC_INLINE STRLEN*
164S_get_invlist_iter_addr(SV* invlist)
165{
166 /* Return the address of the UV that contains the current iteration
167 * position */
168
169 PERL_ARGS_ASSERT_GET_INVLIST_ITER_ADDR;
170
171 assert(is_invlist(invlist));
172
173 return &(((XINVLIST*) SvANY(invlist))->iterator);
174}
175
176PERL_STATIC_INLINE void
177S_invlist_iterinit(SV* invlist) /* Initialize iterator for invlist */
178{
179 PERL_ARGS_ASSERT_INVLIST_ITERINIT;
180
181 *get_invlist_iter_addr(invlist) = 0;
182}
183
184PERL_STATIC_INLINE void
185S_invlist_iterfinish(SV* invlist)
186{
187 /* Terminate iterator for invlist. This is to catch development errors.
188 * Any iteration that is interrupted before completed should call this
189 * function. Functions that add code points anywhere else but to the end
190 * of an inversion list assert that they are not in the middle of an
191 * iteration. If they were, the addition would make the iteration
192 * problematical: if the iteration hadn't reached the place where things
193 * were being added, it would be ok */
194
195 PERL_ARGS_ASSERT_INVLIST_ITERFINISH;
196
197 *get_invlist_iter_addr(invlist) = (STRLEN) UV_MAX;
198}
199
200STATIC bool
201S_invlist_iternext(SV* invlist, UV* start, UV* end)
202{
203 /* An C<invlist_iterinit> call on <invlist> must be used to set this up.
204 * This call sets in <*start> and <*end>, the next range in <invlist>.
205 * Returns <TRUE> if successful and the next call will return the next
206 * range; <FALSE> if was already at the end of the list. If the latter,
207 * <*start> and <*end> are unchanged, and the next call to this function
208 * will start over at the beginning of the list */
209
210 STRLEN* pos = get_invlist_iter_addr(invlist);
211 UV len = _invlist_len(invlist);
212 UV *array;
213
214 PERL_ARGS_ASSERT_INVLIST_ITERNEXT;
215
216 if (*pos >= len) {
217 *pos = (STRLEN) UV_MAX; /* Force iterinit() to be required next time */
218 return FALSE;
219 }
220
221 array = invlist_array(invlist);
222
223 *start = array[(*pos)++];
224
225 if (*pos >= len) {
226 *end = UV_MAX;
227 }
228 else {
229 *end = array[(*pos)++] - 1;
230 }
231
232 return TRUE;
233}
234
235#endif
236
237#ifndef PERL_IN_REGCOMP_C
4c60406d
KW
238
239/* These symbols are only needed later in regcomp.c */
240# undef TO_INTERNAL_SIZE
241# undef FROM_INTERNAL_SIZE
81e983c1 242#endif
3dd7db29
JK
243
244#endif /* PERL_INVLIST_INLINE_H_ */