Merge pull request #13961 from Snuffleupagus/simpler-regexp

Simplify some regular expressions
This commit is contained in:
Tim van der Meij 2021-09-04 15:39:30 +02:00 committed by GitHub
commit 680f33c31c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 38 deletions

View File

@ -208,7 +208,7 @@ function isWhiteSpace(ch) {
* each part of the path. * each part of the path.
*/ */
function parseXFAPath(path) { function parseXFAPath(path) {
const positionPattern = /(.+)\[([0-9]+)\]$/; const positionPattern = /(.+)\[(\d+)\]$/;
return path.split(".").map(component => { return path.split(".").map(component => {
const m = component.match(positionPattern); const m = component.match(positionPattern);
if (m) { if (m) {
@ -428,10 +428,7 @@ function validateCSSFont(cssFontInfo) {
} else { } else {
// See https://developer.mozilla.org/en-US/docs/Web/CSS/custom-ident. // See https://developer.mozilla.org/en-US/docs/Web/CSS/custom-ident.
for (const ident of fontFamily.split(/[ \t]+/)) { for (const ident of fontFamily.split(/[ \t]+/)) {
if ( if (/^(\d|(-(\d|-)))/.test(ident) || !/^[\w-\\]+$/.test(ident)) {
/^([0-9]|(-([0-9]|-)))/.test(ident) ||
!/^[a-zA-Z0-9\-_\\]+$/.test(ident)
) {
warn( warn(
`XFA - FontFamily contains some invalid <custom-ident>: ${fontFamily}.` `XFA - FontFamily contains some invalid <custom-ident>: ${fontFamily}.`
); );

View File

@ -576,7 +576,7 @@ const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT = const EMPTY_FINGERPRINT =
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"; "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
const PDF_HEADER_VERSION_REGEXP = /^[1-9]\.[0-9]$/; const PDF_HEADER_VERSION_REGEXP = /^[1-9]\.\d$/;
function find(stream, signature, limit = 1024, backwards = false) { function find(stream, signature, limit = 1024, backwards = false) {
if ( if (
@ -988,7 +988,7 @@ class PDFDocument {
} }
let fontFamily = descriptor.get("FontFamily"); let fontFamily = descriptor.get("FontFamily");
// For example, "Wingdings 3" is not a valid font name in the css specs. // For example, "Wingdings 3" is not a valid font name in the css specs.
fontFamily = fontFamily.replace(/[ ]+([0-9])/g, "$1"); fontFamily = fontFamily.replace(/[ ]+(\d)/g, "$1");
const fontWeight = descriptor.get("FontWeight"); const fontWeight = descriptor.get("FontWeight");
// Angle is expressed in degrees counterclockwise in PDF // Angle is expressed in degrees counterclockwise in PDF

View File

@ -117,8 +117,8 @@ const TOKEN = {
}; };
const hexPattern = /^[uU]([0-9a-fA-F]{4,8})/; const hexPattern = /^[uU]([0-9a-fA-F]{4,8})/;
const numberPattern = /^[0-9]*(?:\.[0-9]*)?(?:[Ee][+-]?[0-9]+)?/; const numberPattern = /^\d*(?:\.\d*)?(?:[Ee][+-]?\d+)?/;
const dotNumberPattern = /^[0-9]*(?:[Ee][+-]?[0-9]+)?/; const dotNumberPattern = /^\d*(?:[Ee][+-]?\d+)?/;
const eolPattern = /[\r\n]+/; const eolPattern = /[\r\n]+/;
const identifierPattern = new RegExp("^[\\p{L}_$!][\\p{L}\\p{N}_$]*", "u"); const identifierPattern = new RegExp("^[\\p{L}_$!][\\p{L}\\p{N}_$]*", "u");

View File

@ -657,7 +657,7 @@ class Barcode extends XFAObject {
"shift-jis", "shift-jis",
"ucs-2", "ucs-2",
"utf-16", "utf-16",
].includes(k) || k.match(/iso-8859-[0-9]{2}/), ].includes(k) || k.match(/iso-8859-\d{2}/),
}); });
this.checksum = getStringOption(attributes.checksum, [ this.checksum = getStringOption(attributes.checksum, [
"none", "none",
@ -5274,7 +5274,7 @@ class Submit extends XFAObject {
"shift-jis", "shift-jis",
"ucs-2", "ucs-2",
"utf-16", "utf-16",
].includes(k) || k.match(/iso-8859-[0-9]{2}/), ].includes(k) || k.match(/iso-8859-\d{2}/),
}); });
this.use = attributes.use || ""; this.use = attributes.use || "";
this.usehref = attributes.usehref || ""; this.usehref = attributes.usehref || "";

View File

@ -22,7 +22,7 @@ const dimConverters = {
in: x => x * 72, in: x => x * 72,
px: x => x, px: x => x,
}; };
const measurementPattern = /([+-]?[0-9]+\.?[0-9]*)(.*)/; const measurementPattern = /([+-]?\d+\.?\d*)(.*)/;
function stripQuotes(str) { function stripQuotes(str) {
if (str.startsWith("'") || str.startsWith('"')) { if (str.startsWith("'") || str.startsWith('"')) {

View File

@ -90,7 +90,7 @@ class AForm {
str = `0${str}`; str = `0${str}`;
} }
const numbers = str.match(/([0-9]+)/g); const numbers = str.match(/(\d+)/g);
if (numbers.length === 0) { if (numbers.length === 0) {
return null; return null;
} }
@ -202,13 +202,13 @@ class AForm {
if (sepStyle > 1) { if (sepStyle > 1) {
// comma sep // comma sep
pattern = event.willCommit pattern = event.willCommit
? /^[+-]?([0-9]+(,[0-9]*)?|,[0-9]+)$/ ? /^[+-]?(\d+(,\d*)?|,\d+)$/
: /^[+-]?[0-9]*,?[0-9]*$/; : /^[+-]?\d*,?\d*$/;
} else { } else {
// dot sep // dot sep
pattern = event.willCommit pattern = event.willCommit
? /^[+-]?([0-9]+(\.[0-9]*)?|\.[0-9]+)$/ ? /^[+-]?(\d+(\.\d*)?|\.\d+)$/
: /^[+-]?[0-9]*\.?[0-9]*$/; : /^[+-]?\d*\.?\d*$/;
} }
if (!pattern.test(value)) { if (!pattern.test(value)) {

View File

@ -59,7 +59,7 @@ class Util extends PDFObject {
throw new TypeError("First argument of printf must be a string"); throw new TypeError("First argument of printf must be a string");
} }
const pattern = /%(,[0-4])?([+ 0#]+)?([0-9]+)?(\.[0-9]+)?(.)/g; const pattern = /%(,[0-4])?([+ 0#]+)?(\d+)?(\.\d+)?(.)/g;
const PLUS = 1; const PLUS = 1;
const SPACE = 2; const SPACE = 2;
const ZERO = 4; const ZERO = 4;
@ -406,13 +406,13 @@ class Util extends PDFObject {
}, },
}, },
mm: { mm: {
pattern: `([0-9]{2})`, pattern: `(\\d{2})`,
action: (value, data) => { action: (value, data) => {
data.month = parseInt(value) - 1; data.month = parseInt(value) - 1;
}, },
}, },
m: { m: {
pattern: `([0-9]{1,2})`, pattern: `(\\d{1,2})`,
action: (value, data) => { action: (value, data) => {
data.month = parseInt(value) - 1; data.month = parseInt(value) - 1;
}, },
@ -430,73 +430,73 @@ class Util extends PDFObject {
}, },
}, },
dd: { dd: {
pattern: "([0-9]{2})", pattern: "(\\d{2})",
action: (value, data) => { action: (value, data) => {
data.day = parseInt(value); data.day = parseInt(value);
}, },
}, },
d: { d: {
pattern: "([0-9]{1,2})", pattern: "(\\d{1,2})",
action: (value, data) => { action: (value, data) => {
data.day = parseInt(value); data.day = parseInt(value);
}, },
}, },
yyyy: { yyyy: {
pattern: "([0-9]{4})", pattern: "(\\d{4})",
action: (value, data) => { action: (value, data) => {
data.year = parseInt(value); data.year = parseInt(value);
}, },
}, },
yy: { yy: {
pattern: "([0-9]{2})", pattern: "(\\d{2})",
action: (value, data) => { action: (value, data) => {
data.year = 2000 + parseInt(value); data.year = 2000 + parseInt(value);
}, },
}, },
HH: { HH: {
pattern: "([0-9]{2})", pattern: "(\\d{2})",
action: (value, data) => { action: (value, data) => {
data.hours = parseInt(value); data.hours = parseInt(value);
}, },
}, },
H: { H: {
pattern: "([0-9]{1,2})", pattern: "(\\d{1,2})",
action: (value, data) => { action: (value, data) => {
data.hours = parseInt(value); data.hours = parseInt(value);
}, },
}, },
hh: { hh: {
pattern: "([0-9]{2})", pattern: "(\\d{2})",
action: (value, data) => { action: (value, data) => {
data.hours = parseInt(value); data.hours = parseInt(value);
}, },
}, },
h: { h: {
pattern: "([0-9]{1,2})", pattern: "(\\d{1,2})",
action: (value, data) => { action: (value, data) => {
data.hours = parseInt(value); data.hours = parseInt(value);
}, },
}, },
MM: { MM: {
pattern: "([0-9]{2})", pattern: "(\\d{2})",
action: (value, data) => { action: (value, data) => {
data.minutes = parseInt(value); data.minutes = parseInt(value);
}, },
}, },
M: { M: {
pattern: "([0-9]{1,2})", pattern: "(\\d{1,2})",
action: (value, data) => { action: (value, data) => {
data.minutes = parseInt(value); data.minutes = parseInt(value);
}, },
}, },
ss: { ss: {
pattern: "([0-9]{2})", pattern: "(\\d{2})",
action: (value, data) => { action: (value, data) => {
data.seconds = parseInt(value); data.seconds = parseInt(value);
}, },
}, },
s: { s: {
pattern: "([0-9]{1,2})", pattern: "(\\d{1,2})",
action: (value, data) => { action: (value, data) => {
data.seconds = parseInt(value); data.seconds = parseInt(value);
}, },

View File

@ -2036,7 +2036,7 @@ describe("annotation", function () {
expect(oldData.ref).toEqual(Ref.get(123, 0)); expect(oldData.ref).toEqual(Ref.get(123, 0));
expect(newData.ref).toEqual(Ref.get(2, 0)); expect(newData.ref).toEqual(Ref.get(2, 0));
oldData.data = oldData.data.replace(/\(D:[0-9]+\)/, "(date)"); oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
expect(oldData.data).toEqual( expect(oldData.data).toEqual(
"123 0 obj\n" + "123 0 obj\n" +
"<< /Type /Annot /Subtype /Widget /FT /Tx /DA (/Helv 5 Tf) /DR " + "<< /Type /Annot /Subtype /Widget /FT /Tx /DA (/Helv 5 Tf) /DR " +
@ -2167,7 +2167,7 @@ describe("annotation", function () {
expect(oldData.ref).toEqual(Ref.get(123, 0)); expect(oldData.ref).toEqual(Ref.get(123, 0));
expect(newData.ref).toEqual(Ref.get(2, 0)); expect(newData.ref).toEqual(Ref.get(2, 0));
oldData.data = oldData.data.replace(/\(D:[0-9]+\)/, "(date)"); oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
expect(oldData.data).toEqual( expect(oldData.data).toEqual(
"123 0 obj\n" + "123 0 obj\n" +
"<< /Type /Annot /Subtype /Widget /FT /Tx /DA (/Goth 5 Tf) /DR " + "<< /Type /Annot /Subtype /Widget /FT /Tx /DA (/Goth 5 Tf) /DR " +
@ -2576,7 +2576,7 @@ describe("annotation", function () {
task, task,
annotationStorage annotationStorage
); );
oldData.data = oldData.data.replace(/\(D:[0-9]+\)/, "(date)"); oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
expect(oldData.ref).toEqual(Ref.get(123, 0)); expect(oldData.ref).toEqual(Ref.get(123, 0));
expect(oldData.data).toEqual( expect(oldData.data).toEqual(
"123 0 obj\n" + "123 0 obj\n" +
@ -2876,7 +2876,7 @@ describe("annotation", function () {
); );
expect(data.length).toEqual(2); expect(data.length).toEqual(2);
const [radioData, parentData] = data; const [radioData, parentData] = data;
radioData.data = radioData.data.replace(/\(D:[0-9]+\)/, "(date)"); radioData.data = radioData.data.replace(/\(D:\d+\)/, "(date)");
expect(radioData.ref).toEqual(Ref.get(123, 0)); expect(radioData.ref).toEqual(Ref.get(123, 0));
expect(radioData.data).toEqual( expect(radioData.data).toEqual(
"123 0 obj\n" + "123 0 obj\n" +
@ -2939,7 +2939,7 @@ describe("annotation", function () {
); );
expect(data.length).toEqual(2); expect(data.length).toEqual(2);
const [radioData, parentData] = data; const [radioData, parentData] = data;
radioData.data = radioData.data.replace(/\(D:[0-9]+\)/, "(date)"); radioData.data = radioData.data.replace(/\(D:\d+\)/, "(date)");
expect(radioData.ref).toEqual(Ref.get(123, 0)); expect(radioData.ref).toEqual(Ref.get(123, 0));
expect(radioData.data).toEqual( expect(radioData.data).toEqual(
"123 0 obj\n" + "123 0 obj\n" +
@ -3389,7 +3389,7 @@ describe("annotation", function () {
expect(oldData.ref).toEqual(Ref.get(123, 0)); expect(oldData.ref).toEqual(Ref.get(123, 0));
expect(newData.ref).toEqual(Ref.get(1, 0)); expect(newData.ref).toEqual(Ref.get(1, 0));
oldData.data = oldData.data.replace(/\(D:[0-9]+\)/, "(date)"); oldData.data = oldData.data.replace(/\(D:\d+\)/, "(date)");
expect(oldData.data).toEqual( expect(oldData.data).toEqual(
"123 0 obj\n" + "123 0 obj\n" +
"<< /Type /Annot /Subtype /Widget /FT /Ch /DA (/Helv 5 Tf) /DR " + "<< /Type /Annot /Subtype /Widget /FT /Ch /DA (/Helv 5 Tf) /DR " +