1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_3/RegExp/octal-002.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,184 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * 1.11 + * Date: 31 July 2002 1.12 + * SUMMARY: Testing regexps containing octal escape sequences 1.13 + * This is an elaboration of mozilla/js/tests/ecma_2/RegExp/octal-003.js 1.14 + * 1.15 + * See http://bugzilla.mozilla.org/show_bug.cgi?id=141078 1.16 + * for a reference on octal escape sequences in regexps. 1.17 + * 1.18 + * NOTE: 1.19 + * We will use the identities '\011' === '\u0009' === '\x09' === '\t' 1.20 + * 1.21 + * The first is an octal escape sequence (\(0-3)OO; O an octal digit). 1.22 + * See ECMA-262 Edition 2, Section 7.7.4 "String Literals". These were 1.23 + * dropped in Edition 3 but we support them for backward compatibility. 1.24 + * 1.25 + * The second is a Unicode escape sequence (\uHHHH; H a hex digit). 1.26 + * Since octal 11 = hex 9, the two escapes define the same character. 1.27 + * 1.28 + * The third is a hex escape sequence (\xHH; H a hex digit). 1.29 + * Since hex 09 = hex 0009, this defines the same character. 1.30 + * 1.31 + * The fourth is the familiar escape sequence for a horizontal tab, 1.32 + * defined in the ECMA spec as having Unicode value \u0009. 1.33 + */ 1.34 +//----------------------------------------------------------------------------- 1.35 +var i = 0; 1.36 +var BUGNUMBER = 141078; 1.37 +var summary = 'Testing regexps containing octal escape sequences'; 1.38 +var status = ''; 1.39 +var statusmessages = new Array(); 1.40 +var pattern = ''; 1.41 +var patterns = new Array(); 1.42 +var string = ''; 1.43 +var strings = new Array(); 1.44 +var actualmatch = ''; 1.45 +var actualmatches = new Array(); 1.46 +var expectedmatch = ''; 1.47 +var expectedmatches = new Array(); 1.48 + 1.49 + 1.50 +/* 1.51 + * Test a string containing the null character '\0' followed by the string '11' 1.52 + * 1.53 + * 'a' + String.fromCharCode(0) + '11'; 1.54 + * 1.55 + * Note we can't simply write 'a\011', because '\011' would be interpreted 1.56 + * as the octal escape sequence for the tab character (see above). 1.57 + * 1.58 + * We should get no match from the regexp /.\011/, because it should be 1.59 + * looking for the octal escape sequence \011, i.e. the tab character - 1.60 + * 1.61 + */ 1.62 +status = inSection(1); 1.63 +pattern = /.\011/; 1.64 +string = 'a' + String.fromCharCode(0) + '11'; 1.65 +actualmatch = string.match(pattern); 1.66 +expectedmatch = null; 1.67 +addThis(); 1.68 + 1.69 + 1.70 +/* 1.71 + * Try same thing with 'xx' in place of '11'. 1.72 + * 1.73 + * Should get a match now, because the octal escape sequence in the regexp 1.74 + * has been reduced from \011 to \0, and '\0' is present in the string - 1.75 + */ 1.76 +status = inSection(2); 1.77 +pattern = /.\0xx/; 1.78 +string = 'a' + String.fromCharCode(0) + 'xx'; 1.79 +actualmatch = string.match(pattern); 1.80 +expectedmatch = Array(string); 1.81 +addThis(); 1.82 + 1.83 + 1.84 +/* 1.85 + * Same thing; don't use |String.fromCharCode(0)| this time. 1.86 + * There is no ambiguity in '\0xx': it is the null character 1.87 + * followed by two x's, no other interpretation is possible. 1.88 + */ 1.89 +status = inSection(3); 1.90 +pattern = /.\0xx/; 1.91 +string = 'a\0xx'; 1.92 +actualmatch = string.match(pattern); 1.93 +expectedmatch = Array(string); 1.94 +addThis(); 1.95 + 1.96 + 1.97 +/* 1.98 + * This one should produce a match. The two-character string 1.99 + * 'a' + '\011' is duplicated in the pattern and test string: 1.100 + */ 1.101 +status = inSection(4); 1.102 +pattern = /.\011/; 1.103 +string = 'a\011'; 1.104 +actualmatch = string.match(pattern); 1.105 +expectedmatch = Array(string); 1.106 +addThis(); 1.107 + 1.108 + 1.109 +/* 1.110 + * Same as above, only now, for the second character of the string, 1.111 + * use the Unicode escape '\u0009' instead of the octal escape '\011' 1.112 + */ 1.113 +status = inSection(5); 1.114 +pattern = /.\011/; 1.115 +string = 'a\u0009'; 1.116 +actualmatch = string.match(pattern); 1.117 +expectedmatch = Array(string); 1.118 +addThis(); 1.119 + 1.120 + 1.121 +/* 1.122 + * Same as above, only now for the second character of the string, 1.123 + * use the hex escape '\x09' instead of the octal escape '\011' 1.124 + */ 1.125 +status = inSection(6); 1.126 +pattern = /.\011/; 1.127 +string = 'a\x09'; 1.128 +actualmatch = string.match(pattern); 1.129 +expectedmatch = Array(string); 1.130 +addThis(); 1.131 + 1.132 + 1.133 +/* 1.134 + * Same as above, only now for the second character of the string, 1.135 + * use the escape '\t' instead of the octal escape '\011' 1.136 + */ 1.137 +status = inSection(7); 1.138 +pattern = /.\011/; 1.139 +string = 'a\t'; 1.140 +actualmatch = string.match(pattern); 1.141 +expectedmatch = Array(string); 1.142 +addThis(); 1.143 + 1.144 + 1.145 +/* 1.146 + * Return to the string from Section 1. 1.147 + * 1.148 + * Unlike Section 1, use the RegExp() function to create the 1.149 + * regexp pattern: null character followed by the string '11'. 1.150 + * 1.151 + * Since this is exactly what the string is, we should get a match - 1.152 + */ 1.153 +status = inSection(8); 1.154 +string = 'a' + String.fromCharCode(0) + '11'; 1.155 +pattern = RegExp(string); 1.156 +actualmatch = string.match(pattern); 1.157 +expectedmatch = Array(string); 1.158 +addThis(); 1.159 + 1.160 + 1.161 + 1.162 + 1.163 +//------------------------------------------------------------------------------------------------- 1.164 +test(); 1.165 +//------------------------------------------------------------------------------------------------- 1.166 + 1.167 + 1.168 + 1.169 +function addThis() 1.170 +{ 1.171 + statusmessages[i] = status; 1.172 + patterns[i] = pattern; 1.173 + strings[i] = string; 1.174 + actualmatches[i] = actualmatch; 1.175 + expectedmatches[i] = expectedmatch; 1.176 + i++; 1.177 +} 1.178 + 1.179 + 1.180 +function test() 1.181 +{ 1.182 + enterFunc ('test'); 1.183 + printBugNumber(BUGNUMBER); 1.184 + printStatus (summary); 1.185 + testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); 1.186 + exitFunc ('test'); 1.187 +}