From 74ebb0bec0ba38345636e53c31073c4bc3de1c9e Mon Sep 17 00:00:00 2001 From: PiggyPiglet Date: Fri, 1 Jul 2022 23:31:51 +0800 Subject: [PATCH] remove regex replacer & associated tests --- .../replacer/RegexReplacer.java | 72 ------------------- .../java/me/clip/placeholderapi/Values.java | 2 - .../replacer/ReplacerBenchmarks.java | 10 --- .../replacer/ReplacerUnitTester.java | 28 -------- 4 files changed, 112 deletions(-) delete mode 100644 src/main/java/me/clip/placeholderapi/replacer/RegexReplacer.java diff --git a/src/main/java/me/clip/placeholderapi/replacer/RegexReplacer.java b/src/main/java/me/clip/placeholderapi/replacer/RegexReplacer.java deleted file mode 100644 index b9e1261..0000000 --- a/src/main/java/me/clip/placeholderapi/replacer/RegexReplacer.java +++ /dev/null @@ -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 . - */ - -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((?[a-zA-Z0-9]+)_)(?[^%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 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()); - } - -} diff --git a/src/test/java/me/clip/placeholderapi/Values.java b/src/test/java/me/clip/placeholderapi/Values.java index da23a34..91c91f6 100644 --- a/src/test/java/me/clip/placeholderapi/Values.java +++ b/src/test/java/me/clip/placeholderapi/Values.java @@ -23,7 +23,6 @@ package me.clip.placeholderapi; import com.google.common.collect.ImmutableMap; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.replacer.CharsReplacer; -import me.clip.placeholderapi.replacer.RegexReplacer; import me.clip.placeholderapi.replacer.Replacer; import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.NotNull; @@ -40,7 +39,6 @@ public interface Values { Replacer CHARS_REPLACER = new CharsReplacer(Replacer.Closure.PERCENT); - Replacer REGEX_REPLACER = new RegexReplacer(Replacer.Closure.PERCENT); final class MockPlayerPlaceholderExpansion extends PlaceholderExpansion { diff --git a/src/test/java/me/clip/placeholderapi/replacer/ReplacerBenchmarks.java b/src/test/java/me/clip/placeholderapi/replacer/ReplacerBenchmarks.java index cec02bf..a824318 100644 --- a/src/test/java/me/clip/placeholderapi/replacer/ReplacerBenchmarks.java +++ b/src/test/java/me/clip/placeholderapi/replacer/ReplacerBenchmarks.java @@ -30,19 +30,9 @@ public class ReplacerBenchmarks { 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 public void measureCharsReplacerLargeText() { 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); - } - } \ No newline at end of file diff --git a/src/test/java/me/clip/placeholderapi/replacer/ReplacerUnitTester.java b/src/test/java/me/clip/placeholderapi/replacer/ReplacerUnitTester.java index 30b7f5c..4924a7e 100644 --- a/src/test/java/me/clip/placeholderapi/replacer/ReplacerUnitTester.java +++ b/src/test/java/me/clip/placeholderapi/replacer/ReplacerUnitTester.java @@ -37,12 +37,6 @@ public final class ReplacerUnitTester { 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 void testCharsReplacerProducesExpectedSentence() { assertEquals(String.format( @@ -51,36 +45,14 @@ public final class ReplacerUnitTester { 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 void testResultsAreTheSameAsReplacement() { final String resultChars = Values.CHARS_REPLACER .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); } - @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 void testCharsReplacerIgnoresMalformed() { final String text = "10% and %hello world 15%";