xjmdoo commented on code in PR #24156:
URL: https://github.com/apache/flink/pull/24156#discussion_r1471695921


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -1174,6 +1174,112 @@ public static String toBase64(byte[] bytes) {
         return Base64.getEncoder().encodeToString(bytes);
     }
 
+    public static BinaryStringData jsonQuote(BinaryStringData input) {
+        if (input == null) {
+            return null;
+        }
+
+        StringBuilder result = new StringBuilder("\"");
+
+        for (int i = 0; i < input.getSizeInBytes(); ) {
+            int codePoint = Character.codePointAt(input.toString(), i);
+            int charCount = Character.charCount(codePoint);
+
+            switch (codePoint) {
+                case '"':
+                    result.append("\\\"");
+                    break;
+                case '\\':
+                    result.append("\\\\");
+                    break;
+                case '\b':
+                    result.append("\\b");
+                    break;
+                case '\f':
+                    result.append("\\f");
+                    break;
+                case '\n':
+                    result.append("\\n");
+                    break;
+                case '\r':
+                    result.append("\\r");
+                    break;
+                case '\t':
+                    result.append("\\t");
+                    break;
+                default:
+                    result.append(Character.toChars(codePoint));

Review Comment:
   You could call `result.appendCodePoint(codePoint)` here to simplify to code.



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -1174,6 +1174,112 @@ public static String toBase64(byte[] bytes) {
         return Base64.getEncoder().encodeToString(bytes);
     }
 
+    public static BinaryStringData jsonQuote(BinaryStringData input) {
+        if (input == null) {
+            return null;
+        }
+
+        StringBuilder result = new StringBuilder("\"");
+
+        for (int i = 0; i < input.getSizeInBytes(); ) {
+            int codePoint = Character.codePointAt(input.toString(), i);
+            int charCount = Character.charCount(codePoint);

Review Comment:
   I think it would be better to do the conversion to String only once outside 
the for-loop. At that point you could use `str.length()` instead of 
`getSizeInBytes()`. Now that we have a String inside the loop it's easier to 
get the codepoint by `str.codePointAt(i)`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to