main
Last change
on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1001 bytes
|
Line | |
---|
1 | 'use strict';
|
---|
2 | var globalThis = require('../internals/global-this');
|
---|
3 | var uncurryThis = require('../internals/function-uncurry-this');
|
---|
4 |
|
---|
5 | var Uint8Array = globalThis.Uint8Array;
|
---|
6 | var SyntaxError = globalThis.SyntaxError;
|
---|
7 | var parseInt = globalThis.parseInt;
|
---|
8 | var min = Math.min;
|
---|
9 | var NOT_HEX = /[^\da-f]/i;
|
---|
10 | var exec = uncurryThis(NOT_HEX.exec);
|
---|
11 | var stringSlice = uncurryThis(''.slice);
|
---|
12 |
|
---|
13 | module.exports = function (string, into) {
|
---|
14 | var stringLength = string.length;
|
---|
15 | if (stringLength % 2 !== 0) throw new SyntaxError('String should be an even number of characters');
|
---|
16 | var maxLength = into ? min(into.length, stringLength / 2) : stringLength / 2;
|
---|
17 | var bytes = into || new Uint8Array(maxLength);
|
---|
18 | var read = 0;
|
---|
19 | var written = 0;
|
---|
20 | while (written < maxLength) {
|
---|
21 | var hexits = stringSlice(string, read, read += 2);
|
---|
22 | if (exec(NOT_HEX, hexits)) throw new SyntaxError('String should only contain hex characters');
|
---|
23 | bytes[written++] = parseInt(hexits, 16);
|
---|
24 | }
|
---|
25 | return { bytes: bytes, read: read };
|
---|
26 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.