| 64 | // cleanClass() sorts the ranges (pairs of elements) of this CharClass, |
| 65 | // merges them, and eliminates duplicates. |
| 66 | CharClass cleanClass() { |
| 67 | if (len < 4) { |
| 68 | return this; |
| 69 | } |
| 70 | |
| 71 | // Sort by lo increasing, hi decreasing to break ties. |
| 72 | qsortIntPair(r, 0, len - 2); |
| 73 | |
| 74 | // Merge abutting, overlapping. |
| 75 | int w = 2; // write index |
| 76 | for (int i = 2; i < len; i += 2) { |
| 77 | int lo = r[i]; |
| 78 | int hi = r[i + 1]; |
| 79 | if (lo <= r[w - 1] + 1) { |
| 80 | // merge with previous range |
| 81 | if (hi > r[w - 1]) { |
| 82 | r[w - 1] = hi; |
| 83 | } |
| 84 | continue; |
| 85 | } |
| 86 | // new disjoint range |
| 87 | r[w] = lo; |
| 88 | r[w + 1] = hi; |
| 89 | w += 2; |
| 90 | } |
| 91 | len = w; |
| 92 | |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | // appendLiteral() appends the literal |x| to this CharClass. |
| 97 | CharClass appendLiteral(int x, int flags) { |