From 498e6dc6485541c049183cfcbf3fffac8716583d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 7 Mar 2026 06:46:56 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Optimize=20Regex=20compilation=20in?= =?UTF-8?q?=20extractJsonData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: EchoOfMaridia <71672129+EchoOfMaridia@users.noreply.github.com> --- src/main/kotlin/Util/Util.kt | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/Util/Util.kt b/src/main/kotlin/Util/Util.kt index bb75494..b0746ad 100644 --- a/src/main/kotlin/Util/Util.kt +++ b/src/main/kotlin/Util/Util.kt @@ -469,6 +469,19 @@ inline fun templateBasedReconstruction(text: String): T? } +// Pre-compiled patterns for extracting key-value pairs from malformed JSON +private val jsonExtractionPatterns = listOf( + // "key": "value" or 'key': 'value' + Regex("\"([^\"]+)\"\\s*:\\s*\"([^\"]*(?:\\\\.[^\"]*)*)\""), + Regex("'([^']+)'\\s*:\\s*'([^']*(?:\\\\.[^']*)*)'"), + // "key": value (unquoted values) + Regex("\"([^\"]+)\"\\s*:\\s*([^,}\\]\\n]+?)(?=[,}\\]\\n]|$)"), + // key: "value" (unquoted keys) + Regex("([a-zA-Z_][\\w\\s-]*)\\s*:\\s*\"([^\"]*(?:\\\\.[^\"]*)*)\""), + // key: value (both unquoted) + Regex("([a-zA-Z_][\\w\\s-]*)\\s*:\\s*([^,}\\]\\n]+?)(?=[,}\\]\\n]|$)") +) + /** * Extracts key-value pairs from malformed JSON using regex patterns as fallback. * Returns a Map that can be manually processed when standard JSON parsing fails. @@ -477,20 +490,7 @@ fun extractJsonData(malformedJson: String): Map { val result = mutableMapOf() - // More comprehensive pattern for key-value pairs - val patterns = listOf( - // "key": "value" or 'key': 'value' - Regex("\"([^\"]+)\"\\s*:\\s*\"([^\"]*(?:\\\\.[^\"]*)*)\""), - Regex("'([^']+)'\\s*:\\s*'([^']*(?:\\\\.[^']*)*)'"), - // "key": value (unquoted values) - Regex("\"([^\"]+)\"\\s*:\\s*([^,}\\]\\n]+?)(?=[,}\\]\\n]|$)"), - // key: "value" (unquoted keys) - Regex("([a-zA-Z_][\\w\\s-]*)\\s*:\\s*\"([^\"]*(?:\\\\.[^\"]*)*)\""), - // key: value (both unquoted) - Regex("([a-zA-Z_][\\w\\s-]*)\\s*:\\s*([^,}\\]\\n]+?)(?=[,}\\]\\n]|$)") - ) - - for(pattern in patterns) + for(pattern in jsonExtractionPatterns) { pattern.findAll(malformedJson).forEach { match -> val key = match.groupValues[1].trim()