Lines Matching full:r
119 slre_dump(const struct slre *r, FILE *fp) in slre_dump() argument
123 for (pc = 0; pc < r->code_size; pc++) { in slre_dump()
125 op = r->code[pc]; in slre_dump()
131 (void) fprintf(fp, "%d ", r->code[pc + 1]); in slre_dump()
136 pc + r->code[pc + 1] - i); in slre_dump()
140 print_character_set(fp, r->data + in slre_dump()
141 r->code[pc + 1], r->code[pc + 2]); in slre_dump()
146 for (j = 0; j < r->code[pc + 2]; j++) { in slre_dump()
147 ch = r->data[r->code[pc + 1] + j]; in slre_dump()
166 set_jump_offset(struct slre *r, int pc, int offset) in set_jump_offset() argument
168 assert(offset < r->code_size); in set_jump_offset()
170 if (r->code_size - offset > 0xff) in set_jump_offset()
171 r->err_str = "Jump offset is too big"; in set_jump_offset()
173 r->code[pc] = (unsigned char) (r->code_size - offset); in set_jump_offset()
177 emit(struct slre *r, int code) in emit() argument
179 if (r->code_size >= (int) (sizeof(r->code) / sizeof(r->code[0]))) in emit()
180 r->err_str = "RE is too long (code overflow)"; in emit()
182 r->code[r->code_size++] = (unsigned char) code; in emit()
186 store_char_in_data(struct slre *r, int ch) in store_char_in_data() argument
188 if (r->data_size >= (int) sizeof(r->data)) in store_char_in_data()
189 r->err_str = "RE is too long (data overflow)"; in store_char_in_data()
191 r->data[r->data_size++] = ch; in store_char_in_data()
195 exact(struct slre *r, const char **re) in exact() argument
197 int old_data_size = r->data_size; in exact()
200 store_char_in_data(r, *(*re)++); in exact()
202 emit(r, EXACT); in exact()
203 emit(r, old_data_size); in exact()
204 emit(r, r->data_size - old_data_size); in exact()
216 case 'r': in get_escape_char()
217 res = '\r'; in get_escape_char()
243 anyof(struct slre *r, const char **re) in anyof() argument
245 int esc, old_data_size = r->data_size, op = ANYOF; in anyof()
256 emit(r, op); in anyof()
257 emit(r, old_data_size); in anyof()
258 emit(r, r->data_size - old_data_size); in anyof()
265 store_char_in_data(r, 0); in anyof()
266 store_char_in_data(r, esc >> 8); in anyof()
268 store_char_in_data(r, esc); in anyof()
272 store_char_in_data(r, (*re)[-1]); in anyof()
276 r->err_str = "No closing ']' bracket"; in anyof()
280 relocate(struct slre *r, int begin, int shift) in relocate() argument
282 emit(r, END); in relocate()
283 memmove(r->code + begin + shift, r->code + begin, r->code_size - begin); in relocate()
284 r->code_size += shift; in relocate()
288 quantifier(struct slre *r, int prev, int op) in quantifier() argument
290 if (r->code[prev] == EXACT && r->code[prev + 2] > 1) { in quantifier()
291 r->code[prev + 2]--; in quantifier()
292 emit(r, EXACT); in quantifier()
293 emit(r, r->code[prev + 1] + r->code[prev + 2]); in quantifier()
294 emit(r, 1); in quantifier()
295 prev = r->code_size - 3; in quantifier()
297 relocate(r, prev, 2); in quantifier()
298 r->code[prev] = op; in quantifier()
299 set_jump_offset(r, prev + 1, prev); in quantifier()
303 exact_one_char(struct slre *r, int ch) in exact_one_char() argument
305 emit(r, EXACT); in exact_one_char()
306 emit(r, r->data_size); in exact_one_char()
307 emit(r, 1); in exact_one_char()
308 store_char_in_data(r, ch); in exact_one_char()
312 fixup_branch(struct slre *r, int fixup) in fixup_branch() argument
315 emit(r, END); in fixup_branch()
316 set_jump_offset(r, fixup, fixup - 2); in fixup_branch()
321 compile(struct slre *r, const char **re) in compile() argument
326 level = r->num_caps; in compile()
327 branch_start = last_op = r->code_size; in compile()
337 emit(r, BOL); in compile()
340 emit(r, EOL); in compile()
343 last_op = r->code_size; in compile()
344 emit(r, ANY); in compile()
347 last_op = r->code_size; in compile()
348 anyof(r, re); in compile()
351 last_op = r->code_size; in compile()
354 emit(r, esc >> 8); in compile()
356 exact_one_char(r, esc); in compile()
359 last_op = r->code_size; in compile()
360 cap_no = ++r->num_caps; in compile()
361 emit(r, OPEN); in compile()
362 emit(r, cap_no); in compile()
364 compile(r, re); in compile()
366 r->err_str = "No closing bracket"; in compile()
370 emit(r, CLOSE); in compile()
371 emit(r, cap_no); in compile()
375 fixup_branch(r, fixup); in compile()
377 r->err_str = "Unbalanced brackets"; in compile()
390 quantifier(r, last_op, op); in compile()
393 quantifier(r, last_op, QUEST); in compile()
396 fixup_branch(r, fixup); in compile()
397 relocate(r, branch_start, 3); in compile()
398 r->code[branch_start] = BRANCH; in compile()
399 set_jump_offset(r, branch_start + 1, branch_start); in compile()
401 r->code[fixup] = 0xff; in compile()
405 last_op = r->code_size; in compile()
406 exact(r, re); in compile()
412 slre_compile(struct slre *r, const char *re) in slre_compile() argument
414 r->err_str = NULL; in slre_compile()
415 r->code_size = r->data_size = r->num_caps = r->anchored = 0; in slre_compile()
418 r->anchored++; in slre_compile()
420 emit(r, OPEN); /* This will capture what matches full RE */ in slre_compile()
421 emit(r, 0); in slre_compile()
424 compile(r, &re); in slre_compile()
426 if (r->code[2] == BRANCH) in slre_compile()
427 fixup_branch(r, 4); in slre_compile()
429 emit(r, CLOSE); in slre_compile()
430 emit(r, 0); in slre_compile()
431 emit(r, END); in slre_compile()
433 return (r->err_str == NULL ? 1 : 0); in slre_compile()
440 loop_greedy(const struct slre *r, int pc, const char *s, int len, int *ofs) in loop_greedy() argument
446 while (match(r, pc + 2, s, len, ofs, NULL)) { in loop_greedy()
448 if (match(r, pc + r->code[pc + 1], s, len, ofs, NULL)) in loop_greedy()
457 loop_non_greedy(const struct slre *r, int pc, const char *s, int len, int *ofs) in loop_non_greedy() argument
461 while (match(r, pc + 2, s, len, ofs, NULL)) { in loop_non_greedy()
463 if (match(r, pc + r->code[pc + 1], s, len, ofs, NULL)) in loop_non_greedy()
503 match(const struct slre *r, int pc, const char *s, int len, in match() argument
508 while (res && r->code[pc] != END) { in match()
510 assert(pc < r->code_size); in match()
511 assert(pc < (int) (sizeof(r->code) / sizeof(r->code[0]))); in match()
513 switch (r->code[pc]) { in match()
516 res = match(r, pc + 3, s, len, ofs, caps); in match()
519 res = match(r, pc + r->code[pc + 1], in match()
522 pc += r->code[pc + 2]; in match()
526 n = r->code[pc + 2]; /* String length */ in match()
527 if (n <= len - *ofs && !memcmp(s + *ofs, r->data + in match()
528 r->code[pc + 1], n)) { in match()
537 if (!match(r, pc + 2, s, len, ofs, caps)) in match()
539 pc += r->code[pc + 1]; in match()
543 loop_greedy(r, pc, s, len, ofs); in match()
544 pc += r->code[pc + 1]; in match()
548 loop_non_greedy(r, pc, s, len, ofs); in match()
549 pc += r->code[pc + 1]; in match()
552 res = match(r, pc + 2, s, len, ofs, caps); in match()
556 loop_greedy(r, pc, s, len, ofs); in match()
557 pc += r->code[pc + 1]; in match()
560 res = match(r, pc + 2, s, len, ofs, caps); in match()
564 loop_non_greedy(r, pc, s, len, ofs); in match()
565 pc += r->code[pc + 1]; in match()
603 res = is_any_of(r->data + r->code[pc + 1], in match()
604 r->code[pc + 2], s, ofs); in match()
610 res = is_any_but(r->data + r->code[pc + 1], in match()
611 r->code[pc + 2], s, ofs); in match()
624 caps[r->code[pc + 1]].ptr = s + *ofs; in match()
629 caps[r->code[pc + 1]].len = (s + *ofs) - in match()
630 caps[r->code[pc + 1]].ptr; in match()
637 printf("unknown cmd (%d) at %d\n", r->code[pc], pc); in match()
647 slre_match(const struct slre *r, const char *buf, int len, in slre_match() argument
652 if (r->anchored) { in slre_match()
653 res = match(r, 0, buf, len, &ofs, caps); in slre_match()
657 res = match(r, 0, buf, len, &ofs, caps); in slre_match()