Skip to content

Commit

Permalink
minor perf optimize #475
Browse files Browse the repository at this point in the history
  • Loading branch information
tminglei committed Mar 20, 2020
1 parent 7ff6365 commit b750d71
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit b750d71

Please sign in to comment.