From 51ebcf999b248e1b3d5ca788efad0d8366ff9b4a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 4 May 2016 01:06:34 -0700 Subject: [PATCH] faster FindAndDelete. --- lib/bcoin/script.js | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index 386f2218..5d35b4b0 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -1972,6 +1972,44 @@ Script.array = function(value) { */ Script.prototype.removeData = function removeData(data) { + var index = []; + var i, op; + + // We need to go forward first. We can't go + // backwards (this is consensus code and we + // need to be aware of bad pushes). + for (i = 0; i < this.code.length; i++) { + op = this.code[i]; + + if (Script.isBadPush(op)) + break; + + if (!Buffer.isBuffer(op)) + continue; + + if (!Script.checkMinimal(op)) + continue; + + if (utils.equal(op, data)) + index.push(i); + } + + if (index.length === 0) + return 0; + + // Go backwards and splice out the data. + for (i = index.length - 1; i >= 0; i--) + this.code.splice(index[i], 1); + + if (this.raw) { + delete this.raw; + this.encode(); + } + + return index.length; +}; + +Script.prototype.removeDataRaw = function removeDataRaw(data) { var total = 0; var p, sig, raw, i, a, b, op, size;