Reimplement "legacy" shorthand syntax

i.e. using ( ) for groups, and | for list separation
This commit is contained in:
Luck 2019-12-24 13:24:20 +00:00
parent 63b890d522
commit 88c432496b
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -25,6 +25,7 @@
package me.lucko.luckperms.common.node.utils; package me.lucko.luckperms.common.node.utils;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
@ -42,7 +43,7 @@ public enum ShorthandParser {
*/ */
NUMERIC_RANGE { NUMERIC_RANGE {
@Override @Override
public Iterator<String> extract(String input) { public Iterator<String> extract(String input) throws NumberFormatException {
int index = input.indexOf(RANGE_SEPARATOR); int index = input.indexOf(RANGE_SEPARATOR);
if (index == -1 || index == 0 || index == (input.length() - 1)) { if (index == -1 || index == 0 || index == (input.length() - 1)) {
return null; return null;
@ -77,14 +78,14 @@ public enum ShorthandParser {
}, },
/** /**
* Expands "aa,bb,cc" to ["aa", "bb", "cc"] * Expands "aa,bb|cc" to ["aa", "bb", "cc"]
*/ */
LIST { LIST {
private final Splitter splitter = Splitter.on(LIST_SEPARATOR).omitEmptyStrings(); private final Splitter splitter = Splitter.on(CharMatcher.anyOf(new String(new char[]{LIST_SEPARATOR, LIST_SEPARATOR_2}))).omitEmptyStrings();
@Override @Override
public Iterator<String> extract(String input) { public Iterator<String> extract(String input) {
if (input.indexOf(LIST_SEPARATOR) == -1) { if (indexOfEither(input, LIST_SEPARATOR, LIST_SEPARATOR_2) == -1) {
return Iterators.singletonIterator(input); return Iterators.singletonIterator(input);
} }
return this.splitter.split(input).iterator(); return this.splitter.split(input).iterator();
@ -104,8 +105,14 @@ public enum ShorthandParser {
private static final char OPEN_GROUP = '{'; private static final char OPEN_GROUP = '{';
/** Character used to close a group */ /** Character used to close a group */
private static final char CLOSE_GROUP = '}'; private static final char CLOSE_GROUP = '}';
/** Another character used to open a group */
private static final char OPEN_GROUP_2 = '(';
/** Another character used to close a group */
private static final char CLOSE_GROUP_2 = ')';
/** Character used to separate items in a list */ /** Character used to separate items in a list */
private static final char LIST_SEPARATOR = ','; private static final char LIST_SEPARATOR = ',';
/** Another character used to separate items in a list */
private static final char LIST_SEPARATOR_2 = '|';
/** Character used to indicate a range between two values */ /** Character used to indicate a range between two values */
private static final char RANGE_SEPARATOR = '-'; private static final char RANGE_SEPARATOR = '-';
@ -126,13 +133,13 @@ public enum ShorthandParser {
while (true) { while (true) {
boolean work = false; boolean work = false;
for (String str : results) { for (String string : results) {
Set<String> expanded = matchGroup(str); Set<String> expanded = matchGroup(string);
if (expanded != null) { if (expanded != null) {
work = true; work = true;
workSet.addAll(expanded); workSet.addAll(expanded);
} else { } else {
workSet.add(str); workSet.add(string);
} }
} }
@ -155,29 +162,29 @@ public enum ShorthandParser {
return results; return results;
} }
private static Set<String> matchGroup(String s) { private static Set<String> matchGroup(String input) {
int openingIndex = s.indexOf(OPEN_GROUP); int openingIndex = indexOfEither(input, OPEN_GROUP, OPEN_GROUP_2);
if (openingIndex == -1) { if (openingIndex == -1) {
return null; return null;
} }
int closingIndex = s.indexOf(CLOSE_GROUP); int closingIndex = indexOfEither(input, CLOSE_GROUP, CLOSE_GROUP_2);
if (closingIndex < openingIndex) { if (closingIndex < openingIndex) {
return null; return null;
} }
String before = s.substring(0, openingIndex); String before = input.substring(0, openingIndex);
String after = s.substring(closingIndex + 1); String after = input.substring(closingIndex + 1);
String between = s.substring(openingIndex + 1, closingIndex); String between = input.substring(openingIndex + 1, closingIndex);
Set<String> results = new HashSet<>(); Set<String> results = new HashSet<>();
for (ShorthandParser parser : PARSERS) { for (ShorthandParser parser : PARSERS) {
try { try {
Iterator<String> res = parser.extract(between); Iterator<String> extracted = parser.extract(between);
if (res != null) { if (extracted != null) {
while (res.hasNext()) { while (extracted.hasNext()) {
results.add(before + res.next() + after); results.add(before + extracted.next() + after);
} }
// break after one parser has matched // break after one parser has matched
@ -191,6 +198,14 @@ public enum ShorthandParser {
return results; return results;
} }
private static int indexOfEither(String s, char c1, char c2) {
int index = s.indexOf(c1);
if (index != -1) {
return index;
}
return s.indexOf(c2);
}
/** /**
* Implements an iterator over a given range of ints. * Implements an iterator over a given range of ints.
*/ */