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