This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
update epigraphs for v5.22.0 RCs
[perl5.git] / inline_invlist.c
1 /*    inline_invlist.c
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.
7  */
8
9 #if defined(PERL_IN_UTF8_C) || defined(PERL_IN_REGCOMP_C) || defined(PERL_IN_REGEXEC_C)
10
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
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
20 PERL_STATIC_INLINE bool*
21 S_get_invlist_offset_addr(SV* invlist)
22 {
23     /* Return the address of the field that says whether the inversion list is
24      * offset (it contains 1) or not (contains 0) */
25     PERL_ARGS_ASSERT_GET_INVLIST_OFFSET_ADDR;
26
27     assert(SvTYPE(invlist) == SVt_INVLIST);
28
29     return &(((XINVLIST*) SvANY(invlist))->is_offset);
30 }
31
32 PERL_STATIC_INLINE UV
33 S__invlist_len(SV* const invlist)
34 {
35     /* Returns the current number of elements stored in the inversion list's
36      * array */
37
38     PERL_ARGS_ASSERT__INVLIST_LEN;
39
40     assert(SvTYPE(invlist) == SVt_INVLIST);
41
42     return (SvCUR(invlist) == 0)
43            ? 0
44            : FROM_INTERNAL_SIZE(SvCUR(invlist)) - *get_invlist_offset_addr(invlist);
45 }
46
47 PERL_STATIC_INLINE bool
48 S__invlist_contains_cp(SV* const invlist, const UV cp)
49 {
50     /* Does <invlist> contain code point <cp> as part of the set? */
51
52     IV index = _invlist_search(invlist, cp);
53
54     PERL_ARGS_ASSERT__INVLIST_CONTAINS_CP;
55
56     return index >= 0 && ELEMENT_RANGE_MATCHES_INVLIST(index);
57 }
58
59 PERL_STATIC_INLINE UV*
60 S_invlist_array(SV* const invlist)
61 {
62     /* Returns the pointer to the inversion list's array.  Every time the
63      * length changes, this needs to be called in case malloc or realloc moved
64      * it */
65
66     PERL_ARGS_ASSERT_INVLIST_ARRAY;
67
68     /* Must not be empty.  If these fail, you probably didn't check for <len>
69      * being non-zero before trying to get the array */
70     assert(_invlist_len(invlist));
71
72     /* The very first element always contains zero, The array begins either
73      * there, or if the inversion list is offset, at the element after it.
74      * The offset header field determines which; it contains 0 or 1 to indicate
75      * how much additionally to add */
76     assert(0 == *(SvPVX(invlist)));
77     return ((UV *) SvPVX(invlist) + *get_invlist_offset_addr(invlist));
78 }
79
80 #   if defined(PERL_IN_UTF8_C) || defined(PERL_IN_REGEXEC_C)
81
82 /* These symbols are only needed later in regcomp.c */
83 #       undef TO_INTERNAL_SIZE
84 #       undef FROM_INTERNAL_SIZE
85 #   endif
86
87 #endif