]> zoso.dev Git - buffer.git/commitdiff
Add support for fill()
authorTõnis Tiigi <tonistiigi@gmail.com>
Wed, 27 Feb 2013 19:10:07 +0000 (21:10 +0200)
committerRomain Beauxis <toots@rastageeks.org>
Wed, 27 Feb 2013 20:54:31 +0000 (14:54 -0600)
index.js
test/buffer.js

index 07931fef017237719b28ab72a261d777f3717080..1d81be08ab7bb41e25895789e987a5b9df04d36e 100644 (file)
--- a/index.js
+++ b/index.js
@@ -305,6 +305,19 @@ SlowBuffer.prototype.copy = function(target, targetstart, sourcestart, sourceend
   }
 };
 
+SlowBuffer.prototype.fill = function(value, start, end) {
+  if (end > this.length) {
+    throw new Error('oob');
+  }
+  if (start > end) {
+    throw new Error('oob');
+  }
+
+  for (var i = start; i < end; i++) {
+    this[i] = value;
+  }
+}
+
 function coerce(length) {
   // Coerce length to a number (possibly NaN), round up
   // in case it's fractional (e.g. 123.456) then do a
index 99443ec2c6d04cd3f3aad5d0a36826e4dfdee100..0255f948e7082a77c395c6074e41e31f94449737 100644 (file)
@@ -206,3 +206,13 @@ test("buffer from buffer", function (t) {
     t.equal(b1.toString('hex'), b2.toString('hex'));
     t.end();
 });
+
+test("fill", function(t) {
+    t.plan(1);
+    var b1 = new Buffer(10);
+    var b2 = new buffer.Buffer(10);
+    b1.fill(2);
+    b2.fill(2);
+    t.equal(b1.toString('hex'), b2.toString('hex'));
+    t.end();
+})