2 tre-match-utils.h - TRE matcher helper definitions
4 This software is released under a BSD-style license.
5 See the file LICENSE for details and copyright.
9 #define str_source ((const tre_str_source*)string)
15 /* Wide character and multibyte support. */
17 #define GET_NEXT_WCHAR() \
20 if (type == STR_BYTE) \
23 if (len >= 0 && pos >= len) \
26 next_c = (unsigned char)(*str_byte++); \
28 else if (type == STR_WIDE) \
31 if (len >= 0 && pos >= len) \
34 next_c = *str_wide++; \
36 else if (type == STR_MBS) \
38 pos += pos_add_next; \
39 if (str_byte == NULL) \
56 w = tre_mbrtowc(&next_c, str_byte, (size_t)max, &mbstate); \
57 if (w == (size_t)-1 || w == (size_t)-2) \
59 if (w == 0 && len >= 0) \
73 else if (type == STR_USER) \
75 pos += pos_add_next; \
76 str_user_end = str_source->get_next_char(&next_c, &pos_add_next, \
77 str_source->context); \
79 } while(/*CONSTCOND*/0)
81 #else /* !TRE_MULTIBYTE */
83 /* Wide character support, no multibyte support. */
85 #define GET_NEXT_WCHAR() \
88 if (type == STR_BYTE) \
91 if (len >= 0 && pos >= len) \
94 next_c = (unsigned char)(*str_byte++); \
96 else if (type == STR_WIDE) \
99 if (len >= 0 && pos >= len) \
102 next_c = *str_wide++; \
104 else if (type == STR_USER) \
106 pos += pos_add_next; \
107 str_user_end = str_source->get_next_char(&next_c, &pos_add_next, \
108 str_source->context); \
110 } while(/*CONSTCOND*/0)
112 #endif /* !TRE_MULTIBYTE */
114 #else /* !TRE_WCHAR */
116 /* No wide character or multibyte support. */
118 #define GET_NEXT_WCHAR() \
121 if (type == STR_BYTE) \
124 if (len >= 0 && pos >= len) \
127 next_c = (unsigned char)(*str_byte++); \
129 else if (type == STR_USER) \
131 pos += pos_add_next; \
132 str_user_end = str_source->get_next_char(&next_c, &pos_add_next, \
133 str_source->context); \
135 } while(/*CONSTCOND*/0)
137 #endif /* !TRE_WCHAR */
141 #define IS_WORD_CHAR(c) ((c) == L'_' || tre_isalnum(c))
143 #define CHECK_ASSERTIONS(assertions) \
144 (((assertions & ASSERT_AT_BOL) \
145 && (pos > 0 || reg_notbol) \
146 && (prev_c != L'\n' || !reg_newline)) \
147 || ((assertions & ASSERT_AT_EOL) \
148 && (next_c != L'\0' || reg_noteol) \
149 && (next_c != L'\n' || !reg_newline)) \
150 || ((assertions & ASSERT_AT_BOW) \
151 && (IS_WORD_CHAR(prev_c) || !IS_WORD_CHAR(next_c))) \
152 || ((assertions & ASSERT_AT_EOW) \
153 && (!IS_WORD_CHAR(prev_c) || IS_WORD_CHAR(next_c))) \
154 || ((assertions & ASSERT_AT_WB) \
155 && (pos != 0 && next_c != L'\0' \
156 && IS_WORD_CHAR(prev_c) == IS_WORD_CHAR(next_c))) \
157 || ((assertions & ASSERT_AT_WB_NEG) \
158 && (pos == 0 || next_c == L'\0' \
159 || IS_WORD_CHAR(prev_c) != IS_WORD_CHAR(next_c))))
161 #define CHECK_CHAR_CLASSES(trans_i, tnfa, eflags) \
162 (((trans_i->assertions & ASSERT_CHAR_CLASS) \
163 && !(tnfa->cflags & REG_ICASE) \
164 && !tre_isctype((tre_cint_t)prev_c, trans_i->u.class)) \
165 || ((trans_i->assertions & ASSERT_CHAR_CLASS) \
166 && (tnfa->cflags & REG_ICASE) \
167 && !tre_isctype(tre_tolower((tre_cint_t)prev_c),trans_i->u.class) \
168 && !tre_isctype(tre_toupper((tre_cint_t)prev_c),trans_i->u.class)) \
169 || ((trans_i->assertions & ASSERT_CHAR_CLASS_NEG) \
170 && tre_neg_char_classes_match(trans_i->neg_classes,(tre_cint_t)prev_c,\
171 tnfa->cflags & REG_ICASE)))
176 /* Returns 1 if `t1' wins `t2', 0 otherwise. */
178 tre_tag_order(int num_tags, tre_tag_direction_t *tag_directions,
182 for (i = 0; i < num_tags; i++)
184 if (tag_directions[i] == TRE_TAG_MINIMIZE)
204 tre_neg_char_classes_match(tre_ctype_t *classes, tre_cint_t wc, int icase)
206 DPRINT(("neg_char_classes_test: %p, %d, %d\n", classes, wc, icase));
207 while (*classes != (tre_ctype_t)0)
208 if ((!icase && tre_isctype(wc, *classes))
209 || (icase && (tre_isctype(tre_toupper(wc), *classes)
210 || tre_isctype(tre_tolower(wc), *classes))))
211 return 1; /* Match. */
214 return 0; /* No match. */