mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-09 12:30:15 +01:00
Improve DurationParser regex
This commit is contained in:
parent
96c53091d0
commit
0e64b61be4
@ -73,12 +73,38 @@ public enum DurationFormatter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
DurationFormatter(int accuracy) {
|
DurationFormatter(int accuracy) {
|
||||||
if (accuracy <= 0) {
|
|
||||||
throw new IllegalArgumentException("accuracy must be >= 1");
|
|
||||||
}
|
|
||||||
this.accuracy = accuracy;
|
this.accuracy = accuracy;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats {@code duration} as a string.
|
||||||
|
*
|
||||||
|
* @param duration the duration
|
||||||
|
* @return the formatted string
|
||||||
|
*/
|
||||||
|
public String format(Duration duration) {
|
||||||
|
long seconds = duration.getSeconds();
|
||||||
|
StringBuilder output = new StringBuilder();
|
||||||
|
int outputSize = 0;
|
||||||
|
|
||||||
|
for (Unit unit : this.units) {
|
||||||
|
long n = seconds / unit.duration;
|
||||||
|
if (n > 0) {
|
||||||
|
seconds -= unit.duration * n;
|
||||||
|
output.append(' ').append(n).append(unit.toString(n));
|
||||||
|
outputSize++;
|
||||||
|
}
|
||||||
|
if (seconds <= 0 || outputSize >= this.accuracy) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (output.length() == 0) {
|
||||||
|
return "0" + this.units[this.units.length - 1].stringPlural;
|
||||||
|
}
|
||||||
|
return output.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
protected String formatUnitPlural(ChronoUnit unit) {
|
protected String formatUnitPlural(ChronoUnit unit) {
|
||||||
return " " + unit.name().toLowerCase();
|
return " " + unit.name().toLowerCase();
|
||||||
}
|
}
|
||||||
@ -104,30 +130,4 @@ public enum DurationFormatter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String format(long seconds) {
|
|
||||||
StringBuilder output = new StringBuilder();
|
|
||||||
int outputSize = 0;
|
|
||||||
|
|
||||||
for (Unit unit : this.units) {
|
|
||||||
long n = seconds / unit.duration;
|
|
||||||
if (n > 0) {
|
|
||||||
seconds -= unit.duration * n;
|
|
||||||
output.append(' ').append(n).append(unit.toString(n));
|
|
||||||
outputSize++;
|
|
||||||
}
|
|
||||||
if (seconds <= 0 || outputSize >= this.accuracy) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (output.length() == 0) {
|
|
||||||
return "0" + this.units[this.units.length - 1].stringPlural;
|
|
||||||
}
|
|
||||||
return output.substring(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String format(Duration duration) {
|
|
||||||
return format(duration.getSeconds());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,9 +25,11 @@
|
|||||||
|
|
||||||
package me.lucko.luckperms.common.util;
|
package me.lucko.luckperms.common.util;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Arrays;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -38,28 +40,21 @@ import java.util.stream.Collectors;
|
|||||||
public final class DurationParser {
|
public final class DurationParser {
|
||||||
private DurationParser() {}
|
private DurationParser() {}
|
||||||
|
|
||||||
private static final ChronoUnit[] UNITS = new ChronoUnit[]{
|
private static final Map<ChronoUnit, String> UNITS_PATTERNS = ImmutableMap.<ChronoUnit, String>builder()
|
||||||
ChronoUnit.YEARS,
|
.put(ChronoUnit.YEARS, "y(?:ear)?s?")
|
||||||
ChronoUnit.MONTHS,
|
.put(ChronoUnit.MONTHS, "mo(?:nth)?s?")
|
||||||
ChronoUnit.WEEKS,
|
.put(ChronoUnit.WEEKS, "w(?:eek)?s?")
|
||||||
ChronoUnit.DAYS,
|
.put(ChronoUnit.DAYS, "d(?:ay)?s?")
|
||||||
ChronoUnit.HOURS,
|
.put(ChronoUnit.HOURS, "h(?:our|r)?s?")
|
||||||
ChronoUnit.MINUTES,
|
.put(ChronoUnit.MINUTES, "m(?:inute|in)?s?")
|
||||||
ChronoUnit.SECONDS
|
.put(ChronoUnit.SECONDS, "(?:s(?:econd|ec)?s?)?")
|
||||||
};
|
.build();
|
||||||
|
|
||||||
private static final String PATTERN_STRING = Arrays.stream(UNITS)
|
private static final ChronoUnit[] UNITS = UNITS_PATTERNS.keySet().toArray(new ChronoUnit[0]);
|
||||||
.limit(UNITS.length - 1) // skip seconds
|
|
||||||
.map(unit -> {
|
private static final String PATTERN_STRING = UNITS_PATTERNS.values().stream()
|
||||||
if (unit == ChronoUnit.MONTHS) {
|
.map(pattern -> "(?:(\\d+)\\s*" + pattern + "[,\\s]*)?")
|
||||||
return "mo";
|
.collect(Collectors.joining());
|
||||||
}
|
|
||||||
// use the first char as the abbreviation
|
|
||||||
return String.valueOf(Character.toLowerCase(unit.name().charAt(0)));
|
|
||||||
})
|
|
||||||
.map(abbreviation -> "(?:([0-9]+)\\s*" + abbreviation + "[a-z]*[,\\s]*)?")
|
|
||||||
.collect(Collectors.joining())
|
|
||||||
.concat("(?:([0-9]+)\\s*(?:s[a-z]*)?)?");
|
|
||||||
|
|
||||||
private static final Pattern PATTERN = Pattern.compile(PATTERN_STRING, Pattern.CASE_INSENSITIVE);
|
private static final Pattern PATTERN = Pattern.compile(PATTERN_STRING, Pattern.CASE_INSENSITIVE);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user