limn-hl 0.1.1 → 0.2.0

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.
Files changed (73) hide show
  1. package/README.md +105 -174
  2. package/dist/detect.d.ts +7 -1
  3. package/dist/detect.js +592 -502
  4. package/dist/engine.js +45 -1
  5. package/dist/generated/detect.d.ts +11 -0
  6. package/dist/generated/detect.js +2434 -0
  7. package/dist/generated/grammars.d.ts +119 -0
  8. package/dist/generated/grammars.js +613 -0
  9. package/dist/generated/lang.d.ts +6 -0
  10. package/dist/{lang/index.js → generated/lang.js} +82 -56
  11. package/dist/{theme/generated.d.ts → generated/themes.d.ts} +1 -1
  12. package/dist/lang.d.ts +1 -0
  13. package/dist/lang.js +1 -0
  14. package/dist/render/ansi.d.ts +1 -1
  15. package/dist/render/html.d.ts +1 -1
  16. package/dist/{theme/index.d.ts → theme.d.ts} +1 -1
  17. package/dist/theme.js +1 -0
  18. package/dist/vm/compile.d.ts +4 -0
  19. package/dist/vm/compile.js +390 -0
  20. package/dist/vm/exec.d.ts +3 -0
  21. package/dist/vm/exec.js +218 -0
  22. package/dist/vm/format.d.ts +255 -0
  23. package/dist/vm/format.js +248 -0
  24. package/dist/vm/index.d.ts +2 -0
  25. package/dist/vm/index.js +2 -0
  26. package/dist/vm/pat.d.ts +4 -0
  27. package/dist/vm/pat.js +701 -0
  28. package/dist/vm/quoted.d.ts +8 -0
  29. package/dist/vm/quoted.js +190 -0
  30. package/dist/vm/scan.d.ts +3 -0
  31. package/dist/vm/scan.js +499 -0
  32. package/dist/wordtable.d.ts +8 -4
  33. package/dist/wordtable.js +107 -30
  34. package/package.json +6 -6
  35. package/dist/lang/clike.d.ts +0 -15
  36. package/dist/lang/clike.js +0 -35
  37. package/dist/lang/clike_helpers.d.ts +0 -8
  38. package/dist/lang/clike_helpers.js +0 -220
  39. package/dist/lang/code.d.ts +0 -45
  40. package/dist/lang/code.js +0 -779
  41. package/dist/lang/config.d.ts +0 -5
  42. package/dist/lang/config.js +0 -164
  43. package/dist/lang/css.d.ts +0 -5
  44. package/dist/lang/css.js +0 -125
  45. package/dist/lang/docs.d.ts +0 -12
  46. package/dist/lang/docs.js +0 -462
  47. package/dist/lang/formats.d.ts +0 -10
  48. package/dist/lang/formats.js +0 -295
  49. package/dist/lang/html.d.ts +0 -11
  50. package/dist/lang/html.js +0 -298
  51. package/dist/lang/index.d.ts +0 -20
  52. package/dist/lang/js.d.ts +0 -9
  53. package/dist/lang/js.js +0 -532
  54. package/dist/lang/js_helpers.d.ts +0 -7
  55. package/dist/lang/js_helpers.js +0 -308
  56. package/dist/lang/json.d.ts +0 -3
  57. package/dist/lang/json.js +0 -136
  58. package/dist/lang/markdown.d.ts +0 -3
  59. package/dist/lang/markdown.js +0 -249
  60. package/dist/lang/python.d.ts +0 -3
  61. package/dist/lang/python.js +0 -335
  62. package/dist/lang/scripting.d.ts +0 -7
  63. package/dist/lang/scripting.js +0 -216
  64. package/dist/lang/shell.d.ts +0 -7
  65. package/dist/lang/shell.js +0 -404
  66. package/dist/lang/specs_gen.d.ts +0 -66
  67. package/dist/lang/specs_gen.js +0 -381
  68. package/dist/lang/terminal.d.ts +0 -9
  69. package/dist/lang/terminal.js +0 -550
  70. package/dist/lang/yaml.d.ts +0 -4
  71. package/dist/lang/yaml.js +0 -176
  72. package/dist/theme/index.js +0 -1
  73. /package/dist/{theme/generated.js → generated/themes.js} +0 -0
package/dist/detect.js CHANGED
@@ -1,565 +1,655 @@
1
- const DETECT_MAX_BYTES = 4096;
1
+ import {
2
+ DETECT_ENTRY_LANGS,
3
+ DETECT_ENTRY_WEIGHTS,
4
+ DETECT_FEAT_COUNT,
5
+ DETECT_FEAT_COUNTS,
6
+ DETECT_FEAT_KEYS,
7
+ DETECT_LANG_BIAS,
8
+ DETECT_LANG_COUNT,
9
+ DETECT_LANG_NAMES,
10
+ DETECT_MAX_BYTES,
11
+ DETECT_PREFIXES,
12
+ DETECT_SHEBANGS
13
+ } from "./generated/detect.js";
14
+ const utf8Buf = new Uint8Array(DETECT_MAX_BYTES + 4);
15
+ const scoresBuf = new Int32Array(DETECT_LANG_COUNT);
16
+ const seenBuf = new Uint32Array(DETECT_FEAT_COUNT + 31 >>> 5);
17
+ function encodeUTF8Prefix(src, out, limit) {
18
+ let w = 0;
19
+ const len = src.length;
20
+ for (let i = 0;i < len && w < limit; i++) {
21
+ let cp = src.charCodeAt(i);
22
+ if (cp < 128) {
23
+ out[w++] = cp;
24
+ } else if (cp < 2048) {
25
+ out[w++] = 192 | cp >> 6;
26
+ out[w++] = 128 | cp & 63;
27
+ } else if (cp >= 55296 && cp <= 56319 && i + 1 < len) {
28
+ const lo = src.charCodeAt(i + 1);
29
+ if (lo >= 56320 && lo <= 57343) {
30
+ i++;
31
+ cp = 65536 + (cp - 55296 << 10) + (lo - 56320);
32
+ out[w++] = 240 | cp >> 18;
33
+ out[w++] = 128 | cp >> 12 & 63;
34
+ out[w++] = 128 | cp >> 6 & 63;
35
+ out[w++] = 128 | cp & 63;
36
+ } else {
37
+ out[w++] = 239;
38
+ out[w++] = 191;
39
+ out[w++] = 189;
40
+ }
41
+ } else {
42
+ out[w++] = 224 | cp >> 12;
43
+ out[w++] = 128 | cp >> 6 & 63;
44
+ out[w++] = 128 | cp & 63;
45
+ }
46
+ }
47
+ return w < limit ? w : limit;
48
+ }
49
+ let featMap = null;
50
+ let featOffsets = null;
51
+ function ensureTable() {
52
+ if (featMap !== null && featOffsets !== null) {
53
+ return { map: featMap, offsets: featOffsets };
54
+ }
55
+ const map = new Map;
56
+ const offsets = new Uint32Array(DETECT_FEAT_COUNT);
57
+ let off = 0;
58
+ for (let i = 0;i < DETECT_FEAT_COUNT; i++) {
59
+ map.set(DETECT_FEAT_KEYS[i], i);
60
+ offsets[i] = off;
61
+ off += DETECT_FEAT_COUNTS[i];
62
+ }
63
+ featMap = map;
64
+ featOffsets = offsets;
65
+ return { map, offsets };
66
+ }
67
+ function isASCIIWS(c) {
68
+ return c === 32 || c === 9 || c === 10 || c === 13;
69
+ }
70
+ function isAlpha(c) {
71
+ return c >= 65 && c <= 90 || c >= 97 && c <= 122;
72
+ }
73
+ function isDigit(c) {
74
+ return c >= 48 && c <= 57;
75
+ }
2
76
  function isIdentStart(c) {
3
- return c >= 97 && c <= 122 || c >= 65 && c <= 90 || c === 95 || c >= 128;
77
+ return isAlpha(c) || c === 95;
4
78
  }
5
79
  function isIdentCont(c) {
6
- return c >= 97 && c <= 122 || c >= 65 && c <= 90 || c >= 48 && c <= 57 || c === 95 || c >= 128;
80
+ return isAlpha(c) || isDigit(c) || c === 95;
7
81
  }
8
- function hasPrefixFold(s, prefix) {
9
- if (s.length < prefix.length) {
10
- return false;
82
+ function sliceASCII(b, start, end) {
83
+ let out = "";
84
+ for (let i = start;i < end; i++) {
85
+ out += String.fromCharCode(b[i]);
11
86
  }
87
+ return out;
88
+ }
89
+ function indexOfByte(b, needle, from, to) {
90
+ for (let i = from;i < to; i++) {
91
+ if (b[i] === needle)
92
+ return i;
93
+ }
94
+ return -1;
95
+ }
96
+ function startsWithASCII(b, from, to, prefix, fold) {
97
+ if (to - from < prefix.length)
98
+ return false;
12
99
  for (let i = 0;i < prefix.length; i++) {
13
- let a = s.charCodeAt(i);
14
- let b = prefix.charCodeAt(i);
15
- if (a >= 65 && a <= 90)
16
- a += 32;
17
- if (b >= 65 && b <= 90)
18
- b += 32;
19
- if (a !== b) {
20
- return false;
100
+ let a = b[from + i];
101
+ let p = prefix.charCodeAt(i);
102
+ if (fold) {
103
+ if (a >= 65 && a <= 90)
104
+ a += 32;
105
+ if (p >= 65 && p <= 90)
106
+ p += 32;
21
107
  }
108
+ if (a !== p)
109
+ return false;
22
110
  }
23
111
  return true;
24
112
  }
25
- const HTTP_METHODS = [
26
- "GET ",
27
- "POST ",
28
- "PUT ",
29
- "DELETE ",
30
- "PATCH ",
31
- "HEAD ",
32
- "OPTIONS ",
33
- "HTTP/"
34
- ];
35
- function startsWithHTTPMethod(s) {
36
- for (const m of HTTP_METHODS) {
37
- if (s.startsWith(m)) {
113
+ function containsASCII(b, from, to, needle) {
114
+ const nl = needle.length;
115
+ if (nl === 0)
116
+ return true;
117
+ const limit = to - nl;
118
+ const first = needle.charCodeAt(0);
119
+ outer:
120
+ for (let i = from;i <= limit; i++) {
121
+ if (b[i] !== first)
122
+ continue;
123
+ for (let j = 1;j < nl; j++) {
124
+ if (b[i + j] !== needle.charCodeAt(j))
125
+ continue outer;
126
+ }
38
127
  return true;
39
128
  }
40
- }
41
129
  return false;
42
130
  }
43
- function isSimpleKey(s) {
44
- if (s === "") {
45
- return false;
46
- }
47
- for (let i = 0;i < s.length; i++) {
48
- const ch = s.charCodeAt(i);
49
- if (isIdentCont(ch) || ch === 45 || ch === 46) {
50
- continue;
131
+ function checkFastPrefix(b, n) {
132
+ let ts = 0;
133
+ while (ts < n && isASCIIWS(b[ts]))
134
+ ts++;
135
+ let te = n;
136
+ while (te > ts && isASCIIWS(b[te - 1]))
137
+ te--;
138
+ if (ts >= te)
139
+ return "plain";
140
+ if (te - ts >= 2 && b[ts] === 35 && b[ts + 1] === 33) {
141
+ const nl = indexOfByte(b, 10, ts, te);
142
+ const le = nl >= 0 ? nl : te;
143
+ for (let i = 0;i < DETECT_SHEBANGS.length; i++) {
144
+ const r = DETECT_SHEBANGS[i];
145
+ if (containsASCII(b, ts, le, r[0]))
146
+ return r[1];
51
147
  }
52
- return false;
53
148
  }
54
- return true;
149
+ for (let i = 0;i < DETECT_PREFIXES.length; i++) {
150
+ const r = DETECT_PREFIXES[i];
151
+ if (startsWithASCII(b, ts, te, r[0], r[2]))
152
+ return r[1];
153
+ }
154
+ return "";
55
155
  }
56
- var L;
57
- ((L) => {
58
- L[L["Go"] = 0] = "Go";
59
- L[L["JS"] = 1] = "JS";
60
- L[L["TS"] = 2] = "TS";
61
- L[L["JSX"] = 3] = "JSX";
62
- L[L["TSX"] = 4] = "TSX";
63
- L[L["Py"] = 5] = "Py";
64
- L[L["Rs"] = 6] = "Rs";
65
- L[L["C"] = 7] = "C";
66
- L[L["CPP"] = 8] = "CPP";
67
- L[L["CS"] = 9] = "CS";
68
- L[L["Java"] = 10] = "Java";
69
- L[L["Kt"] = 11] = "Kt";
70
- L[L["Swift"] = 12] = "Swift";
71
- L[L["Zig"] = 13] = "Zig";
72
- L[L["Dart"] = 14] = "Dart";
73
- L[L["PHP"] = 15] = "PHP";
74
- L[L["SQL"] = 16] = "SQL";
75
- L[L["Rb"] = 17] = "Rb";
76
- L[L["Lua"] = 18] = "Lua";
77
- L[L["Nix"] = 19] = "Nix";
78
- L[L["JSON"] = 20] = "JSON";
79
- L[L["YAML"] = 21] = "YAML";
80
- L[L["TOML"] = 22] = "TOML";
81
- L[L["INI"] = 23] = "INI";
82
- L[L["HTML"] = 24] = "HTML";
83
- L[L["XML"] = 25] = "XML";
84
- L[L["CSS"] = 26] = "CSS";
85
- L[L["SCSS"] = 27] = "SCSS";
86
- L[L["Vue"] = 28] = "Vue";
87
- L[L["Svelte"] = 29] = "Svelte";
88
- L[L["Astro"] = 30] = "Astro";
89
- L[L["MD"] = 31] = "MD";
90
- L[L["Bash"] = 32] = "Bash";
91
- L[L["Docker"] = 33] = "Docker";
92
- L[L["Make"] = 34] = "Make";
93
- L[L["Diff"] = 35] = "Diff";
94
- L[L["Count"] = 36] = "Count";
95
- })(L ||= {});
96
- const NAMES = [
97
- "go",
98
- "js",
99
- "ts",
100
- "jsx",
101
- "tsx",
102
- "py",
103
- "rs",
104
- "c",
105
- "cpp",
106
- "cs",
107
- "java",
108
- "kt",
109
- "swift",
110
- "zig",
111
- "dart",
112
- "php",
113
- "sql",
114
- "rb",
115
- "lua",
116
- "nix",
117
- "json",
118
- "yaml",
119
- "toml",
120
- "ini",
121
- "html",
122
- "xml",
123
- "css",
124
- "scss",
125
- "vue",
126
- "svelte",
127
- "astro",
128
- "md",
129
- "bash",
130
- "docker",
131
- "make",
132
- "diff"
133
- ];
134
- export function detect(src) {
135
- if (src.length > DETECT_MAX_BYTES) {
136
- src = src.slice(0, DETECT_MAX_BYTES);
156
+ function isqrtInt(x) {
157
+ if (x <= 0)
158
+ return 0;
159
+ let r = Math.floor(Math.sqrt(x));
160
+ while ((r + 1) * (r + 1) <= x)
161
+ r++;
162
+ while (r * r > x)
163
+ r--;
164
+ return r;
165
+ }
166
+ function detectImpl(src, reg) {
167
+ const b = utf8Buf;
168
+ const n = encodeUTF8Prefix(src, b, DETECT_MAX_BYTES);
169
+ const fast = checkFastPrefix(b, n);
170
+ if (fast !== "" && (reg === null || fast === "plain" || reg.lookup(fast) !== undefined)) {
171
+ return fast;
137
172
  }
138
- const trimmed = src.trim();
139
- if (trimmed === "") {
173
+ let ts = 0;
174
+ while (ts < n && isASCIIWS(b[ts]))
175
+ ts++;
176
+ let te = n;
177
+ while (te > ts && isASCIIWS(b[te - 1]))
178
+ te--;
179
+ if (ts >= te)
140
180
  return "plain";
141
- }
142
- if (trimmed.startsWith("#!")) {
143
- const nl = trimmed.indexOf(`
144
- `);
145
- const shebang = nl >= 0 ? trimmed.slice(0, nl) : trimmed;
146
- if (shebang.includes("bash") || shebang.includes("/sh") || shebang.includes("zsh")) {
147
- return "bash";
148
- }
149
- if (shebang.includes("python"))
150
- return "py";
151
- if (shebang.includes("node") || shebang.includes("deno") || shebang.includes("bun")) {
152
- return "js";
153
- }
154
- if (shebang.includes("ruby"))
155
- return "rb";
156
- if (shebang.includes("lua"))
157
- return "lua";
158
- if (shebang.includes("php"))
159
- return "php";
160
- }
161
- if (trimmed.startsWith("<?php"))
162
- return "php";
163
- if (trimmed.startsWith("<?xml"))
164
- return "xml";
165
- if (hasPrefixFold(trimmed, "<!doctype html"))
166
- return "html";
167
- if (startsWithHTTPMethod(trimmed))
168
- return "http";
169
- if (trimmed.startsWith(`---
170
- `) && trimmed.indexOf(`
171
- ---`, 4) >= 0) {
172
- if (trimmed.includes("client:") || trimmed.includes("set:html") || trimmed.includes("<")) {
173
- return "astro";
181
+ const { map, offsets } = ensureTable();
182
+ scoresBuf.fill(0);
183
+ seenBuf.fill(0);
184
+ let hits = 0;
185
+ const addKey = (k) => {
186
+ const id = map.get(k);
187
+ if (id === undefined)
188
+ return;
189
+ const word = id >>> 5;
190
+ const bit = 1 << (id & 31);
191
+ if ((seenBuf[word] & bit) !== 0)
192
+ return;
193
+ seenBuf[word] = seenBuf[word] | bit;
194
+ hits++;
195
+ const start = offsets[id];
196
+ const end = start + DETECT_FEAT_COUNTS[id];
197
+ for (let idx = start;idx < end; idx++) {
198
+ const lang = DETECT_ENTRY_LANGS[idx];
199
+ scoresBuf[lang] = scoresBuf[lang] + DETECT_ENTRY_WEIGHTS[idx];
174
200
  }
201
+ };
202
+ const firstB = b[ts];
203
+ const lastB = b[te - 1];
204
+ if (firstB === 123 && lastB === 125)
205
+ addKey("F:wrap_brace");
206
+ else if (firstB === 91 && lastB === 93)
207
+ addKey("F:wrap_bracket");
208
+ else if (firstB === 60 && lastB === 62)
209
+ addKey("F:wrap_angle");
210
+ if (ts + 4 <= n && b[ts] === 45 && b[ts + 1] === 45 && b[ts + 2] === 45 && b[ts + 3] === 10 && containsASCII(b, ts + 4, n, `
211
+ ---`)) {
212
+ addKey("F:frontmatter");
175
213
  }
176
- const scores = new Int32Array(36 /* Count */);
177
- if (trimmed.startsWith("{") && trimmed.endsWith("}") || trimmed.startsWith("[") && trimmed.endsWith("]")) {
178
- scores[20 /* JSON */] += 25;
179
- }
180
- const n = src.length;
181
214
  let i = 0;
215
+ let nonEmptyLines = 0;
216
+ let semiEOLLines = 0;
217
+ let colonEOLLines = 0;
218
+ let braceEOLLines = 0;
219
+ let tabIndentLines = 0;
220
+ let space2IndentLines = 0;
221
+ let space4IndentLines = 0;
222
+ let keyColonLines = 0;
223
+ let keyEqLines = 0;
224
+ let dotKeyEqLines = 0;
225
+ let digitBOLLines = 0;
226
+ let col0PMLines = 0;
227
+ let bracketLines = 0;
228
+ let quotedColonHits = 0;
229
+ let braceCount = 0;
230
+ let braceDepth = 0;
231
+ let maxBraceDepth = 0;
232
+ let punctChars = 0;
233
+ let alphaChars = 0;
234
+ let nonASCIIBytes = 0;
235
+ let nonWSChars = 0;
236
+ let wordCount = 0;
237
+ let lowerWordCount = 0;
238
+ let upperWordCount = 0;
239
+ let camelWordCount = 0;
240
+ let snakeWordCount = 0;
241
+ let prosePunctHits = 0;
242
+ let tsHits = 0;
243
+ let dottedQuadHits = 0;
244
+ let upperTags = 0;
245
+ let closeTags = 0;
182
246
  let atLineStart = true;
183
- let hasJSXTag = false;
184
247
  while (i < n) {
248
+ let c = b[i];
185
249
  if (atLineStart) {
186
250
  const lineStartPos = i;
187
- while (i < n && (src.charCodeAt(i) === 32 || src.charCodeAt(i) === 9)) {
251
+ let indentSpaces = 0;
252
+ if (c === 9)
253
+ tabIndentLines++;
254
+ while (i < n && (b[i] === 32 || b[i] === 9)) {
255
+ if (b[i] === 32)
256
+ indentSpaces++;
188
257
  i++;
189
258
  }
190
259
  if (i >= n)
191
260
  break;
261
+ if (b[i] === 10 || b[i] === 13) {
262
+ i++;
263
+ continue;
264
+ }
192
265
  atLineStart = false;
193
- const ch = src.charCodeAt(i);
194
- if (ch === 35) {
195
- if (src.startsWith("# ", i) || src.startsWith("## ", i) || src.startsWith("### ", i)) {
196
- scores[31 /* MD */] += 35;
197
- } else if (src.startsWith("#include ", i)) {
198
- scores[7 /* C */] += 80;
199
- scores[8 /* CPP */] += 80;
200
- }
201
- } else if (ch === 96) {
202
- if (src.startsWith("```", i)) {
203
- scores[31 /* MD */] += 50;
204
- }
205
- } else if (ch === 64) {
206
- if (src.startsWith("@import ", i) || src.startsWith("@media ", i) || src.startsWith("@layer ", i)) {
207
- scores[26 /* CSS */] += 45;
208
- scores[27 /* SCSS */] += 45;
209
- } else if (src.startsWith("@mixin ", i) || src.startsWith("@include ", i) || src.startsWith("@use ", i)) {
210
- scores[27 /* SCSS */] += 90;
211
- } else if (src.startsWith("@@ ", i)) {
212
- scores[35 /* Diff */] += 150;
213
- }
214
- } else if (ch === 46) {
215
- if (src.startsWith(".PHONY:", i)) {
216
- scores[34 /* Make */] += 120;
217
- }
218
- } else if (ch === 43 || ch === 45) {
219
- if (i === lineStartPos && i + 1 < n && src.charCodeAt(i + 1) !== ch) {
220
- scores[35 /* Diff */] += 6;
221
- }
222
- } else if (ch === 36) {
223
- scores[27 /* SCSS */] += 35;
224
- } else if (ch === 91) {
225
- const nl = src.indexOf(`
226
- `, i);
227
- const tl = (nl >= 0 ? src.slice(i, nl) : src.slice(i)).trim();
228
- if (tl.length > 2 && tl.endsWith("]") && !tl.includes(",") && !tl.includes('"') && !tl.includes("'")) {
229
- scores[22 /* TOML */] += 25;
230
- scores[23 /* INI */] += 25;
231
- }
232
- } else if (ch === 70 || ch === 82 || ch === 69 || ch === 87) {
233
- if (src.startsWith("FROM ", i) || src.startsWith("RUN ", i) || src.startsWith("ENTRYPOINT ", i) || src.startsWith("WORKDIR ", i)) {
234
- scores[33 /* Docker */] += 120;
235
- }
236
- } else if (ch === 100) {
237
- if (src.startsWith("diff --git ", i)) {
238
- scores[35 /* Diff */] += 150;
239
- }
240
- } else if (ch === 105) {
241
- if (src.startsWith("ifneq ", i) || src.startsWith("ifeq ", i)) {
242
- scores[34 /* Make */] += 120;
243
- } else if (src.startsWith("import java.", i) || src.startsWith("import javax.", i)) {
244
- scores[10 /* Java */] += 200;
245
- } else if (src.startsWith("import 'package:", i)) {
246
- scores[14 /* Dart */] += 150;
247
- }
248
- } else if (ch === 112) {
249
- if (src.startsWith("package ", i)) {
250
- scores[0 /* Go */] += 70;
251
- scores[10 /* Java */] += 40;
252
- scores[11 /* Kt */] += 40;
266
+ nonEmptyLines++;
267
+ if (indentSpaces === 2)
268
+ space2IndentLines++;
269
+ else if (indentSpaces === 4)
270
+ space4IndentLines++;
271
+ const nl = indexOfByte(b, 10, i, n);
272
+ const le = nl >= 0 ? nl : n;
273
+ let rle = le;
274
+ while (rle > i && (b[rle - 1] === 32 || b[rle - 1] === 9 || b[rle - 1] === 13)) {
275
+ rle--;
276
+ }
277
+ if (rle > i) {
278
+ const endCh = b[rle - 1];
279
+ if (endCh === 59)
280
+ semiEOLLines++;
281
+ else if (endCh === 58)
282
+ colonEOLLines++;
283
+ else if (endCh === 123 || endCh === 125)
284
+ braceEOLLines++;
285
+ }
286
+ const c0 = b[i];
287
+ if (isDigit(c0)) {
288
+ digitBOLLines++;
289
+ } else if (isIdentStart(c0)) {
290
+ let j = i + 1;
291
+ while (j < le && isIdentCont(b[j]))
292
+ j++;
293
+ if (j - i <= 24) {
294
+ const lw = sliceASCII(b, i, j);
295
+ addKey("LW:" + lw);
296
+ let k = j;
297
+ let hasDot = false;
298
+ while (k < le && (b[k] === 45 || b[k] === 46)) {
299
+ if (b[k] === 46)
300
+ hasDot = true;
301
+ k++;
302
+ while (k < le && isIdentCont(b[k]))
303
+ k++;
304
+ }
305
+ let ks = k;
306
+ while (ks < le && (b[ks] === 32 || b[ks] === 9))
307
+ ks++;
308
+ if (ks < le && b[ks] === 58 && (ks + 1 === le || b[ks + 1] === 32 || b[ks + 1] === 9 || b[ks + 1] === 13)) {
309
+ keyColonLines++;
310
+ } else if (ks < le && b[ks] === 61 && (ks + 1 === le || b[ks + 1] !== 61)) {
311
+ keyEqLines++;
312
+ if (hasDot)
313
+ dotKeyEqLines++;
314
+ }
315
+ if (j - i <= 14) {
316
+ let k2 = j;
317
+ while (k2 < le && (b[k2] === 32 || b[k2] === 9))
318
+ k2++;
319
+ if (k2 > j && k2 < le) {
320
+ const c2 = b[k2];
321
+ if (isIdentStart(c2)) {
322
+ let k3 = k2 + 1;
323
+ while (k3 < le && isIdentCont(b[k3]))
324
+ k3++;
325
+ if (k3 - k2 <= 14) {
326
+ addKey("LW2:" + lw + " " + sliceASCII(b, k2, k3));
327
+ }
328
+ } else if (c2 === 34) {
329
+ addKey("LW2:" + lw + ' "');
330
+ } else if (c2 === 123) {
331
+ addKey("LW2:" + lw + " {");
332
+ }
333
+ }
334
+ }
253
335
  }
254
- } else if (ch === 117) {
255
- if (src.startsWith("using System", i)) {
256
- scores[9 /* CS */] += 180;
336
+ } else if (c0 >= 33 && c0 < 128) {
337
+ if (c0 === 91 && rle - i >= 3 && b[rle - 1] === 93 && indexOfByte(b, 44, i + 1, rle - 1) < 0 && indexOfByte(b, 34, i + 1, rle - 1) < 0 && indexOfByte(b, 123, i + 1, rle - 1) < 0) {
338
+ bracketLines++;
257
339
  }
258
- }
259
- const nl = src.indexOf(`
260
- `, i);
261
- const line = nl >= 0 ? src.slice(i, nl) : src.slice(i);
262
- const colon = line.indexOf(":");
263
- if (colon > 0 && isSimpleKey(line.slice(0, colon))) {
264
- if (colon + 1 === line.length || line.charCodeAt(colon + 1) === 32) {
265
- scores[21 /* YAML */] += 14;
340
+ let j = i;
341
+ while (j < le && j - i < 3 && b[j] >= 33 && b[j] < 128 && !isIdentCont(b[j])) {
342
+ j++;
266
343
  }
267
- }
268
- const eq = line.indexOf("=");
269
- if (eq > 0 && isSimpleKey(line.slice(0, eq).trim())) {
270
- if (line.includes('"') || line.includes("'") || line.includes("[")) {
271
- scores[22 /* TOML */] += 8;
272
- } else {
273
- scores[23 /* INI */] += 8;
344
+ if (j > i) {
345
+ const lp = sliceASCII(b, i, j);
346
+ addKey("LP:" + lp);
347
+ if (lp.length >= 2)
348
+ addKey("LP:" + lp[0]);
349
+ if (i === lineStartPos && (c0 === 43 || c0 === 45) && j - i === 1) {
350
+ col0PMLines++;
351
+ }
274
352
  }
275
353
  }
276
- if (line.includes('":')) {
277
- scores[20 /* JSON */] += 8;
278
- }
279
- if (line.includes(";") && line.includes("=") && !line.includes("{")) {
280
- scores[19 /* Nix */] += 8;
281
- }
354
+ c = b[i];
282
355
  }
283
- const ch = src.charCodeAt(i);
284
- if (ch === 10) {
356
+ if (c === 10) {
285
357
  atLineStart = true;
286
358
  i++;
287
359
  continue;
288
360
  }
289
- if (isIdentStart(ch)) {
290
- const start = i;
361
+ if (c === 32 || c === 9 || c === 13) {
291
362
  i++;
292
- while (i < n && isIdentCont(src.charCodeAt(i))) {
293
- i++;
363
+ continue;
364
+ }
365
+ nonWSChars++;
366
+ if (c >= 128) {
367
+ let j = i + 1;
368
+ while (j < n && b[j] >= 128)
369
+ j++;
370
+ const run = j - i;
371
+ nonASCIIBytes += run;
372
+ nonWSChars += run - 1;
373
+ i = j;
374
+ continue;
375
+ }
376
+ if (isIdentStart(c)) {
377
+ let j = i + 1;
378
+ let hasUpper = c >= 65 && c <= 90;
379
+ let hasLower = c >= 97 && c <= 122;
380
+ let hasUnder = c === 95;
381
+ while (j < n && isIdentCont(b[j])) {
382
+ const cj = b[j];
383
+ if (cj >= 65 && cj <= 90)
384
+ hasUpper = true;
385
+ else if (cj >= 97 && cj <= 122)
386
+ hasLower = true;
387
+ else if (cj === 95)
388
+ hasUnder = true;
389
+ j++;
294
390
  }
295
- const w = src.slice(start, i);
296
- switch (w) {
297
- case "func":
298
- scores[0 /* Go */] += 45;
299
- scores[12 /* Swift */] += 45;
300
- break;
301
- case "fmt":
302
- if (i < n && src.charCodeAt(i) === 46)
303
- scores[0 /* Go */] += 80;
304
- break;
305
- case "chan":
306
- case "defer":
307
- case "fallthrough":
308
- case "go":
309
- scores[0 /* Go */] += 40;
310
- break;
311
- case "const":
312
- case "let":
313
- case "async":
314
- case "await":
315
- case "export":
316
- case "import":
317
- scores[1 /* JS */] += 10;
318
- scores[2 /* TS */] += 10;
319
- break;
320
- case "function":
321
- case "typeof":
322
- case "undefined":
323
- scores[1 /* JS */] += 25;
324
- scores[2 /* TS */] += 20;
325
- break;
326
- case "console":
327
- if (src.startsWith(".log", i)) {
328
- scores[1 /* JS */] += 40;
329
- scores[2 /* TS */] += 30;
330
- }
331
- break;
332
- case "document":
333
- if (i < n && src.charCodeAt(i) === 46)
334
- scores[1 /* JS */] += 25;
335
- break;
336
- case "interface":
337
- scores[2 /* TS */] += 45;
338
- scores[4 /* TSX */] += 35;
339
- break;
340
- case "satisfies":
341
- scores[2 /* TS */] += 60;
342
- break;
343
- case "className":
344
- if (i < n && src.charCodeAt(i) === 61) {
345
- scores[3 /* JSX */] += 60;
346
- scores[4 /* TSX */] += 65;
391
+ const wlen = j - i;
392
+ alphaChars += wlen;
393
+ nonWSChars += wlen - 1;
394
+ wordCount++;
395
+ if (hasLower && !hasUpper && !hasUnder)
396
+ lowerWordCount++;
397
+ if (hasUpper && hasLower)
398
+ camelWordCount++;
399
+ if (hasUpper && !hasLower && wlen >= 2)
400
+ upperWordCount++;
401
+ if (hasUnder && wlen >= 3)
402
+ snakeWordCount++;
403
+ if (wlen <= 24) {
404
+ const w = sliceASCII(b, i, j);
405
+ addKey("W:" + w);
406
+ if (j < n) {
407
+ const nx = b[j];
408
+ if (nx === 33) {
409
+ addKey("WA:" + w + "!");
410
+ } else if (nx === 58) {
411
+ if (j + 1 < n && b[j + 1] === 58) {
412
+ addKey("WA:" + w + "::");
413
+ } else {
414
+ addKey("WA:" + w + ":");
415
+ }
416
+ } else if (nx === 40) {
417
+ addKey("WA:" + w + "(");
418
+ } else if (nx === 46 && j + 1 < n && isAlpha(b[j + 1])) {
419
+ addKey("WA:" + w + ".");
420
+ } else if (nx === 61 && (j + 1 >= n || b[j + 1] !== 61)) {
421
+ addKey("WA:" + w + "=");
422
+ } else if (nx === 60 && j + 1 < n && isAlpha(b[j + 1])) {
423
+ addKey("WA:" + w + "<");
424
+ } else if ((nx === 34 || nx === 39) && wlen === 1) {
425
+ addKey("WA:" + w + '"');
426
+ } else if (nx === 45 && j + 1 < n && b[j + 1] === 62) {
427
+ addKey("WA:" + w + "->");
428
+ } else if (nx === 45 && j + 1 < n && isAlpha(b[j + 1]) && wlen <= 6) {
429
+ addKey("WA:" + w + "-");
347
430
  }
348
- break;
349
- case "def":
350
- case "None":
351
- case "True":
352
- case "False":
353
- scores[5 /* Py */] += 25;
354
- break;
355
- case "__init__":
356
- scores[5 /* Py */] += 100;
357
- break;
358
- case "elif":
359
- scores[5 /* Py */] += 30;
360
- scores[32 /* Bash */] += 30;
361
- break;
362
- case "self":
363
- if (i < n && src.charCodeAt(i) === 46)
364
- scores[5 /* Py */] += 35;
365
- break;
366
- case "mut":
367
- case "pub":
368
- case "crate":
369
- case "match":
370
- scores[6 /* Rs */] += 30;
371
- break;
372
- case "println":
373
- if (i < n && src.charCodeAt(i) === 33)
374
- scores[6 /* Rs */] += 90;
375
- break;
376
- case "impl":
377
- scores[6 /* Rs */] += 45;
378
- break;
379
- case "std":
380
- if (src.startsWith("::", i))
381
- scores[8 /* CPP */] += 90;
382
- break;
383
- case "iostream":
384
- case "vector":
385
- scores[8 /* CPP */] += 80;
386
- break;
387
- case "template":
388
- if (i < n && src.charCodeAt(i) === 60)
389
- scores[8 /* CPP */] += 70;
390
- break;
391
- case "nullptr":
392
- scores[8 /* CPP */] += 60;
393
- break;
394
- case "Console":
395
- if (i < n && src.charCodeAt(i) === 46)
396
- scores[9 /* CS */] += 90;
397
- break;
398
- case "System":
399
- if (src.startsWith(".out.", i))
400
- scores[10 /* Java */] += 100;
401
- break;
402
- case "main":
403
- if (start >= 19 && src.startsWith("public static void ", start - 19)) {
404
- scores[10 /* Java */] += 150;
405
- }
406
- break;
407
- case "companion":
408
- if (src.startsWith(" object", i))
409
- scores[11 /* Kt */] += 120;
410
- break;
411
- case "fun":
412
- scores[11 /* Kt */] += 40;
413
- break;
414
- case "val":
415
- case "when":
416
- case "object":
417
- scores[11 /* Kt */] += 25;
418
- break;
419
- case "guard":
420
- if (src.startsWith(" let ", i))
421
- scores[12 /* Swift */] += 120;
422
- break;
423
- case "comptime":
424
- scores[13 /* Zig */] += 120;
425
- break;
426
- case "orelse":
427
- scores[13 /* Zig */] += 80;
428
- break;
429
- case "BuildContext":
430
- scores[14 /* Dart */] += 120;
431
- break;
432
- case "Widget":
433
- scores[14 /* Dart */] += 90;
434
- break;
435
- case "SELECT":
436
- case "select":
437
- scores[16 /* SQL */] += 60;
438
- break;
439
- case "FROM":
440
- case "WHERE":
441
- scores[16 /* SQL */] += 40;
442
- break;
443
- case "pkgs":
444
- scores[19 /* Nix */] += 80;
445
- break;
446
- case "mkDerivation":
447
- scores[19 /* Nix */] += 150;
448
- break;
449
- case "nixpkgs":
450
- scores[19 /* Nix */] += 90;
451
- break;
452
- case "local":
453
- scores[18 /* Lua */] += 35;
454
- break;
455
- case "elseif":
456
- scores[18 /* Lua */] += 60;
457
- break;
458
- case "attr_accessor":
459
- scores[17 /* Rb */] += 120;
460
- break;
461
- case "end":
462
- if (i < n && src.charCodeAt(i) === 10)
463
- scores[17 /* Rb */] += 25;
464
- break;
465
- case "echo":
466
- scores[32 /* Bash */] += 25;
467
- break;
468
- case "then":
469
- case "fi":
470
- case "done":
471
- scores[32 /* Bash */] += 30;
472
- break;
473
- case "esac":
474
- scores[32 /* Bash */] += 80;
475
- break;
476
- case "struct":
477
- case "enum":
478
- scores[6 /* Rs */] += 10;
479
- scores[0 /* Go */] += 10;
480
- scores[13 /* Zig */] += 15;
481
- scores[12 /* Swift */] += 10;
482
- break;
483
- case "nil":
484
- scores[0 /* Go */] += 15;
485
- scores[18 /* Lua */] += 15;
486
- scores[17 /* Rb */] += 15;
487
- scores[12 /* Swift */] += 15;
488
- break;
489
- case "xmlns":
490
- if (i < n && src.charCodeAt(i) === 61)
491
- scores[25 /* XML */] += 70;
492
- break;
431
+ }
493
432
  }
433
+ i = j;
494
434
  continue;
495
435
  }
496
- if (ch === 58) {
497
- if (i + 1 < n && src.charCodeAt(i + 1) === 61) {
498
- scores[0 /* Go */] += 40;
499
- i += 2;
500
- continue;
436
+ if (isDigit(c)) {
437
+ let j = i + 1;
438
+ let dots = 0;
439
+ while (j < n && (isDigit(b[j]) || b[j] === 46 || b[j] === 95 || b[j] === 120 || b[j] === 88)) {
440
+ if (b[j] === 46)
441
+ dots++;
442
+ j++;
501
443
  }
502
- if (src.startsWith(": string", i) || src.startsWith(": number", i) || src.startsWith(": boolean", i)) {
503
- scores[2 /* TS */] += 40;
504
- scores[4 /* TSX */] += 30;
444
+ if (dots === 3 && j - i <= 15)
445
+ dottedQuadHits++;
446
+ if (j - i === 4 && i + 9 < n && (b[i + 4] === 45 || b[i + 4] === 47) && isDigit(b[i + 5]) && isDigit(b[i + 6]) && b[i + 7] === b[i + 4]) {
447
+ tsHits++;
448
+ } else if (j - i === 2 && i + 7 < n && b[i + 2] === 58 && isDigit(b[i + 3]) && isDigit(b[i + 4]) && b[i + 5] === 58) {
449
+ tsHits++;
450
+ } else if (j - i === 6 && i + 12 < n && b[i + 6] === 32 && isDigit(b[i + 7]) && isDigit(b[i + 8]) && isDigit(b[i + 12])) {
451
+ tsHits++;
505
452
  }
506
- } else if (ch === 47) {
507
- if (i + 1 < n && src.charCodeAt(i + 1) === 62) {
508
- hasJSXTag = true;
509
- i += 2;
453
+ nonWSChars += j - i - 1;
454
+ i = j;
455
+ continue;
456
+ }
457
+ if (c === 34 || c === 39) {
458
+ const q = c;
459
+ punctChars++;
460
+ const qch = String.fromCharCode(q);
461
+ addKey("S:" + qch);
462
+ if (i + 2 < n && b[i + 1] === q && b[i + 2] === q) {
463
+ addKey("S:" + qch + qch + qch);
464
+ i += 3;
510
465
  continue;
511
466
  }
512
- } else if (ch === 60) {
513
- if (src.startsWith("<template>", i)) {
514
- scores[28 /* Vue */] += 120;
515
- } else if (src.startsWith("<script setup", i)) {
516
- scores[28 /* Vue */] += 150;
517
- } else if (src.startsWith("<html", i)) {
518
- scores[24 /* HTML */] += 80;
519
- } else if (src.startsWith("<div", i) || src.startsWith("<span", i)) {
520
- scores[24 /* HTML */] += 35;
521
- } else if (src.startsWith("<svg", i)) {
522
- scores[25 /* XML */] += 70;
523
- }
524
- } else if (ch === 123) {
525
- if (src.startsWith("{#if ", i) || src.startsWith("{#each ", i)) {
526
- scores[29 /* Svelte */] += 140;
527
- }
528
- } else if (ch === 64) {
529
- if (src.startsWith("@import(", i)) {
530
- scores[13 /* Zig */] += 120;
531
- } else if (src.startsWith("@State ", i)) {
532
- scores[12 /* Swift */] += 80;
467
+ let j = i + 1;
468
+ while (j < n && j - i < 80 && b[j] !== q && b[j] !== 10) {
469
+ if (b[j] === 92 && j + 1 < n) {
470
+ j += 2;
471
+ continue;
472
+ }
473
+ j++;
533
474
  }
534
- } else if (ch === 38) {
535
- if (src.startsWith("&mut ", i)) {
536
- scores[6 /* Rs */] += 70;
475
+ if (j < n && b[j] === q) {
476
+ let k = j + 1;
477
+ while (k < n && (b[k] === 32 || b[k] === 9))
478
+ k++;
479
+ if (k < n && b[k] === 58)
480
+ quotedColonHits++;
481
+ i = j + 1;
482
+ continue;
537
483
  }
538
- } else if (ch === 118) {
539
- if (src.startsWith("v-for=", i) || src.startsWith("v-if=", i)) {
540
- scores[28 /* Vue */] += 90;
484
+ i++;
485
+ continue;
486
+ }
487
+ punctChars++;
488
+ if (c === 123) {
489
+ braceCount++;
490
+ braceDepth++;
491
+ if (braceDepth > maxBraceDepth)
492
+ maxBraceDepth = braceDepth;
493
+ } else if (c === 125) {
494
+ braceCount++;
495
+ if (braceDepth > 0)
496
+ braceDepth--;
497
+ }
498
+ if ((c === 46 || c === 44) && i + 1 < n && b[i + 1] === 32)
499
+ prosePunctHits++;
500
+ if ((c === 64 || c === 36 || c === 35 || c === 92 || c === 58 || c === 37) && i + 1 < n) {
501
+ const nx = b[i + 1];
502
+ if (isAlpha(nx) || nx === 95) {
503
+ let j = i + 2;
504
+ while (j < n && (isIdentCont(b[j]) || (c === 64 || c === 35 || c === 36) && b[j] === 45)) {
505
+ j++;
506
+ }
507
+ if (j - i <= 24) {
508
+ addKey("P:" + sliceASCII(b, i, j));
509
+ addKey("S:" + String.fromCharCode(c));
510
+ i = j;
511
+ continue;
512
+ }
541
513
  }
542
- } else if (ch === 111) {
543
- if (src.startsWith("on:click", i)) {
544
- scores[29 /* Svelte */] += 90;
514
+ }
515
+ if (c === 60 && i + 1 < n) {
516
+ const nx = b[i + 1];
517
+ if (nx === 47 && i + 2 < n && isAlpha(b[i + 2])) {
518
+ closeTags++;
519
+ addKey("S:</");
520
+ i += 2;
521
+ continue;
522
+ } else if (isAlpha(nx)) {
523
+ let j = i + 1;
524
+ while (j < n && (isIdentCont(b[j]) || b[j] === 45 || b[j] === 58))
525
+ j++;
526
+ if (j < n && (b[j] === 32 || b[j] === 9 || b[j] === 10 || b[j] === 13 || b[j] === 62 || b[j] === 47)) {
527
+ if (nx >= 65 && nx <= 90)
528
+ upperTags++;
529
+ if (j - (i + 1) <= 20) {
530
+ const tag = sliceASCII(b, i + 1, j);
531
+ addKey("T:" + tag);
532
+ const col = tag.indexOf(":");
533
+ if (col > 0)
534
+ addKey("T:" + tag.slice(0, col + 1));
535
+ }
536
+ }
545
537
  }
546
538
  }
547
- i++;
539
+ let j = i + 1;
540
+ while (j < n && j - i < 3 && b[j] >= 33 && b[j] < 128 && !isIdentCont(b[j]) && b[j] !== 34 && b[j] !== 39) {
541
+ j++;
542
+ }
543
+ if (c >= 33 && c < 128) {
544
+ const sym = sliceASCII(b, i, j);
545
+ addKey("S:" + sym);
546
+ if (sym.length === 3)
547
+ addKey("S:" + sym.slice(0, 2));
548
+ if (sym.length >= 2)
549
+ addKey("S:" + sym[0]);
550
+ }
551
+ nonWSChars += j - i - 1;
552
+ i = j;
548
553
  }
549
- if ((scores[1 /* JS */] > 0 || scores[2 /* TS */] > 0) && hasJSXTag) {
550
- if (scores[2 /* TS */] > scores[1 /* JS */]) {
551
- scores[4 /* TSX */] += scores[2 /* TS */] + 30;
552
- } else {
553
- scores[3 /* JSX */] += scores[1 /* JS */] + 30;
554
+ if (nonEmptyLines <= 1)
555
+ addKey("F:single_line");
556
+ else if (nonEmptyLines <= 4)
557
+ addKey("F:few_lines");
558
+ if (nonEmptyLines > 0) {
559
+ if (semiEOLLines >= 2 || semiEOLLines / nonEmptyLines >= 0.35)
560
+ addKey("F:semi_eol");
561
+ if (colonEOLLines >= 2 || colonEOLLines / nonEmptyLines >= 0.25)
562
+ addKey("F:colon_eol");
563
+ if (braceEOLLines >= 2)
564
+ addKey("F:brace_eol");
565
+ if (tabIndentLines >= 2)
566
+ addKey("F:tab_indent");
567
+ if (space2IndentLines >= 2 || nonEmptyLines <= 5 && space2IndentLines >= 1)
568
+ addKey("F:space2_indent");
569
+ if (space4IndentLines >= 2 || nonEmptyLines <= 5 && space4IndentLines >= 1)
570
+ addKey("F:space4_indent");
571
+ if (col0PMLines >= 1)
572
+ addKey("F:col0_pm");
573
+ if (col0PMLines >= 3)
574
+ addKey("F:col0_pm_many");
575
+ if (keyColonLines >= 2 || nonEmptyLines <= 5 && keyColonLines >= 1)
576
+ addKey("F:key_colon");
577
+ if (keyEqLines >= 2 || nonEmptyLines <= 5 && keyEqLines >= 1)
578
+ addKey("F:key_eq");
579
+ if (dotKeyEqLines >= 2 || nonEmptyLines <= 5 && dotKeyEqLines >= 1)
580
+ addKey("F:dot_key_eq");
581
+ if (digitBOLLines >= 2)
582
+ addKey("F:digit_bol");
583
+ if (digitBOLLines >= 2 && digitBOLLines / nonEmptyLines >= 0.5)
584
+ addKey("F:digit_bol_most");
585
+ }
586
+ if (bracketLines >= 1)
587
+ addKey("F:bracket_line");
588
+ if (quotedColonHits >= 1)
589
+ addKey("F:quoted_colon");
590
+ if (quotedColonHits >= 3)
591
+ addKey("F:quoted_colon_many");
592
+ if (braceCount === 0)
593
+ addKey("F:no_braces");
594
+ else if (braceCount >= 4)
595
+ addKey("F:many_braces");
596
+ if (maxBraceDepth >= 2)
597
+ addKey("F:brace_depth_2");
598
+ if (maxBraceDepth >= 3)
599
+ addKey("F:brace_depth_3");
600
+ if (tsHits >= 1)
601
+ addKey("F:timestamp");
602
+ if (tsHits >= 3)
603
+ addKey("F:timestamp_many");
604
+ if (dottedQuadHits >= 1)
605
+ addKey("F:dotted_quad");
606
+ if (upperTags >= 1)
607
+ addKey("F:upper_tag");
608
+ if (closeTags >= 1)
609
+ addKey("F:close_tag");
610
+ if (nonWSChars > 0) {
611
+ const punctRatio = punctChars / nonWSChars;
612
+ const textRatio = (alphaChars + nonASCIIBytes) / nonWSChars;
613
+ if (punctRatio < 0.06)
614
+ addKey("F:punct_very_low");
615
+ else if (punctRatio < 0.14)
616
+ addKey("F:punct_low");
617
+ else if (punctRatio > 0.3)
618
+ addKey("F:punct_high");
619
+ if (textRatio > 0.78 && punctRatio < 0.08 && (prosePunctHits >= 2 || nonASCIIBytes >= 12)) {
620
+ addKey("F:prose_like");
621
+ }
622
+ if (wordCount >= 4 && lowerWordCount / wordCount >= 0.65 && punctRatio < 0.08 && braceCount === 0) {
623
+ addKey("F:prose_sentence");
554
624
  }
555
625
  }
626
+ if (wordCount >= 5) {
627
+ if (camelWordCount / wordCount >= 0.2)
628
+ addKey("F:camel_case");
629
+ if (snakeWordCount / wordCount >= 0.2)
630
+ addKey("F:snake_case");
631
+ if (upperWordCount / wordCount >= 0.25)
632
+ addKey("F:upper_words");
633
+ }
634
+ const mul = hits > 0 ? isqrtInt(hits * 256) : 16;
556
635
  let bestIdx = -1;
557
- let bestScore = 24;
558
- for (let idx = 0;idx < scores.length; idx++) {
559
- if (scores[idx] > bestScore) {
560
- bestScore = scores[idx];
561
- bestIdx = idx;
636
+ let bestVal = 0;
637
+ for (let c = 0;c < DETECT_LANG_COUNT; c++) {
638
+ const name = DETECT_LANG_NAMES[c];
639
+ if (reg !== null && name !== "plain" && reg.lookup(name) === undefined) {
640
+ continue;
641
+ }
642
+ const val = scoresBuf[c] * 256 + DETECT_LANG_BIAS[c] * mul;
643
+ if (bestIdx < 0 || val > bestVal) {
644
+ bestIdx = c;
645
+ bestVal = val;
562
646
  }
563
647
  }
564
- return bestIdx < 0 ? "plain" : NAMES[bestIdx];
648
+ return bestIdx >= 0 ? DETECT_LANG_NAMES[bestIdx] : "plain";
649
+ }
650
+ export function detect(src) {
651
+ return detectImpl(src, null);
652
+ }
653
+ export function detectIn(src, reg) {
654
+ return detectImpl(src, reg);
565
655
  }