indices-array-unicode-match.js (4577B)
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 16. If _fullUnicode_ is *true*, set _e_ to ! GetStringIndex(_S_, _Input_, _e_). 15 ... 16 26. Let _match_ be the Match { [[StartIndex]]: _lastIndex_, [[EndIndex]]: _e_ }. 17 27. Let _indices_ be a new empty List. 18 ... 19 29. Add _match_ as the last element of _indices_. 20 ... 21 35. For each integer _i_ such that _i_ > 0 and _i_ <= _n_, in ascending order, do 22 ... 23 f. Else, 24 i. Let _captureStart_ be _captureI_'s _startIndex_. 25 ii. Let _captureEnd_ be _captureI_'s _endIndex_. 26 iii. If _fullUnicode_ is *true*, then 27 1. Set _captureStart_ to ! GetStringIndex(_S_, _Input_, _captureStart_). 28 1. Set _captureEnd_ to ! GetStringIndex(_S_, _Input_, _captureEnd_). 29 iv. Let _capture_ be the Match { [[StartIndex]]: _captureStart_, [[EndIndex]]: _captureEnd_ }. 30 v. Append _capture_ to _indices_. 31 ... 32 36. If _hasIndices_ is *true*, then 33 a. Let _indicesArray_ be MakeIndicesArray(_S_, _indices_, _groupNames_, _hasGroups_). 34 b. Perform ! CreateDataProperty(_A_, `"indices"`, _indicesArray_). 35 36 GetStringIndex ( S, Input, e ) 37 ... 38 4. Let _eUTF_ be the smallest index into _S_ that corresponds to the character at element _e_ of _Input_. If _e_ is greater than or equal to the number of elements in _Input_, then _eUTF_ is the number of code units in _S_. 39 5. Return _eUTF_. 40 ---*/ 41 42 assert.deepEqual([[1, 2], [1, 2]], "bab".match(/(a)/du).indices); 43 assert.deepEqual([[0, 3], [1, 2]], "bab".match(/.(a)./du).indices); 44 assert.deepEqual([[0, 3], [1, 2], [2, 3]], "bab".match(/.(a)(.)/du).indices); 45 assert.deepEqual([[0, 3], [1, 3]], "bab".match(/.(\w\w)/du).indices); 46 assert.deepEqual([[0, 3], [0, 3]], "bab".match(/(\w\w\w)/du).indices); 47 assert.deepEqual([[0, 3], [0, 2], [2, 3]], "bab".match(/(\w\w)(\w)/du).indices); 48 assert.deepEqual([[0, 2], [0, 2], undefined], "bab".match(/(\w\w)(\W)?/du).indices); 49 50 let groups = /(?<a>.)(?<b>.)(?<c>.)\k<c>\k<b>\k<a>/du.exec("abccba").indices.groups; 51 assert.compareArray([0, 1], groups.a); 52 assert.compareArray([1, 2], groups.b); 53 assert.compareArray([2, 3], groups.c); 54 verifyProperty(groups, "a", { 55 enumerable: true, 56 writable: true, 57 configurable: true 58 }); 59 verifyProperty(groups, "b", { 60 enumerable: true, 61 writable: true, 62 configurable: true 63 }); 64 verifyProperty(groups, "c", { 65 enumerable: true, 66 writable: true, 67 configurable: true 68 }); 69 70 // "𝐁" is U+1d401 MATHEMATICAL BOLD CAPITAL B 71 // - Also representable as the code point "\u{1d401}" 72 // - Also representable as the surrogate pair "\uD835\uDC01" 73 74 // Verify assumptions: 75 assert.sameValue("𝐁".length, 2, 'The length of "𝐁" is 2'); 76 assert.sameValue("\u{1d401}".length, 2, 'The length of "\\u{1d401}" is 2'); 77 assert.sameValue("\uD835\uDC01".length, 2, 'The length of "\\uD835\\uDC01" is 2'); 78 assert.sameValue(2, "𝐁".match(/./u)[0].length, 'The length of a single code point match against "𝐁" is 2 (with /du flag)'); 79 assert.sameValue(2, "\u{1d401}".match(/./u)[0].length, 'The length of a single code point match against "\\u{1d401}" is 2 (with /du flag)'); 80 assert.sameValue(2, "\uD835\uDC01".match(/./u)[0].length, 'The length of a single code point match against "\\ud835\\udc01" is 2 (with /du flag)'); 81 82 assert.compareArray([0, 2], "𝐁".match(/./du).indices[0], 'Indices for unicode match against "𝐁" (with /du flag)'); 83 assert.compareArray([0, 2], "\u{1d401}".match(/./du).indices[0], 'Indices for unicode match against \\u{1d401} (with /du flag)'); 84 assert.compareArray([0, 2], "\uD835\uDC01".match(/./du).indices[0], 'Indices for unicode match against \\ud835\\udc01 (with /du flag)'); 85 assert.compareArray([0, 2], "𝐁".match(/(?<a>.)/du).indices.groups.a, 'Indices for unicode match against 𝐁 in groups.a (with /du flag)'); 86 assert.compareArray([0, 2], "\u{1d401}".match(/(?<a>.)/du).indices.groups.a, 'Indices for unicode match against \\u{1d401} in groups.a (with /du flag)'); 87 assert.compareArray([0, 2], "\uD835\uDC01".match(/(?<a>.)/du).indices.groups.a, 'Indices for unicode match against \\ud835\\udc01 in groups.a (with /du flag)'); 88 89 reportCompare(0, 0);