Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 1 | // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Murdoch | 61f157c | 2016-09-16 13:49:30 +0100 | [diff] [blame] | 5 | // Flags: --harmony-regexp-lookbehind |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 6 | |
| 7 | // Back reference does not end in the middle of a surrogate pair. |
| 8 | function replace(string) { |
| 9 | return string.replace(/L/g, "\ud800") |
| 10 | .replace(/l/g, "\ud801") |
| 11 | .replace(/T/g, "\udc00") |
| 12 | .replace(/\./g, "[^]"); |
| 13 | } |
| 14 | |
| 15 | function test(expectation, regexp_source, subject) { |
| 16 | if (expectation !== null) expectation = expectation.map(replace); |
| 17 | subject = replace(subject); |
| 18 | regexp_source = replace(regexp_source); |
| 19 | assertEquals(expectation, new RegExp(regexp_source, "u").exec(subject)); |
| 20 | } |
| 21 | |
| 22 | // Back reference does not end in the middle of a surrogate pair. |
| 23 | test(null, "(L)\\1", "LLT"); |
| 24 | test(["LLTLl", "L", "l"], "(L).*\\1(.)", "LLTLl"); |
| 25 | test(null, "(aL).*\\1", "aLaLT"); |
| 26 | test(["aLaLTaLl", "aL", "l"], "(aL).*\\1(.)", "aLaLTaLl"); |
| 27 | |
| 28 | var s = "TabcLxLTabcLxTabcLTyTabcLz"; |
| 29 | test([s, "TabcL", "z"], "([^x]+).*\\1(.)", s); |
| 30 | |
| 31 | // Back reference does not start in the middle of a surrogate pair. |
| 32 | test(["TLTabTc", "T", "c"], "(T).*\\1(.)", "TLTabTc"); |
| 33 | |
| 34 | // Lookbehinds. |
| 35 | test(null, "(?<=\\1(T)x)", "LTTx"); |
| 36 | test(["", "b", "T"], "(?<=(.)\\2.*(T)x)", "bTaLTTx"); |
| 37 | test(null, "(?<=\\1.*(L)x)", "LTLx"); |
| 38 | test(["", "b", "L"], "(?<=(.)\\2.*(L)x)", "bLaLTLx"); |
| 39 | |
| 40 | |
| 41 | test(null, "([^x]+)x*\\1", "LxLT"); |
| 42 | test(null, "([^x]+)x*\\1", "TxLT"); |
| 43 | test(null, "([^x]+)x*\\1", "LTxL"); |
| 44 | test(null, "([^x]+)x*\\1", "LTxT"); |
| 45 | test(null, "([^x]+)x*\\1", "xLxLT"); |
| 46 | test(null, "([^x]+)x*\\1", "xTxLT"); |
| 47 | test(null, "([^x]+)x*\\1", "xLTxL"); |
| 48 | test(null, "([^x]+)x*\\1", "xLTxT"); |
| 49 | test(null, "([^x]+)x*\\1", "xxxLxxLTxx"); |
| 50 | test(null, "([^x]+)x*\\1", "xxxTxxLTxx"); |
| 51 | test(null, "([^x]+)x*\\1", "xxxLTxxLxx"); |
| 52 | test(null, "([^x]+)x*\\1", "xxxLTxxTxx"); |
| 53 | test(["LTTxxLTT", "LTT"], "([^x]+)x*\\1", "xxxLTTxxLTTxx"); |