1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $Number = GetIntrinsic('%Number%');
|
---|
6 | var $SyntaxError = require('es-errors/syntax');
|
---|
7 | var $TypeError = require('es-errors/type');
|
---|
8 |
|
---|
9 | var IsIntegralNumber = require('./IsIntegralNumber');
|
---|
10 | var substring = require('./substring');
|
---|
11 |
|
---|
12 | var isNaN = require('../helpers/isNaN');
|
---|
13 |
|
---|
14 | // https://262.ecma-international.org/14.0/#sec-parsehexoctet
|
---|
15 |
|
---|
16 | module.exports = function ParseHexOctet(string, position) {
|
---|
17 | if (typeof string !== 'string') {
|
---|
18 | throw new $TypeError('Assertion failed: `string` must be a String');
|
---|
19 | }
|
---|
20 | if (!IsIntegralNumber(position) || position < 0) {
|
---|
21 | throw new $TypeError('Assertion failed: `position` must be a nonnegative integer');
|
---|
22 | }
|
---|
23 |
|
---|
24 | var len = string.length; // step 1
|
---|
25 | if ((position + 2) > len) { // step 2
|
---|
26 | var error = new $SyntaxError('requested a position on a string that does not contain 2 characters at that position'); // step 2.a
|
---|
27 | return [error]; // step 2.b
|
---|
28 | }
|
---|
29 | var hexDigits = substring(string, position, position + 2); // step 3
|
---|
30 |
|
---|
31 | var n = $Number('0x' + hexDigits);
|
---|
32 | if (isNaN(n)) {
|
---|
33 | return [new $SyntaxError('Invalid hexadecimal characters')];
|
---|
34 | }
|
---|
35 | return n;
|
---|
36 |
|
---|
37 | /*
|
---|
38 | 4. Let _parseResult_ be ParseText(StringToCodePoints(_hexDigits_), |HexDigits[~Sep]|).
|
---|
39 | 5. If _parseResult_ is not a Parse Node, return _parseResult_.
|
---|
40 | 6. Let _n_ be the unsigned 8-bit value corresponding with the MV of _parseResult_.
|
---|
41 | 7. Return _n_.
|
---|
42 | */
|
---|
43 | };
|
---|