From 0496b623e6c6be648169767fd193b43ebdf3c8e7 Mon Sep 17 00:00:00 2001 From: DragonKnightOfBreeze Date: Wed, 11 Sep 2024 11:49:42 +0800 Subject: [PATCH] =?UTF-8?q?1.3.21=20=E4=BC=98=E5=8C=96=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E4=B8=8E=E5=86=85=E5=AD=98=E5=8D=A0=E7=94=A8=20-=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=90=84=E7=A7=8D=E8=B7=AF=E5=BE=84=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../windea/pls/model/ParadoxExpressionPath.kt | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/icu/windea/pls/model/ParadoxExpressionPath.kt b/src/main/kotlin/icu/windea/pls/model/ParadoxExpressionPath.kt index d0b22a18f..f8c5a31f3 100644 --- a/src/main/kotlin/icu/windea/pls/model/ParadoxExpressionPath.kt +++ b/src/main/kotlin/icu/windea/pls/model/ParadoxExpressionPath.kt @@ -86,41 +86,35 @@ fun ParadoxExpressionPath.matchEntire(other: List, ignoreCase: Boolean = private fun doResolve(originalPath: String): ParadoxExpressionPath { if(originalPath.isEmpty()) return EmptyParadoxExpressionPath - return ParadoxExpressionPathImpl(originalPath) + val mayBeQuoted = originalPath.contains('"') + if(!mayBeQuoted) return ParadoxExpressionPathImpl.Unquoted(originalPath) + return ParadoxExpressionPathImpl.Default(originalPath) } private fun doResolve(originalSubPaths: List): ParadoxExpressionPath { if(originalSubPaths.isEmpty()) return EmptyParadoxExpressionPath - return ParadoxExpressionPathImpl(originalSubPaths) + val mayBeQuoted = originalSubPaths.any { it.contains('"') } + if(!mayBeQuoted) return ParadoxExpressionPathImpl.Unquoted(originalSubPaths) + return ParadoxExpressionPathImpl.Default(originalSubPaths) } -//to optimize memory, it's better to use cache, or make property 'path' and 'originalPath' computed - -//12 + 4 * 4 = 28 -> 32 -private class ParadoxExpressionPathImpl : ParadoxExpressionPath { - override val path: String - override val subPaths: List - override val originalPath: String - override val originalSubPaths: List +//12 + 4 * 2 = 20 -> 24 +private abstract class ParadoxExpressionPathImpl : ParadoxExpressionPath { + final override val originalPath: String + final override val originalSubPaths: List override val length: Int get() = subPaths.size constructor(originalPath: String) { this.originalPath = originalPath this.originalSubPaths = path2SubPaths(originalPath) - val mayBeQuoted = originalPath.contains('"') - this.subPaths = if(mayBeQuoted) originalSubPaths.map { it.unquote() } else originalSubPaths - this.path = if(mayBeQuoted) subPaths2Path(subPaths) else originalPath } constructor(originalSubPaths: List) { this.originalPath = subPaths2Path(originalSubPaths) this.originalSubPaths = originalSubPaths - val mayBeQuoted = originalPath.contains('"') - this.subPaths = if(mayBeQuoted) originalSubPaths.map { it.unquote() } else originalSubPaths - this.path = if(mayBeQuoted) subPaths2Path(subPaths) else originalPath } - private fun path2SubPaths(path: String): List { + protected fun path2SubPaths(path: String): List { return buildList { val builder = StringBuilder() var escape = false @@ -143,7 +137,7 @@ private class ParadoxExpressionPathImpl : ParadoxExpressionPath { } } - private fun subPaths2Path(subPaths: List): String { + protected fun subPaths2Path(subPaths: List): String { val builder = StringBuilder() var isFirst = true subPaths.forEach { p -> @@ -159,6 +153,26 @@ private class ParadoxExpressionPathImpl : ParadoxExpressionPath { override fun equals(other: Any?) = this === other || other is ParadoxExpressionPath && path == other.path override fun hashCode() = path.hashCode() override fun toString() = path + + //12 + 4 * 2 = 20 -> 24 + class Unquoted: ParadoxExpressionPathImpl { + override val path: String get() = originalPath + override val subPaths: List get() = originalSubPaths + + constructor(originalPath: String) : super(originalPath) + + constructor(originalSubPaths: List) : super(originalSubPaths) + } + + //12 + 4 * 4 = 28 -> 32 + class Default : ParadoxExpressionPathImpl { + override val subPaths: List = originalSubPaths.map { it.unquote() } + override val path: String = subPaths2Path(subPaths) + + constructor(originalPath: String) : super(originalPath) + + constructor(originalSubPaths: List) : super(originalSubPaths) + } } private object EmptyParadoxExpressionPath : ParadoxExpressionPath {