This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Revert "regcomp.c: Use a weird value in a place where ignored"
[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
f1bcae08 9#if defined(PERL_IN_UTF8_C) || defined(PERL_IN_REGCOMP_C) || defined(PERL_IN_REGEXEC_C) || defined(PERL_IN_TOKE_C)
81e983c1 10
9e7f4f43
KW
11/* An element is in an inversion list iff its index is even numbered: 0, 2, 4,
12 * etc */
13#define ELEMENT_RANGE_MATCHES_INVLIST(i) (! ((i) & 1))
14#define PREV_RANGE_MATCHES_INVLIST(i) (! ELEMENT_RANGE_MATCHES_INVLIST(i))
15
4c60406d
KW
16/* This converts to/from our UVs to what the SV code is expecting: bytes. */
17#define TO_INTERNAL_SIZE(x) ((x) * sizeof(UV))
18#define FROM_INTERNAL_SIZE(x) ((x)/ sizeof(UV))
19
510ceaa0
KW
20PERL_STATIC_INLINE bool
21S_is_invlist(SV* const invlist)
22{
23 PERL_ARGS_ASSERT_IS_INVLIST;
24
25 return SvTYPE(invlist) == SVt_INVLIST;
26}
27
4c60406d 28PERL_STATIC_INLINE bool*
dc3bf405 29S_get_invlist_offset_addr(SV* invlist)
9e7f4f43 30{
4c60406d
KW
31 /* Return the address of the field that says whether the inversion list is
32 * offset (it contains 1) or not (contains 0) */
4c60406d 33 PERL_ARGS_ASSERT_GET_INVLIST_OFFSET_ADDR;
9e7f4f43 34
510ceaa0 35 assert(is_invlist(invlist));
f49d8074 36
4c60406d 37 return &(((XINVLIST*) SvANY(invlist))->is_offset);
9e7f4f43
KW
38}
39
40PERL_STATIC_INLINE UV
dc3bf405 41S__invlist_len(SV* const invlist)
9e7f4f43
KW
42{
43 /* Returns the current number of elements stored in the inversion list's
44 * array */
45
46 PERL_ARGS_ASSERT__INVLIST_LEN;
47
510ceaa0 48 assert(is_invlist(invlist));
f49d8074 49
4c60406d
KW
50 return (SvCUR(invlist) == 0)
51 ? 0
52 : FROM_INTERNAL_SIZE(SvCUR(invlist)) - *get_invlist_offset_addr(invlist);
9e7f4f43
KW
53}
54
55PERL_STATIC_INLINE bool
dc3bf405 56S__invlist_contains_cp(SV* const invlist, const UV cp)
9e7f4f43
KW
57{
58 /* Does <invlist> contain code point <cp> as part of the set? */
59
60 IV index = _invlist_search(invlist, cp);
61
62 PERL_ARGS_ASSERT__INVLIST_CONTAINS_CP;
63
64 return index >= 0 && ELEMENT_RANGE_MATCHES_INVLIST(index);
65}
66
551cedb5
KW
67PERL_STATIC_INLINE UV*
68S_invlist_array(SV* const invlist)
69{
70 /* Returns the pointer to the inversion list's array. Every time the
71 * length changes, this needs to be called in case malloc or realloc moved
72 * it */
73
74 PERL_ARGS_ASSERT_INVLIST_ARRAY;
75
76 /* Must not be empty. If these fail, you probably didn't check for <len>
77 * being non-zero before trying to get the array */
78 assert(_invlist_len(invlist));
79
80 /* The very first element always contains zero, The array begins either
81 * there, or if the inversion list is offset, at the element after it.
82 * The offset header field determines which; it contains 0 or 1 to indicate
83 * how much additionally to add */
84 assert(0 == *(SvPVX(invlist)));
85 return ((UV *) SvPVX(invlist) + *get_invlist_offset_addr(invlist));
86}
87
e6166da6 88# if defined(PERL_IN_REGEXEC_C)
4c60406d
KW
89
90/* These symbols are only needed later in regcomp.c */
91# undef TO_INTERNAL_SIZE
92# undef FROM_INTERNAL_SIZE
93# endif
94
81e983c1 95#endif