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 #include "tre-internal.h"
11 #define str_source ((const tre_str_source*)string)
17 /* Wide character and multibyte support. */
20 * Because all multibyte encodings are exclusively single-shift encoding,
21 * with the shift codes having the high bit set, we can make an optimization
22 * for STR_MBS that only calls tre_mbrtowc_l() when a high-bit character
23 * is detected, and just do a direct copy for ASCII characters.
25 #define GET_NEXT_WCHAR() \
32 if (len >= 0 && pos >= len) \
35 next_c = (unsigned char)(*str_byte++); \
39 if (len >= 0 && pos >= len) \
42 next_c = *str_wide++; \
45 pos += pos_add_next; \
46 if (__builtin_expect(len >= 0 && pos >= len, 0)) \
51 else if (__builtin_expect(!(*str_byte & 0x80), 1)) \
53 next_c = (unsigned char)(*str_byte++); \
64 w = tre_mbrtowc_l(&next_c, str_byte, (size_t)max, &mbstate, \
66 if (w == (size_t)-1 || w == (size_t)-2) \
68 if (w == 0 && len >= 0) \
82 } while(/*CONSTCOND*/0)
84 #else /* !TRE_MULTIBYTE */
86 /* Wide character support, no multibyte support. */
87 #error TRE_MULTIBYTE undefined
89 #define GET_NEXT_WCHAR() \
92 if (type == STR_BYTE) \
95 if (len >= 0 && pos >= len) \
98 next_c = (unsigned char)(*str_byte++); \
100 else if (type == STR_WIDE) \
103 if (len >= 0 && pos >= len) \
106 next_c = *str_wide++; \
108 } while(/*CONSTCOND*/0)
110 #endif /* !TRE_MULTIBYTE */
112 #else /* !TRE_WCHAR */
114 /* No wide character or multibyte support. */
115 #error TRE_WCHAR undefined
117 #define GET_NEXT_WCHAR() \
120 if (type == STR_BYTE) \
123 if (len >= 0 && pos >= len) \
126 next_c = (unsigned char)(*str_byte++); \
128 } while(/*CONSTCOND*/0)
130 #endif /* !TRE_WCHAR */
134 /* Assumes tre_tnfa_t *tnfa in scope */
135 #define IS_WORD_CHAR(c) ((c) == L'_' || tre_isalnum_l(c, tnfa->loc))
137 #define CHECK_ASSERTIONS(assertions) \
138 (((assertions & ASSERT_AT_BOL) \
139 && (pos > 0 || reg_notbol) \
140 && (prev_c != L'\n' || !reg_newline)) \
141 || ((assertions & ASSERT_AT_EOL) \
142 && (next_c != L'\0' || reg_noteol) \
143 && (next_c != L'\n' || !reg_newline)) \
144 || ((assertions & ASSERT_AT_BOW) \
145 && (IS_WORD_CHAR(prev_c) || !IS_WORD_CHAR(next_c))) \
146 || ((assertions & ASSERT_AT_EOW) \
147 && (!IS_WORD_CHAR(prev_c) || IS_WORD_CHAR(next_c))) \
148 || ((assertions & ASSERT_AT_WB) \
149 && (pos != 0 && next_c != L'\0' \
150 && IS_WORD_CHAR(prev_c) == IS_WORD_CHAR(next_c))) \
151 || ((assertions & ASSERT_AT_WB_NEG) \
152 && (pos == 0 || next_c == L'\0' \
153 || IS_WORD_CHAR(prev_c) != IS_WORD_CHAR(next_c))))
155 #define CHECK_CHAR_CLASSES(trans_i, tnfa, eflags) \
156 ((trans_i->assertions & ASSERT_BRACKET_MATCH) \
157 && !tre_bracket_match(trans_i->u.bracket_match_list,(tre_cint_t)prev_c, \
164 tre_tag_get(const tre_tag_t
*tags
, int i
)
167 return tags
->count
> 0 ? tags
->value
: -1;
171 tre_tag_set(tre_tag_t
*tags
, int i
, int val
, int touch
)
174 if (tags
->count
++ == 0)
181 tre_tag_reset(tre_tag_t
*tags
, int i
)
187 tre_tag_touch_get(const tre_tag_t
*tags
, int i
)
189 return tags
[i
].touch
;
194 tre_print_tags(const tre_tag_t
*tags
, int num_tags
)
197 for (i
= 0; i
< num_tags
; i
++, tags
++)
202 DPRINT(("%d:(0,-1)", i
));
205 DPRINT(("%d:(1,%d)", i
, tags
->first
));
208 DPRINT(("%d:(%d,%d,%d)", i
, tags
->count
, tags
->first
,
212 if (i
< (num_tags
- 1))
218 tre_print_tags_all(const tre_tag_t
*tags
, int num_tags
)
221 for (i
= 0; i
< num_tags
; i
++, tags
++)
226 DPRINT(("%d:(0,-1)/%d", i
, tags
->touch
));
229 DPRINT(("%d:(1,%d)/%d", i
, tags
->first
, tags
->touch
));
232 DPRINT(("%d:(%d,%d,%d)/%d", i
, tags
->count
, tags
->first
,
233 tags
->value
, tags
->touch
));
236 if (i
< (num_tags
- 1))
240 #endif /* TRE_DEBUG */
242 /* Return < 0, = 0 or > 0 depending on how the start/end pairs of a minimal
243 * group between t1 and t2 compare (t1 loses if < 0, t1 wins if > 0) */
245 tre_minimal_tag_order(int start
, int end
, const tre_tag_t
*tags1
,
246 const tre_tag_t
*tags2
)
248 const tre_tag_t
*t1
, *t2
;
252 /* We need both start tags to be set */
253 if (t1
->count
== 0 || t2
->count
== 0)
256 /* The start tags must be equal */
257 if (t1
->value
!= t2
->value
)
262 /* For the end tags, we prefer set over unset, because unset means that
263 * the end tag is still growing */
266 /* if t2 is set, t1 loses since it is unset */
270 /* if t2 not set, t1 wins since it is set */
271 else if (t2
->count
== 0)
274 /* least current value wins */
275 return t2
->value
- t1
->value
;
278 /* Return < 0, = 0 or > 0 depending on how the i-th item of t1 and t2 compare
279 * (t1 loses if < 0, t1 wins if > 0) */
281 tre_tag_order_1(int i
, tre_tag_direction_t dir
, const tre_tag_t
*t1
,
290 case TRE_TAG_MINIMIZE
:
291 /* least current value wins (because tags are initialized to all zeros,
292 * unset wins over set; also, tre_minimal_tag_order() will have already
293 * been run, which checks for being unset) */
294 return t2
->value
- t1
->value
;
296 case TRE_TAG_MAXIMIZE
:
300 /* if neither t1 and t2 are set, try next tag */
303 /* t2 is set, t1 loses since it is unset */
306 /* if t2 not set, t1 wins since it is set */
307 else if (t2
->count
== 0)
309 /* greatest initial value wins */
310 if ((diff
= t1
->first
- t2
->first
) != 0)
312 /* least number of times the tag was set, wins */
313 if ((diff
= t2
->count
- t1
->count
) != 0)
315 /* if the tags were only set once, they only have initial values */
318 /* greatest current value wins */
319 return t1
->value
- t2
->value
;
321 case TRE_TAG_LEFT_MAXIMIZE
:
325 /* if neither t1 and t2 are set, try next tag */
328 /* t2 is set, t1 loses since it is unset */
331 /* if t2 not set, t1 wins since it is set */
332 else if (t2
->count
== 0)
334 /* least initial value wins */
335 if ((diff
= t2
->first
- t1
->first
) != 0)
337 /* least number of times the tag was set, wins */
338 if ((diff
= t2
->count
- t1
->count
) != 0)
340 /* if the tags were only set once, they only have initial values */
343 /* greatest current value wins */
344 return t1
->value
- t2
->value
;
347 /* Shouldn't happen: only assert if TRE_DEBUG defined */
355 #define _MORE_DEBUGGING
356 #endif /* TRE_DEBUG */
358 /* Returns 1 if `t1' wins `t2', 0 otherwise. */
360 #ifdef _MORE_DEBUGGING
361 _tre_tag_order(int num_tags
, tre_tag_direction_t
*tag_directions
,
362 const tre_tag_t
*t1
, const tre_tag_t
*t2
)
363 #else /* !_MORE_DEBUGGING */
364 tre_tag_order(int num_tags
, tre_tag_direction_t
*tag_directions
,
365 const tre_tag_t
*t1
, const tre_tag_t
*t2
)
366 #endif /* !_MORE_DEBUGGING */
370 for (i
= 0; i
< num_tags
; i
++)
372 if ((ret
= tre_tag_order_1(i
, tag_directions
[i
], t1
, t2
)) != 0)
379 #ifdef _MORE_DEBUGGING
381 tre_tag_order(int num_tags
, tre_tag_direction_t
*tag_directions
,
382 const tre_tag_t
*t1
, const tre_tag_t
*t2
)
384 int ret
= _tre_tag_order(num_tags
, tag_directions
, t1
, t2
);
385 DPRINT(("tre_tag_order: "));
386 tre_print_tags(t1
, num_tags
);
387 DPRINT((" %s ", ret
? "wins" : "doesn't win"));
388 tre_print_tags(t2
, num_tags
);
392 #endif /* _MORE_DEBUGGING */
394 int __collate_equiv_value(locale_t loc
, const wchar_t *str
, size_t len
);
397 tre_bracket_match(tre_bracket_match_list_t
* __restrict list
, tre_cint_t wc
,
398 const tre_tnfa_t
* __restrict tnfa
)
402 tre_bracket_match_t
*b
;
404 int we
, ue
, le
, got_equiv
= 0;
405 int icase
= ((tnfa
->cflags
& REG_ICASE
) != 0);
407 DPRINT(("tre_bracket_match: %p, %d, %d\n", list
, wc
, icase
));
410 if (tre_islower_l(wc
, tnfa
->loc
))
413 uc
= tre_toupper_l(wc
, tnfa
->loc
);
415 else if (tre_isupper_l(wc
, tnfa
->loc
))
418 lc
= tre_tolower_l(wc
, tnfa
->loc
);
425 for (i
= 0, b
= list
->bracket_matches
; i
< list
->num_bracket_matches
;
430 case TRE_BRACKET_MATCH_TYPE_CHAR
:
432 match
= (b
->value
== uc
|| b
->value
== lc
);
434 match
= (b
->value
== wc
);
436 case TRE_BRACKET_MATCH_TYPE_RANGE_BEGIN
:
438 tre_cint_t start
= b
->value
, end
;
439 if (++i
>= list
->num_bracket_matches
||
440 (++b
)->type
!= TRE_BRACKET_MATCH_TYPE_RANGE_END
)
442 DPRINT(("tre_bracket_match: no following range end\n"));
451 ue
= __collate_equiv_value(tnfa
->loc
, &uc
, 1);
452 le
= __collate_equiv_value(tnfa
->loc
, &lc
, 1);
455 we
= __collate_equiv_value(tnfa
->loc
, &wc
, 1);
459 match
= ((start
<= ue
&& ue
<= end
) ||
460 (start
<= le
&& le
<= end
));
462 match
= (start
<= we
&& we
<= end
);
465 case TRE_BRACKET_MATCH_TYPE_RANGE_END
:
466 DPRINT(("tre_bracket_match: range end without preceeding start\n"));
469 case TRE_BRACKET_MATCH_TYPE_CLASS
:
471 match
= (tre_isctype_l(uc
, b
->value
, tnfa
->loc
) ||
472 tre_isctype_l(lc
, b
->value
, tnfa
->loc
));
474 match
= (tre_isctype_l(wc
, b
->value
, tnfa
->loc
));
476 case TRE_BRACKET_MATCH_TYPE_EQUIVALENCE
:
481 ue
= __collate_equiv_value(tnfa
->loc
, &uc
, 1);
482 le
= __collate_equiv_value(tnfa
->loc
, &lc
, 1);
485 we
= __collate_equiv_value(tnfa
->loc
, &wc
, 1);
489 match
= (b
->value
== ue
|| b
->value
== le
);
491 match
= (b
->value
== we
);
494 DPRINT(("tre_bracket_match: unknown type %d\n", b
->type
));
502 if (list
->flags
& TRE_BRACKET_MATCH_FLAG_NEGATE
) {
503 if ((tnfa
->cflags
& REG_NEWLINE
) && wc
== '\n') return 0;