Skip to content

Commit 6cd385c

Browse files
committed
Increase offset for relative fill()s
1 parent 0bad3ed commit 6cd385c

11 files changed

+62
-44
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
A fast and complete ByteBuffer implementation using either ArrayBuffers in the browser or node Buffers under node.js,
44
generated from a single source tree through [MetaScript](https://github.com/dcodeIO/MetaScript).
55

6-
If you are looking for ByteBuffer.js 2, [that's the branch](https://github.com/dcodeIO/ByteBuffer.js/tree/ByteBuffer2).
6+
If you are looking for ByteBuffer.js 2 (ArrayBuffers only), [that's the branch](https://github.com/dcodeIO/ByteBuffer.js/tree/ByteBuffer2).
77

88
What can it do?
99
---------------

dist/ByteBufferAB.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1827,8 +1827,8 @@
18271827
* read if omitted.
18281828
* @returns {!ByteBuffer} this
18291829
* @expose
1830-
* @example `A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
1831-
* @example `An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
1830+
* @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
1831+
* @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
18321832
*/
18331833
ByteBuffer.prototype.append = function(source, encoding, offset) {
18341834
if (typeof encoding === 'number' || typeof encoding !== 'string') {
@@ -2075,6 +2075,8 @@
20752075
* @example `someByteBuffer.clear().fill(0)` fills the entire backing buffer with zeroes
20762076
*/
20772077
ByteBuffer.prototype.fill = function(value, begin, end) {
2078+
var relative = typeof begin === 'undefined';
2079+
if (relative) begin = this.offset;
20782080
if (typeof value === 'string' && value.length > 0)
20792081
value = value.charCodeAt(0);
20802082
if (typeof begin === 'undefined') begin = this.offset;
@@ -2094,6 +2096,9 @@
20942096
}
20952097
if (begin >= end) return this; // Nothing to fill
20962098
while (begin < end) this.view.setUint8(begin++, value);
2099+
if (relative) {
2100+
this.offset = begin;
2101+
}
20972102
return this;
20982103
};
20992104

dist/ByteBufferAB.min.js

+27-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ByteBufferAB.min.js.gz

11 Bytes
Binary file not shown.

dist/ByteBufferAB.min.map

+1-1
Large diffs are not rendered by default.

dist/ByteBufferNB.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1978,8 +1978,8 @@ module.exports = (function() {
19781978
* read if omitted.
19791979
* @returns {!ByteBuffer} this
19801980
* @expose
1981-
* @example `A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
1982-
* @example `An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
1981+
* @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
1982+
* @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
19831983
*/
19841984
ByteBuffer.prototype.append = function(source, encoding, offset) {
19851985
if (typeof encoding === 'number' || typeof encoding !== 'string') {
@@ -2222,6 +2222,8 @@ module.exports = (function() {
22222222
* @example `someByteBuffer.clear().fill(0)` fills the entire backing buffer with zeroes
22232223
*/
22242224
ByteBuffer.prototype.fill = function(value, begin, end) {
2225+
var relative = typeof begin === 'undefined';
2226+
if (relative) begin = this.offset;
22252227
if (typeof value === 'string' && value.length > 0)
22262228
value = value.charCodeAt(0);
22272229
if (typeof begin === 'undefined') begin = this.offset;
@@ -2241,6 +2243,10 @@ module.exports = (function() {
22412243
}
22422244
if (begin >= end) return this; // Nothing to fill
22432245
this.buffer.fill(value, begin, end);
2246+
begin = end;
2247+
if (relative) {
2248+
this.offset = begin;
2249+
}
22442250
return this;
22452251
};
22462252

docs/module-ByteBuffer.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -4538,9 +4538,9 @@ <h5>Returns:</h5>
45384538

45394539
<h5>Examples</h5>
45404540

4541-
<pre class="prettyprint"><code> `A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`</code></pre>
4541+
<pre class="prettyprint"><code> A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`</code></pre>
45424542

4543-
<pre class="prettyprint"><code> `An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`</code></pre>
4543+
<pre class="prettyprint"><code> An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`</code></pre>
45444544

45454545

45464546
</dd>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "bytebuffer",
33
"version": "3.0.0-pre",
44
"author": "Daniel Wirtz <[email protected]>",
5-
"description": "A full-featured, zero-copy aware ByteBuffer implementation for both typed arrays and node buffers.",
5+
"description": "The swiss army knife for binary data in JavaScript.",
66
"main": "ByteBuffer.js",
77
"repository": {
88
"type": "git",

src/methods/append.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* read if omitted.
1414
* @returns {!ByteBuffer} this
1515
* @expose
16-
* @example `A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
17-
* @example `An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
16+
* @example A relative `<01 02>03.append(<04 05>)` will result in `<01 02 04 05>, 04 05|`
17+
* @example An absolute `<01 02>03.append(04 05>, 1)` will result in `<01 04>05, 04 05|`
1818
*/
1919
ByteBuffer.prototype.append = function(source, encoding, offset) {
2020
if (typeof encoding === 'number' || typeof encoding !== 'string') {

src/methods/fill.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
* Overwrites this ByteBuffer's contents with the specified value. Contents are the bytes between
33
* {@link ByteBuffer#offset} and {@link ByteBuffer#limit}.
44
* @param {number|string} value Byte value to fill with. If given as a string, the first character is used.
5-
* @param {number=} begin Begin offset, defaults to {@link ByteBuffer#offset}.
5+
* @param {number=} begin Begin offset. Will use and increase {@link ByteBuffer#offset} by the number of bytes
6+
* written if omitted. defaults to {@link ByteBuffer#offset}.
67
* @param {number=} end End offset, defaults to {@link ByteBuffer#limit}.
78
* @returns {!ByteBuffer} this
89
* @expose
910
* @example `someByteBuffer.clear().fill(0)` fills the entire backing buffer with zeroes
1011
*/
1112
ByteBuffer.prototype.fill = function(value, begin, end) {
13+
//? RELATIVE(undefined, 'begin');
1214
if (typeof value === 'string' && value.length > 0)
1315
value = value.charCodeAt(0);
1416
if (typeof begin === 'undefined') begin = this.offset;
@@ -18,11 +20,15 @@ ByteBuffer.prototype.fill = function(value, begin, end) {
1820
//? ASSERT_RANGE();
1921
}
2022
if (begin >= end) return this; // Nothing to fill
21-
//? if (NODE)
23+
//? if (NODE) {
2224
this.buffer.fill(value, begin, end);
23-
//? else {
25+
begin = end;
26+
//? } else {
2427
while (begin < end) this.view.setUint8(begin++, value);
2528
//? }
29+
if (relative) {
30+
this.offset = begin;
31+
}
2632
return this;
2733
};
2834

tests/suite.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @author Daniel Wirtz <[email protected]>
2020
*/ //
2121
var ByteBuffer = require("../index.js");
22+
ByteBuffer.ByteBufferAB = require("../dist/ByteBufferAB.min.js"); // Test minified version
2223

2324
/**
2425
* Constructs a new Sandbox for module loaders and shim testing.
@@ -339,7 +340,7 @@ function makeSuite(ByteBuffer) {
339340

340341
suite.methods.copyTo = function(test) {
341342
var bb = ByteBuffer.wrap("\x01"),
342-
bb2 = new ByteBuffer(2).fill(0);
343+
bb2 = new ByteBuffer(2).fill(0).flip();
343344
test.equal(bb.toDebug(), "<01>");
344345
// Modifies source and target offsets
345346
bb.copyTo(bb2 /* all offsets omitted */);
@@ -352,14 +353,14 @@ function makeSuite(ByteBuffer) {
352353
test.equal(bb.toDebug(), "01|"); // Read 1 byte
353354
test.equal(bb2.toDebug(), "01 01|"); // Written 1 byte at 2
354355
bb.reset();
355-
bb2.reset().fill(0);
356+
bb2.clear().fill(0).flip();
356357
// Modifies source offsets only
357358
bb.copyTo(bb2, 0 /* source offsets omitted */);
358359
test.equal(bb.toDebug(), "01|"); // Read 1 byte
359360
test.equal(bb2.toDebug(), "<01 00>"); // Written 1 byte (no change)
360361
// Modifies no offsets at all
361362
bb.reset();
362-
bb2.fill(0);
363+
bb2.fill(0).flip();
363364
bb.copyTo(bb2, 1, 0, bb.capacity() /* no offsets omitted */);
364365
test.equal(bb.toDebug(), "<01>"); // Read 1 byte (no change)
365366
test.equal(bb2.toDebug(), "<00 01>"); // Written 1 byte (no change)

0 commit comments

Comments
 (0)