SEPURI-SAI-KRISHNA commented on code in PR #29004:
URL: https://github.com/apache/flink/pull/29004#discussion_r3837408952


##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -345,29 +335,49 @@ public static String rpad(String base, int len, String 
pad) {
             return "";
         }
 
-        char[] data = new char[len];
-        char[] baseChars = base.toCharArray();
-        char[] padChars = pad.toCharArray();
+        // Lengths are counted in characters (code points) rather than UTF-16 
code units, so that a
+        // supplementary-plane character is never truncated into an unpaired 
surrogate.
+        final int keepEnd = endOfFirstCodePoints(base, len);
+        final int baseLen = base.codePointCount(0, keepEnd);
 
-        int pos = 0;
+        final StringBuilder sb = new StringBuilder(len);
 
-        // copy the base
-        while (pos < base.length() && pos < len) {
-            data[pos] = baseChars[pos];
-            pos += 1;
-        }
+        // copy the base shortened to len characters, then the padding
+        sb.append(base, 0, keepEnd);
+        appendPadding(sb, pad, len - baseLen);
 
-        // copy the padding
-        while (pos < len) {
-            int i = 0;
-            while (i < pad.length() && i < len - pos) {
-                data[pos + i] = padChars[i];
-                i += 1;
-            }
-            pos += pad.length();
+        return sb.toString();
+    }
+
+    /**
+     * Returns the index just past the first {@code count} characters (code 
points) of {@code str},
+     * or {@code str.length()} if it holds fewer than that. Scanning stops at 
{@code count} so that
+     * a base far longer than the requested length is not walked in full.

Review Comment:
   Fair. Cut to one line each.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to