Package net.fortytwo.ripple.libs.string

Source Code of net.fortytwo.ripple.libs.string.Substring

package net.fortytwo.ripple.libs.string;

import net.fortytwo.flow.Sink;
import net.fortytwo.ripple.RippleException;
import net.fortytwo.ripple.model.ModelConnection;
import net.fortytwo.ripple.model.PrimitiveStackMapping;
import net.fortytwo.ripple.model.RippleList;
import net.fortytwo.ripple.model.RippleValue;

/**
* A primitive which consumes a string and two integer indexes, then
* produces the substring between the first index (inclusive) and the second
* index (exclusive).
*
* @author Joshua Shinavier (http://fortytwo.net)
*/
public class Substring extends PrimitiveStackMapping {
    private static final String[] IDENTIFIERS = {
            StringLibrary.NS_2013_03 + "substring",
            StringLibrary.NS_2008_08 + "substring",
            StringLibrary.NS_2007_08 + "substring"};

    public String[] getIdentifiers() {
        return IDENTIFIERS;
    }

    public Substring()
            throws RippleException {
        super();
    }

    public Parameter[] getParameters() {
        return new Parameter[]{
                new Parameter("s", null, true),
                new Parameter("beginIndex", null, true),
                new Parameter("endIndex", null, true)};
    }

    public String getComment() {
        return "s beginIndex endIndex  =>  s2 -- where s2 is the substring of s which begins" +
                " at the specified beginIndex and extends to the character at index endIndex - 1";
    }

    public void apply(final RippleList arg,
                      final Sink<RippleList> solutions,
                      final ModelConnection mc) throws RippleException {
        RippleList stack = arg;

        int begin, end;
        RippleValue s;
        String result;

        end = mc.toNumericValue(stack.getFirst()).intValue();
        stack = stack.getRest();
        begin = mc.toNumericValue(stack.getFirst()).intValue();
        stack = stack.getRest();
        s = stack.getFirst();
        stack = stack.getRest();

        try {
            result = mc.toString(s).substring(begin, end);
            solutions.put(
                    stack.push(StringLibrary.value(result, mc, s)));
        } catch (IndexOutOfBoundsException e) {
            // Silent fail.
        }
    }
}
TOP

Related Classes of net.fortytwo.ripple.libs.string.Substring

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.