A Prog is a compiled regular expression program.
| 15 | * A Prog is a compiled regular expression program. |
| 16 | */ |
| 17 | final class Prog { |
| 18 | |
| 19 | Inst[] inst = new Inst[10]; |
| 20 | int instSize = 0; |
| 21 | int start; // index of start instruction |
| 22 | int numCap = 2; // number of CAPTURE insts in re |
| 23 | // 2 => implicit ( and ) for whole match $0 |
| 24 | |
| 25 | // Constructs an empty program. |
| 26 | Prog() {} |
| 27 | |
| 28 | // Returns the instruction at the specified pc. |
| 29 | // Precondition: pc > 0 && pc < numInst(). |
| 30 | Inst getInst(int pc) { |
| 31 | return inst[pc]; |
| 32 | } |
| 33 | |
| 34 | // Returns the number of instructions in this program. |
| 35 | int numInst() { |
| 36 | return instSize; |
| 37 | } |
| 38 | |
| 39 | // Adds a new instruction to this program, with operator |op| and |pc| equal |
| 40 | // to |numInst()|. |
| 41 | void addInst(int op) { |
| 42 | if (instSize >= inst.length) { |
| 43 | inst = Arrays.copyOf(inst, inst.length * 2); |
| 44 | } |
| 45 | inst[instSize] = new Inst(op); |
| 46 | instSize++; |
| 47 | } |
| 48 | |
| 49 | // skipNop() follows any no-op or capturing instructions and returns the |
| 50 | // resulting instruction. |
| 51 | Inst skipNop(int pc) { |
| 52 | Inst i = inst[pc]; |
| 53 | while (i.op == Inst.NOP || i.op == Inst.CAPTURE) { |
| 54 | i = inst[pc]; |
| 55 | pc = i.out; |
| 56 | } |
| 57 | return i; |
| 58 | } |
| 59 | |
| 60 | // prefix() returns a pair of a literal string that all matches for the |
| 61 | // regexp must start with, and a boolean which is true if the prefix is the |
| 62 | // entire match. The string is returned by appending to |prefix|. |
| 63 | boolean prefix(StringBuilder prefix) { |
| 64 | Inst i = skipNop(start); |
| 65 | |
| 66 | // Avoid allocation of buffer if prefix is empty. |
| 67 | if (!Inst.isRuneOp(i.op) || i.runes.length != 1) { |
| 68 | return i.op == Inst.MATCH; // (append "" to prefix) |
| 69 | } |
| 70 | |
| 71 | // Have prefix; gather characters. |
| 72 | while (Inst.isRuneOp(i.op) && i.runes.length == 1 && (i.arg & RE2.FOLD_CASE) == 0) { |
| 73 | prefix.appendCodePoint(i.runes[0]); // an int, not a byte. |
| 74 | i = skipNop(i.out); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…