Skip to content

Commit 18b901d

Browse files
committed
Optimize alias matching
1 parent 9051332 commit 18b901d

File tree

3 files changed

+18
-63
lines changed

3 files changed

+18
-63
lines changed

src/main/java/com/laytonsmith/core/AliasCore.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ public boolean alias(String command, final MCCommandSender sender) {
166166
}
167167

168168
Script script = null;
169+
String[] args = command.split(" ");
169170
for(Script s : scripts) {
170-
if(s.match(command)) {
171+
if(s.match(args)) {
171172
script = s;
172173
break;
173174
}

src/main/java/com/laytonsmith/core/Script.java

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -580,82 +580,35 @@ private void doDebugOutput(String nodeName, List<ParseTree> children) {
580580
}
581581

582582
public boolean match(String command) {
583+
String[] cmds = command.split(" ");
584+
return match(cmds);
585+
}
586+
587+
public boolean match(String[] args) {
583588
if(cleft == null) {
584589
//The compilation error happened during the signature declaration, so
585590
//we can't match it, nor can we even tell if it's what they intended for us to run.
586591
return false;
587592
}
588593
boolean caseSensitive = Prefs.CaseSensitive();
589-
String[] cmds = command.split(" ");
590-
List<String> args = new ArrayList<>(Arrays.asList(cmds));
591-
boolean isAMatch = true;
592-
StringBuilder lastVar = new StringBuilder();
593-
int lastJ = 0;
594594
for(int j = 0; j < cleft.size(); j++) {
595-
if(!isAMatch) {
596-
break;
597-
}
598-
lastJ = j;
599595
Mixed c = cleft.get(j);
600-
if(args.size() <= j) {
601-
if(!Construct.IsCType(c, ConstructType.VARIABLE) || !((Variable) c).isOptional()) {
602-
isAMatch = false;
603-
}
604-
break;
596+
if(args.length <= j) {
597+
// If this optional, the rest are too.
598+
return c instanceof Variable v && v.isOptional();
605599
}
606-
String arg = args.get(j);
607-
if(!Construct.IsCType(c, ConstructType.VARIABLE)) {
600+
if(!(c instanceof Variable)) {
601+
String arg = args[j];
608602
if(caseSensitive && !c.val().equals(arg) || !caseSensitive && !c.val().equalsIgnoreCase(arg)) {
609-
isAMatch = false;
610-
continue;
603+
return false;
611604
}
612-
} else {
605+
} else if(((Variable) c).isOptional()) {
613606
//It's a variable. If it's optional, the rest of them are optional too, so as long as the size of
614607
//args isn't greater than the size of cleft, it's a match
615-
if(((Variable) c).isOptional()) {
616-
if(args.size() <= cleft.size()) {
617-
return true;
618-
} else {
619-
Mixed fin = cleft.get(cleft.size() - 1);
620-
if(fin instanceof Variable) {
621-
if(((Variable) fin).isFinal()) {
622-
return true;
623-
}
624-
}
625-
return false;
626-
}
627-
}
628-
}
629-
if(j == cleft.size() - 1) {
630-
if(Construct.IsCType(cleft.get(j), ConstructType.VARIABLE)) {
631-
Variable lv = (Variable) cleft.get(j);
632-
if(lv.isFinal()) {
633-
for(int a = j; a < args.size(); a++) {
634-
if(lastVar.length() == 0) {
635-
lastVar.append(args.get(a));
636-
} else {
637-
lastVar.append(" ").append(args.get(a));
638-
}
639-
}
640-
}
641-
}
642-
}
643-
}
644-
boolean lastIsFinal = false;
645-
if(cleft.get(cleft.size() - 1) instanceof Variable) {
646-
Variable v = (Variable) cleft.get(cleft.size() - 1);
647-
if(v.isFinal()) {
648-
lastIsFinal = true;
608+
return args.length <= cleft.size() || cleft.get(cleft.size() - 1) instanceof Variable v && v.isFinal();
649609
}
650610
}
651-
if((cleft.get(lastJ) instanceof Variable && ((Variable) cleft.get(lastJ)).isOptional())) {
652-
return true;
653-
}
654-
655-
if(cleft.size() != cmds.length && !lastIsFinal) {
656-
isAMatch = false;
657-
}
658-
return isAMatch;
611+
return args.length == cleft.size() || cleft.get(cleft.size() - 1) instanceof Variable v && v.isFinal();
659612
}
660613

661614
public List<Variable> getVariables(String command) {

src/main/java/com/laytonsmith/core/functions/Meta.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,9 @@ public CBoolean exec(Target t, Environment environment, Mixed... args)
605605
throws ConfigRuntimeException {
606606
AliasCore ac = Static.getAliasCore();
607607

608+
String[] cmdArgs = args[0].val().split(" ");
608609
for(Script s : ac.getScripts()) {
609-
if(s.match(args[0].val())) {
610+
if(s.match(cmdArgs)) {
610611
return CBoolean.TRUE;
611612
}
612613
}

0 commit comments

Comments
 (0)