ParseHexOctet.js 1.4 KB

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