unicode-escapes-in-regexps.js 6.29 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// ES6 extends the \uxxxx escape and also allows \u{xxxxx}.

7
// Flags: --harmony-unicode-regexps --harmony-regexps
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

function testRegexpHelper(r) {
  assertTrue(r.test("foo"));
  assertTrue(r.test("boo"));
  assertFalse(r.test("moo"));
}


(function TestUnicodeEscapes() {
  testRegexpHelper(/(\u0066|\u0062)oo/);
  testRegexpHelper(/(\u0066|\u0062)oo/u);
  testRegexpHelper(/(\u{0066}|\u{0062})oo/u);
  testRegexpHelper(/(\u{66}|\u{000062})oo/u);

  // Note that we need \\ inside a string, otherwise it's interpreted as a
  // unicode escape inside a string.
  testRegexpHelper(new RegExp("(\\u0066|\\u0062)oo"));
  testRegexpHelper(new RegExp("(\\u0066|\\u0062)oo", "u"));
  testRegexpHelper(new RegExp("(\\u{0066}|\\u{0062})oo", "u"));
  testRegexpHelper(new RegExp("(\\u{66}|\\u{000062})oo", "u"));

  // Though, unicode escapes via strings should work too.
  testRegexpHelper(new RegExp("(\u0066|\u0062)oo"));
  testRegexpHelper(new RegExp("(\u0066|\u0062)oo", "u"));
  testRegexpHelper(new RegExp("(\u{0066}|\u{0062})oo", "u"));
  testRegexpHelper(new RegExp("(\u{66}|\u{000062})oo", "u"));
})();


(function TestUnicodeEscapesInCharacterClasses() {
  testRegexpHelper(/[\u0062-\u0066]oo/);
  testRegexpHelper(/[\u0062-\u0066]oo/u);
  testRegexpHelper(/[\u{0062}-\u{0066}]oo/u);
  testRegexpHelper(/[\u{62}-\u{00000066}]oo/u);

  // Note that we need \\ inside a string, otherwise it's interpreted as a
  // unicode escape inside a string.
  testRegexpHelper(new RegExp("[\\u0062-\\u0066]oo"));
  testRegexpHelper(new RegExp("[\\u0062-\\u0066]oo", "u"));
  testRegexpHelper(new RegExp("[\\u{0062}-\\u{0066}]oo", "u"));
  testRegexpHelper(new RegExp("[\\u{62}-\\u{00000066}]oo", "u"));

  // Though, unicode escapes via strings should work too.
  testRegexpHelper(new RegExp("[\u0062-\u0066]oo"));
  testRegexpHelper(new RegExp("[\u0062-\u0066]oo", "u"));
  testRegexpHelper(new RegExp("[\u{0062}-\u{0066}]oo", "u"));
  testRegexpHelper(new RegExp("[\u{62}-\u{00000066}]oo", "u"));
})();


(function TestBraceEscapesWithoutUnicodeFlag() {
  // \u followed by illegal escape will be parsed as u. {x} will be the
  // character count.
  function helper1(r) {
    assertFalse(r.test("fbar"));
    assertFalse(r.test("fubar"));
    assertTrue(r.test("fuubar"));
    assertFalse(r.test("fuuubar"));
  }
  helper1(/f\u{2}bar/);
  helper1(new RegExp("f\\u{2}bar"));

  function helper2(r) {
    assertFalse(r.test("fbar"));
    assertTrue(r.test("fubar"));
    assertTrue(r.test("fuubar"));
    assertFalse(r.test("fuuubar"));
  }

  helper2(/f\u{1,2}bar/);
  helper2(new RegExp("f\\u{1,2}bar"));

  function helper3(r) {
    assertTrue(r.test("u"));
    assertTrue(r.test("{"));
    assertTrue(r.test("2"));
    assertTrue(r.test("}"));
    assertFalse(r.test("q"));
    assertFalse(r.test("("));
    assertFalse(r.test(")"));
  }
  helper3(/[\u{2}]/);
  helper3(new RegExp("[\\u{2}]"));
})();


(function TestInvalidEscapes() {
  // Without the u flag, invalid unicode escapes and other invalid escapes are
  // treated as identity escapes.
  function helper1(r) {
    assertTrue(r.test("firstuxz89second"));
  }
  helper1(/first\u\x\z\8\9second/);
  helper1(new RegExp("first\\u\\x\\z\\8\\9second"));

  function helper2(r) {
    assertTrue(r.test("u"));
    assertTrue(r.test("x"));
    assertTrue(r.test("z"));
    assertTrue(r.test("8"));
    assertTrue(r.test("9"));
    assertFalse(r.test("q"));
    assertFalse(r.test("7"));
  }
  helper2(/[\u\x\z\8\9]/);
  helper2(new RegExp("[\\u\\x\\z\\8\\9]"));

  // However, with the u flag, these are treated as invalid escapes.
  assertThrows("/\\u/u", SyntaxError);
  assertThrows("/\\u12/u", SyntaxError);
  assertThrows("/\\ufoo/u", SyntaxError);
  assertThrows("/\\x/u", SyntaxError);
  assertThrows("/\\xfoo/u", SyntaxError);
  assertThrows("/\\z/u", SyntaxError);
  assertThrows("/\\8/u", SyntaxError);
  assertThrows("/\\9/u", SyntaxError);

  assertThrows("new RegExp('\\\\u', 'u')", SyntaxError);
  assertThrows("new RegExp('\\\\u12', 'u')", SyntaxError);
  assertThrows("new RegExp('\\\\ufoo', 'u')", SyntaxError);
  assertThrows("new RegExp('\\\\x', 'u')", SyntaxError);
  assertThrows("new RegExp('\\\\xfoo', 'u')", SyntaxError);
  assertThrows("new RegExp('\\\\z', 'u')", SyntaxError);
  assertThrows("new RegExp('\\\\8', 'u')", SyntaxError);
  assertThrows("new RegExp('\\\\9', 'u')", SyntaxError);
})();


(function TestTooBigHexEscape() {
  // The hex number inside \u{} has a maximum value.
  /\u{10ffff}/u
  new RegExp("\\u{10ffff}", "u")
  assertThrows("/\\u{110000}/u", SyntaxError);
  assertThrows("new RegExp('\\\\u{110000}', 'u')", SyntaxError);

  // Without the u flag, they're of course fine ({x} is the count).
  /\u{110000}/
  new RegExp("\\u{110000}")
})();


(function TestSyntaxEscapes() {
  // Syntax escapes work the same with or without the u flag.
  function helper(r) {
    assertTrue(r.test("foo[bar"));
    assertFalse(r.test("foo]bar"));
  }
  helper(/foo\[bar/);
  helper(new RegExp("foo\\[bar"));
  helper(/foo\[bar/u);
  helper(new RegExp("foo\\[bar", "u"));
})();


(function TestUnicodeSurrogates() {
  // U+10E6D corresponds to the surrogate pair [U+D803, U+DE6D].
  function helper(r) {
    assertTrue(r.test("foo\u{10e6d}bar"));
  }
  helper(/foo\ud803\ude6dbar/u);
  helper(new RegExp("foo\\ud803\\ude6dbar", "u"));
})();


(function AllFlags() {
  // Test that we can pass all possible regexp flags and they work properly.
  function helper1(r) {
    assertTrue(r.global);
    assertTrue(r.ignoreCase);
    assertTrue(r.multiline);
    assertTrue(r.sticky);
    assertTrue(r.unicode);
  }

  helper1(/foo/gimyu);
  helper1(new RegExp("foo", "gimyu"));

  function helper2(r) {
    assertFalse(r.global);
    assertFalse(r.ignoreCase);
    assertFalse(r.multiline);
    assertFalse(r.sticky);
    assertFalse(r.unicode);
  }

  helper2(/foo/);
  helper2(new RegExp("foo"));
})();


(function DuplicatedFlags() {
  // Test that duplicating the u flag is not allowed.
  assertThrows("/foo/ugu");
  assertThrows("new RegExp('foo', 'ugu')");
})();


(function ToString() {
  // Test that the u flag is included in the string representation of regexps.
  function helper(r) {
    assertEquals(r.toString(), "/foo/u");
  }
  helper(/foo/u);
  helper(new RegExp("foo", "u"));
})();