mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2024-11-18 00:46:55 +01:00
Merge pull request #830 from montlikadani/improve-replacer-process
Remove regex replacer & associated tests
This commit is contained in:
commit
8b7a217796
@ -1,72 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is part of PlaceholderAPI
|
|
||||||
*
|
|
||||||
* PlaceholderAPI
|
|
||||||
* Copyright (c) 2015 - 2021 PlaceholderAPI Team
|
|
||||||
*
|
|
||||||
* PlaceholderAPI free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* PlaceholderAPI is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package me.clip.placeholderapi.replacer;
|
|
||||||
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.regex.Matcher;
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.OfflinePlayer;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
public final class RegexReplacer implements Replacer {
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
private final Pattern pattern;
|
|
||||||
|
|
||||||
public RegexReplacer(@NotNull final Closure closure) {
|
|
||||||
this.pattern = Pattern.compile(String
|
|
||||||
.format("\\%s((?<identifier>[a-zA-Z0-9]+)_)(?<parameters>[^%s%s]+)\\%s", closure.head,
|
|
||||||
closure.head, closure.tail, closure.tail));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Override
|
|
||||||
public String apply(@NotNull final String text, @Nullable final OfflinePlayer player,
|
|
||||||
@NotNull final Function<String, @Nullable PlaceholderExpansion> lookup) {
|
|
||||||
final Matcher matcher = pattern.matcher(text);
|
|
||||||
if (!matcher.find()) {
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
|
|
||||||
final StringBuffer builder = new StringBuffer();
|
|
||||||
|
|
||||||
do {
|
|
||||||
final String identifier = matcher.group("identifier");
|
|
||||||
final String parameters = matcher.group("parameters");
|
|
||||||
|
|
||||||
final PlaceholderExpansion expansion = lookup.apply(identifier);
|
|
||||||
if (expansion == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
final String requested = expansion.onRequest(player, parameters);
|
|
||||||
matcher.appendReplacement(builder, requested != null ? requested : matcher.group(0));
|
|
||||||
}
|
|
||||||
while (matcher.find());
|
|
||||||
|
|
||||||
return ChatColor.translateAlternateColorCodes('&', matcher.appendTail(builder).toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -23,7 +23,6 @@ package me.clip.placeholderapi;
|
|||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
import me.clip.placeholderapi.replacer.CharsReplacer;
|
import me.clip.placeholderapi.replacer.CharsReplacer;
|
||||||
import me.clip.placeholderapi.replacer.RegexReplacer;
|
|
||||||
import me.clip.placeholderapi.replacer.Replacer;
|
import me.clip.placeholderapi.replacer.Replacer;
|
||||||
import org.bukkit.OfflinePlayer;
|
import org.bukkit.OfflinePlayer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@ -40,7 +39,6 @@ public interface Values {
|
|||||||
|
|
||||||
|
|
||||||
Replacer CHARS_REPLACER = new CharsReplacer(Replacer.Closure.PERCENT);
|
Replacer CHARS_REPLACER = new CharsReplacer(Replacer.Closure.PERCENT);
|
||||||
Replacer REGEX_REPLACER = new RegexReplacer(Replacer.Closure.PERCENT);
|
|
||||||
|
|
||||||
|
|
||||||
final class MockPlayerPlaceholderExpansion extends PlaceholderExpansion {
|
final class MockPlayerPlaceholderExpansion extends PlaceholderExpansion {
|
||||||
|
@ -30,19 +30,9 @@ public class ReplacerBenchmarks {
|
|||||||
Values.CHARS_REPLACER.apply(Values.SMALL_TEXT, null, Values.PLACEHOLDERS::get);
|
Values.CHARS_REPLACER.apply(Values.SMALL_TEXT, null, Values.PLACEHOLDERS::get);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
public void measureRegexReplacerSmallText() {
|
|
||||||
Values.REGEX_REPLACER.apply(Values.SMALL_TEXT, null, Values.PLACEHOLDERS::get);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public void measureCharsReplacerLargeText() {
|
public void measureCharsReplacerLargeText() {
|
||||||
Values.CHARS_REPLACER.apply(Values.LARGE_TEXT, null, Values.PLACEHOLDERS::get);
|
Values.CHARS_REPLACER.apply(Values.LARGE_TEXT, null, Values.PLACEHOLDERS::get);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
|
||||||
public void measureRegexReplacerLargeText() {
|
|
||||||
Values.REGEX_REPLACER.apply(Values.LARGE_TEXT, null, Values.PLACEHOLDERS::get);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -37,12 +37,6 @@ public final class ReplacerUnitTester {
|
|||||||
Values.CHARS_REPLACER.apply("%player_name%", null, Values.PLACEHOLDERS::get));
|
Values.CHARS_REPLACER.apply("%player_name%", null, Values.PLACEHOLDERS::get));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRegexReplacerProducesExpectedSingleValue() {
|
|
||||||
assertEquals(PLAYER_NAME,
|
|
||||||
Values.REGEX_REPLACER.apply("%player_name%", null, Values.PLACEHOLDERS::get));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCharsReplacerProducesExpectedSentence() {
|
void testCharsReplacerProducesExpectedSentence() {
|
||||||
assertEquals(String.format(
|
assertEquals(String.format(
|
||||||
@ -51,36 +45,14 @@ public final class ReplacerUnitTester {
|
|||||||
Values.CHARS_REPLACER.apply(Values.LARGE_TEXT, null, Values.PLACEHOLDERS::get));
|
Values.CHARS_REPLACER.apply(Values.LARGE_TEXT, null, Values.PLACEHOLDERS::get));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void testRegexReplacerProducesExpectedSentence() {
|
|
||||||
assertEquals(String.format(
|
|
||||||
"My name is %s and my location is (%s, %s, %s), this placeholder is invalid %%server_name%%",
|
|
||||||
PLAYER_NAME, PLAYER_X, PLAYER_Y, PLAYER_Z),
|
|
||||||
Values.REGEX_REPLACER.apply(Values.LARGE_TEXT, null, Values.PLACEHOLDERS::get));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testResultsAreTheSameAsReplacement() {
|
void testResultsAreTheSameAsReplacement() {
|
||||||
final String resultChars = Values.CHARS_REPLACER
|
final String resultChars = Values.CHARS_REPLACER
|
||||||
.apply("%player_name%", null, Values.PLACEHOLDERS::get);
|
.apply("%player_name%", null, Values.PLACEHOLDERS::get);
|
||||||
final String resultRegex = Values.REGEX_REPLACER
|
|
||||||
.apply("%player_name%", null, Values.PLACEHOLDERS::get);
|
|
||||||
|
|
||||||
assertEquals(resultChars, resultRegex);
|
|
||||||
|
|
||||||
assertEquals(PLAYER_NAME, resultChars);
|
assertEquals(PLAYER_NAME, resultChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void testResultsAreTheSameNoReplacement() {
|
|
||||||
final String resultChars = Values.CHARS_REPLACER
|
|
||||||
.apply("%player_location%", null, Values.PLACEHOLDERS::get);
|
|
||||||
final String resultRegex = Values.REGEX_REPLACER
|
|
||||||
.apply("%player_location%", null, Values.PLACEHOLDERS::get);
|
|
||||||
|
|
||||||
assertEquals(resultChars, resultRegex);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testCharsReplacerIgnoresMalformed() {
|
void testCharsReplacerIgnoresMalformed() {
|
||||||
final String text = "10% and %hello world 15%";
|
final String text = "10% and %hello world 15%";
|
||||||
|
Loading…
Reference in New Issue
Block a user