Commit | Line | Data |
---|---|---|
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 | ||
dbb3849a | 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) |
81e983c1 | 13 | |
9e7f4f43 KW |
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 | ||
4c60406d KW |
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 | ||
510ceaa0 KW |
23 | PERL_STATIC_INLINE bool |
24 | S_is_invlist(SV* const invlist) | |
25 | { | |
26 | PERL_ARGS_ASSERT_IS_INVLIST; | |
27 | ||
28 | return SvTYPE(invlist) == SVt_INVLIST; | |
29 | } | |
30 | ||
4c60406d | 31 | PERL_STATIC_INLINE bool* |
dc3bf405 | 32 | S_get_invlist_offset_addr(SV* invlist) |
9e7f4f43 | 33 | { |
4c60406d KW |
34 | /* Return the address of the field that says whether the inversion list is |
35 | * offset (it contains 1) or not (contains 0) */ | |
4c60406d | 36 | PERL_ARGS_ASSERT_GET_INVLIST_OFFSET_ADDR; |
9e7f4f43 | 37 | |
510ceaa0 | 38 | assert(is_invlist(invlist)); |
f49d8074 | 39 | |
4c60406d | 40 | return &(((XINVLIST*) SvANY(invlist))->is_offset); |
9e7f4f43 KW |
41 | } |
42 | ||
43 | PERL_STATIC_INLINE UV | |
dc3bf405 | 44 | S__invlist_len(SV* const invlist) |
9e7f4f43 KW |
45 | { |
46 | /* Returns the current number of elements stored in the inversion list's | |
47 | * array */ | |
48 | ||
49 | PERL_ARGS_ASSERT__INVLIST_LEN; | |
50 | ||
510ceaa0 | 51 | assert(is_invlist(invlist)); |
f49d8074 | 52 | |
4c60406d KW |
53 | return (SvCUR(invlist) == 0) |
54 | ? 0 | |
55 | : FROM_INTERNAL_SIZE(SvCUR(invlist)) - *get_invlist_offset_addr(invlist); | |
9e7f4f43 KW |
56 | } |
57 | ||
58 | PERL_STATIC_INLINE bool | |
dc3bf405 | 59 | S__invlist_contains_cp(SV* const invlist, const UV cp) |
9e7f4f43 KW |
60 | { |
61 | /* Does <invlist> contain code point <cp> as part of the set? */ | |
62 | ||
63 | IV index = _invlist_search(invlist, cp); | |
64 | ||
65 | PERL_ARGS_ASSERT__INVLIST_CONTAINS_CP; | |
66 | ||
67 | return index >= 0 && ELEMENT_RANGE_MATCHES_INVLIST(index); | |
68 | } | |
69 | ||
551cedb5 KW |
70 | PERL_STATIC_INLINE UV* |
71 | S_invlist_array(SV* const invlist) | |
72 | { | |
73 | /* Returns the pointer to the inversion list's array. Every time the | |
74 | * length changes, this needs to be called in case malloc or realloc moved | |
75 | * it */ | |
76 | ||
77 | PERL_ARGS_ASSERT_INVLIST_ARRAY; | |
78 | ||
79 | /* Must not be empty. If these fail, you probably didn't check for <len> | |
80 | * being non-zero before trying to get the array */ | |
81 | assert(_invlist_len(invlist)); | |
82 | ||
83 | /* The very first element always contains zero, The array begins either | |
84 | * there, or if the inversion list is offset, at the element after it. | |
85 | * The offset header field determines which; it contains 0 or 1 to indicate | |
86 | * how much additionally to add */ | |
87 | assert(0 == *(SvPVX(invlist))); | |
88 | return ((UV *) SvPVX(invlist) + *get_invlist_offset_addr(invlist)); | |
89 | } | |
90 | ||
e6166da6 | 91 | # if defined(PERL_IN_REGEXEC_C) |
4c60406d KW |
92 | |
93 | /* These symbols are only needed later in regcomp.c */ | |
94 | # undef TO_INTERNAL_SIZE | |
95 | # undef FROM_INTERNAL_SIZE | |
96 | # endif | |
97 | ||
81e983c1 | 98 | #endif |
3dd7db29 JK |
99 | |
100 | #endif /* PERL_INVLIST_INLINE_H_ */ |