Skip to content

Commit

Permalink
Optimize dotAll dot output (#91)
Browse files Browse the repository at this point in the history
* optimize: use [^] for [\s\S]

* update test fixtures
  • Loading branch information
JLHwung authored Sep 13, 2024
1 parent 55f3e5e commit 2ae1f91
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions rewrite-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ const processCharacterClass = (
// The transform here does not support lone surrogates.
update(
characterClassItem,
`(?!${surrogateOrBMPSetStr})[\\s\\S]|${astralNegativeSetStr}`
`(?!${surrogateOrBMPSetStr})[^]|${astralNegativeSetStr}`
);
} else {
// Generate negative set directly when case folding is not involved.
Expand All @@ -530,7 +530,7 @@ const processCharacterClass = (
update(characterClassItem, negativeSet.toString({ bmpOnly: bmpOnly }));
}
} else {
update(characterClassItem, `(?!${setStr})[\\s\\S]`);
update(characterClassItem, `(?!${setStr})[^]`);
}
}
} else {
Expand Down Expand Up @@ -589,7 +589,7 @@ const processTerm = (item, regenerateOptions, groups) => {
);
} else if (config.transform.dotAllFlag || config.modifiersData.s) {
// TODO: consider changing this at the regenerate level.
update(item, '[\\s\\S]');
update(item, '[^]');
}
break;
case 'characterClass':
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/character-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ const characterClassFixtures = [
{
pattern: '[^K]', // LATIN CAPITAL LETTER K
flags: 'iu',
expected: '(?:(?![K\\u212A\\uD800-\\uDFFF])[\\s\\S]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])',
expected: '(?:(?![K\\u212A\\uD800-\\uDFFF])[^]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])',
options: { unicodeFlag: 'transform' }
},
{
pattern: '[^k]', // LATIN SMALL LETTER K
flags: 'iu',
expected: '(?:(?![k\\u212A\\uD800-\\uDFFF])[\\s\\S]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])',
expected: '(?:(?![k\\u212A\\uD800-\\uDFFF])[^]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])',
options: { unicodeFlag: 'transform' }
},
{
pattern: '[^\u212a]', // KELVIN SIGN
flags: 'iu',
expected: '(?:(?![K\\u212A\\uD800-\\uDFFF])[\\s\\S]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])',
expected: '(?:(?![K\\u212A\\uD800-\\uDFFF])[^]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])',
options: { unicodeFlag: 'transform' }
},
{
Expand All @@ -38,7 +38,7 @@ const characterClassFixtures = [
{
pattern: '[^\u{1D50E}]', // MATHEMATICAL FRAKTUR CAPITAL K
flags: 'iu',
expected: '(?:(?![\\uD800-\\uDFFF])[\\s\\S]|[\\uD800-\\uD834\\uD836-\\uDBFF][\\uDC00-\\uDFFF]|\\uD835[\\uDC00-\\uDD0D\\uDD0F-\\uDFFF])',
expected: '(?:(?![\\uD800-\\uDFFF])[^]|[\\uD800-\\uD834\\uD836-\\uDBFF][\\uDC00-\\uDFFF]|\\uD835[\\uDC00-\\uDD0D\\uDD0F-\\uDFFF])',
options: { unicodeFlag: 'transform' }
},
{
Expand Down
8 changes: 4 additions & 4 deletions tests/fixtures/dot-all-flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ const dotAllFlagFixtures = [
{
'pattern': '.',
'flags': 's',
'expected': '[\\s\\S]'
'expected': '[^]'
},
{
'pattern': '.',
'flags': 'gimsy',
'expected': '[\\s\\S]'
'expected': '[^]'
},
{
'pattern': '.',
'flags': 's',
'expected': '[\\s\\S]',
'expected': '[^]',
options: { unicodeFlag: 'transform' }
},
{
'pattern': '.',
'flags': 'gimsy',
'expected': '[\\s\\S]',
'expected': '[^]',
options: { unicodeFlag: 'transform' }
},
{
Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const modifiersFixtures = [
// +s
{
'pattern': '(?s:.)',
'expected': '(?:[\\s\\S])',
'expected': '(?:[^])',
},
// -i
{
Expand Down Expand Up @@ -113,7 +113,7 @@ const modifiersFixtures = [
{
'pattern': '(?-ims:^[a-z].)(^[a-z].)',
'flags': 'ims',
'expected': '(?:^[a-z].)((?:^|(?<=[\\n\\r\\u2028\\u2029]))[A-Za-z][\\s\\S])',
'expected': '(?:^[a-z].)((?:^|(?<=[\\n\\r\\u2028\\u2029]))[A-Za-z][^])',
'expectedFlags': '',
},
];
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/unicode-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ const unicodeSetFixtures = [
flags: 'sv',
matches: ['\n'],
options: { unicodeSetsFlag: 'transform', dotAllFlag: 'transform' },
expected: '[\\s\\S]'
expected: '[^]'
}
];

Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/unicode.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const unicodeFixtures = [
{
'pattern': '[^a]',
'flags': FLAGS_WITHOUT_UNICODE,
'transpiled': '(?:(?![a\\uD800-\\uDFFF])[\\s\\S]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])'
'transpiled': '(?:(?![a\\uD800-\\uDFFF])[^]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])'
},
{
'pattern': '[^a]',
Expand All @@ -191,7 +191,7 @@ const unicodeFixtures = [
'pattern': '[^a]',
'nonMatches': ['a', 'A'],
'flags': FLAGS_WITH_UNICODE_WITH_I,
'transpiled': '(?:(?![a\\uD800-\\uDFFF])[\\s\\S]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])'
'transpiled': '(?:(?![a\\uD800-\\uDFFF])[^]|[\\uD800-\\uDBFF][\\uDC00-\\uDFFF])'
},
{
'pattern': '[ab]+',
Expand Down

0 comments on commit 2ae1f91

Please sign in to comment.