| 77 | // every length, which backtracks across whitespace runs and is O(n²) — a |
| 78 | // small padded query could freeze the event loop for tens of seconds. |
| 79 | function parseBlock(all, block, qs) { |
| 80 | if (block.length === 0) return |
| 81 | |
| 82 | var len = block.length |
| 83 | var clauseStart = 0 |
| 84 | // Compose for the clause currently being read; the leftmost clause is `or`. |
| 85 | var compose = 'or' |
| 86 | var i = 0 |
| 87 | |
| 88 | while (i < len) { |
| 89 | var ch = block[i] |
| 90 | |
| 91 | if (ch === ',') { |
| 92 | // `,\s*` delimiter. A delimiter at the very start (i === 0) has no left |
| 93 | // clause — the original never emits one there. |
| 94 | if (i !== 0) pushClause(all, qs, block.slice(clauseStart, i), compose) |
| 95 | i++ |
| 96 | while (i < len && SPACE.test(block[i])) i++ |
| 97 | compose = 'or' |
| 98 | clauseStart = i |
| 99 | continue |
| 100 | } |
| 101 | |
| 102 | if (SPACE.test(ch)) { |
| 103 | // Possible `\s+and\s+` or `\s+or\s+`. Scan the whitespace run once |
| 104 | // (linear, no backtracking), then check the following keyword. |
| 105 | var q = i |
| 106 | while (q < len && SPACE.test(block[q])) q++ |
| 107 | |
| 108 | if ( |
| 109 | q + 3 < len && |
| 110 | (block[q] === 'a' || block[q] === 'A') && |
| 111 | (block[q + 1] === 'n' || block[q + 1] === 'N') && |
| 112 | (block[q + 2] === 'd' || block[q + 2] === 'D') && |
| 113 | SPACE.test(block[q + 3]) |
| 114 | ) { |
| 115 | // The leading `\s+` of `\s+and\s+` absorbs whitespace at the block |
| 116 | // start, so a delimiter at i === 0 has no left clause. |
| 117 | if (i !== 0) pushClause(all, qs, block.slice(clauseStart, i), compose) |
| 118 | var afterAnd = q + 3 |
| 119 | while (afterAnd < len && SPACE.test(block[afterAnd])) afterAnd++ |
| 120 | compose = 'and' |
| 121 | i = afterAnd |
| 122 | clauseStart = afterAnd |
| 123 | continue |
| 124 | } else if ( |
| 125 | q + 2 < len && |
| 126 | (block[q] === 'o' || block[q] === 'O') && |
| 127 | (block[q + 1] === 'r' || block[q + 1] === 'R') && |
| 128 | SPACE.test(block[q + 2]) |
| 129 | ) { |
| 130 | if (i !== 0) pushClause(all, qs, block.slice(clauseStart, i), compose) |
| 131 | var afterOr = q + 2 |
| 132 | while (afterOr < len && SPACE.test(block[afterOr])) afterOr++ |
| 133 | compose = 'or' |
| 134 | i = afterOr |
| 135 | clauseStart = afterOr |
| 136 | continue |