Use RegExp.prototype.test instead of String.prototype.match where it is OK

There is no sense in using String.prototype.match if the retrieved matched results
are not used.

By the way, if a bit of performance (especially RAM usage) is preferred over
unification, then, obviously, conditions like
if (/SIGNED MESSAGE/.test(header[1])) {
can be changed to
if (header[1].indexOf('SIGNED MESSAGE') !== -1) {
This commit is contained in:
Artem Chudinov 2015-08-20 04:24:56 +05:00 committed by evilaliv3
parent 30926e4738
commit 068d38d832

View File

@ -49,14 +49,14 @@ function getType(text) {
// BEGIN PGP MESSAGE, PART X/Y // BEGIN PGP MESSAGE, PART X/Y
// Used for multi-part messages, where the armor is split amongst Y // Used for multi-part messages, where the armor is split amongst Y
// parts, and this is the Xth part out of Y. // parts, and this is the Xth part out of Y.
if (header[1].match(/MESSAGE, PART \d+\/\d+/)) { if (/MESSAGE, PART \d+\/\d+/.test(header[1])) {
return enums.armor.multipart_section; return enums.armor.multipart_section;
} else } else
// BEGIN PGP MESSAGE, PART X // BEGIN PGP MESSAGE, PART X
// Used for multi-part messages, where this is the Xth part of an // Used for multi-part messages, where this is the Xth part of an
// unspecified number of parts. Requires the MESSAGE-ID Armor // unspecified number of parts. Requires the MESSAGE-ID Armor
// Header to be used. // Header to be used.
if (header[1].match(/MESSAGE, PART \d+/)) { if (/MESSAGE, PART \d+/.test(header[1])) {
return enums.armor.multipart_last; return enums.armor.multipart_last;
} else } else
@ -64,25 +64,25 @@ function getType(text) {
// Used for detached signatures, OpenPGP/MIME signatures, and // Used for detached signatures, OpenPGP/MIME signatures, and
// cleartext signatures. Note that PGP 2.x uses BEGIN PGP MESSAGE // cleartext signatures. Note that PGP 2.x uses BEGIN PGP MESSAGE
// for detached signatures. // for detached signatures.
if (header[1].match(/SIGNED MESSAGE/)) { if (/SIGNED MESSAGE/.test(header[1])) {
return enums.armor.signed; return enums.armor.signed;
} else } else
// BEGIN PGP MESSAGE // BEGIN PGP MESSAGE
// Used for signed, encrypted, or compressed files. // Used for signed, encrypted, or compressed files.
if (header[1].match(/MESSAGE/)) { if (/MESSAGE/.test(header[1])) {
return enums.armor.message; return enums.armor.message;
} else } else
// BEGIN PGP PUBLIC KEY BLOCK // BEGIN PGP PUBLIC KEY BLOCK
// Used for armoring public keys. // Used for armoring public keys.
if (header[1].match(/PUBLIC KEY BLOCK/)) { if (/PUBLIC KEY BLOCK/.test(header[1])) {
return enums.armor.public_key; return enums.armor.public_key;
} else } else
// BEGIN PGP PRIVATE KEY BLOCK // BEGIN PGP PRIVATE KEY BLOCK
// Used for armoring private keys. // Used for armoring private keys.
if (header[1].match(/PRIVATE KEY BLOCK/)) { if (/PRIVATE KEY BLOCK/.test(header[1])) {
return enums.armor.private_key; return enums.armor.private_key;
} }
} }
@ -240,7 +240,7 @@ function splitHeaders(text) {
*/ */
function verifyHeaders(headers) { function verifyHeaders(headers) {
for (var i = 0; i < headers.length; i++) { for (var i = 0; i < headers.length; i++) {
if (!headers[i].match(/^(Version|Comment|MessageID|Hash|Charset): .+$/)) { if (!/^(Version|Comment|MessageID|Hash|Charset): .+$/.test(headers[i])) {
throw new Error('Improperly formatted armor header: ' + headers[i]); throw new Error('Improperly formatted armor header: ' + headers[i]);
} }
} }