Debian Codegraph @

js/src/tests/test262/built-ins/RegExp/match-indices/indices-array-non-unicode-match.js

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