Skip to content

Commit 8932617

Browse files
committed
test action input constructor without Regex
1 parent 5c4856d commit 8932617

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/main/java/me/hsgamer/bettergui/builder/ActionBuilder.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
import me.hsgamer.hscore.common.CollectionUtils;
1212

1313
import java.util.List;
14-
import java.util.Optional;
15-
import java.util.regex.Matcher;
16-
import java.util.regex.Pattern;
1714
import java.util.stream.Collectors;
1815

1916
/**
@@ -51,19 +48,41 @@ public List<Action> build(Menu menu, Object object) {
5148
}
5249

5350
public interface Input extends ActionInput, MenuElement {
54-
Pattern pattern = Pattern.compile("\\s*([\\w\\-$]+)\\s*(?:\\((.*?)\\))?\\s*(?::\\s*(.*))?");
51+
static ActionInput create(String input) {
52+
input = input.trim();
5553

56-
static Input create(Menu menu, String input) {
57-
ActionInput actionInput;
58-
Matcher matcher = pattern.matcher(input);
59-
if (matcher.matches()) {
60-
String type = matcher.group(1);
61-
String option = Optional.ofNullable(matcher.group(2)).orElse("");
62-
String value = Optional.ofNullable(matcher.group(3)).orElse("");
63-
actionInput = ActionInput.create(type, option, value);
54+
// Find the colon to separate type/option from value
55+
int colonIndex = input.indexOf(':');
56+
String typeOptionPart = colonIndex == -1 ? input : input.substring(0, colonIndex);
57+
String value = colonIndex == -1 ? "" : input.substring(colonIndex + 1).trim();
58+
59+
typeOptionPart = typeOptionPart.trim();
60+
61+
// Find the opening parenthesis to separate type from option
62+
int openParenIndex = typeOptionPart.indexOf('(');
63+
String type;
64+
String option = "";
65+
66+
if (openParenIndex == -1) {
67+
type = typeOptionPart;
6468
} else {
65-
actionInput = ActionInput.create("", "", input);
69+
type = typeOptionPart.substring(0, openParenIndex).trim();
70+
int closeParenIndex = typeOptionPart.lastIndexOf(')');
71+
if (closeParenIndex > openParenIndex) {
72+
option = typeOptionPart.substring(openParenIndex + 1, closeParenIndex).trim();
73+
}
6674
}
75+
76+
// If no type is found, use the entire input as value
77+
if (type.isEmpty() && value.isEmpty()) {
78+
return ActionInput.create("", "", input);
79+
}
80+
81+
return ActionInput.create(type, option, value);
82+
}
83+
84+
static Input create(Menu menu, String input) {
85+
ActionInput actionInput = create(input);
6786
return new Input() {
6887
@Override
6988
public String getType() {

0 commit comments

Comments
 (0)