use of com.github.anba.es6draft.regexp.MatcherState in project es6draft by anba.
the class RegExpPrototype method matchResultOrNull.
/**
* 21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S ) (1)
*
* @param cx
* the execution context
* @param r
* the regular expression object
* @param s
* the string
* @param lastIndex
* the lastIndex position
* @param sticky
* the sticky flag
* @param storeResult
* {@code true} if the match result is stored
* @return the match result or {@code null}
*/
private static MatcherResult matchResultOrNull(ExecutionContext cx, RegExpObject r, String s, int lastIndex, boolean sticky, boolean storeResult) {
/* step 1 */
assert r.getRegExpMatcher() != null;
/* step 12.a */
if (lastIndex > s.length()) {
return null;
}
/* step 9 */
RegExpMatcher matcher = r.getRegExpMatcher();
/* step 10 (not applicable) */
/* steps 11-12 */
MatcherState m = matcher.matcher(s);
boolean matchSucceeded;
if (!sticky) {
matchSucceeded = m.find(lastIndex);
} else {
matchSucceeded = m.matches(lastIndex);
}
/* steps 12.a, 12.c */
if (!matchSucceeded) {
return null;
}
if (cx.getRuntimeContext().isEnabled(CompatibilityOption.LegacyRegExp)) {
if (cx.getRealm() == r.getRealm()) {
if (!r.isLegacyFeaturesEnabled()) {
RegExpConstructor.invalidateLastMatchResult(cx);
storeResult = false;
}
} else {
storeResult = false;
}
}
MatcherResult matchResult = m.toMatchResult();
if (storeResult) {
RegExpConstructor.storeLastMatchResult(cx, s, matchResult);
}
return matchResult;
}
use of com.github.anba.es6draft.regexp.MatcherState in project es6draft by anba.
the class RegExpPrototype method RegExpReplace.
private static List<MatchResult> RegExpReplace(ExecutionContext cx, RegExpObject rx, String s, boolean global, boolean fullUnicode) {
if (!global) {
/* step 11.a */
MatchResult result = matchResultOrNull(cx, rx, s, false);
/* step 11.b */
if (result == null) {
return Collections.emptyList();
}
/* step 11.c */
return Collections.singletonList(result);
}
/* step 8 */
/* step 8.a (not applicable) */
/* step 8.b */
RegExpSetLastIndex(cx, rx, 0);
int lastIndex = 0;
/* step 9 */
ArrayList<MatchResult> results = new ArrayList<>();
/* steps 10-11 */
MatcherState matcher = rx.getRegExpMatcher().matcher(s);
boolean sticky = rx.isSet(RegExpObject.Flags.Sticky);
while (true) {
/* step 11.a */
boolean matchSucceeded = matchOrFind(matcher, lastIndex, sticky);
/* step 11.b */
if (!matchSucceeded) {
break;
}
/* step 11.c */
MatchResult result = matcher.toMatchResult();
results.add(result);
lastIndex = result.end();
if (result.start() == lastIndex) {
lastIndex = AdvanceStringIndex(s, lastIndex, fullUnicode);
}
if (lastIndex > s.length()) {
break;
}
}
return results;
}
use of com.github.anba.es6draft.regexp.MatcherState in project es6draft by anba.
the class RegExpPrototype method RegExpSplit.
private static ArrayObject RegExpSplit(ExecutionContext cx, RegExpObject rx, String s, boolean unicodeMatching, int lim, boolean isShared) {
/* steps 1-10, 13 (not applicable) */
/* step 11 */
ArrayObject a = ArrayCreate(cx, 0);
/* step 12 */
int lengthA = 0;
/* step 14 */
int size = s.length();
/* step 15 */
int p = 0;
/* step 16 */
if (lim == 0) {
return a;
}
/* step 17 */
if (size == 0) {
MatchResult result;
if (isShared) {
result = matchResultOrNull(cx, rx, s, 0, true, true);
} else {
result = matchResultOrNull(cx, rx, s, true);
}
if (result != null) {
return a;
}
CreateDataProperty(cx, a, 0, s);
return a;
}
if (!isShared) {
/* step 19.a */
RegExpSetLastIndex(cx, rx, 0);
}
/* step 18 */
int q = p;
/* step 19 */
int lastStart = -1;
MatcherState matcher = rx.getRegExpMatcher().matcher(s);
boolean storeResult = true, invalidateResult = false;
if (cx.getRuntimeContext().isEnabled(CompatibilityOption.LegacyRegExp)) {
if (cx.getRealm() == rx.getRealm()) {
if (!rx.isLegacyFeaturesEnabled()) {
invalidateResult = true;
storeResult = false;
}
} else {
storeResult = false;
}
}
while (q != size) {
/* steps 19.a-c */
boolean match = matcher.find(q);
if (!match) {
break;
}
MatcherResult result = matcher.toMatchResult();
if (storeResult) {
RegExpConstructor.storeLastMatchResult(cx, s, result);
} else if (invalidateResult) {
RegExpConstructor.invalidateLastMatchResult(cx);
invalidateResult = false;
}
/* steps 19.d.i-ii */
int e = result.end();
/* steps 19.d.iii-iv */
if (e == p) {
/* step 19.d.iii */
q = AdvanceStringIndex(s, q, unicodeMatching);
} else {
/* step 19.d.iv */
String t = s.substring(p, lastStart = result.start());
CreateDataProperty(cx, a, lengthA, t);
lengthA += 1;
if (lengthA == lim) {
if (!isShared) {
RegExpSetLastIndex(cx, rx, e);
}
return a;
}
int groupCount = result.groupCount();
for (int i = 1; i <= groupCount; ++i) {
String cap = result.group(i);
CreateDataProperty(cx, a, lengthA, cap != null ? cap : UNDEFINED);
lengthA += 1;
if (lengthA == lim) {
if (!isShared) {
RegExpSetLastIndex(cx, rx, e);
}
return a;
}
}
p = e;
q = p;
}
}
if (lastStart == size) {
return a;
}
/* step 20 */
String t = s.substring(p, size);
/* step 21 */
CreateDataProperty(cx, a, lengthA, t);
/* step 22 */
return a;
}
use of com.github.anba.es6draft.regexp.MatcherState in project es6draft by anba.
the class RegExpPrototype method RegExpMatch.
private static Object RegExpMatch(ExecutionContext cx, RegExpObject rx, String s, boolean global, boolean fullUnicode) {
/* step 5 */
if (!global) {
MatcherResult result = matchResultOrNull(cx, rx, s, true);
if (result == null) {
return NULL;
}
return toMatchArray(cx, s, result);
}
/* step 6 */
/* step 6.a (not applicable) */
/* step 6.b */
RegExpSetLastIndex(cx, rx, 0);
int lastIndex = 0;
/* step 6.c */
ArrayObject array = null;
MatcherResult lastResult = null;
/* steps 6.d-e */
MatcherState matcher = rx.getRegExpMatcher().matcher(s);
boolean sticky = rx.isSet(RegExpObject.Flags.Sticky);
for (int n = 0; ; ++n) {
/* step 6.e.i */
boolean matchSucceeded = matchOrFind(matcher, lastIndex, sticky);
/* step 6.e.ii */
if (!matchSucceeded) {
break;
}
if (array == null) {
array = ArrayCreate(cx, 0);
}
lastResult = matcher.toMatchResult();
lastIndex = lastResult.end();
/* step 6.e.iii */
CreateDataProperty(cx, array, n, lastResult.group());
if (lastResult.start() == lastIndex) {
lastIndex = AdvanceStringIndex(s, lastIndex, fullUnicode);
}
if (lastIndex > s.length()) {
break;
}
}
if (lastResult == null) {
return NULL;
}
boolean storeResult = true;
if (cx.getRuntimeContext().isEnabled(CompatibilityOption.LegacyRegExp)) {
if (cx.getRealm() == rx.getRealm()) {
if (!rx.isLegacyFeaturesEnabled()) {
RegExpConstructor.invalidateLastMatchResult(cx);
storeResult = false;
}
} else {
storeResult = false;
}
}
if (storeResult) {
RegExpConstructor.storeLastMatchResult(cx, s, lastResult);
}
return array;
}
Aggregations