| 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 gTestfile = '15.10.6.2-1.js'; |
| 51 | var i = 0; |
| 52 | var BUGNUMBER = '(none)'; |
| 53 | var summary = 'Testing regexps with the global flag set'; |
| 54 | var status = ''; |
| 55 | var statusmessages = new Array(); |
| 56 | var pattern = ''; |
| 57 | var patterns = new Array(); |
| 58 | var string = ''; |
| 59 | var strings = new Array(); |
| 60 | var actualmatch = ''; |
| 61 | var actualmatches = new Array(); |
| 62 | var expectedmatch = ''; |
| 63 | var expectedmatches = new Array(); |
| 64 | |
| 65 | |
| 66 | status = inSection(1); |
| 67 | string = 'a b c d e'; |
| 68 | pattern = /\w\s\w/g; |
| 69 | actualmatch = string.match(pattern); |
| 70 | expectedmatch = ['a b','c d']; // see above explanation - |
| 71 | addThis(); |
| 72 | |
| 73 | |
| 74 | status = inSection(2); |
| 75 | string = '12345678'; |
| 76 | pattern = /\d\d\d/g; |
| 77 | actualmatch = string.match(pattern); |
| 78 | expectedmatch = ['123','456']; |
| 79 | addThis(); |
| 80 | |
| 81 | |
| 82 | |
| 83 | //----------------------------------------------------------------------------- |
| 84 | test(); |
| 85 | //----------------------------------------------------------------------------- |
| 86 | |
| 87 | |
| 88 | |
| 89 | function addThis() |
| 90 | { |
| 91 | statusmessages[i] = status; |
| 92 | patterns[i] = pattern; |
| 93 | strings[i] = string; |
| 94 | actualmatches[i] = actualmatch; |
| 95 | expectedmatches[i] = expectedmatch; |
| 96 | i++; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | function test() |
| 101 | { |
| 102 | enterFunc ('test'); |
| 103 | printBugNumber(BUGNUMBER); |
| 104 | printStatus (summary); |
| 105 | testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); |
| 106 | exitFunc ('test'); |
| 107 | } |