From 30e3ca49652235e7cf4eda0a168183c724fa91c5 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Wed, 24 Mar 2010 04:30:31 +0000 Subject: [PATCH] Added splitByLengths function. git-svn-id: http://google-refine.googlecode.com/svn/trunk@351 7d457c2a-affb-35e4-300a-418c747d4874 --- .../functions/strings/SplitByLengths.java | 49 +++++++++++++++++++ .../gel/ControlFunctionRegistry.java | 2 + 2 files changed, 51 insertions(+) create mode 100644 src/main/java/com/metaweb/gridworks/expr/functions/strings/SplitByLengths.java diff --git a/src/main/java/com/metaweb/gridworks/expr/functions/strings/SplitByLengths.java b/src/main/java/com/metaweb/gridworks/expr/functions/strings/SplitByLengths.java new file mode 100644 index 000000000..6f218694c --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/expr/functions/strings/SplitByLengths.java @@ -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(); + } +} diff --git a/src/main/java/com/metaweb/gridworks/gel/ControlFunctionRegistry.java b/src/main/java/com/metaweb/gridworks/gel/ControlFunctionRegistry.java index fdf3598d5..bd2b1c7b3 100644 --- a/src/main/java/com/metaweb/gridworks/gel/ControlFunctionRegistry.java +++ b/src/main/java/com/metaweb/gridworks/gel/ControlFunctionRegistry.java @@ -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());