Merge pull request #666 from altrisi/extra-splitting

[Suggestion] Allow splitting lists by element value
This commit is contained in:
gnembon authored and GitHub committed 2021-01-13 21:23:49 -04:00
commit d563e65c77
6 files changed
+60 -16

No files matched your search

@@ -347,6 +347,24 @@ public class ListValue extends AbstractListValue implements ContainerValueInterf
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
public double readDoubleNumber()
{
@@ -203,6 +203,11 @@ public class MapValue extends AbstractListValue implements ContainerValueInterfa
throw new InternalExpressionException("Cannot slice a map value");
}
@Override
public Value split(Value delimiter) {
throw new InternalExpressionException("Cannot split a map value");
}
@Override
public double readDoubleNumber()
{
@@ -1,5 +1,7 @@
package carpet.script.value;
import java.util.ArrayList;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import net.minecraft.nbt.StringTag;
@@ -68,6 +70,11 @@ public class NullValue extends NumericValue // TODO check nonsingleton code
return StringTag.of("null");
}
@Override
public Value split(Value delimiter) {
return ListValue.wrap(new ArrayList<Value>());
}
@Override
public JsonElement toJson()
{
@@ -6,10 +6,12 @@ import com.google.gson.JsonPrimitive;
import net.minecraft.nbt.Tag;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;
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;
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()
{
String s = getString();