@theoremjs/agents 0.3.4-next.813.gc3645e9 → 0.3.4-next.817.g7602a31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -108,6 +108,75 @@ function allowed(list, read, scan) {
108
108
  }
109
109
  return answer;
110
110
  }
111
+ function keywordNode() {
112
+ return { goto: new Map(), next: new Map(), fail: undefined, ends: [] };
113
+ }
114
+ const AUTOMATA = new WeakMap();
115
+ function keywordTrie(words) {
116
+ const root = keywordNode();
117
+ for (const word of words) {
118
+ let node = root;
119
+ for (let i = 0; i < word.length; i += 1) {
120
+ const code = word.charCodeAt(i);
121
+ const child = node.goto.get(code) ?? keywordNode();
122
+ node.goto.set(code, child);
123
+ node = child;
124
+ }
125
+ node.ends.push(word);
126
+ }
127
+ return root;
128
+ }
129
+ /** Links each node of `root`'s trie to its longest proper suffix, breadth first, and folds that in. */
130
+ function linkTrie(root) {
131
+ const queue = [root];
132
+ for (let head = 0; head < queue.length; head += 1) {
133
+ const node = queue[head] ?? root;
134
+ const back = node.fail;
135
+ if (back) {
136
+ node.ends.push(...back.ends);
137
+ for (const [code, target] of back.next)
138
+ node.next.set(code, target);
139
+ }
140
+ for (const [code, child] of node.goto) {
141
+ node.next.set(code, child);
142
+ child.fail = back?.next.get(code) ?? root;
143
+ queue.push(child);
144
+ }
145
+ }
146
+ }
147
+ function keywordAutomaton(rules) {
148
+ const cached = AUTOMATA.get(rules);
149
+ if (cached)
150
+ return cached;
151
+ const words = [...new Set(rules.flatMap((rule) => rule.keywords))];
152
+ const root = keywordTrie(words);
153
+ linkTrie(root);
154
+ const automaton = { words: words.length, root };
155
+ AUTOMATA.set(rules, automaton);
156
+ return automaton;
157
+ }
158
+ /** Where each keyword of `rules` is first in `lower`, by keyword: what `indexOf` finds. */
159
+ function firstKeywords(lower, rules) {
160
+ const { words, root } = keywordAutomaton(rules);
161
+ const first = new Map();
162
+ let node = root;
163
+ for (let i = 0; i < lower.length && first.size < words; i += 1) {
164
+ node = node.next.get(lower.charCodeAt(i)) ?? root;
165
+ for (const word of node.ends) {
166
+ if (!first.has(word))
167
+ first.set(word, i + 1 - word.length);
168
+ }
169
+ }
170
+ return first;
171
+ }
172
+ /** Whether one of `rule`'s keywords is first in the text by `end`, as `firstKeywords` found them. */
173
+ function keywordsBy(rule, first, end) {
174
+ return (rule.keywords.length === 0 ||
175
+ rule.keywords.some((word) => {
176
+ const at = first.get(word);
177
+ return at !== undefined && at + word.length <= end;
178
+ }));
179
+ }
111
180
  /** Whether one of `rule`'s keywords is in `lower`, the text in lower case, by `end`. */
112
181
  function keywordsIn(rule, lower, end = lower.length) {
113
182
  return (rule.keywords.length === 0 ||
@@ -117,6 +186,16 @@ function keywordsIn(rule, lower, end = lower.length) {
117
186
  }));
118
187
  }
119
188
  const FINDERS = new WeakMap();
189
+ const GLOBALS = new WeakMap();
190
+ /** `pattern` reading every match. */
191
+ function everyMatch(pattern) {
192
+ let found = GLOBALS.get(pattern);
193
+ if (!found) {
194
+ found = new RegExp(pattern.source, `${pattern.flags.replace('g', '')}g`);
195
+ GLOBALS.set(pattern, found);
196
+ }
197
+ return found;
198
+ }
120
199
  /** `pattern` with its indices, read from the start of a string. */
121
200
  function finder(pattern) {
122
201
  let found = FINDERS.get(pattern);
@@ -167,14 +246,14 @@ function credentialSecret(rule, global, text, match, at, scan = scanOf(text)) {
167
246
  /** The secrets `rules` find in `text`, each one's span. */
168
247
  function credentialSpans(text, rules, global) {
169
248
  const lower = text.toLowerCase();
249
+ const first = firstKeywords(lower, rules);
170
250
  const scan = scanOf(text);
171
251
  const spans = [];
172
252
  for (const rule of rules) {
173
- if (!keywordsIn(rule, lower))
253
+ if (!keywordsBy(rule, first, lower.length))
174
254
  continue;
175
- const pattern = new RegExp(rule.pattern.source, `${rule.pattern.flags}g`);
176
- for (const match of text.matchAll(pattern)) {
177
- if (match[0] === '' || !keywordsIn(rule, lower, match.index + match[0].length))
255
+ for (const match of text.matchAll(everyMatch(rule.pattern))) {
256
+ if (match[0] === '' || !keywordsBy(rule, first, match.index + match[0].length))
178
257
  continue;
179
258
  const secret = credentialSecret(rule, global, text, match[0], match.index, scan);
180
259
  if (secret)