From b750d712c42c5c9ba1443f2ec873cb7e227c5bc5 Mon Sep 17 00:00:00 2001 From: tuminglei Date: Fri, 20 Mar 2020 09:51:31 +0800 Subject: [PATCH] minor perf optimize #475 --- .../tminglei/slickpg/utils/JsonUtils.scala | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/com/github/tminglei/slickpg/utils/JsonUtils.scala b/core/src/main/scala/com/github/tminglei/slickpg/utils/JsonUtils.scala index c39acc3d..026029bb 100644 --- a/core/src/main/scala/com/github/tminglei/slickpg/utils/JsonUtils.scala +++ b/core/src/main/scala/com/github/tminglei/slickpg/utils/JsonUtils.scala @@ -1,11 +1,21 @@ package com.github.tminglei.slickpg.utils +import java.util.regex.Pattern + object JsonUtils { - // remove ctrl char `\u0000` which will fail json parsers, but keep normal like `\\u0000` + + private val CLEAN_PATTERNS = List( + (Pattern.compile("\u0000", Pattern.LITERAL), ""), + (Pattern.compile("(\\\\\\\\)"), "_/_$1__"), // replace to protect `\\u0000` + (Pattern.compile("\\u0000", Pattern.LITERAL), ""), + (Pattern.compile("_/_(\\\\\\\\)__"), "$1") // replace back `\\u0000` + ) + /** remove ctrl char `\u0000` which will fail json parsers, but keep normal like `\\u0000` */ def clean(str: String): String = { - str.replace("\u0000", "") - .replaceAll("(\\\\\\\\)", "_/_$1__") // replace to protect `\\u0000` - .replace("\\u0000", "") - .replaceAll("_/_(\\\\\\\\)__", "$1") // replace back `\\u0000` + var ret = str + for (p <- CLEAN_PATTERNS) { + ret = p._1.matcher(ret).replaceAll(p._2) + } + ret } }