|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 /* |
|
7 * Date: 23 October 2001 |
|
8 * |
|
9 * SUMMARY: Testing regexps with the global flag set. |
|
10 * NOT every substring fitting the given pattern will be matched. |
|
11 * The parent string is CONSUMED as successive matches are found. |
|
12 * |
|
13 * From the ECMA-262 Final spec: |
|
14 * |
|
15 * 15.10.6.2 RegExp.prototype.exec(string) |
|
16 * Performs a regular expression match of string against the regular |
|
17 * expression and returns an Array object containing the results of |
|
18 * the match, or null if the string did not match. |
|
19 * |
|
20 * The string ToString(string) is searched for an occurrence of the |
|
21 * regular expression pattern as follows: |
|
22 * |
|
23 * 1. Let S be the value of ToString(string). |
|
24 * 2. Let length be the length of S. |
|
25 * 3. Let lastIndex be the value of the lastIndex property. |
|
26 * 4. Let i be the value of ToInteger(lastIndex). |
|
27 * 5. If the global property is false, let i = 0. |
|
28 * 6. If i < 0 or i > length then set lastIndex to 0 and return null. |
|
29 * 7. Call [[Match]], giving it the arguments S and i. |
|
30 * If [[Match]] returned failure, go to step 8; |
|
31 * otherwise let r be its State result and go to step 10. |
|
32 * 8. Let i = i+1. |
|
33 * 9. Go to step 6. |
|
34 * 10. Let e be r's endIndex value. |
|
35 * 11. If the global property is true, set lastIndex to e. |
|
36 * |
|
37 * etc. |
|
38 * |
|
39 * |
|
40 * So when the global flag is set, |lastIndex| is incremented every time |
|
41 * there is a match; not from i to i+1, but from i to "endIndex" e: |
|
42 * |
|
43 * e = (index of last input character matched so far by the pattern) + 1 |
|
44 * |
|
45 * Thus in the example below, the first endIndex e occurs after the |
|
46 * first match 'a b'. The next match will begin AFTER this, and so |
|
47 * will NOT be 'b c', but rather 'c d'. Similarly, 'd e' won't be matched. |
|
48 */ |
|
49 //----------------------------------------------------------------------------- |
|
50 var i = 0; |
|
51 var BUGNUMBER = '(none)'; |
|
52 var summary = 'Testing regexps with the global flag set'; |
|
53 var status = ''; |
|
54 var statusmessages = new Array(); |
|
55 var pattern = ''; |
|
56 var patterns = new Array(); |
|
57 var string = ''; |
|
58 var strings = new Array(); |
|
59 var actualmatch = ''; |
|
60 var actualmatches = new Array(); |
|
61 var expectedmatch = ''; |
|
62 var expectedmatches = new Array(); |
|
63 |
|
64 |
|
65 status = inSection(1); |
|
66 string = 'a b c d e'; |
|
67 pattern = /\w\s\w/g; |
|
68 actualmatch = string.match(pattern); |
|
69 expectedmatch = ['a b','c d']; // see above explanation - |
|
70 addThis(); |
|
71 |
|
72 |
|
73 status = inSection(2); |
|
74 string = '12345678'; |
|
75 pattern = /\d\d\d/g; |
|
76 actualmatch = string.match(pattern); |
|
77 expectedmatch = ['123','456']; |
|
78 addThis(); |
|
79 |
|
80 |
|
81 |
|
82 //----------------------------------------------------------------------------- |
|
83 test(); |
|
84 //----------------------------------------------------------------------------- |
|
85 |
|
86 |
|
87 |
|
88 function addThis() |
|
89 { |
|
90 statusmessages[i] = status; |
|
91 patterns[i] = pattern; |
|
92 strings[i] = string; |
|
93 actualmatches[i] = actualmatch; |
|
94 expectedmatches[i] = expectedmatch; |
|
95 i++; |
|
96 } |
|
97 |
|
98 |
|
99 function test() |
|
100 { |
|
101 enterFunc ('test'); |
|
102 printBugNumber(BUGNUMBER); |
|
103 printStatus (summary); |
|
104 testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); |
|
105 exitFunc ('test'); |
|
106 } |