Add new split functions
This commit is contained in:
5 files changed
+52
-16
No files matched your search
@@ -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;
|
||||||
@@ -63,6 +65,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