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