indices-array-non-unicode-match.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 Ron Buckton. All rights reserved.
  2. // This code is governed by the BSD license found in the LICENSE file.
  3. /*---
  4. description: Basic matching cases with non-unicode matches.
  5. includes: [compareArray.js, propertyHelper.js, deepEqual.js]
  6. esid: sec-regexpbuiltinexec
  7. features: [regexp-match-indices]
  8. info: |
  9. Runtime Semantics: RegExpBuiltinExec ( R, S )
  10. ...
  11. 4. Let _lastIndex_ be ? ToLength(? Get(_R_, `"lastIndex")).
  12. ...
  13. 25. Let _indices_ be a new empty List.
  14. 26. Let _match_ be the Match { [[StartIndex]]: _lastIndex_, [[EndIndex]]: _e_ }.
  15. 27. Add _match_ as the last element of _indices_.
  16. ...
  17. 33. For each integer _i_ such that _i_ > 0 and _i_ <= _n_, in ascending order, do
  18. ...
  19. f. Else,
  20. i. Let _captureStart_ be _captureI_'s _startIndex_.
  21. ii. Let _captureEnd_ be _captureI_'s _endIndex_.
  22. ...
  23. iv. Let _capture_ be the Match { [[StartIndex]]: _captureStart_, [[EndIndex]]: _captureEnd_ }.
  24. v. Append _capture_ to _indices_.
  25. ...
  26. 34. Let _indicesArray_ be MakeIndicesArray( _S_, _indices_, _groupNames_).
  27. ---*/
  28. assert.deepEqual([[1, 2], [1, 2]], "bab".match(/(a)/).indices);
  29. assert.deepEqual([[0, 3], [1, 2]], "bab".match(/.(a)./).indices);
  30. assert.deepEqual([[0, 3], [1, 2], [2, 3]], "bab".match(/.(a)(.)/).indices);
  31. assert.deepEqual([[0, 3], [1, 3]], "bab".match(/.(\w\w)/).indices);
  32. assert.deepEqual([[0, 3], [0, 3]], "bab".match(/(\w\w\w)/).indices);
  33. assert.deepEqual([[0, 3], [0, 2], [2, 3]], "bab".match(/(\w\w)(\w)/).indices);
  34. assert.deepEqual([[0, 2], [0, 2], undefined], "bab".match(/(\w\w)(\W)?/).indices);
  35. let groups = /(?<a>.)(?<b>.)(?<c>.)\k<c>\k<b>\k<a>/.exec("abccba").indices.groups;
  36. assert.compareArray([0, 1], groups.a);
  37. assert.compareArray([1, 2], groups.b);
  38. assert.compareArray([2, 3], groups.c);
  39. verifyProperty(groups, "a", {
  40. enumerable: true,
  41. writable: true,
  42. configurable: true
  43. });
  44. verifyProperty(groups, "b", {
  45. enumerable: true,
  46. writable: true,
  47. configurable: true
  48. });
  49. verifyProperty(groups, "c", {
  50. enumerable: true,
  51. writable: true,
  52. configurable: true
  53. });
  54. // "𝐁" is U+1d401 MATHEMATICAL BOLD CAPITAL B
  55. // - Also representable as the code point "\u{1d401}"
  56. // - Also representable as the surrogate pair "\uD835\uDC01"
  57. // Verify assumptions:
  58. assert.sameValue("𝐁".length, 2, 'The length of "𝐁" is 2');
  59. assert.sameValue("\u{1d401}".length, 2, 'The length of "\\u{1d401}" is 2');
  60. assert.sameValue("\uD835\uDC01".length, 2, 'The length of "\\uD835\\uDC01" is 2');
  61. assert.sameValue("𝐁".match(/./)[0].length, 1, 'The length of a single code unit match against "𝐁" is 1 (without /u flag)');
  62. assert.sameValue("\u{1d401}".match(/./)[0].length, 1, 'The length of a single code unit match against "\\u{1d401}" is 1 (without /u flag)');
  63. assert.sameValue("\uD835\uDC01".match(/./)[0].length, 1, 'The length of a single code unit match against "\\ud835\\udc01" is 1 (without /u flag)');
  64. assert.compareArray([0, 1], "𝐁".match(/./).indices[0], 'Indices for non-unicode match against "𝐁" (without /u flag)');
  65. assert.compareArray([0, 1], "\u{1d401}".match(/./).indices[0], 'Indices for non-unicode match against "\\u{1d401}" (without /u flag)');
  66. assert.compareArray([0, 1], "\uD835\uDC01".match(/./).indices[0], 'Indices for non-unicode match against "\\ud835\\udc01" (without /u flag)');
  67. assert.compareArray([0, 1], "𝐁".match(/(?<a>.)/).indices.groups.a, 'Indices for non-unicode match against "𝐁" in groups.a (without /u flag)');
  68. assert.compareArray([0, 1], "\u{1d401}".match(/(?<a>.)/).indices.groups.a, 'Indices for non-unicode match against "\\u{1d401}" in groups.a (without /u flag)');
  69. assert.compareArray([0, 1], "\uD835\uDC01".match(/(?<a>.)/).indices.groups.a, 'Indices for non-unicode match against "\\ud835\\udc01" in groups.a (without /u flag)');