|
55 | 55 | sx: seq[(NodeIdx, CaptIdx)] |
56 | 56 | # use custom len because setLen(0) is slower, |
57 | 57 | # and {.noInit.} makes no difference |
58 | | - si: int |
59 | | - ss: set[int16] |
| 58 | + si: int16 |
| 59 | + ss: seq[int16] |
60 | 60 |
|
61 | | -func newSubmatches*(): Submatches {.inline.} = |
| 61 | +func newSubmatches*(size: int): Submatches {.inline.} = |
62 | 62 | result = new Submatches |
63 | 63 | result.sx = newSeq[(NodeIdx, CaptIdx)](8) |
| 64 | + result.ss = newSeq[int16](size) |
64 | 65 | result.si = 0 |
65 | 66 |
|
66 | 67 | func `[]`*(sm: Submatches, i: int): (NodeIdx, CaptIdx) {.inline.} = |
67 | 68 | assert i < sm.si |
68 | 69 | sm.sx[i] |
69 | 70 |
|
| 71 | +func hasState*(sm: Submatches, n: int16): bool {.inline.} = |
| 72 | + sm.ss[n] < sm.si and sm.sx[sm.ss[n]][0] == n |
| 73 | + |
70 | 74 | func add*(sm: var Submatches, item: (NodeIdx, CaptIdx)) {.inline.} = |
71 | | - assert item[0] notin sm.ss |
| 75 | + assert not sm.hasState(item[0]) |
72 | 76 | assert sm.si <= sm.sx.len |
73 | 77 | if (sm.si == sm.sx.len).unlikely: |
74 | 78 | sm.sx.setLen(sm.sx.len * 2) |
75 | 79 | sm.sx[sm.si] = item |
76 | | - sm.si += 1 |
77 | | - sm.ss.incl(item[0]) |
| 80 | + sm.ss[item[0]] = sm.si |
| 81 | + sm.si += 1'i16 |
78 | 82 |
|
79 | 83 | func len*(sm: Submatches): int {.inline.} = |
80 | 84 | sm.si |
81 | 85 |
|
82 | | -func hasState*(sm: Submatches, n: int16): bool {.inline.} = |
83 | | - n in sm.ss |
84 | | - |
85 | 86 | func clear*(sm: var Submatches) {.inline.} = |
86 | | - for i in 0 .. sm.len-1: |
87 | | - assert sm.sx[i][0] in sm.ss |
88 | | - sm.ss.excl sm.sx[i][0] |
89 | 87 | sm.si = 0 |
90 | 88 |
|
91 | 89 | iterator items*(sm: Submatches): (NodeIdx, CaptIdx) {.inline.} = |
@@ -263,8 +261,8 @@ func matchImpl*( |
263 | 261 | captLong {.used.} = -1 |
264 | 262 | iPrevLong {.used.} = start |
265 | 263 | if hasTransitionsZ: |
266 | | - smA = newSubmatches() |
267 | | - smB = newSubmatches() |
| 264 | + smA = newSubmatches(regex.transitions.all.len) |
| 265 | + smB = newSubmatches(regex.transitions.all.len) |
268 | 266 | smA.add((0'i16, -1'i32)) |
269 | 267 | #echo regex.dfa |
270 | 268 | while i < len(text): |
|
0 commit comments