From e02d645c070329ce4e3ea56788a584c35d114c0a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 26 Jun 2017 12:41:00 -0700 Subject: [PATCH] util: optimize isUInt32 and isInt32. --- lib/utils/util.js | 30 +++++++++++++++--------------- lib/utils/validator.js | 4 ++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/utils/util.js b/lib/utils/util.js index 376c9426..30c17a62 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -85,17 +85,6 @@ util.concat = function concat(a, b) { return data; }; -/** - * Test whether a string is base58 (note that you - * may get a false positive on a hex string). - * @param {String?} obj - * @returns {Boolean} - */ - -util.isBase58 = function isBase58(obj) { - return typeof obj === 'string' && /^[1-9a-zA-Z]+$/.test(obj); -}; - /** * Return hrtime (shim for browser). * @param {Array} time @@ -125,6 +114,17 @@ util.hrtime = function hrtime(time) { return process.hrtime(); }; +/** + * Test whether a string is base58 (note that you + * may get a false positive on a hex string). + * @param {String?} obj + * @returns {Boolean} + */ + +util.isBase58 = function isBase58(obj) { + return typeof obj === 'string' && /^[1-9a-zA-Z]+$/.test(obj); +}; + /** * Test whether a string is hex (length must be even). * Note that this _could_ await a false positive on @@ -279,7 +279,7 @@ util.isInt = function isInt(value) { */ util.isInt8 = function isInt8(value) { - return util.isInt(value) && value >= -0x80 && value <= 0x7f; + return (value | 0) === value && value >= -0x80 && value <= 0x7f; }; /** @@ -289,7 +289,7 @@ util.isInt8 = function isInt8(value) { */ util.isUInt8 = function isUInt8(value) { - return util.isInt(value) && value >= 0 && value <= 0xff; + return (value >>> 0) === value && value >= 0 && value <= 0xff; }; /** @@ -299,7 +299,7 @@ util.isUInt8 = function isUInt8(value) { */ util.isInt32 = function isInt32(value) { - return util.isInt(value) && value >= -0x80000000 && value <= 0x7fffffff; + return (value | 0) === value; }; /** @@ -309,7 +309,7 @@ util.isInt32 = function isInt32(value) { */ util.isUInt32 = function isUInt32(value) { - return util.isInt(value) && value >= 0 && value <= 0xffffffff; + return (value >>> 0) === value; }; /** diff --git a/lib/utils/validator.js b/lib/utils/validator.js index a5a2922f..77b913a8 100644 --- a/lib/utils/validator.js +++ b/lib/utils/validator.js @@ -208,7 +208,7 @@ Validator.prototype.u32 = function u32(key, fallback) { if (value === null) return fallback; - if (value % 1 !== 0 || value < 0 || value > 0xffffffff) + if ((value >>> 0) !== value) throw new ValidationError(fmt(key) + ' must be a uint32.'); return value; @@ -252,7 +252,7 @@ Validator.prototype.i32 = function i32(key, fallback) { if (value === null) return fallback; - if (value % 1 !== 0 || value < -0x80000000 || value > 0x7fffffff) + if ((value | 0) !== value) throw new ValidationError(fmt(key) + ' must be an int32.'); return value;