initial commit
This commit is contained in:
parent
3c9ee26b06
commit
22186c33b9
1458 changed files with 282792 additions and 0 deletions
49
java/src/game/command/PatternReplacer.java
Normal file
49
java/src/game/command/PatternReplacer.java
Normal file
|
@ -0,0 +1,49 @@
|
|||
package game.command;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PatternReplacer {
|
||||
private final String variable;
|
||||
private final boolean matchAll;
|
||||
private final Pattern pattern;
|
||||
private final Function<String, String> function;
|
||||
|
||||
public PatternReplacer(String variable, boolean matchAll, Function<String, String> function) {
|
||||
this.variable = variable;
|
||||
this.matchAll = matchAll;
|
||||
this.pattern = Pattern.compile("\\$\\((" + variable + (matchAll ? "[^\\)]*" : "") + ")\\)");
|
||||
this.function = function;
|
||||
}
|
||||
|
||||
public void replaceVar(StringBuffer sb) {
|
||||
String str = sb.toString();
|
||||
sb.delete(0, sb.length());
|
||||
Matcher matcher = this.pattern.matcher(str);
|
||||
while(matcher.find()) {
|
||||
String orig = matcher.group(1);
|
||||
String rep = this.function.apply(orig);
|
||||
if(rep == null)
|
||||
throw new ScriptException("Variable '%s' konnte in diesem Kontext nicht ersetzt werden", orig);
|
||||
matcher.appendReplacement(sb, rep);
|
||||
}
|
||||
matcher.appendTail(sb);
|
||||
}
|
||||
|
||||
public String getVariable() {
|
||||
return this.variable;
|
||||
}
|
||||
|
||||
public boolean matchesAll() {
|
||||
return this.matchAll;
|
||||
}
|
||||
|
||||
public Pattern getPattern() {
|
||||
return this.pattern;
|
||||
}
|
||||
|
||||
public Function<String, String> getFunction() {
|
||||
return this.function;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue