/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.script.mutator;

import divconq.script.Activity;
import divconq.script.IMutator;
import divconq.script.StackEntry;
import divconq.struct.Struct;
import divconq.struct.scalar.StringStruct;
import divconq.util.StringUtil;
import divconq.xml.XElement;

public class Substring implements IMutator {

	@Override
	public void mutate(Activity activity, StackEntry stack, XElement code, Struct dest) {
		int from = (int) stack.intFromSource(code, "From", 0); 
		int to = (int) stack.intFromSource(code, "To", 0); 
		int length = (int) stack.intFromSource(code, "Length", 0); 
				
		if (dest instanceof StringStruct) {
			StringStruct idest = (StringStruct)dest;
			String val = idest.getValue();
			
			if (StringUtil.isEmpty(val))
				return;
			
			if (to > 0) 
				idest.setValue(val.substring(from, to));
			else if (length > 0) 
				idest.setValue(val.substring(from, from + length));
			else
				idest.setValue(val.substring(from));
		}
	}
}
