Merge pull request #666 from altrisi/extra-splitting
[Suggestion] Allow splitting lists by element value
This commit is contained in:
6 files changed
+60
-16
No files matched your search
@@ -160,11 +160,19 @@ join('-', 'foo', 'bar') => foo-bar
|
|||||||
Splits a string under `expr` by `delim` which can be a regular expression. If no delimiter is specified, it splits
|
Splits a string under `expr` by `delim` which can be a regular expression. If no delimiter is specified, it splits
|
||||||
by characters.
|
by characters.
|
||||||
|
|
||||||
|
If `expr` is a list, it will split the list into multiple sublists by the element (s) which equal `delim`, or which equal the empty string
|
||||||
|
in case no delimiter is specified.
|
||||||
|
|
||||||
|
Splitting a `null` value will return an empty list.
|
||||||
|
|
||||||
<pre>
|
<pre>
|
||||||
split('foo') => [f, o, o]
|
split('foo') => [f, o, o]
|
||||||
split('','foo') => [f, o, o]
|
split('','foo') => [f, o, o]
|
||||||
split('.','foo.bar') => []
|
split('.','foo.bar') => []
|
||||||
split('\\.','foo.bar') => [foo, bar]
|
split('\\.','foo.bar') => [foo, bar]
|
||||||
|
split(1,[2,5,1,2,3,1,5,6]) => [[2,5],[2,3],[5,6]]
|
||||||
|
split(1,[1,2,3,1,4,5,1] => [[], [2,3], [4,5], []]
|
||||||
|
split(null) => []
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
### `slice(expr, from, to?)`
|
### `slice(expr, from, to?)`
|
||||||
|
|||||||
@@ -14,10 +14,8 @@ import carpet.script.value.StringValue;
|
|||||||
import carpet.script.value.Value;
|
import carpet.script.value.Value;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.PatternSyntaxException;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class DataStructures {
|
public class DataStructures {
|
||||||
@@ -54,30 +52,23 @@ public class DataStructures {
|
|||||||
|
|
||||||
expression.addFunction("split", (lv) ->
|
expression.addFunction("split", (lv) ->
|
||||||
{
|
{
|
||||||
String delimiter;
|
Value delimiter;
|
||||||
String hwat;
|
Value hwat;
|
||||||
if (lv.size() == 1)
|
if (lv.size() == 1)
|
||||||
{
|
{
|
||||||
hwat = lv.get(0).getString();
|
hwat = lv.get(0);
|
||||||
delimiter = "";
|
delimiter = StringValue.EMPTY;
|
||||||
}
|
}
|
||||||
else if (lv.size() == 2)
|
else if (lv.size() == 2)
|
||||||
{
|
{
|
||||||
delimiter = lv.get(0).getString();
|
delimiter = lv.get(0);
|
||||||
hwat = lv.get(1).getString();
|
hwat = lv.get(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new InternalExpressionException("'split' takes 1 or 2 arguments");
|
throw new InternalExpressionException("'split' takes 1 or 2 arguments");
|
||||||
}
|
}
|
||||||
try
|
return hwat.split(delimiter);
|
||||||
{
|
|
||||||
return ListValue.wrap(Arrays.stream(hwat.split(delimiter)).map(StringValue::new).collect(Collectors.toList()));
|
|
||||||
}
|
|
||||||
catch (PatternSyntaxException pse)
|
|
||||||
{
|
|
||||||
throw new InternalExpressionException("Incorrect pattern for 'split': "+pse.getMessage());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expression.addFunction("slice", (lv) ->
|
expression.addFunction("slice", (lv) ->
|
||||||
|
|||||||
@@ -347,6 +347,24 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
|
|||||||
return new ListValue((Collection<? extends Value>) getItems().subList(from, to));
|
return new ListValue((Collection<? extends Value>) getItems().subList(from, to));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value split(Value delimiter) {
|
||||||
|
ListValue result = new ListValue();
|
||||||
|
int startIndex = 0;
|
||||||
|
int index = 0;
|
||||||
|
for (Value val : this.items)
|
||||||
|
{
|
||||||
|
index++;
|
||||||
|
if (val.equals(delimiter))
|
||||||
|
{
|
||||||
|
result.items.add(ListValue.wrap(this.items.subList(startIndex, index-1)));
|
||||||
|
startIndex = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.items.add(ListValue.wrap(this.items.subList(startIndex, length())));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double readDoubleNumber()
|
public double readDoubleNumber()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -203,6 +203,11 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
|
|||||||
throw new InternalExpressionException("Cannot slice a map value");
|
throw new InternalExpressionException("Cannot slice a map value");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value split(Value delimiter) {
|
||||||
|
throw new InternalExpressionException("Cannot split a map value");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double readDoubleNumber()
|
public double readDoubleNumber()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package carpet.script.value;
|
package carpet.script.value;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonNull;
|
import com.google.gson.JsonNull;
|
||||||
import net.minecraft.nbt.StringTag;
|
import net.minecraft.nbt.StringTag;
|
||||||
@@ -68,6 +70,11 @@ public class NullValue extends NumericValue // TODO check nonsingleton code
|
|||||||
return StringTag.of("null");
|
return StringTag.of("null");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Value split(Value delimiter) {
|
||||||
|
return ListValue.wrap(new ArrayList<Value>());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonElement toJson()
|
public JsonElement toJson()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ import com.google.gson.JsonPrimitive;
|
|||||||
import net.minecraft.nbt.Tag;
|
import net.minecraft.nbt.Tag;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.regex.PatternSyntaxException;
|
import java.util.regex.PatternSyntaxException;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public abstract class Value implements Comparable<Value>, Cloneable
|
public abstract class Value implements Comparable<Value>, Cloneable
|
||||||
{
|
{
|
||||||
@@ -188,6 +190,19 @@ public abstract class Value implements Comparable<Value>, Cloneable
|
|||||||
if (from > to) return StringValue.EMPTY;
|
if (from > to) return StringValue.EMPTY;
|
||||||
return new StringValue(value.substring(from, to));
|
return new StringValue(value.substring(from, to));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Value split(Value delimiter)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return ListValue.wrap(Arrays.stream(getString().split(delimiter.getString())).map(StringValue::new).collect(Collectors.toList()));
|
||||||
|
}
|
||||||
|
catch (PatternSyntaxException pse)
|
||||||
|
{
|
||||||
|
throw new InternalExpressionException("Incorrect pattern for 'split': "+pse.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public double readDoubleNumber()
|
public double readDoubleNumber()
|
||||||
{
|
{
|
||||||
String s = getString();
|
String s = getString();
|
||||||
|
|||||||
Reference in new issue
Block a user