Added splitByLengths function.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@351 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-03-24 04:30:31 +00:00
parent 4df1c4107a
commit 30e3ca4965
2 changed files with 51 additions and 0 deletions

View File

@ -0,0 +1,49 @@
package com.metaweb.gridworks.expr.functions.strings;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.expr.EvalError;
import com.metaweb.gridworks.gel.ControlFunctionRegistry;
import com.metaweb.gridworks.gel.Function;
public class SplitByLengths implements Function {
public Object call(Properties bindings, Object[] args) {
if (args.length >= 2 && args[0] != null) {
Object o = args[0];
String s = o instanceof String ? (String) o : o.toString();
String[] results = new String[args.length - 1];
int lastIndex = 0;
for (int i = 1; i < args.length; i++) {
int thisIndex = lastIndex;
Object o2 = args[i];
if (o2 instanceof Number) {
thisIndex = Math.min(s.length(), lastIndex + Math.max(0, ((Number) o2).intValue()));
}
results[i - 1] = s.substring(lastIndex, thisIndex);
lastIndex = thisIndex;
}
return results;
}
return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects 1 string and 1 or more numbers");
}
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("description"); writer.value("Returns the array of strings obtained by splitting s into substrings with the given lengths");
writer.key("params"); writer.value("string s, number n, ...");
writer.key("returns"); writer.value("array");
writer.endObject();
}
}

View File

@ -32,6 +32,7 @@ import com.metaweb.gridworks.expr.functions.math.Pow;
import com.metaweb.gridworks.expr.functions.math.Round;
import com.metaweb.gridworks.expr.functions.math.Sum;
import com.metaweb.gridworks.expr.functions.strings.Contains;
import com.metaweb.gridworks.expr.functions.strings.SplitByLengths;
import com.metaweb.gridworks.expr.functions.strings.Diff;
import com.metaweb.gridworks.expr.functions.strings.EndsWith;
import com.metaweb.gridworks.expr.functions.strings.Fingerprint;
@ -123,6 +124,7 @@ public class ControlFunctionRegistry {
registerFunction("replaceChars", new ReplaceChars());
registerFunction("split", new Split());
registerFunction("splitByCharType", new SplitByCharType());
registerFunction("splitByLengths", new SplitByLengths());
registerFunction("partition", new Partition());
registerFunction("rpartition", new RPartition());
registerFunction("trim", new Trim());