Skip to content

Commit

Permalink
remove outputOffset
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot committed Jan 7, 2024
1 parent 8dc1667 commit 3cb9470
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 52 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ assert.deepStrictEqual([...target], [102, 111, 111, 98, 97, 114, 0, 0]);
assert.deepStrictEqual({ read, written }, { read: 8, written: 6 });
```

This method takes an optional final options bag with the same options as above, plus an `outputOffset` option which allows specifying a position in the target array to write to without needing to create a subarray.
This method takes an optional final options bag with the same options as above.

As with `encodeInto`, there is not explicit support for writing to specified offset of the target, but you can accomplish that by creating a subarray.

`Uint8Array.fromHexInto` is the same except for hex.

Expand Down
2 changes: 1 addition & 1 deletion playground/index-raw.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ <h3>Options</h3>
<h3>Writing to an existing Uint8Array</h3>
<p>The <code>Uint8Array.fromBase64Into</code> method allows writing to an existing Uint8Array. Like the <a href="https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encodeInto">TextEncoder <code>encodeInto</code> method</a>, it returns a <code>{ read, written }</code> pair.</p>

<p>This method takes an optional final options bag with the same options as above, plus an <code>outputOffset</code> option which allows specifying a position in the target array to write to without needing to create a subarray.</p>
<p>This method takes an optional final options bag with the same options as above.</p>

<pre class="language-js"><code class="language-js">
let target = new Uint8Array(7);
Expand Down
33 changes: 6 additions & 27 deletions playground/polyfill-core.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,15 @@ export function base64ToUint8Array(string, options, into) {
if (!['loose', 'strict', 'stop-before-partial'].includes(lastChunkHandling)) {
throw new TypeError('expected lastChunkHandling to be either "loose", "strict", or "stop-before-partial"');
}
let outputOffset = 0;
if (into) {
outputOffset = opts.outputOffset;
if (typeof outputOffset === 'undefined') {
outputOffset = 0;
} else if (typeof outputOffset !== 'number' || Math.round(outputOffset) !== outputOffset || outputOffset < 0 || outputOffset >= into.length) {
// TODO: do we want to accept negative wrap-around offsets? probably?
throw new RangeError('outputOffset must be an integer between 0 and into.length');
}
}

let maxLength = into ? (into.length - outputOffset) : 2 ** 53 - 1;
let maxLength = into ? into.length : 2 ** 53 - 1;

let { bytes, read } = fromBase64(string, alphabet, lastChunkHandling, maxLength);

bytes = new Uint8Array(bytes);
if (into) {
assert(bytes.length <= into.length - outputOffset);
into.set(bytes, outputOffset);
assert(bytes.length <= into.length);
into.set(bytes);
}

return { read, bytes };
Expand All @@ -257,18 +247,7 @@ export function hexToUint8Array(string, options, into) {
throw new SyntaxError('string should only contain hex characters');
}

let outputOffset = 0;
if (into) {
let opts = getOptions(options);
outputOffset = opts.outputOffset;
if (typeof outputOffset === 'undefined') {
outputOffset = 0;
} else if (typeof outputOffset !== 'number' || Math.round(number) !== number || number < 0 || number >= into.length) {
// TODO: do we want to accept negative wrap-around offsets? probably?
throw new RangeError('outputOffset must be an integer between 0 and into.length');
}
}
let maxLength = into ? (into.length - outputOffset) : 2 ** 53 - 1;
let maxLength = into ? into.length : 2 ** 53 - 1;

// TODO should hex allow whitespace?
// TODO should hex support lastChunkHandling? (only 'strict' or 'stop-before-partial')
Expand All @@ -286,8 +265,8 @@ export function hexToUint8Array(string, options, into) {

bytes = new Uint8Array(bytes);
if (into) {
assert(bytes.length <= into.length - outputOffset);
into.set(bytes, outputOffset);
assert(bytes.length <= into.length);
into.set(bytes);
}

return { read: index, bytes };
Expand Down
20 changes: 4 additions & 16 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,15 @@ <h1>Uint8Array.fromBase64Into ( _string_, _into_ [ , _options_ ] )</h1>
1. Let _lastChunkHandling_ be ? Get(_opts_, *"lastChunkHandling"*).
1. If _lastChunkHandling_ is *undefined*, set _lastChunkHandling_ to *"loose"*.
1. If _lastChunkHandling_ is not one of *"loose"*, *"strict"*, or *"stop-before-partial"*, throw a *TypeError* exception.
1. Let _outputOffset_ be ? Get(_opts_, *"outputOffset"*).
1. If _outputOffset_ is *undefined*, set _outputOffset_ to *+0*<sub>𝔽</sub>.
1. If _outputOffset_ is not an integral Number, throw a *RangeError* exception.
1. Set _outputOffset_ to ℝ(_outputOffset_).
1. Let _taRecord_ be MakeTypedArrayWithBufferWitnessRecord(_into_, ~seq-cst~).
1. Let _byteLength_ be TypedArrayByteLength(_taRecord_).
1. If _outputOffset_ < 0 or _outputOffset__byteLength_, then
1. Throw a *RangeError* exception.
1. Let _maxLength_ be _byteLength_ - _outputOffset_.
1. Let _maxLength_ be _byteLength_.
1. Let _result_ be ? FromBase64(_string_, _alphabet_, _lastChunkHandling_, _maxLength_).
1. Let _bytes_ be _result_.[[Bytes]].
1. Let _written_ be the length of _bytes_.
1. NOTE: FromBase64 does not invoke any user code, so the ArrayBuffer backing _into_ cannot have been detached or shrunk.
1. Assert: _written__byteLength_.
1. Let _offset_ be _into_.[[ByteOffset]] + _outputOffset_.
1. Let _offset_ be _into_.[[ByteOffset]].
1. Let _index_ be 0.
1. Repeat, while _index_ < _written_,
1. Let _byte_ be _bytes_[i].
Expand Down Expand Up @@ -127,21 +121,15 @@ <h1>Uint8Array.fromHexInto ( _string_, _into_ [ , _options_ ] )</h1>
1. Perform ? RequireInternalSlot(_into_, [[TypedArrayName]]).
1. If _into_.[[TypedArrayName]] is not *"Uint8Array"*, throw a *TypeError* exception.
1. Let _opts_ be ? GetOptionsObject(_options_).
1. Let _outputOffset_ be ? Get(_opts_, *"outputOffset"*).
1. If _outputOffset_ is *undefined*, set _outputOffset_ to *+0*<sub>𝔽</sub>.
1. If _outputOffset_ is not an integral Number, throw a *RangeError* exception.
1. Set _outputOffset_ to ℝ(_outputOffset_).
1. Let _taRecord_ be MakeTypedArrayWithBufferWitnessRecord(_into_, ~seq-cst~).
1. Let _byteLength_ be TypedArrayByteLength(_taRecord_).
1. If _outputOffset_ < 0 or _outputOffset__byteLength_, then
1. Throw a *RangeError* exception.
1. Let _maxLength_ be _byteLength_ - _outputOffset_.
1. Let _maxLength_ be _byteLength_.
1. Let _result_ be ? FromHex(_string_, _maxLength_).
1. Let _bytes_ be _result_.[[Bytes]].
1. Let _written_ be the length of _bytes_.
1. NOTE: FromHex does not invoke any user code, so the ArrayBuffer backing _into_ cannot have been detached or shrunk.
1. Assert: _written__byteLength_.
1. Let _offset_ be _into_.[[ByteOffset]] + _outputOffset_.
1. Let _offset_ be _into_.[[ByteOffset]].
1. Let _index_ be 0.
1. Repeat, while _index_ < _written_,
1. Let _byte_ be _bytes_[i].
Expand Down
7 changes: 0 additions & 7 deletions test-polyfill.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,6 @@ test('writing to an existing buffer', async t => {
assert.deepStrictEqual([...output], [...foobarOutput.slice(0, 5), 0, 0, 0]);
assert.deepStrictEqual({ read, written }, { read: 17, written: 5 });
});

await t.test('buffer exact, padded, outputOffset', () => {
let output = new Uint8Array(6);
let { read, written } = Uint8Array.fromBase64Into(foobaInput + '=', output, { outputOffset: 1 });
assert.deepStrictEqual([...output], [0, ...foobarOutput.slice(0, 5)]);
assert.deepStrictEqual({ read, written }, { read: 8, written: 5 });
});
});

test('stop-before-partial', async t => {
Expand Down

0 comments on commit 3cb9470

Please sign in to comment.