Optimize character class escape tests

Previously, these tests simply compared against an equivalent regular expression pattern by generating a large string containing either all code points up to U+FFFF or U+10FFFF, depending on the `u` flag, and then performing:

    str.replace(re, '') === str.replace(otherRe, '');

These two `String#replace` calls can be optimized into a single `RegExp#test` call by following the pattern used in the property escapes tests at https://github.com/mathiasbynens/unicode-property-escapes-tests.
This commit is contained in:
Mathias Bynens 2018-11-06 16:14:14 -08:00
parent 01550ab13a
commit e558b29b69
No known key found for this signature in database
GPG Key ID: B6A1C4401A39330B
24 changed files with 312 additions and 192 deletions

View File

@ -37,21 +37,21 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x0030, 0x0039],
],
});
const re = /\d/ug;
const matchingRange = /[0-9]/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,21 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x0030, 0x0039],
],
});
const re = /\d+/ug;
const matchingRange = /[0-9]+/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,21 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x0030, 0x0039],
],
});
const re = /\d+/g;
const matchingRange = /[0-9]+/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,21 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x0030, 0x0039],
],
});
const re = /\d/g;
const matchingRange = /[0-9]/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,22 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x000000, 0x00002F],
[0x00003A, 0x10FFFF],
],
});
const re = /\D/ug;
const matchingRange = /[\0-\/:-\u{10FFFF}]/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,22 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x000000, 0x00002F],
[0x00003A, 0x10FFFF],
],
});
const re = /\D+/ug;
const matchingRange = /[\0-\/:-\u{10FFFF}]+/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,22 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x000000, 0x00002F],
[0x00003A, 0x00FFFF],
],
});
const re = /\D+/g;
const matchingRange = /[\0-\/:-\uFFFF]+/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,22 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x000000, 0x00002F],
[0x00003A, 0x00FFFF],
],
});
const re = /\D/g;
const matchingRange = /[\0-\/:-\uFFFF]/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,33 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x000008],
[0x00000E, 0x00001F],
[0x000021, 0x00009F],
[0x0000A1, 0x00167F],
[0x001681, 0x001FFF],
[0x00200B, 0x002027],
[0x00202A, 0x00202E],
[0x002030, 0x00205E],
[0x002060, 0x002FFF],
[0x003001, 0x00DBFF],
[0x00E000, 0x00FEFE],
[0x00FF00, 0x10FFFF],
],
});
const re = /\S/ug;
const matchingRange = /[\0-\x08\x0E-\x1F!-\x9F\xA1-\u167F\u1681-\u1FFF\u200B-\u2027\u202A-\u202E\u2030-\u205E\u2060-\u2FFF\u3001-\uFEFE\uFF00-\u{10FFFF}]/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,33 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x000008],
[0x00000E, 0x00001F],
[0x000021, 0x00009F],
[0x0000A1, 0x00167F],
[0x001681, 0x001FFF],
[0x00200B, 0x002027],
[0x00202A, 0x00202E],
[0x002030, 0x00205E],
[0x002060, 0x002FFF],
[0x003001, 0x00DBFF],
[0x00E000, 0x00FEFE],
[0x00FF00, 0x10FFFF],
],
});
const re = /\S+/ug;
const matchingRange = /[\0-\x08\x0E-\x1F!-\x9F\xA1-\u167F\u1681-\u1FFF\u200B-\u2027\u202A-\u202E\u2030-\u205E\u2060-\u2FFF\u3001-\uFEFE\uFF00-\u{10FFFF}]+/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,33 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x000008],
[0x00000E, 0x00001F],
[0x000021, 0x00009F],
[0x0000A1, 0x00167F],
[0x001681, 0x001FFF],
[0x00200B, 0x002027],
[0x00202A, 0x00202E],
[0x002030, 0x00205E],
[0x002060, 0x002FFF],
[0x003001, 0x00DBFF],
[0x00E000, 0x00FEFE],
[0x00FF00, 0x00FFFF],
],
});
const re = /\S+/g;
const matchingRange = /[\0-\x08\x0E-\x1F!-\x9F\xA1-\u167F\u1681-\u1FFF\u200B-\u2027\u202A-\u202E\u2030-\u205E\u2060-\u2FFF\u3001-\uFEFE\uFF00-\uFFFF]+/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,33 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x000008],
[0x00000E, 0x00001F],
[0x000021, 0x00009F],
[0x0000A1, 0x00167F],
[0x001681, 0x001FFF],
[0x00200B, 0x002027],
[0x00202A, 0x00202E],
[0x002030, 0x00205E],
[0x002060, 0x002FFF],
[0x003001, 0x00DBFF],
[0x00E000, 0x00FEFE],
[0x00FF00, 0x00FFFF],
],
});
const re = /\S/g;
const matchingRange = /[\0-\x08\x0E-\x1F!-\x9F\xA1-\u167F\u1681-\u1FFF\u200B-\u2027\u202A-\u202E\u2030-\u205E\u2060-\u2FFF\u3001-\uFEFE\uFF00-\uFFFF]/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,26 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [0x000060],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x00002F],
[0x00003A, 0x000040],
[0x00005B, 0x00005E],
[0x00007B, 0x00DBFF],
[0x00E000, 0x10FFFF],
],
});
const re = /\W/ug;
const matchingRange = /[\0-\/:-@\[-\^`\{-\u{10FFFF}]/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,26 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [0x000060],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x00002F],
[0x00003A, 0x000040],
[0x00005B, 0x00005E],
[0x00007B, 0x00DBFF],
[0x00E000, 0x10FFFF],
],
});
const re = /\W+/ug;
const matchingRange = /[\0-\/:-@\[-\^`\{-\u{10FFFF}]+/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,26 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [0x000060],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x00002F],
[0x00003A, 0x000040],
[0x00005B, 0x00005E],
[0x00007B, 0x00DBFF],
[0x00E000, 0x00FFFF],
],
});
const re = /\W+/g;
const matchingRange = /[\0-\/:-@\[-\^`\{-\uFFFF]+/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,26 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [0x000060],
ranges: [
[0x00DC00, 0x00DFFF],
[0x000000, 0x00002F],
[0x00003A, 0x000040],
[0x00005B, 0x00005E],
[0x00007B, 0x00DBFF],
[0x00E000, 0x00FFFF],
],
});
const re = /\W/g;
const matchingRange = /[\0-\/:-@\[-\^`\{-\uFFFF]/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,31 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [
0x000020,
0x0000A0,
0x001680,
0x00202F,
0x00205F,
0x003000,
0x00FEFF,
],
ranges: [
[0x000009, 0x00000D],
[0x002000, 0x00200A],
[0x002028, 0x002029],
],
});
const re = /\s/ug;
const matchingRange = /[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,31 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [
0x000020,
0x0000A0,
0x001680,
0x00202F,
0x00205F,
0x003000,
0x00FEFF,
],
ranges: [
[0x000009, 0x00000D],
[0x002000, 0x00200A],
[0x002028, 0x002029],
],
});
const re = /\s+/ug;
const matchingRange = /[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]+/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,31 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [
0x000020,
0x0000A0,
0x001680,
0x00202F,
0x00205F,
0x003000,
0x00FEFF,
],
ranges: [
[0x000009, 0x00000D],
[0x002000, 0x00200A],
[0x002028, 0x002029],
],
});
const re = /\s+/g;
const matchingRange = /[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]+/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,31 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [
0x000020,
0x0000A0,
0x001680,
0x00202F,
0x00205F,
0x003000,
0x00FEFF,
],
ranges: [
[0x000009, 0x00000D],
[0x002000, 0x00200A],
[0x002028, 0x002029],
],
});
const re = /\s/g;
const matchingRange = /[\t-\r \xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,23 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [0x00005F],
ranges: [
[0x000030, 0x000039],
[0x000041, 0x00005A],
[0x000061, 0x00007A],
],
});
const re = /\w/ug;
const matchingRange = /[0-9A-Z_a-z]/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,23 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0x10FFFF]] });
const str = buildString({
loneCodePoints: [0x00005F],
ranges: [
[0x000030, 0x000039],
[0x000041, 0x00005A],
[0x000061, 0x00007A],
],
});
const re = /\w+/ug;
const matchingRange = /[0-9A-Z_a-z]+/ug;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,23 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [0x00005F],
ranges: [
[0x000030, 0x000039],
[0x000041, 0x00005A],
[0x000061, 0x00007A],
],
});
const re = /\w+/g;
const matchingRange = /[0-9A-Z_a-z]+/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}

View File

@ -37,21 +37,23 @@ features: [String.fromCodePoint]
includes: [regExpUtils.js]
---*/
const str = buildString({ loneCodePoints: [], ranges: [[0, 0xFFFF]] });
const str = buildString({
loneCodePoints: [0x00005F],
ranges: [
[0x000030, 0x000039],
[0x000041, 0x00005A],
[0x000061, 0x00007A],
],
});
const re = /\w/g;
const matchingRange = /[0-9A-Z_a-z]/g;
const errors = [];
function matching(str) {
return str.replace(re, '') === str.replace(matchingRange, '');
}
if (!matching(str)) {
if (!re.test(str)) {
// Error, let's find out where
for (const char of str) {
if (!matching(char)) {
if (!re.test(char)) {
errors.push('0x' + char.codePointAt(0).toString(16));
}
}