ParseHexOctet.js 1.3 KB

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