tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

functional-replace-global.js (1841B)


      1 // Copyright 2017 the V8 project authors. All rights reserved.
      2 // This code is governed by the BSD license found in the LICENSE file.
      3 
      4 /*---
      5 description: >
      6  Function argument to String.prototype.replace gets groups as the last argument
      7 esid: sec-regexp.prototype-@@replace
      8 features: [regexp-named-groups]
      9 info: |
     10  RegExp.prototype [ @@replace ] ( string, replaceValue )
     11    14. Repeat, for each result in results,
     12      j. Let namedCaptures be ? Get(result, "groups").
     13      k. If functionalReplace is true, then
     14        iv. If namedCaptures is not undefined,
     15          1. Append namedCaptures as the last element of replacerArgs.
     16 ---*/
     17 
     18 let source = "(?<fst>.)(?<snd>.)";
     19 let alternateSource = "(?<fst>.)|(?<snd>.)";
     20 
     21 for (let flags of ["g", "gu"]) {
     22  let i = 0;
     23  let re = new RegExp(source, flags);
     24  let result = "abcd".replace(re,
     25      (match, fst, snd, offset, str, groups) => {
     26    if (i == 0) {
     27      assert.sameValue("ab", match);
     28      assert.sameValue("a", groups.fst);
     29      assert.sameValue("b", groups.snd);
     30      assert.sameValue("a", fst);
     31      assert.sameValue("b", snd);
     32      assert.sameValue(0, offset);
     33      assert.sameValue("abcd", str);
     34    } else if (i == 1) {
     35      assert.sameValue("cd", match);
     36      assert.sameValue("c", groups.fst);
     37      assert.sameValue("d", groups.snd);
     38      assert.sameValue("c", fst);
     39      assert.sameValue("d", snd);
     40      assert.sameValue(2, offset);
     41      assert.sameValue("abcd", str);
     42    } else {
     43      assertUnreachable();
     44    }
     45    i++;
     46    return `${groups.snd}${groups.fst}`;
     47  });
     48  assert.sameValue("badc", result);
     49  assert.sameValue(i, 2);
     50 
     51  let re2 = new RegExp(alternateSource, flags);
     52  assert.sameValue("undefinedundefinedundefinedundefined",
     53      "abcd".replace(re2,
     54            (match, fst, snd, offset, str, groups) => groups.snd));
     55 }
     56 
     57 
     58 reportCompare(0, 0);