Lines Matching refs:r

120 slre_dump(const struct slre *r, FILE *fp)  in slre_dump()  argument
124 for (pc = 0; pc < r->code_size; pc++) { in slre_dump()
126 op = r->code[pc]; in slre_dump()
132 (void) fprintf(fp, "%d ", r->code[pc + 1]); in slre_dump()
137 pc + r->code[pc + 1] - i); in slre_dump()
141 print_character_set(fp, r->data + in slre_dump()
142 r->code[pc + 1], r->code[pc + 2]); in slre_dump()
147 for (j = 0; j < r->code[pc + 2]; j++) { in slre_dump()
148 ch = r->data[r->code[pc + 1] + j]; in slre_dump()
167 set_jump_offset(struct slre *r, int pc, int offset) in set_jump_offset() argument
169 assert(offset < r->code_size); in set_jump_offset()
171 if (r->code_size - offset > 0xff) in set_jump_offset()
172 r->err_str = "Jump offset is too big"; in set_jump_offset()
174 r->code[pc] = (unsigned char) (r->code_size - offset); in set_jump_offset()
178 emit(struct slre *r, int code) in emit() argument
180 if (r->code_size >= (int) (sizeof(r->code) / sizeof(r->code[0]))) in emit()
181 r->err_str = "RE is too long (code overflow)"; in emit()
183 r->code[r->code_size++] = (unsigned char) code; in emit()
187 store_char_in_data(struct slre *r, int ch) in store_char_in_data() argument
189 if (r->data_size >= (int) sizeof(r->data)) in store_char_in_data()
190 r->err_str = "RE is too long (data overflow)"; in store_char_in_data()
192 r->data[r->data_size++] = ch; in store_char_in_data()
196 exact(struct slre *r, const char **re) in exact() argument
198 int old_data_size = r->data_size; in exact()
201 store_char_in_data(r, *(*re)++); in exact()
203 emit(r, EXACT); in exact()
204 emit(r, old_data_size); in exact()
205 emit(r, r->data_size - old_data_size); in exact()
244 anyof(struct slre *r, const char **re) in anyof() argument
246 int esc, old_data_size = r->data_size, op = ANYOF; in anyof()
257 emit(r, op); in anyof()
258 emit(r, old_data_size); in anyof()
259 emit(r, r->data_size - old_data_size); in anyof()
266 store_char_in_data(r, 0); in anyof()
267 store_char_in_data(r, esc >> 8); in anyof()
269 store_char_in_data(r, esc); in anyof()
273 store_char_in_data(r, (*re)[-1]); in anyof()
277 r->err_str = "No closing ']' bracket"; in anyof()
281 relocate(struct slre *r, int begin, int shift) in relocate() argument
283 emit(r, END); in relocate()
284 memmove(r->code + begin + shift, r->code + begin, r->code_size - begin); in relocate()
285 r->code_size += shift; in relocate()
289 quantifier(struct slre *r, int prev, int op) in quantifier() argument
291 if (r->code[prev] == EXACT && r->code[prev + 2] > 1) { in quantifier()
292 r->code[prev + 2]--; in quantifier()
293 emit(r, EXACT); in quantifier()
294 emit(r, r->code[prev + 1] + r->code[prev + 2]); in quantifier()
295 emit(r, 1); in quantifier()
296 prev = r->code_size - 3; in quantifier()
298 relocate(r, prev, 2); in quantifier()
299 r->code[prev] = op; in quantifier()
300 set_jump_offset(r, prev + 1, prev); in quantifier()
304 exact_one_char(struct slre *r, int ch) in exact_one_char() argument
306 emit(r, EXACT); in exact_one_char()
307 emit(r, r->data_size); in exact_one_char()
308 emit(r, 1); in exact_one_char()
309 store_char_in_data(r, ch); in exact_one_char()
313 fixup_branch(struct slre *r, int fixup) in fixup_branch() argument
316 emit(r, END); in fixup_branch()
317 set_jump_offset(r, fixup, fixup - 2); in fixup_branch()
322 compile(struct slre *r, const char **re) in compile() argument
327 level = r->num_caps; in compile()
328 branch_start = last_op = r->code_size; in compile()
338 emit(r, BOL); in compile()
341 emit(r, EOL); in compile()
344 last_op = r->code_size; in compile()
345 emit(r, ANY); in compile()
348 last_op = r->code_size; in compile()
349 anyof(r, re); in compile()
352 last_op = r->code_size; in compile()
355 emit(r, esc >> 8); in compile()
357 exact_one_char(r, esc); in compile()
360 last_op = r->code_size; in compile()
361 cap_no = ++r->num_caps; in compile()
362 emit(r, OPEN); in compile()
363 emit(r, cap_no); in compile()
365 compile(r, re); in compile()
367 r->err_str = "No closing bracket"; in compile()
371 emit(r, CLOSE); in compile()
372 emit(r, cap_no); in compile()
376 fixup_branch(r, fixup); in compile()
378 r->err_str = "Unbalanced brackets"; in compile()
391 quantifier(r, last_op, op); in compile()
394 quantifier(r, last_op, QUEST); in compile()
397 fixup_branch(r, fixup); in compile()
398 relocate(r, branch_start, 3); in compile()
399 r->code[branch_start] = BRANCH; in compile()
400 set_jump_offset(r, branch_start + 1, branch_start); in compile()
402 r->code[fixup] = 0xff; in compile()
406 last_op = r->code_size; in compile()
407 exact(r, re); in compile()
413 slre_compile(struct slre *r, const char *re) in slre_compile() argument
415 r->err_str = NULL; in slre_compile()
416 r->code_size = r->data_size = r->num_caps = r->anchored = 0; in slre_compile()
419 r->anchored++; in slre_compile()
421 emit(r, OPEN); /* This will capture what matches full RE */ in slre_compile()
422 emit(r, 0); in slre_compile()
425 compile(r, &re); in slre_compile()
427 if (r->code[2] == BRANCH) in slre_compile()
428 fixup_branch(r, 4); in slre_compile()
430 emit(r, CLOSE); in slre_compile()
431 emit(r, 0); in slre_compile()
432 emit(r, END); in slre_compile()
434 return (r->err_str == NULL ? 1 : 0); in slre_compile()
441 loop_greedy(const struct slre *r, int pc, const char *s, int len, int *ofs) in loop_greedy() argument
447 while (match(r, pc + 2, s, len, ofs, NULL)) { in loop_greedy()
449 if (match(r, pc + r->code[pc + 1], s, len, ofs, NULL)) in loop_greedy()
458 loop_non_greedy(const struct slre *r, int pc, const char *s, int len, int *ofs) in loop_non_greedy() argument
462 while (match(r, pc + 2, s, len, ofs, NULL)) { in loop_non_greedy()
464 if (match(r, pc + r->code[pc + 1], s, len, ofs, NULL)) in loop_non_greedy()
504 match(const struct slre *r, int pc, const char *s, int len, in match() argument
509 while (res && r->code[pc] != END) { in match()
511 assert(pc < r->code_size); in match()
512 assert(pc < (int) (sizeof(r->code) / sizeof(r->code[0]))); in match()
514 switch (r->code[pc]) { in match()
517 res = match(r, pc + 3, s, len, ofs, caps); in match()
520 res = match(r, pc + r->code[pc + 1], in match()
523 pc += r->code[pc + 2]; in match()
527 n = r->code[pc + 2]; /* String length */ in match()
528 if (n <= len - *ofs && !memcmp(s + *ofs, r->data + in match()
529 r->code[pc + 1], n)) { in match()
538 if (!match(r, pc + 2, s, len, ofs, caps)) in match()
540 pc += r->code[pc + 1]; in match()
544 loop_greedy(r, pc, s, len, ofs); in match()
545 pc += r->code[pc + 1]; in match()
549 loop_non_greedy(r, pc, s, len, ofs); in match()
550 pc += r->code[pc + 1]; in match()
553 res = match(r, pc + 2, s, len, ofs, caps); in match()
557 loop_greedy(r, pc, s, len, ofs); in match()
558 pc += r->code[pc + 1]; in match()
561 res = match(r, pc + 2, s, len, ofs, caps); in match()
565 loop_non_greedy(r, pc, s, len, ofs); in match()
566 pc += r->code[pc + 1]; in match()
604 res = is_any_of(r->data + r->code[pc + 1], in match()
605 r->code[pc + 2], s, ofs); in match()
611 res = is_any_but(r->data + r->code[pc + 1], in match()
612 r->code[pc + 2], s, ofs); in match()
625 caps[r->code[pc + 1]].ptr = s + *ofs; in match()
630 caps[r->code[pc + 1]].len = (s + *ofs) - in match()
631 caps[r->code[pc + 1]].ptr; in match()
638 printf("unknown cmd (%d) at %d\n", r->code[pc], pc); in match()
648 slre_match(const struct slre *r, const char *buf, int len, in slre_match() argument
653 if (r->anchored) { in slre_match()
654 res = match(r, 0, buf, len, &ofs, caps); in slre_match()
658 res = match(r, 0, buf, len, &ofs, caps); in slre_match()