mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 05:50:43 -07:00
perf: introduce AST to avoid reparse
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
import { VARIABLE } from '../util/character'
|
||||
|
||||
const trie = {
|
||||
a: { n: { d: { end: true, needBoundary: true } } },
|
||||
o: { r: { end: true, needBoundary: true } },
|
||||
c: { o: { n: { t: { a: { i: { n: { s: { end: true, needBoundary: true } } } } } } } },
|
||||
'=': { '=': { end: true } },
|
||||
'!': { '=': { end: true } },
|
||||
'>': { end: true, '=': { end: true } },
|
||||
'<': { end: true, '=': { end: true } }
|
||||
}
|
||||
|
||||
export function matchOperator (str: string, begin: number, end = str.length) {
|
||||
let node = trie
|
||||
let i = begin
|
||||
let info
|
||||
while (node[str[i]] && i < end) {
|
||||
node = node[str[i++]]
|
||||
if (node['end']) info = node
|
||||
}
|
||||
if (!info) return -1
|
||||
if (info['needBoundary'] && str.charCodeAt(i) & VARIABLE) return -1
|
||||
return i
|
||||
}
|
||||
Reference in New Issue
Block a user