Expressions that are just numbers will be immediately converted rather than being evaluated

This commit is contained in:
Auxilor
2023-06-18 21:11:14 +02:00
parent c85b8c08c7
commit 245b577adc
2 changed files with 8 additions and 5 deletions

View File

@@ -15,6 +15,8 @@ class DelegatedExpressionHandler(
.build()
override fun evaluate(expression: String, context: PlaceholderContext): Double? {
expression.fastToDoubleOrNull()?.let { return it }
// Peak performance (totally not having fun with bitwise operators)
val hash = (((expression.hashCode() shl 5) - expression.hashCode()) xor
(context.player?.uniqueId?.hashCode() ?: 0)

View File

@@ -26,8 +26,10 @@ interface ExpressionHandler {
fun evaluate(expression: String, context: PlaceholderContext): Double?
}
private fun String.fastToDoubleOrNull(): Double? {
if (isEmpty()) {
fun String.fastToDoubleOrNull(): Double? {
val len = length
if (len == 0) {
return null
}
@@ -39,8 +41,7 @@ private fun String.fastToDoubleOrNull(): Double? {
var decimalPart = 0.0
var decimalIdx = -1
while (idx < length) {
while (idx < len) {
when (val char = this[idx]) {
'.' -> {
if (decimalIdx != -1) return null
@@ -62,7 +63,7 @@ private fun String.fastToDoubleOrNull(): Double? {
idx++
}
decimalPart /= 10.0.pow((length - decimalIdx - 1).toDouble())
decimalPart /= 10.0.pow((len - decimalIdx - 1).toDouble())
return if (isNegative) -(integerPart + decimalPart) else integerPart + decimalPart
}