annotate th_regex.c @ 649:2c9260f5cf44

Tweedle.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 25 Jan 2020 11:35:59 +0200
parents 91c43398c6fc
children 18fe45e61b2b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Simple regular expression matching functionality
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2020 Tecnic Software productions (TNSP)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "th_util.h"
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include "th_regex.h"
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11
635
d191ded8a790 Improve the experimental regex matching debugging macros.
Matti Hamalainen <ccr@tnsp.org>
parents: 614
diff changeset
12 #ifdef TH_EXPERIMENTAL_REGEX_DEBUG
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
13 BOOL th_dbg_re_flags = FALSE;
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
14
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
15 # define DBG_RE_MATCH(...) do { \
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
16 if (th_dbg_re_flags) \
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
17 { \
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
18 th_regex_dump_indent(stdout, level); \
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
19 fprintf(stdout, __VA_ARGS__); \
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
20 } \
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
21 } while (0)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
22 #else
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
23 # define DBG_RE_MATCH(...)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
24 #endif
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
25
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
26
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
27 #ifdef TH_EXPERIMENTAL_REGEX
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
28
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
29 enum
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
30 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
31 TH_RE_MATCH_ONCE,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
32 TH_RE_MATCH_COUNT,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
33 TH_RE_MATCH_ANCHOR_START,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
34 TH_RE_MATCH_ANCHOR_END,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
35 };
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
36
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
37
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
38 enum
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
39 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
40 TH_RE_TYPE_CHAR,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
41 TH_RE_TYPE_STR,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
42 TH_RE_TYPE_ANY_CHAR,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
43 TH_RE_TYPE_LIST,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
44 TH_RE_TYPE_LIST_REVERSE,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
45 TH_RE_TYPE_SUBEXPR,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
46 };
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
47
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
48
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
49 static const char *re_match_modes[] =
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
50 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
51 "ONCE",
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
52 "COUNT",
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
53 "ANCHOR START",
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
54 "ANCHOR END",
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
55 };
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
56
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
57
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
58 static const char *re_match_types[] =
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
59 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
60 "CHAR",
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
61 "STR",
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
62 "ANY",
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
63 "LIST",
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
64 "LIST REVERSE",
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
65 "SUBEXPR",
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
66 };
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
67
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
69 typedef struct
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
70 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
71 int type;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
72 th_regex_char_t start, end;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
73
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
74 size_t nchars;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
75 th_regex_char_t *chars;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
76 } th_regex_list_item_t;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
77
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
78
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
79 typedef struct
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
80 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
81 size_t nitems, itemssize;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
82 th_regex_list_item_t *items;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
83 } th_regex_list_t;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
84
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
85
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
86 typedef struct
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
87 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
88 int mode, type;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
89 ssize_t repeatMin, repeatMax;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
90
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
91 struct {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
92 th_regex_char_t chr;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
93 th_regex_char_t *str;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
94 th_regex_list_t list;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
95
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
96 th_regex_t *expr;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
97 } match;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
98 } th_regex_node_t;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
99
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
100
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
101 struct th_regex_t
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
102 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
103 size_t nnodes, nodessize;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
104 th_regex_node_t *nodes;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
105 };
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
106
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
107
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
108 typedef struct
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
110 const th_regex_char_t *pattern;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
111 size_t offs;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
113 th_regex_t *data;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
114
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
115 size_t nstack, stacksize;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
116 th_regex_t **stack;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
117
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
118 th_regex_char_t *buf;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
119 size_t bufSize, bufPos;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
120
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
121 } th_regex_parse_ctx_t;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
124 static void th_regex_node_init(th_regex_node_t *node)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
126 memset(node, 0, sizeof(th_regex_node_t));
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 node->mode = TH_RE_MATCH_ONCE;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
131 static int th_regex_strndup(th_regex_char_t **pdst,
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
132 const th_regex_char_t *src, const size_t len)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 if (pdst == NULL)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 return THERR_NULLPTR;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
137 if (UINTPTR_MAX / sizeof(th_regex_char_t) < len + 1)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 return THERR_BOUNDS;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
140 if ((*pdst = (th_regex_char_t *)
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
141 th_malloc((len + 1) * sizeof(th_regex_char_t))) == NULL)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 return THERR_MALLOC;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
144 memcpy(*pdst, src, len * sizeof(th_regex_char_t));
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 (*pdst)[len] = 0;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 return THERR_OK;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
151 static int th_regex_parse_ctx_get_prev_node(
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
152 th_regex_parse_ctx_t *ctx, th_regex_node_t **pnode)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 if (ctx->data != NULL && ctx->data->nnodes > 0)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 *pnode = &ctx->data->nodes[ctx->data->nnodes - 1];
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 return THERR_OK;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 else
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 return THERR_INVALID_DATA;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
164 static int th_regex_parse_ctx_push(th_regex_parse_ctx_t *ctx)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 if (ctx->stack == NULL || ctx->nstack + 1 >= ctx->stacksize)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 ctx->stacksize += 16;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 if ((ctx->stack = th_realloc(ctx->stack,
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
171 ctx->stacksize * sizeof(th_regex_node_t *))) == NULL)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 return THERR_MALLOC;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 ctx->stack[ctx->nstack] = ctx->data;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 ctx->nstack++;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 ctx->data = NULL;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 return THERR_OK;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
183 static int th_regex_parse_ctx_pop(th_regex_parse_ctx_t *ctx, th_regex_t **data)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185 if (ctx->nstack > 0)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 *data = ctx->data;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 ctx->nstack--;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 ctx->data = ctx->stack[ctx->nstack];
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190 return THERR_OK;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 else
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193 return THERR_INVALID_DATA;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
197 static int th_regex_parse_ctx_node_commit(th_regex_parse_ctx_t *ctx, th_regex_node_t *node)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
199 th_regex_t *data = ctx->data;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201 if (data == NULL)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
203 if ((data = ctx->data = th_malloc0(sizeof(th_regex_t))) == NULL)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 return THERR_MALLOC;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207 if (data->nodes == NULL || data->nnodes + 1 >= data->nodessize)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209 data->nodessize += 16;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 if ((data->nodes = th_realloc(data->nodes,
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
211 data->nodessize * sizeof(th_regex_node_t))) == NULL)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 return THERR_MALLOC;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
215 memcpy(&data->nodes[data->nnodes], node, sizeof(th_regex_node_t));
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 data->nnodes++;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 return THERR_OK;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
222 static BOOL th_regex_find_next(const th_regex_char_t *str,
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 const size_t start, size_t *offs,
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
224 const th_regex_char_t delim)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 for (*offs = start; str[*offs] != 0; (*offs)++)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 if (str[*offs] == delim)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 return TRUE;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 return FALSE;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
235 static BOOL th_regex_parse_ssize_t(const th_regex_char_t *str,
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 ssize_t *value)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
238 th_regex_char_t ch;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 BOOL neg;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241 if (*str == '-')
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 str++;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 neg = TRUE;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 else
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 neg = FALSE;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 // Is the value negative?
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250 while ((ch = *str++))
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 if (ch >= '0' && ch <= '9')
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 *value *= 10;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 *value += ch - '0';
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 else
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 return FALSE;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 if (neg)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 *value = -(*value);
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 return TRUE;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
268 static void th_regex_list_item_init(th_regex_list_item_t *item)
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
269 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
270 memset(item, 0, sizeof(th_regex_list_item_t));
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
271 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
272
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
273
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
274 static int th_regex_list_add_item(th_regex_list_t *list, th_regex_list_item_t *item)
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
275 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
276 if (list->items == NULL || list->nitems + 1 >= list->itemssize)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
277 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
278 list->itemssize += 16;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
279
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
280 if ((list->items = th_realloc(list->items,
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
281 list->itemssize * sizeof(th_regex_list_item_t))) == NULL)
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
282 return THERR_MALLOC;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
283 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
284
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
285 memcpy(&list->items[list->nitems], item, sizeof(th_regex_list_item_t));
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
286 list->nitems++;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
287
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
288 return THERR_OK;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
289 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
290
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
291
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
292 static void th_regex_list_free(th_regex_list_t *list)
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
293 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
294 if (list != NULL)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
295 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
296 for (size_t n = 0; n < list->nitems; n++)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
297 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
298 th_free(list->items[n].chars);
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
299 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
300 th_free(list->items);
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
301 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
302 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
303
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
304
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
305 static int th_regex_parse_list(const th_regex_char_t *str,
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
306 const size_t slen, th_regex_list_t *list)
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
307 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
308 th_regex_char_t *tmp = NULL;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
309 th_regex_list_item_t item;
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
310 int res;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
311
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
312 if ((res = th_regex_strndup(&tmp, str, slen)) != THERR_OK)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
313 goto out;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
314
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
315 // Handle ranges like [A-Z]
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
316 for (size_t offs = 0; offs < slen; offs++)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
317 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
318 th_regex_char_t
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
319 *prev = (offs > 0) ? tmp + offs - 1 : NULL,
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
320 *curr = tmp + offs,
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
321 *next = (offs + 1 < slen) ? tmp + offs + 1 : NULL;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
322
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
323 if (*curr == '-')
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
324 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
325 if (prev != NULL && next != NULL)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
326 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
327 // Range
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
328 th_regex_list_item_init(&item);
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
329 item.type = 1;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
330 item.start = *prev;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
331 item.end = *next;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
332
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
333 if (item.start >= item.end)
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
334 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
335 res = THERR_INVALID_DATA;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
336 goto out;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
337 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
338
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
339 *curr = *prev = *next = 0;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
340
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
341 if ((res = th_regex_list_add_item(list, &item)) != THERR_OK)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
342 goto out;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
343 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
344 else
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
345 if (next != NULL)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
346 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
347 res = THERR_INVALID_DATA;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
348 goto out;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
349 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
350 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
351 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
352
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
353 // Count number of remaining characters
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
354 th_regex_list_item_init(&item);
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
355 item.type = 0;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
356 item.nchars = 0;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
357
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
358 for (size_t offs = 0; offs < slen; offs++)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
359 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
360 th_regex_char_t curr = tmp[offs];
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
361 if (curr != 0)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
362 item.nchars++;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
363 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
364
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
365 if (item.nchars > 0)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
366 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
367 if ((item.chars = th_malloc(sizeof(th_regex_char_t) * item.nchars)) == NULL)
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
368 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
369 res = THERR_MALLOC;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
370 goto out;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
371 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
372
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
373 for (size_t offs = 0, n = 0; offs < slen; offs++)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
374 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
375 th_regex_char_t curr = tmp[offs];
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
376 if (curr != 0)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
377 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
378 item.chars[n] = curr;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
379 n++;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
380 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
381 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
382
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
383 if ((res = th_regex_list_add_item(list, &item)) != THERR_OK)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
384 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
385 th_free(item.chars);
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
386 goto out;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
387 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
388 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
389
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
390 out:
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
391 th_free(tmp);
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
392 return res;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
393 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
394
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
395
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
396 static int th_regex_parse_ctx_node_commit_strchr_do(th_regex_parse_ctx_t *ctx,
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
397 const th_regex_char_t *buf, const size_t bufLen)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
398 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
399 th_regex_node_t node;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
400 th_regex_node_init(&node);
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
401
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
402 if (bufLen > 1)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
403 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
404 int res;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
405 node.type = TH_RE_TYPE_STR;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
406 if ((res = th_regex_strndup(&node.match.str, buf, bufLen)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
407 return res;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
408 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
409 else
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
410 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
411 node.type = TH_RE_TYPE_CHAR;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
412 node.match.chr = buf[0];
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
413 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
414
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
415 return th_regex_parse_ctx_node_commit(ctx, &node);
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
416 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
417
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
418
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
419 static int th_regex_parse_ctx_node_commit_strchr(th_regex_parse_ctx_t *ctx, const BOOL split)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
420 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
421 int res = THERR_OK;;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
422
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
423 if (ctx->bufPos > 0)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
424 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
425 if (ctx->bufPos > 1 && split)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
426 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
427 if ((res = th_regex_parse_ctx_node_commit_strchr_do(ctx, ctx->buf, ctx->bufPos - 1)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
428 return res;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
429
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
430 res = th_regex_parse_ctx_node_commit_strchr_do(ctx, ctx->buf + ctx->bufPos - 1, 1);
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
431 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
432 else
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
433 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
434 res = th_regex_parse_ctx_node_commit_strchr_do(ctx, ctx->buf, ctx->bufPos);
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
435 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
436 ctx->bufPos = 0;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
437 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
438
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
439 return res;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
440 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
441
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
442
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
443 int th_regex_compile(th_regex_t **pexpr, const th_regex_char_t *pattern)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 int res = THERR_OK;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
446 th_regex_parse_ctx_t ctx;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
447 th_regex_node_t node, *pnode;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
448 th_regex_char_t *tmp = NULL;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 size_t start;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
451 // Check pointers
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
452 if (pexpr == NULL || pattern == NULL)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 {
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
454 res = THERR_NULLPTR;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
455 goto out;
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
456 }
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
457
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
458 // Initialize parsing context
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
459 memset(&ctx, 0, sizeof(ctx));
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
460 ctx.pattern = pattern;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
461 ctx.bufSize = 256;
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
462
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
463 if ((ctx.buf = th_malloc(ctx.bufSize * sizeof(th_regex_char_t))) == NULL)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
464 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
465 res = THERR_MALLOC;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
466 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
467 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
468
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
469 // Start parsing the pattern
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
470 for (; ctx.pattern[ctx.offs] != 0; ctx.offs++)
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
471 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
472 th_regex_char_t cch = ctx.pattern[ctx.offs];
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
473
613
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
474 switch (cch)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476 case '?':
613
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
477 case '*':
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
478 case '+':
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
479 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, TRUE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
480 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
481
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
482 if ((res = th_regex_parse_ctx_get_prev_node(&ctx, &pnode)) != THERR_OK)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
483 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484
613
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
485 if (cch == '?')
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
486 {
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
487 // Previous token is optional (repeat 0-1 times) (non-greedy matching)
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
488 pnode->mode = TH_RE_MATCH_COUNT;
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
489 pnode->repeatMin = 0;
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
490 pnode->repeatMax = 1;
613
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
491 }
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
492 else
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
493 {
641
9a1ed82abefd Fix parsing of +? and *?.
Matti Hamalainen <ccr@tnsp.org>
parents: 640
diff changeset
494 // Check if previous was a count ("**", "*+", etc.)
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
495 if (pnode->mode == TH_RE_MATCH_COUNT)
613
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
496 {
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
497 res = THERR_INVALID_DATA;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
498 goto out;
613
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
499 }
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
501 pnode->mode = TH_RE_MATCH_COUNT;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502
613
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
503 if (cch == '*')
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
504 {
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
505 // Previous token can repeat 0 or more times
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
506 pnode->repeatMin = 0;
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
507 pnode->repeatMax = -1;
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
508 }
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
509 else
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
510 {
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
511 // Previous token must repeat 1 or more times
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
512 pnode->repeatMin = 1;
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
513 pnode->repeatMax = -1;
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
514 }
2e3b81ae8c8a More work on regexes.
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
515 }
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 case '{':
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
519 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, TRUE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
520 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
521
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522 // {n} | {min,max}
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
523 start = ctx.offs + 1;
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
524 if (!th_regex_find_next(ctx.pattern, start, &ctx.offs, '}'))
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 // End not found
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 res = THERR_INVALID_DATA;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
528 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 th_free(tmp);
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
533 if ((res = th_regex_parse_ctx_get_prev_node(&ctx, &pnode)) != THERR_OK ||
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
534 (res = th_regex_strndup(&tmp, ctx.pattern + start,
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
535 ctx.offs - start)) != THERR_OK)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
536 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
538 pnode->mode = TH_RE_MATCH_COUNT;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 if (th_regex_find_next(tmp, 0, &start, ','))
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 tmp[start] = 0;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 if (!th_regex_parse_ssize_t(tmp, &pnode->repeatMin) ||
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 !th_regex_parse_ssize_t(tmp + start + 1, &pnode->repeatMax))
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 res = THERR_INVALID_DATA;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
547 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 else
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 if (!th_regex_parse_ssize_t(tmp, &pnode->repeatMin))
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554 res = THERR_INVALID_DATA;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
555 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 pnode->repeatMax = pnode->repeatMin;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 if (pnode->repeatMin < 0 || pnode->repeatMax < 1 ||
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 pnode->repeatMax < pnode->repeatMin)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 // Invalid repeat counts
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 res = THERR_INVALID_DATA;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
565 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568
648
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
569 /*
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
570 case '|':
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
571 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
572 goto out;
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
573
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
574 // Alt pattern .. how to handle these?
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
575 break;
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
576 */
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
577
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 case '(':
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
579 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
580 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
581
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 // Start of subpattern
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
583 if ((res = th_regex_parse_ctx_push(&ctx)) != THERR_OK)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
584 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 case ')':
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
588 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
589 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
590
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 // End of subpattern
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 th_regex_node_init(&node);
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 node.type = TH_RE_TYPE_SUBEXPR;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
595 if ((res = th_regex_parse_ctx_pop(&ctx, &node.match.expr)) != THERR_OK ||
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
596 (res = th_regex_parse_ctx_node_commit(&ctx, &node)) != THERR_OK)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
597 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600 case '^':
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
601 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
602 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
603
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 // Start of line anchor
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 th_regex_node_init(&node);
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 node.mode = TH_RE_MATCH_ANCHOR_START;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
608 if ((res = th_regex_parse_ctx_node_commit(&ctx, &node)) != THERR_OK)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
609 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
610 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
611
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612 case '$':
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
613 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
614 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
615
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616 // End of line anchor
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 th_regex_node_init(&node);
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618 node.mode = TH_RE_MATCH_ANCHOR_END;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
620 if ((res = th_regex_parse_ctx_node_commit(&ctx, &node)) != THERR_OK)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
621 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624 case '[':
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
625 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
626 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
627
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 // Start of char list
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
629 start = ctx.offs + 1;
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
630 if (!th_regex_find_next(ctx.pattern, start, &ctx.offs, ']') ||
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
631 ctx.offs == start)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 res = THERR_INVALID_DATA;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
634 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
637 th_regex_node_init(&node);
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
638 if (ctx.pattern[start] == '^')
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
639 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
640 node.type = TH_RE_TYPE_LIST_REVERSE;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
641 start++;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
642 }
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
643 else
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
644 node.type = TH_RE_TYPE_LIST;
638
c4bca120bfb0 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 635
diff changeset
645
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
646 if ((res = th_regex_parse_list(ctx.pattern + start,
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
647 ctx.offs - start, &node.match.list)) != THERR_OK ||
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
648 (res = th_regex_parse_ctx_node_commit(&ctx, &node)) != THERR_OK)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
649 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
652 case '.':
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
653 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
654 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
655
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
656 // Any single character matches
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657 th_regex_node_init(&node);
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
658 node.type = TH_RE_TYPE_ANY_CHAR;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
660 if ((res = th_regex_parse_ctx_node_commit(&ctx, &node)) != THERR_OK)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
661 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
662 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
664 case '\\':
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 // Literal escape
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
666 ctx.offs++;
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
667 if (ctx.pattern[ctx.offs] == 0)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
668 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
669 // End of pattern, error
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670 res = THERR_INVALID_DATA;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
671 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
672 }
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
673 // fall-through
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675 default:
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676 // Given character must match
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
677 if (ctx.bufPos < ctx.bufSize)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
678 ctx.buf[ctx.bufPos++] = ctx.pattern[ctx.offs];
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
679 else
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
680 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
681 goto out;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
686 // Commit last string/char if any
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
687 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
688 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
689
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
690 // Create root node
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
691 th_regex_node_init(&node);
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
692 node.type = TH_RE_TYPE_SUBEXPR;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
693 node.match.expr = ctx.data;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
694 ctx.data = NULL;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
695
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
696 if ((res = th_regex_parse_ctx_node_commit(&ctx, &node)) != THERR_OK)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
697 goto out;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
698
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
699 out:
611
d895b0fd6ad6 Combine code from th_regex_compile() to th_regex_compile_do().
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
700 *pexpr = ctx.data;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
701
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
702 // Free temporary buffers
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
703 th_free(tmp);
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
704 th_free(ctx.buf);
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 return res;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
709 void th_regex_free(th_regex_t *expr)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711 if (expr != NULL)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
713 for (size_t nnode = 0; nnode < expr->nnodes; nnode++)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
714 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
715 th_regex_node_t *node = &expr->nodes[nnode];
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
716
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
717 th_regex_free(node->match.expr);
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
718 th_regex_list_free(&node->match.list);
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
719 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
720
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
721 th_free(expr->nodes);
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
722 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
724
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
725
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
726 static void th_regex_dump_indent(FILE *fh, const int level)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
727 {
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
728 for (int indent = 0; indent < level; indent++)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
729 fprintf(fh, " ");
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
730 }
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
731
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
732
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
733 static void th_regex_dump_node(FILE *fh, const th_regex_node_t *node)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
734 {
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
735 fprintf(fh,
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
736 "%s %s ",
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
737 re_match_modes[node->mode],
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
738 re_match_types[node->type]);
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
739
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
740 if (node->mode == TH_RE_MATCH_COUNT)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
741 {
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
742 fprintf(fh, "min=%" PRId_SSIZE_T ", max=%" PRId_SSIZE_T " : ",
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
743 node->repeatMin, node->repeatMax);
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
744 }
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
745
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
746 switch (node->type)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
747 {
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
748 case TH_RE_TYPE_CHAR:
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
749 fprintf(fh, "'%c'", node->match.chr);
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
750 break;
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
751
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
752 case TH_RE_TYPE_STR:
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
753 fprintf(fh, "\"%s\"", node->match.str);
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
754 break;
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
755
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
756 case TH_RE_TYPE_ANY_CHAR:
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
757 fprintf(fh, ".");
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
758 break;
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
759
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
760 case TH_RE_TYPE_LIST:
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
761 case TH_RE_TYPE_LIST_REVERSE:
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
762 fprintf(fh, "[ ");
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
763 for (size_t n = 0; n < node->match.list.nitems; n++)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
764 {
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
765 const th_regex_list_item_t *li = &node->match.list.items[n];
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
766 if (li->type)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
767 {
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
768 fprintf(fh, "'%c-%c' ", li->start, li->end);
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
769 }
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
770 else
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
771 {
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
772 for (size_t i = 0; i < li->nchars; i++)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
773 fprintf(fh, "'%c' ", li->chars[i]);
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
774 }
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
775 }
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
776 fprintf(fh, "]");
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
777 break;
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
778 }
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
779 }
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
780
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
781
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
782 void th_regex_dump(FILE *fh, const int level, const th_regex_t *expr)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
783 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
784 if (expr != NULL)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
785 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
786 for (size_t nnode = 0; nnode < expr->nnodes; nnode++)
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
787 {
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
788 th_regex_node_t *node = &expr->nodes[nnode];
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
789
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
790 th_regex_dump_indent(fh, level);
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
791
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
792 fprintf(fh,
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
793 "[%" PRIu_SIZE_T "/%" PRIu_SIZE_T "] ",
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
794 nnode + 1, expr->nnodes);
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
795
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
796 th_regex_dump_node(fh, node);
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
797 fprintf(fh, "\n");
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
798
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
799 if (node->type == TH_RE_TYPE_SUBEXPR)
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
800 th_regex_dump(fh, level + 1, node->match.expr);
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
801 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
802 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
803 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
804
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
805
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
806 static BOOL th_regex_match_list(const th_regex_list_t *list, const th_regex_char_t cch)
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
807 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
808 // Could be optimized, perhaps .. sort match.chars, binary search etc?
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
809 for (size_t nitem = 0; nitem < list->nitems; nitem++)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
810 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
811 const th_regex_list_item_t *item = &list->items[nitem];
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
812
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
813 if (item->type == 0)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
814 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
815 for (size_t n = 0; n < item->nchars; n++)
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
816 {
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
817 if (item->chars[n] == cch)
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
818 return TRUE;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
819 }
639
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
820 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
821 else
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
822 {
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
823 if (cch >= item->start && cch <= item->end)
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
824 return TRUE;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
825 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
826 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
827
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
828 return FALSE;
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
829 }
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
830
8c957ad9d4c3 Some more work on regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 638
diff changeset
831
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
832 static BOOL th_regex_match_expr(
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
833 const th_regex_char_t *haystack,
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
834 size_t *offs,
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
835 const th_regex_t *expr,
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
836 const size_t startnode,
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
837 const int flags,
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
838 const int level
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
839 );
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
840
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
841
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
842 static BOOL th_regex_match_one(
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
843 const th_regex_char_t *haystack,
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
844 size_t *offs,
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
845 const th_regex_node_t *node,
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
846 const int flags,
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
847 const int level
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
848 )
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
849 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
850 th_regex_char_t cch;
638
c4bca120bfb0 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 635
diff changeset
851 BOOL res = FALSE;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
852
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
853 switch (node->type)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
854 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
855 case TH_RE_TYPE_SUBEXPR:
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
856 res = th_regex_match_expr(haystack, offs, node->match.expr, 0, flags, level + 1);
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
857 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
858
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
859 case TH_RE_TYPE_LIST:
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
860 case TH_RE_TYPE_LIST_REVERSE:
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
861 if ((cch = haystack[*offs]) == 0)
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
862 res = FALSE;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
863 else
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
864 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
865 res = th_regex_match_list(&node->match.list, cch);
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
867 if (node->type == TH_RE_TYPE_LIST_REVERSE)
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
868 res = !res;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
869
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
870 (*offs)++;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
871 }
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
872 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
873
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
874 case TH_RE_TYPE_ANY_CHAR:
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
875 if ((cch = haystack[*offs]) == 0)
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
876 res = FALSE;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
877 else
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
878 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
879 res = TRUE;
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
880 (*offs)++;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
881 }
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
882 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
883
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
884 case TH_RE_TYPE_CHAR:
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
885 if ((cch = haystack[*offs]) == 0)
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
886 res = FALSE;
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
887 else
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
888 {
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
889 res = (cch == node->match.chr);
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
890 (*offs)++;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
891 }
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
892 break;
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
893
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
894 case TH_RE_TYPE_STR:
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
895 res = TRUE;
648
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
896 for (th_regex_char_t *str = node->match.str;
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
897 res && *str != 0;
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
898 str++, (*offs)++)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
899 {
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
900 if (haystack[*offs] != *str)
645
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
901 res = FALSE;
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
902 }
b897995101b7 More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
Matti Hamalainen <ccr@tnsp.org>
parents: 643
diff changeset
903 break;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
904 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
905
638
c4bca120bfb0 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 635
diff changeset
906 return res;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
907 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
908
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
909
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
910 static BOOL th_regex_match_count(
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
911 const th_regex_char_t *haystack,
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
912 size_t *offs,
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
913 const th_regex_t *expr,
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
914 const th_regex_node_t *node,
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
915 size_t *nnode,
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
916 const int flags,
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
917 const int level
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
918 )
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
919 {
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
920 size_t toffs = *offs, noffs;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
921 BOOL res, match = FALSE;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
922 ssize_t count = 0;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
923
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
924 if (node->repeatMin > 0)
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
925 do
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
926 {
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
927 noffs = toffs;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
928 match = th_regex_match_one(haystack, &toffs, node, flags, level);
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
929 if (match)
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
930 {
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
931 count++;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
932 }
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
933 else
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
934 toffs = noffs;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
935
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
936 if (node->repeatMin >= 0 &&
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
937 count >= node->repeatMin &&
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
938 node->repeatMax > 0 &&
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
939 count >= node->repeatMax)
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
940 break;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
941
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
942 } while (match && toffs > noffs);
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
943
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
944 if (count > 0 || node->repeatMin == 0)
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
945 {
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
946 DBG_RE_MATCH("count=%" PRId_SSIZE_T " \"%s\"\n",
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
947 count, haystack + toffs);
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
948
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
949 match = th_regex_match_expr(haystack, &toffs, expr, *nnode + 1, flags, level + 1);
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
950
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
951 DBG_RE_MATCH("rest expr match=%s \"%s\"\n",
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
952 match ? "YES" : "NO", haystack + toffs);
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
953 }
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
954
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
955 if (match)
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
956 {
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
957 *offs = toffs;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
958 *nnode = expr->nnodes;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
959 }
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
960
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
961 res = match &&
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
962 (
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
963 (node->repeatMax > 0 && count >= node->repeatMax) ||
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
964 (node->repeatMin >= 0 && count >= node->repeatMin)
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
965 );
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
966
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
967 DBG_RE_MATCH("RESULT: match=%s, res=%s\n",
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
968 match ? "YES" : "NO", res ? "YES" : "NO");
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
969
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
970 return res;
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
971 }
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
972
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
973
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
974 static BOOL th_regex_match_expr(
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
975 const th_regex_char_t *haystack,
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
976 size_t *offs,
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
977 const th_regex_t *expr,
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
978 const size_t startnode,
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
979 const int flags,
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
980 const int level
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
981 )
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
982 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
983 BOOL res = TRUE;
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
984 size_t soffs = *offs;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
985
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
986 for (size_t nnode = startnode; res && nnode < expr->nnodes; nnode++)
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
987 {
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
988 const th_regex_node_t *node = &expr->nodes[nnode];
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
989
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
990 #ifdef TH_EXPERIMENTAL_REGEX_DEBUG
648
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
991 if (th_dbg_re_flags)
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
992 {
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
993 th_regex_dump_indent(stdout, level);
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
994 fprintf(stdout,
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
995 "[%" PRIu_SIZE_T "/%" PRIu_SIZE_T "] ",
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
996 nnode + 1, expr->nnodes);
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
997
648
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
998 th_regex_dump_node(stdout, node);
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
999
648
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
1000 fprintf(stdout, " <-> \"%s\"\n",
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
1001 haystack + soffs);
91c43398c6fc Twiddle.
Matti Hamalainen <ccr@tnsp.org>
parents: 647
diff changeset
1002 }
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
1003 #endif
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1004
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1005 switch (node->mode)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1006 {
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1007 case TH_RE_MATCH_ONCE:
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
1008 res = th_regex_match_one(haystack, &soffs, node, flags, level);
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1009 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1010
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
1011 case TH_RE_MATCH_COUNT:
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
1012 res = th_regex_match_count(haystack, &soffs, expr, node, &nnode, flags, level);
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1013 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1014
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1015 case TH_RE_MATCH_ANCHOR_START:
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
1016 res = (soffs == 0);
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1017 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1018
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1019 case TH_RE_MATCH_ANCHOR_END:
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
1020 res = (haystack[soffs] == 0);
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1021 break;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1022 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1023 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1024
643
a2bf1ea05b05 Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 642
diff changeset
1025 if (res)
649
2c9260f5cf44 Tweedle.
Matti Hamalainen <ccr@tnsp.org>
parents: 648
diff changeset
1026 *offs = soffs;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
1027
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
1028 return res;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1029 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1030
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1031
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
1032 int th_regex_match(const th_regex_t *expr, const th_regex_char_t *haystack,
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
1033 size_t *pnmatches, th_regex_match_t **pmatches, const size_t maxmatches,
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1034 const int flags)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1035 {
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1036 size_t nmatches = 0;
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
1037 int level = 0;
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
1038 (void) flags;
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1039
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1040 if (pnmatches != NULL)
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1041 *pnmatches = 0;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1042
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1043 // Check given pattern and string
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1044 if (expr == NULL || haystack == NULL)
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1045 return THERR_NULLPTR;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1046
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1047 // Start matching
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1048 // XXX NOTE .. lots to think about and to take into account:
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1049 // - anchored and unanchored expressions
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1050 // - how to check if the expression has consumed all possibilities?
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1051 // ..
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1052 for (size_t soffs = 0; haystack[soffs] != 0; )
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1053 {
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1054 size_t coffs = soffs;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1055
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
1056 if (th_regex_match_expr(haystack, &coffs, expr, 0, flags, level))
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1057 {
612
cc9ec51b4875 Add some comments and debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
1058 // A match was found, increase count
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1059 nmatches++;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1060
612
cc9ec51b4875 Add some comments and debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
1061 // Deliver to caller if required
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1062 if (pnmatches != NULL)
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1063 *pnmatches = nmatches;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1064
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1065 if (pmatches != NULL)
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1066 {
647
1e7e3f96632e And some more work.
Matti Hamalainen <ccr@tnsp.org>
parents: 645
diff changeset
1067 // Add the match region to the list
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
1068 th_regex_match_t *match = th_malloc0(sizeof(th_regex_match_t));
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1069 if (match == NULL)
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1070 return THERR_MALLOC;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1071
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1072 match->start = soffs;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1073 match->len = coffs - soffs;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1074
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1075 th_llist_append_node((th_llist_t **) pmatches, (th_llist_t *) match);
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1076 }
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1077
612
cc9ec51b4875 Add some comments and debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
1078 // Check match count limit, if set
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1079 if (maxmatches > 0 && nmatches >= maxmatches)
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1080 break;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1081
612
cc9ec51b4875 Add some comments and debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
1082 // If offset was not advanced, increase by one
cc9ec51b4875 Add some comments and debug messages.
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
1083 // otherwise use end of match offset as new start
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1084 if (soffs == coffs)
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1085 soffs++;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1086 else
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1087 soffs = coffs;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1088 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1089 else
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1090 {
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1091 soffs++;
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1092 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1093 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1094
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1095 return THERR_OK;
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1096 }
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1097
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1098
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
1099 static void th_regex_free_match(th_regex_match_t *node)
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1100 {
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1101 (void) node;
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1102 // Nothing to do here at the moment
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1103 }
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1104
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1105
640
9e1f9e1d1487 Aaand some more work. Still just a broken concept.
Matti Hamalainen <ccr@tnsp.org>
parents: 639
diff changeset
1106 void th_regex_free_matches(th_regex_match_t *matches)
610
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1107 {
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1108 th_llist_free_func_node((th_llist_t *) matches,
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1109 (void (*)(th_llist_t *)) th_regex_free_match);
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1110 }
a0e8d9c6300b A bit more work on the regex stuff.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
1111
605
566e6ef41f9d Initial commit of the highly experimental and unfinished regular expression
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1112 #endif // TH_EXPERIMENTAL_REGEX