-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from nicolo-ribaudo/regexp-es2015
Make RegExp#[Symbol.*] methods call exec
- Loading branch information
Showing
13 changed files
with
546 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
var at = require('../internals/string-at')(true); | ||
|
||
// `AdvanceStringIndex` abstract operation | ||
// https://tc39.github.io/ecma262/#sec-advancestringindex | ||
module.exports = function (S, index, unicode) { | ||
return index + (unicode ? at(S, index).length : 1); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var classof = require('../internals/classof-raw'); | ||
var builtinExec = RegExp.prototype.exec; | ||
|
||
// `RegExpExec` abstract operation | ||
// https://tc39.github.io/ecma262/#sec-regexpexec | ||
module.exports = function (R, S) { | ||
var exec = R.exec; | ||
if (typeof exec === 'function') { | ||
var result = exec.call(R, S); | ||
if (typeof result !== 'object') { | ||
throw new TypeError('RegExp exec method returned something other than an Object or null'); | ||
} | ||
return result; | ||
} | ||
|
||
if (classof(R) !== 'RegExp') { | ||
throw new TypeError('RegExp#exec called on incompatible receiver'); | ||
} | ||
|
||
return builtinExec.call(R, S); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,43 @@ | ||
'use strict'; | ||
|
||
var anObject = require('../internals/an-object'); | ||
var toLength = require('../internals/to-length'); | ||
var advanceStringIndex = require('../internals/advance-string-index'); | ||
var regExpExec = require('../internals/regexp-exec'); | ||
var nativeExec = RegExp.prototype.exec; | ||
|
||
// @@match logic | ||
require('../internals/fix-regexp-well-known-symbol-logic')('match', 1, function (defined, MATCH, nativeMatch) { | ||
// `String.prototype.match` method | ||
// https://tc39.github.io/ecma262/#sec-string.prototype.match | ||
return [function match(regexp) { | ||
var O = defined(this); | ||
var matcher = regexp == undefined ? undefined : regexp[MATCH]; | ||
return matcher !== undefined ? matcher.call(regexp, O) : new RegExp(regexp)[MATCH](String(O)); | ||
}, nativeMatch]; | ||
return [ | ||
// `String.prototype.match` method | ||
// https://tc39.github.io/ecma262/#sec-string.prototype.match | ||
function match(regexp) { | ||
var O = defined(this); | ||
var matcher = regexp == undefined ? undefined : regexp[MATCH]; | ||
return matcher !== undefined ? matcher.call(regexp, O) : new RegExp(regexp)[MATCH](String(O)); | ||
}, | ||
// `RegExp.prototype[@@match]` method | ||
// https://tc39.github.io/ecma262/#sec-regexp.prototype-@@match | ||
function (regexp) { | ||
if (regexp.exec === nativeExec) return nativeMatch.call(this, regexp); | ||
|
||
var rx = anObject(regexp); | ||
var S = String(this); | ||
|
||
if (!rx.global) return regExpExec(rx, S); | ||
|
||
var fullUnicode = rx.unicode; | ||
rx.lastIndex = 0; | ||
var A = []; | ||
var n = 0; | ||
var result; | ||
while ((result = regExpExec(rx, S)) !== null) { | ||
var matchStr = String(result[0]); | ||
A[n] = matchStr; | ||
if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode); | ||
n++; | ||
} | ||
return n === 0 ? null : A; | ||
} | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,118 @@ | ||
'use strict'; | ||
|
||
var anObject = require('../internals/an-object'); | ||
var toObject = require('../internals/to-object'); | ||
var toLength = require('../internals/to-length'); | ||
var toInteger = require('../internals/to-integer'); | ||
var advanceStringIndex = require('../internals/advance-string-index'); | ||
var regExpExec = require('../internals/regexp-exec'); | ||
var nativeExec = RegExp.prototype.exec; | ||
var max = Math.max; | ||
var min = Math.min; | ||
var floor = Math.floor; | ||
var SUBSTITUTION_SYMBOLS = /\$([$&`']|\d\d?|<[^>]*>)/g; | ||
var SUBSTITUTION_SYMBOLS_NO_NAMED = /\$([$&`']|\d\d?)/g; | ||
|
||
var maybeToString = function (it) { | ||
return it === undefined ? it : String(it); | ||
}; | ||
|
||
// @@replace logic | ||
require('../internals/fix-regexp-well-known-symbol-logic')('replace', 2, function (defined, REPLACE, nativeReplace) { | ||
// `String.prototype.replace` method | ||
// https://tc39.github.io/ecma262/#sec-string.prototype.replace | ||
return [function replace(searchValue, replaceValue) { | ||
var O = defined(this); | ||
var replacer = searchValue == undefined ? undefined : searchValue[REPLACE]; | ||
return replacer !== undefined | ||
return [ | ||
// `String.prototype.replace` method | ||
// https://tc39.github.io/ecma262/#sec-string.prototype.replace | ||
function replace(searchValue, replaceValue) { | ||
var O = defined(this); | ||
var replacer = searchValue == undefined ? undefined : searchValue[REPLACE]; | ||
return replacer !== undefined | ||
? replacer.call(searchValue, O, replaceValue) | ||
: nativeReplace.call(String(O), searchValue, replaceValue); | ||
}, nativeReplace]; | ||
}, | ||
// `RegExp.prototype[@@replace]` method | ||
// https://tc39.github.io/ecma262/#sec-regexp.prototype-@@replace | ||
function (regexp, replaceValue) { | ||
if (regexp.exec === nativeExec) return nativeReplace.call(this, regexp, replaceValue); | ||
|
||
var rx = anObject(regexp); | ||
var S = String(this); | ||
|
||
var functionalReplace = typeof replaceValue === 'function'; | ||
if (!functionalReplace) replaceValue = String(replaceValue); | ||
|
||
var global = rx.global; | ||
if (global) { | ||
var fullUnicode = rx.unicode; | ||
rx.lastIndex = 0; | ||
} | ||
var results = []; | ||
while (true) { | ||
var result = regExpExec(rx, S); | ||
if (result === null) break; | ||
|
||
results.push(result); | ||
if (!global) break; | ||
|
||
var matchStr = String(result[0]); | ||
if (matchStr === '') rx.lastIndex = advanceStringIndex(S, toLength(rx.lastIndex), fullUnicode); | ||
} | ||
|
||
var accumulatedResult = ''; | ||
var nextSourcePosition = 0; | ||
for (var i = 0; i < results.length; i++) { | ||
result = results[i]; | ||
|
||
var matched = String(result[0]); | ||
var position = max(min(toInteger(result.index), S.length), 0); | ||
var captures = result.slice(1).map(maybeToString); | ||
var namedCaptures = result.groups; | ||
if (functionalReplace) { | ||
var replacerArgs = [matched].concat(captures, position, S); | ||
if (namedCaptures !== undefined) replacerArgs.push(namedCaptures); | ||
var replacement = String(replaceValue.apply(undefined, replacerArgs)); | ||
} else { | ||
replacement = getSubstitution(matched, S, position, captures, namedCaptures, replaceValue); | ||
} | ||
if (position >= nextSourcePosition) { | ||
accumulatedResult += S.slice(nextSourcePosition, position) + replacement; | ||
nextSourcePosition = position + matched.length; | ||
} | ||
} | ||
return accumulatedResult + S.slice(nextSourcePosition); | ||
} | ||
]; | ||
|
||
// https://tc39.github.io/ecma262/#sec-getsubstitution | ||
function getSubstitution(matched, str, position, captures, namedCaptures, replacement) { | ||
var tailPos = position + matched.length; | ||
var m = captures.length; | ||
var symbols = SUBSTITUTION_SYMBOLS_NO_NAMED; | ||
if (namedCaptures !== undefined) { | ||
namedCaptures = toObject(namedCaptures); | ||
symbols = SUBSTITUTION_SYMBOLS; | ||
} | ||
return nativeReplace.call(replacement, symbols, function (match, ch) { | ||
var capture; | ||
switch (ch[0]) { | ||
case '$': return '$'; | ||
case '&': return matched; | ||
case '`': return str.slice(0, position); | ||
case "'": return str.slice(tailPos); | ||
case '<': | ||
capture = namedCaptures[ch.slice(1, -1)]; | ||
break; | ||
default: // \d\d? | ||
var n = +ch; | ||
if (n === 0) return ch; | ||
if (n > m) { | ||
var f = floor(n / 10); | ||
if (f === 0) return ch; | ||
if (f <= m) return captures[f - 1] === undefined ? ch[1] : captures[f - 1] + ch[1]; | ||
return ch; | ||
} | ||
capture = captures[n - 1]; | ||
} | ||
return capture === undefined ? '' : capture; | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,33 @@ | ||
'use strict'; | ||
|
||
var anObject = require('../internals/an-object'); | ||
var sameValue = require('../internals/same-value'); | ||
var regExpExec = require('../internals/regexp-exec'); | ||
var nativeExec = RegExp.prototype.exec; | ||
|
||
// @@search logic | ||
require('../internals/fix-regexp-well-known-symbol-logic')('search', 1, function (defined, SEARCH, nativeSearch) { | ||
// `String.prototype.search` method | ||
// https://tc39.github.io/ecma262/#sec-string.prototype.search | ||
return [function search(regexp) { | ||
var O = defined(this); | ||
var searcher = regexp == undefined ? undefined : regexp[SEARCH]; | ||
return searcher !== undefined ? searcher.call(regexp, O) : new RegExp(regexp)[SEARCH](String(O)); | ||
}, nativeSearch]; | ||
return [ | ||
// `String.prototype.search` method | ||
// https://tc39.github.io/ecma262/#sec-string.prototype.search | ||
function search(regexp) { | ||
var O = defined(this); | ||
var searcher = regexp == undefined ? undefined : regexp[SEARCH]; | ||
return searcher !== undefined ? searcher.call(regexp, O) : new RegExp(regexp)[SEARCH](String(O)); | ||
}, | ||
// `RegExp.prototype[@@search]` method | ||
// https://tc39.github.io/ecma262/#sec-regexp.prototype-@@search | ||
function (regexp) { | ||
if (regexp.exec === nativeExec) return nativeSearch.call(this, regexp); | ||
|
||
var rx = anObject(regexp); | ||
var S = String(this); | ||
|
||
var previousLastIndex = rx.lastIndex; | ||
if (!sameValue(previousLastIndex, 0)) rx.lastIndex = 0; | ||
var result = regExpExec(rx, S); | ||
if (!sameValue(rx.lastIndex, previousLastIndex)) rx.lastIndex = previousLastIndex; | ||
return result === null ? -1 : result.index; | ||
} | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.