Skip to content

Commit

Permalink
fixed slow code
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrx committed Oct 17, 2024
1 parent d041197 commit ef598e0
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions packages/agtree/src/parser/misc/ubo-parameter-list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */
import { StringUtils } from '../../utils/string';
import { type ParameterList } from '../common';
import { COMMA } from '../../utils/constants';
import { COMMA, ESCAPE_CHARACTER } from '../../utils/constants';
import { defaultParserOptions } from '../options';
import { ValueParser } from './value';
import { AdblockSyntaxError } from '../../errors/adblock-syntax-error';
Expand Down Expand Up @@ -61,13 +61,6 @@ export class UboParameterListParser extends ParameterListParser {
const nextSeparatorIndex = StringUtils.skipWS(raw, possibleClosingQuoteIndex + 1);

if (nextSeparatorIndex === length) {
if (requireQuotes) {
throw new AdblockSyntaxError(
'Expected separator, got end of string',
baseOffset + nextSeparatorIndex,
baseOffset + length,
);
}
// If the separator is not found, the param end is the end of the string
paramEnd = StringUtils.skipWSBack(raw, length - 1) + 1;
offset = length;
Expand All @@ -83,13 +76,51 @@ export class UboParameterListParser extends ParameterListParser {
baseOffset + length,
);
}
// Param end should be the last separator before the quote
offset = StringUtils.findNextUnescapedCharacterBackwards(

/**
* At that point found `possibleClosingQuoteIndex` is wrong
* | is `offset`
* ~ is `possibleClosingQuoteIndex`
* ^ is `nextSeparatorIndex`
*
* Example 1: "abc, ').cba='1'"
* | ~^
* Example 2: "abc, ').cba, '1'"
* | ~^
* Example 3: "abc, ').cba='1', cba"
* | ~^
*
* Search for separator before `possibleClosingQuoteIndex`
*/

const separatorIndexBeforeQuote = StringUtils.findNextUnescapedCharacterBackwards(
raw,
separator,
possibleClosingQuoteIndex,
) + 1;
paramEnd = StringUtils.skipWSBack(raw, offset - 2) + 1;
ESCAPE_CHARACTER,
offset + 1,
);
if (separatorIndexBeforeQuote !== -1) {
// Found separator before (Example 2)
paramEnd = StringUtils.skipWSBack(raw, separatorIndexBeforeQuote - 1) + 1;
offset = separatorIndexBeforeQuote + 1;
} else {
// Didn't found separator before, search after
const separatorIndexAfterQuote = StringUtils.findNextUnescapedCharacter(
raw,
separator,
possibleClosingQuoteIndex,
);
if (separatorIndexAfterQuote !== -1) {
// We found separator after (Example 3)
paramEnd = StringUtils.skipWSBack(raw, separatorIndexAfterQuote - 1) + 1;
offset = separatorIndexAfterQuote + 1;
} else {
// If the separator is not found, the param end is the end of the string (Example 1)
paramEnd = StringUtils.skipWSBack(raw, length - 1) + 1;
offset = length;
}
}
}
} else {
if (requireQuotes) {
Expand Down

0 comments on commit ef598e0

Please sign in to comment.