mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Perf: improve processLine performance
This commit is contained in:
parent
6cb410bc98
commit
1ce322a71c
21
Build/lib/process-line.test.ts
Normal file
21
Build/lib/process-line.test.ts
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import { describe, it } from 'mocha';
|
||||||
|
|
||||||
|
import { processLine } from './process-line';
|
||||||
|
import expect from 'expect';
|
||||||
|
|
||||||
|
describe('processLine', () => {
|
||||||
|
([
|
||||||
|
['! comment', null],
|
||||||
|
[' ! comment', null],
|
||||||
|
['// xommwnr', null],
|
||||||
|
['# comment', null],
|
||||||
|
[' # comment', null],
|
||||||
|
['###id', '###id'],
|
||||||
|
['##.class', '##.class'],
|
||||||
|
['## EOF', '## EOF']
|
||||||
|
] as const).forEach(([input, expected]) => {
|
||||||
|
it(input, () => {
|
||||||
|
expect(processLine(input)).toBe(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,33 +1,29 @@
|
|||||||
import { TransformStream } from 'node:stream/web';
|
import { TransformStream } from 'node:stream/web';
|
||||||
|
|
||||||
export function processLine(line: string): string | null {
|
export function processLine(line: string): string | null {
|
||||||
if (!line) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const trimmed: string = line.trim();
|
const trimmed: string = line.trim();
|
||||||
if (trimmed.length === 0) {
|
if (trimmed.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const line_0: string = trimmed[0];
|
const line_0 = trimmed.charCodeAt(0);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
line_0 === ' '
|
// line_0 === 32 /** [space] */
|
||||||
|| line_0 === '\r'
|
// || line_0 === 13 /** \r */
|
||||||
|| line_0 === '\n'
|
// || line_0 === 10 /** \n */
|
||||||
|| line_0 === '!'
|
line_0 === 33 /** ! */
|
||||||
|| (line_0 === '/' && trimmed[1] === '/')
|
|| (line_0 === 47 /** / */ && trimmed.charCodeAt(1) === 47 /** / */)
|
||||||
) {
|
) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (line_0 === '#') {
|
if (line_0 === 35 /** # */) {
|
||||||
if (trimmed[1] !== '#') {
|
if (trimmed.charCodeAt(1) !== 35 /** # */) {
|
||||||
// # Comment
|
// # Comment
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (trimmed[2] === '#' && trimmed[3] === '#') {
|
if (trimmed.charCodeAt(2) === 35 /** # */ && trimmed.charCodeAt(3) === 35) {
|
||||||
// ################## EOF ##################
|
// ################## EOF ##################
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,3 @@
|
|||||||
// const unsupported = Symbol('unsupported');
|
|
||||||
|
|
||||||
// https://sing-box.sagernet.org/configuration/rule-set/source-format/
|
|
||||||
// export const PROCESSOR: Record<string, ((raw: string, type: string, value: string) => [key: keyof SingboxHeadlessRule, value: Required<SingboxHeadlessRule>[keyof SingboxHeadlessRule][number]] | null) | typeof unsupported> = {
|
|
||||||
// 'IP-ASN': unsupported,
|
|
||||||
// 'URL-REGEX': unsupported,
|
|
||||||
// 'USER-AGENT': unsupported
|
|
||||||
// };
|
|
||||||
|
|
||||||
interface SingboxHeadlessRule {
|
interface SingboxHeadlessRule {
|
||||||
domain?: string[],
|
domain?: string[],
|
||||||
domain_suffix?: string[],
|
domain_suffix?: string[],
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user