Skip to content

Commit

Permalink
Merge pull request #13961 from Snuffleupagus/simpler-regexp
Browse files Browse the repository at this point in the history
Simplify some regular expressions
  • Loading branch information
timvandermeij authored Sep 4, 2021
2 parents 258cf1d + c428872 commit 680f33c
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 38 deletions.
7 changes: 2 additions & 5 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function isWhiteSpace(ch) {
* each part of the path.
*/
function parseXFAPath(path) {
const positionPattern = /(.+)\[([0-9]+)\]$/;
const positionPattern = /(.+)\[(\d+)\]$/;
return path.split(".").map(component => {
const m = component.match(positionPattern);
if (m) {
Expand Down Expand Up @@ -428,10 +428,7 @@ function validateCSSFont(cssFontInfo) {
} else {
// See https://developer.mozilla.org/en-US/docs/Web/CSS/custom-ident.
for (const ident of fontFamily.split(/[ \t]+/)) {
if (
/^([0-9]|(-([0-9]|-)))/.test(ident) ||
!/^[a-zA-Z0-9\-_\\]+$/.test(ident)
) {
if (/^(\d|(-(\d|-)))/.test(ident) || !/^[\w-\\]+$/.test(ident)) {
warn(
`XFA - FontFamily contains some invalid <custom-ident>: ${fontFamily}.`
);
Expand Down
4 changes: 2 additions & 2 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT =
"\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) {
if (
Expand Down Expand Up @@ -988,7 +988,7 @@ class PDFDocument {
}
let fontFamily = descriptor.get("FontFamily");
// 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");

// Angle is expressed in degrees counterclockwise in PDF
Expand Down
4 changes: 2 additions & 2 deletions src/core/xfa/formcalc_lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ const TOKEN = {
};

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

Expand Down
4 changes: 2 additions & 2 deletions src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ class Barcode extends XFAObject {
"shift-jis",
"ucs-2",
"utf-16",
].includes(k) || k.match(/iso-8859-[0-9]{2}/),
].includes(k) || k.match(/iso-8859-\d{2}/),
});
this.checksum = getStringOption(attributes.checksum, [
"none",
Expand Down Expand Up @@ -5274,7 +5274,7 @@ class Submit extends XFAObject {
"shift-jis",
"ucs-2",
"utf-16",
].includes(k) || k.match(/iso-8859-[0-9]{2}/),
].includes(k) || k.match(/iso-8859-\d{2}/),
});
this.use = attributes.use || "";
this.usehref = attributes.usehref || "";
Expand Down
2 changes: 1 addition & 1 deletion src/core/xfa/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const dimConverters = {
in: x => x * 72,
px: x => x,
};
const measurementPattern = /([+-]?[0-9]+\.?[0-9]*)(.*)/;
const measurementPattern = /([+-]?\d+\.?\d*)(.*)/;

function stripQuotes(str) {
if (str.startsWith("'") || str.startsWith('"')) {
Expand Down
10 changes: 5 additions & 5 deletions src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class AForm {
str = `0${str}`;
}

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

if (!pattern.test(value)) {
Expand Down
30 changes: 15 additions & 15 deletions src/scripting_api/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Util extends PDFObject {
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 SPACE = 2;
const ZERO = 4;
Expand Down Expand Up @@ -406,13 +406,13 @@ class Util extends PDFObject {
},
},
mm: {
pattern: `([0-9]{2})`,
pattern: `(\\d{2})`,
action: (value, data) => {
data.month = parseInt(value) - 1;
},
},
m: {
pattern: `([0-9]{1,2})`,
pattern: `(\\d{1,2})`,
action: (value, data) => {
data.month = parseInt(value) - 1;
},
Expand All @@ -430,73 +430,73 @@ class Util extends PDFObject {
},
},
dd: {
pattern: "([0-9]{2})",
pattern: "(\\d{2})",
action: (value, data) => {
data.day = parseInt(value);
},
},
d: {
pattern: "([0-9]{1,2})",
pattern: "(\\d{1,2})",
action: (value, data) => {
data.day = parseInt(value);
},
},
yyyy: {
pattern: "([0-9]{4})",
pattern: "(\\d{4})",
action: (value, data) => {
data.year = parseInt(value);
},
},
yy: {
pattern: "([0-9]{2})",
pattern: "(\\d{2})",
action: (value, data) => {
data.year = 2000 + parseInt(value);
},
},
HH: {
pattern: "([0-9]{2})",
pattern: "(\\d{2})",
action: (value, data) => {
data.hours = parseInt(value);
},
},
H: {
pattern: "([0-9]{1,2})",
pattern: "(\\d{1,2})",
action: (value, data) => {
data.hours = parseInt(value);
},
},
hh: {
pattern: "([0-9]{2})",
pattern: "(\\d{2})",
action: (value, data) => {
data.hours = parseInt(value);
},
},
h: {
pattern: "([0-9]{1,2})",
pattern: "(\\d{1,2})",
action: (value, data) => {
data.hours = parseInt(value);
},
},
MM: {
pattern: "([0-9]{2})",
pattern: "(\\d{2})",
action: (value, data) => {
data.minutes = parseInt(value);
},
},
M: {
pattern: "([0-9]{1,2})",
pattern: "(\\d{1,2})",
action: (value, data) => {
data.minutes = parseInt(value);
},
},
ss: {
pattern: "([0-9]{2})",
pattern: "(\\d{2})",
action: (value, data) => {
data.seconds = parseInt(value);
},
},
s: {
pattern: "([0-9]{1,2})",
pattern: "(\\d{1,2})",
action: (value, data) => {
data.seconds = parseInt(value);
},
Expand Down
12 changes: 6 additions & 6 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,7 @@ describe("annotation", function () {
expect(oldData.ref).toEqual(Ref.get(123, 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(
"123 0 obj\n" +
"<< /Type /Annot /Subtype /Widget /FT /Tx /DA (/Helv 5 Tf) /DR " +
Expand Down Expand Up @@ -2167,7 +2167,7 @@ describe("annotation", function () {
expect(oldData.ref).toEqual(Ref.get(123, 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(
"123 0 obj\n" +
"<< /Type /Annot /Subtype /Widget /FT /Tx /DA (/Goth 5 Tf) /DR " +
Expand Down Expand Up @@ -2576,7 +2576,7 @@ describe("annotation", function () {
task,
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.data).toEqual(
"123 0 obj\n" +
Expand Down Expand Up @@ -2876,7 +2876,7 @@ describe("annotation", function () {
);
expect(data.length).toEqual(2);
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.data).toEqual(
"123 0 obj\n" +
Expand Down Expand Up @@ -2939,7 +2939,7 @@ describe("annotation", function () {
);
expect(data.length).toEqual(2);
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.data).toEqual(
"123 0 obj\n" +
Expand Down Expand Up @@ -3389,7 +3389,7 @@ describe("annotation", function () {
expect(oldData.ref).toEqual(Ref.get(123, 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(
"123 0 obj\n" +
"<< /Type /Annot /Subtype /Widget /FT /Ch /DA (/Helv 5 Tf) /DR " +
Expand Down

0 comments on commit 680f33c

Please sign in to comment.