Mercurial > repo
comparison src/ploki/match.c @ 4223:ac0403686959
<oerjan> rm -rf src/ploki; mv ploki src
author | HackBot |
---|---|
date | Fri, 20 Dec 2013 22:18:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4222:b0f3e267bb1e | 4223:ac0403686959 |
---|---|
1 #include "IO.h" | |
2 #include "Str.h" | |
3 #include "main_io.h" | |
4 #include "main_opt.h" | |
5 #include "match.h" | |
6 #include "re.h" | |
7 #include "run.h" | |
8 #include "val.h" | |
9 | |
10 void do_match(struct val *v, t_regex *re) { | |
11 size_t mstart, mend; | |
12 size_t i; | |
13 size_t bra; | |
14 | |
15 if (V_EXT_P(v)) { | |
16 if (Opt.debug & DBG_REGEX) { | |
17 V_STR(v); | |
18 io_write_s(Err, "match: <<"); | |
19 io_write_m(Err, ko_ptr(v->ko), ko_length(v->ko)); | |
20 io_write_m(Err, "\n", 1); | |
21 } | |
22 | |
23 if (!(io_bufred(v->magic.ext) && re_iomatch(re, v->magic.ext, &mstart, &mend))) { | |
24 if (Opt.debug & DBG_REGEX) { | |
25 io_write_s(Err, "...failed\n"); | |
26 } | |
27 while (Interp.match.length) { | |
28 --Interp.match.length; | |
29 v_end(&Interp.match.matches[Interp.match.length]); | |
30 } | |
31 v->type = V_UNDEF; | |
32 return; | |
33 } | |
34 | |
35 if (Opt.debug & DBG_REGEX) { | |
36 io_write_s(Err, "...success!\n"); | |
37 } | |
38 v_set_n(&Interp.result, mend); | |
39 | |
40 bra = re_cabra(re); | |
41 if (Interp.match.size < bra) { | |
42 Interp.match.matches = xrealloc(Interp.match.matches, Interp.match.size = bra); | |
43 } | |
44 if (Interp.m_start.size < bra) { | |
45 Interp.m_start.index = xrealloc(Interp.m_start.index, Interp.m_start.size = bra); | |
46 } | |
47 if (Interp.m_end.size < bra) { | |
48 Interp.m_end.index = xrealloc(Interp.m_end.index, Interp.m_end.size = bra); | |
49 } | |
50 | |
51 for (i = 0; i < bra; ++i) { | |
52 size_t ms, me; | |
53 | |
54 if (re_backref(re, i, &ms, &me)) { | |
55 if (i < Interp.match.length) { | |
56 v_set_undef(&Interp.match.matches[i]); | |
57 } else { | |
58 v_init(&Interp.match.matches[Interp.match.length++]); | |
59 } | |
60 Interp.m_start.index[i] = -1; | |
61 Interp.m_end.index[i] = -1; | |
62 } else { | |
63 if (i >= Interp.match.length) { | |
64 v_init(&Interp.match.matches[Interp.match.length++]); | |
65 } | |
66 v_set_m(&Interp.match.matches[i], io_bufptr(v->magic.ext) + ms, me - ms); | |
67 Interp.m_start.index[i] = ms; | |
68 Interp.m_end.index[i] = me; | |
69 } | |
70 } | |
71 | |
72 io_read(v->magic.ext, NULL, mend); | |
73 V_xxx_OFF(v); | |
74 } else { | |
75 String tmp; | |
76 | |
77 V_STR(v); | |
78 V_xxx_OFF(v); | |
79 | |
80 St_fake(&tmp, (char *)ko_ptr(v->ko), ko_length(v->ko)); | |
81 | |
82 if (Opt.debug & DBG_REGEX) { | |
83 io_write_s(Err, "match: \""); | |
84 io_write(Err, &tmp); | |
85 io_write_s(Err, "\"\n"); | |
86 } | |
87 | |
88 if (!re_match(re, &tmp, &mstart, &mend)) { | |
89 if (Opt.debug & DBG_REGEX) { | |
90 io_write_s(Err, "...failed\n"); | |
91 } | |
92 while (Interp.match.length) { | |
93 --Interp.match.length; | |
94 v_end(&Interp.match.matches[Interp.match.length]); | |
95 } | |
96 v->type = V_UNDEF; | |
97 return; | |
98 } | |
99 | |
100 if (Opt.debug & DBG_REGEX) { | |
101 io_write_s(Err, "...success!\n"); | |
102 } | |
103 v_set_n(&Interp.result, mend); | |
104 | |
105 bra = re_cabra(re); | |
106 if (Interp.match.size < bra) { | |
107 Interp.match.matches = xrealloc(Interp.match.matches, Interp.match.size = bra); | |
108 } | |
109 if (Interp.m_start.size < bra) { | |
110 Interp.m_start.index = xrealloc(Interp.m_start.index, Interp.m_start.size = bra); | |
111 } | |
112 if (Interp.m_end.size < bra) { | |
113 Interp.m_end.index = xrealloc(Interp.m_end.index, Interp.m_end.size = bra); | |
114 } | |
115 | |
116 for (i = 0; i < bra; ++i) { | |
117 size_t ms, me; | |
118 | |
119 if (re_backref(re, i, &ms, &me)) { | |
120 if (i < Interp.match.length) { | |
121 v_set_undef(&Interp.match.matches[i]); | |
122 } else { | |
123 v_init(&Interp.match.matches[Interp.match.length++]); | |
124 } | |
125 Interp.m_start.index[i] = -1; | |
126 Interp.m_end.index[i] = -1; | |
127 } else { | |
128 if (i >= Interp.match.length) { | |
129 v_init(&Interp.match.matches[Interp.match.length++]); | |
130 } | |
131 v_set_m(&Interp.match.matches[i], ko_ptr(v->ko) + ms, me - ms); | |
132 Interp.m_start.index[i] = ms; | |
133 Interp.m_end.index[i] = me; | |
134 } | |
135 } | |
136 } | |
137 | |
138 if (!mstart) { | |
139 ko_cpy_m(v->ko, "0.", 2); | |
140 v->num = 0.0; | |
141 v->type = V_STR_K | V_NUM_K; | |
142 return; | |
143 } | |
144 | |
145 v->num = mstart; | |
146 v->type = V_NUM_K; | |
147 } |