From e376eb3bd1ee29cca8273cb43ba96fdf652951b7 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Wed, 29 Apr 2026 15:34:12 +0900 Subject: [PATCH] =?UTF-8?q?260429=5F01=5FENH:=20StringUtil=EC=97=90=20esca?= =?UTF-8?q?pe=20=EB=A9=94=EC=84=9C=EB=93=9C=EB=93=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../kri/common/feature/util/StringUtil.java | 112 ++++++++++++++++++ 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 4a97d43..3beb939 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } //버전 변경은 여기서 -def commonVer = "beta.1.1.18" +def commonVer = "beta.1.1.19" group = 'com.kri.common' version = commonVer diff --git a/src/main/java/com/kri/common/feature/util/StringUtil.java b/src/main/java/com/kri/common/feature/util/StringUtil.java index a782662..cf05df7 100644 --- a/src/main/java/com/kri/common/feature/util/StringUtil.java +++ b/src/main/java/com/kri/common/feature/util/StringUtil.java @@ -67,4 +67,116 @@ public final class StringUtil { } return s.matches("^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$"); } + + public static String escapeUnsafeText(String value) { + if (value == null) { + return null; + } + + StringBuilder sb = new StringBuilder(value.length()); + + for (int i = 0; i < value.length(); ) { + int cp = value.codePointAt(i); + i += Character.charCount(cp); + + if (cp == '\t' || cp == '\n' || cp == '\r') { + sb.appendCodePoint(cp); + continue; + } + + if (Character.isISOControl(cp)) { + sb.append("\\u") + .append(String.format("%04X", cp)); + continue; + } + + sb.appendCodePoint(cp); + } + + return sb.toString(); + } + + public static String escapeHtml(String value) { + if (value == null) { + return null; + } + + StringBuilder sb = new StringBuilder(value.length()); + + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + switch (c) { + case '&' -> sb.append("&"); + case '<' -> sb.append("<"); + case '>' -> sb.append(">"); + case '"' -> sb.append("""); + case '\'' -> sb.append("'"); + case '/' -> sb.append("/"); + default -> sb.append(c); + } + } + + return sb.toString(); + } + + public static String escapeJson(String value) { + if (value == null) { + return null; + } + + StringBuilder sb = new StringBuilder(value.length() + 16); + + for (int i = 0; i < value.length(); ) { + int cp = value.codePointAt(i); + i += Character.charCount(cp); + + switch (cp) { + case '"' -> sb.append("\\\""); + case '\\' -> sb.append("\\\\"); + case '\b' -> sb.append("\\b"); + case '\f' -> sb.append("\\f"); + case '\n' -> sb.append("\\n"); + case '\r' -> sb.append("\\r"); + case '\t' -> sb.append("\\t"); + default -> { + if (cp < 0x20) { + sb.append("\\u") + .append(String.format("%04X", cp)); + } else { + sb.appendCodePoint(cp); + } + } + } + } + + return sb.toString(); + } + + public static String escapeCsv(String value) { + if (value == null) { + return null; + } + + boolean needsQuote = value.contains(",") + || value.contains("\"") + || value.contains("\n") + || value.contains("\r"); + + String escaped = value.replace("\"", "\"\""); + + return needsQuote ? "\"" + escaped + "\"" : escaped; + } + + public static String escapeExcelFormula(String value) { + if (value == null || value.isEmpty()) { + return value; + } + + char first = value.charAt(0); + if (first == '=' || first == '+' || first == '-' || first == '@') { + return "'" + value; + } + + return value; + } }