Search in sources :

Example 6 with StrBuilder

use of com.github.anba.es6draft.runtime.internal.StrBuilder in project es6draft by anba.

the class RegExpPrototype method GetSubstitution.

/**
 * 21.1.3.14.1 Runtime Semantics: GetSubstitution(matched, str, position, captures, replacement)
 *
 * @param cx
 *            the execution context
 * @param matched
 *            the matched substring
 * @param string
 *            the input string
 * @param position
 *            the match position
 * @param captures
 *            the captured groups
 * @param namedGroups
 *            the optional namedGroups or {@code null}
 * @param replacement
 *            the replace string
 * @return the replacement string
 */
private static String GetSubstitution(ExecutionContext cx, String matched, String string, int position, String[] captures, NamedGroups namedGroups, String replacement) {
    /* step 1 (not applicable) */
    /* step 2 */
    int matchLength = matched.length();
    /* step 3 (not applicable) */
    /* step 4 */
    int stringLength = string.length();
    /* step 5 */
    assert position >= 0;
    /* step 6 */
    assert position <= stringLength;
    /* steps 7-8 (not applicable) */
    /* step 9 */
    int tailPos = Math.min(position + matchLength, stringLength);
    /* step 10 */
    int m = captures.length;
    /* step 11 */
    int cursor = replacement.indexOf('$');
    if (cursor < 0) {
        return replacement;
    }
    final int length = replacement.length();
    int lastCursor = 0;
    StrBuilder result = new StrBuilder(cx);
    for (; ; ) {
        if (lastCursor < cursor) {
            result.append(replacement, lastCursor, cursor);
        }
        if (++cursor == length) {
            result.append('$');
            break;
        }
        assert cursor < length;
        char c = replacement.charAt(cursor++);
        switch(c) {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                {
                    int n = c - '0';
                    if (cursor < length) {
                        char d = replacement.charAt(cursor);
                        if (d >= (n == 0 ? '1' : '0') && d <= '9') {
                            int nn = n * 10 + (d - '0');
                            if (nn <= m) {
                                cursor += 1;
                                n = nn;
                            }
                        }
                    }
                    if (n == 0 || n > m) {
                        assert n >= 0 && n <= 9;
                        result.append('$').append(c);
                    } else {
                        assert n >= 1 && n <= 99;
                        String capture = captures[n - 1];
                        if (capture != null) {
                            result.append(capture);
                        }
                    }
                    break;
                }
            case '&':
                result.append(matched);
                break;
            case '`':
                result.append(string, 0, position);
                break;
            case '\'':
                result.append(string, tailPos, stringLength);
                break;
            case '$':
                result.append('$');
                break;
            case '<':
                if (namedGroups == null) {
                    result.append("$<");
                    break;
                }
                int closing = replacement.indexOf('>', cursor);
                if (closing < 0) {
                    result.append("$<");
                    break;
                }
                String groupName = replacement.substring(cursor, closing);
                String capture = namedGroups.group(groupName);
                if (capture != null) {
                    result.append(capture);
                }
                cursor = closing + 1;
                break;
            default:
                result.append('$').append(c);
                break;
        }
        lastCursor = cursor;
        cursor = replacement.indexOf('$', cursor);
        if (cursor < 0) {
            if (lastCursor < length) {
                result.append(replacement, lastCursor, length);
            }
            break;
        }
    }
    /* step 12 */
    return result.toString();
}
Also used : StrBuilder(com.github.anba.es6draft.runtime.internal.StrBuilder)

Aggregations

StrBuilder (com.github.anba.es6draft.runtime.internal.StrBuilder)6 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)4 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 BigIntObject (com.github.anba.es6draft.runtime.objects.bigint.BigIntObject)2 NumberObject (com.github.anba.es6draft.runtime.objects.number.NumberObject)2 StringObject (com.github.anba.es6draft.runtime.types.builtins.StringObject)2 CompilationException (com.github.anba.es6draft.compiler.CompilationException)1 CompiledFunction (com.github.anba.es6draft.compiler.CompiledFunction)1 ParserException (com.github.anba.es6draft.parser.ParserException)1 ToFlatString (com.github.anba.es6draft.runtime.AbstractOperations.ToFlatString)1 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)1 Source (com.github.anba.es6draft.runtime.internal.Source)1 Intrinsics (com.github.anba.es6draft.runtime.types.Intrinsics)1 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)1