michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Date: 23 October 2001 michael@0: * michael@0: * SUMMARY: Testing regexps with the global flag set. michael@0: * NOT every substring fitting the given pattern will be matched. michael@0: * The parent string is CONSUMED as successive matches are found. michael@0: * michael@0: * From the ECMA-262 Final spec: michael@0: * michael@0: * 15.10.6.2 RegExp.prototype.exec(string) michael@0: * Performs a regular expression match of string against the regular michael@0: * expression and returns an Array object containing the results of michael@0: * the match, or null if the string did not match. michael@0: * michael@0: * The string ToString(string) is searched for an occurrence of the michael@0: * regular expression pattern as follows: michael@0: * michael@0: * 1. Let S be the value of ToString(string). michael@0: * 2. Let length be the length of S. michael@0: * 3. Let lastIndex be the value of the lastIndex property. michael@0: * 4. Let i be the value of ToInteger(lastIndex). michael@0: * 5. If the global property is false, let i = 0. michael@0: * 6. If i < 0 or i > length then set lastIndex to 0 and return null. michael@0: * 7. Call [[Match]], giving it the arguments S and i. michael@0: * If [[Match]] returned failure, go to step 8; michael@0: * otherwise let r be its State result and go to step 10. michael@0: * 8. Let i = i+1. michael@0: * 9. Go to step 6. michael@0: * 10. Let e be r's endIndex value. michael@0: * 11. If the global property is true, set lastIndex to e. michael@0: * michael@0: * etc. michael@0: * michael@0: * michael@0: * So when the global flag is set, |lastIndex| is incremented every time michael@0: * there is a match; not from i to i+1, but from i to "endIndex" e: michael@0: * michael@0: * e = (index of last input character matched so far by the pattern) + 1 michael@0: * michael@0: * Thus in the example below, the first endIndex e occurs after the michael@0: * first match 'a b'. The next match will begin AFTER this, and so michael@0: * will NOT be 'b c', but rather 'c d'. Similarly, 'd e' won't be matched. michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var i = 0; michael@0: var BUGNUMBER = '(none)'; michael@0: var summary = 'Testing regexps with the global flag set'; michael@0: var status = ''; michael@0: var statusmessages = new Array(); michael@0: var pattern = ''; michael@0: var patterns = new Array(); michael@0: var string = ''; michael@0: var strings = new Array(); michael@0: var actualmatch = ''; michael@0: var actualmatches = new Array(); michael@0: var expectedmatch = ''; michael@0: var expectedmatches = new Array(); michael@0: michael@0: michael@0: status = inSection(1); michael@0: string = 'a b c d e'; michael@0: pattern = /\w\s\w/g; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = ['a b','c d']; // see above explanation - michael@0: addThis(); michael@0: michael@0: michael@0: status = inSection(2); michael@0: string = '12345678'; michael@0: pattern = /\d\d\d/g; michael@0: actualmatch = string.match(pattern); michael@0: expectedmatch = ['123','456']; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: test(); michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusmessages[i] = status; michael@0: patterns[i] = pattern; michael@0: strings[i] = string; michael@0: actualmatches[i] = actualmatch; michael@0: expectedmatches[i] = expectedmatch; michael@0: i++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); michael@0: exitFunc ('test'); michael@0: }