cyrillic.js 8.18 KB
Newer Older
1 2 3 4 5 6 7 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
// Copyright 2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Test Unicode character ranges in regexps.


// Cyrillic.
var cyrillic = {
  FIRST: "\u0410",   // A
  first: "\u0430",   // a
  LAST: "\u042f",    // YA
  last: "\u044f",    // ya
  MIDDLE: "\u0427",  // CHE
  middle: "\u0447",   // che
  // Actually no characters are between the cases in Cyrillic.
  BetweenCases: false};

var SIGMA = "\u03a3";
var sigma = "\u03c3";
var alternative_sigma = "\u03c2";

// Greek.
var greek = {
  FIRST: "\u0391",     // ALPHA
  first: "\u03b1",     // alpha
  LAST: "\u03a9",      // OMEGA
  last: "\u03c9",      // omega
  MIDDLE: SIGMA,       // SIGMA
  middle: sigma,       // sigma
  // Epsilon acute is between ALPHA-OMEGA and alpha-omega, ie it
  // is between OMEGA and alpha.
  BetweenCases: "\u03ad"};


function Range(from, to, flags) {
  return new RegExp("[" + from + "-" + to + "]", flags);
}

63
// Test Cyrillic and Greek separately.
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
for (var lang = 0; lang < 2; lang++) {
  var chars = (lang == 0) ? cyrillic : greek;

  for (var i = 0; i < 2; i++) {
    var lc = (i == 0);  // Lower case.
    var first = lc ? chars.first : chars.FIRST;
    var middle = lc ? chars.middle : chars.MIDDLE;
    var last = lc ? chars.last : chars.LAST;
    var first_other_case = lc ? chars.FIRST : chars.first;
    var middle_other_case = lc ? chars.MIDDLE : chars.middle;
    var last_other_case = lc ? chars.LAST : chars.last;

    assertTrue(Range(first, last).test(first), 1);
    assertTrue(Range(first, last).test(middle), 2);
    assertTrue(Range(first, last).test(last), 3);

    assertFalse(Range(first, last).test(first_other_case), 4);
    assertFalse(Range(first, last).test(middle_other_case), 5);
    assertFalse(Range(first, last).test(last_other_case), 6);

    assertTrue(Range(first, last, "i").test(first), 7);
    assertTrue(Range(first, last, "i").test(middle), 8);
    assertTrue(Range(first, last, "i").test(last), 9);

    assertTrue(Range(first, last, "i").test(first_other_case), 10);
    assertTrue(Range(first, last, "i").test(middle_other_case), 11);
    assertTrue(Range(first, last, "i").test(last_other_case), 12);

    if (chars.BetweenCases) {
      assertFalse(Range(first, last).test(chars.BetweenCases), 13);
      assertFalse(Range(first, last, "i").test(chars.BetweenCases), 14);
    }
  }
  if (chars.BetweenCases) {
    assertTrue(Range(chars.FIRST, chars.last).test(chars.BetweenCases), 15);
    assertTrue(Range(chars.FIRST, chars.last, "i").test(chars.BetweenCases), 16);
  }
}

103
// Test range that covers both greek and cyrillic characters.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
for (key in greek) {
  assertTrue(Range(greek.FIRST, cyrillic.last).test(greek[key]), 17 + key);
  if (cyrillic[key]) {
    assertTrue(Range(greek.FIRST, cyrillic.last).test(cyrillic[key]), 18 + key);
  }
}

for (var i = 0; i < 2; i++) {
  var ignore_case = (i == 0);
  var flag = ignore_case ? "i" : "";
  assertTrue(Range(greek.first, cyrillic.LAST, flag).test(greek.first), 19);
  assertTrue(Range(greek.first, cyrillic.LAST, flag).test(greek.middle), 20);
  assertTrue(Range(greek.first, cyrillic.LAST, flag).test(greek.last), 21);

  assertTrue(Range(greek.first, cyrillic.LAST, flag).test(cyrillic.FIRST), 22);
  assertTrue(Range(greek.first, cyrillic.LAST, flag).test(cyrillic.MIDDLE), 23);
  assertTrue(Range(greek.first, cyrillic.LAST, flag).test(cyrillic.LAST), 24);

122 123
  // A range that covers the lower case greek letters and the upper case cyrillic
  // letters.
124 125 126 127 128 129 130 131 132 133
  assertEquals(ignore_case, Range(greek.first, cyrillic.LAST, flag).test(greek.FIRST), 25);
  assertEquals(ignore_case, Range(greek.first, cyrillic.LAST, flag).test(greek.MIDDLE), 26);
  assertEquals(ignore_case, Range(greek.first, cyrillic.LAST, flag).test(greek.LAST), 27);

  assertEquals(ignore_case, Range(greek.first, cyrillic.LAST, flag).test(cyrillic.first), 28);
  assertEquals(ignore_case, Range(greek.first, cyrillic.LAST, flag).test(cyrillic.middle), 29);
  assertEquals(ignore_case, Range(greek.first, cyrillic.LAST, flag).test(cyrillic.last), 30);
}


134 135 136 137
// Sigma is special because there are two lower case versions of the same upper
// case character.  JS requires that case independece means that you should
// convert everything to upper case, so the two sigma variants are equal to each
// other in a case independt comparison.
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
for (var i = 0; i < 2; i++) {
  var simple = (i != 0);
  var name = simple ? "" : "[]";
  var regex = simple ? SIGMA : "[" + SIGMA + "]";

  assertFalse(new RegExp(regex).test(sigma), 31 + name);
  assertFalse(new RegExp(regex).test(alternative_sigma), 32 + name);
  assertTrue(new RegExp(regex).test(SIGMA), 33 + name);

  assertTrue(new RegExp(regex, "i").test(sigma), 34 + name);
  // JSC and Tracemonkey fail this one.
  assertTrue(new RegExp(regex, "i").test(alternative_sigma), 35 + name);
  assertTrue(new RegExp(regex, "i").test(SIGMA), 36 + name);

  regex = simple ? sigma : "[" + sigma + "]";

  assertTrue(new RegExp(regex).test(sigma), 41 + name);
  assertFalse(new RegExp(regex).test(alternative_sigma), 42 + name);
  assertFalse(new RegExp(regex).test(SIGMA), 43 + name);

  assertTrue(new RegExp(regex, "i").test(sigma), 44 + name);
  // JSC and Tracemonkey fail this one.
  assertTrue(new RegExp(regex, "i").test(alternative_sigma), 45 + name);
  assertTrue(new RegExp(regex, "i").test(SIGMA), 46 + name);

  regex = simple ? alternative_sigma : "[" + alternative_sigma + "]";

  assertFalse(new RegExp(regex).test(sigma), 51 + name);
  assertTrue(new RegExp(regex).test(alternative_sigma), 52 + name);
  assertFalse(new RegExp(regex).test(SIGMA), 53 + name);

  // JSC and Tracemonkey fail this one.
  assertTrue(new RegExp(regex, "i").test(sigma), 54 + name);
  assertTrue(new RegExp(regex, "i").test(alternative_sigma), 55 + name);
  // JSC and Tracemonkey fail this one.
  assertTrue(new RegExp(regex, "i").test(SIGMA), 56 + name);
}

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

for (var add_non_ascii_character_to_subject = 0;
     add_non_ascii_character_to_subject < 2;
     add_non_ascii_character_to_subject++) {
  var suffix = add_non_ascii_character_to_subject ? "\ufffe" : "";
  // A range that covers both ASCII and non-ASCII.
  for (var i = 0; i < 2; i++) {
    var full = (i != 0);
    var mixed = full ? "[a-\uffff]" : "[a-" + cyrillic.LAST + "]";
    var f = full ? "f" : "c";
    for (var j = 0; j < 2; j++) {
      var ignore_case = (j == 0);
      var flag = ignore_case ? "i" : "";
      var re = new RegExp(mixed, flag);
      assertEquals(ignore_case || (full && add_non_ascii_character_to_subject),
                   re.test("A" + suffix),
                   58 + flag + f);
      assertTrue(re.test("a" + suffix), 59 + flag + f);
      assertTrue(re.test("~" + suffix), 60 + flag + f);
      assertTrue(re.test(cyrillic.MIDDLE), 61 + flag + f);
      assertEquals(ignore_case || full, re.test(cyrillic.middle), 62 + flag + f);
    }
  }
}