From c7926c1f4daa5c40b7999e8429945f5daf392c34 Mon Sep 17 00:00:00 2001 From: monoid Date: Sun, 29 Jun 2025 16:21:51 +0900 Subject: [PATCH] refactor: optimize CharItem to handle special characters in toString method --- lib/src/main/kotlin/org/example/RegexItem.kt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/src/main/kotlin/org/example/RegexItem.kt b/lib/src/main/kotlin/org/example/RegexItem.kt index 3e0dd77..8f1991e 100644 --- a/lib/src/main/kotlin/org/example/RegexItem.kt +++ b/lib/src/main/kotlin/org/example/RegexItem.kt @@ -76,12 +76,11 @@ class AndThenItem(val left: RegexItem, val right: RegexItem) : RegexItem { } class CharItem(val value: String) : RegexItem { + companion object { + private val META_CHARS = setOf("\\", "+", "*", "?", ".", "(", ")", "|", "[", "]", "^", "$") + } override fun toString(): String = - // escape 특수 문자를 처리하여 출력 - when (value) { - "\\", "+", "*", "?", ".", "(", ")", "|", "[", "]" -> "\\$value" - else -> value // 일반 문자 그대로 반환 - } + if (value in META_CHARS) "\\$value" else value override fun findMatch(str: String, position: Int): AvailableState { return when {