ParseHexOctet.js 1.4 KB

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