justinborromeo commented on a change in pull request #7388: Support LPAD and 
RPAD sql function
URL: https://github.com/apache/incubator-druid/pull/7388#discussion_r273715180
 
 

 ##########
 File path: 
core/src/main/java/org/apache/druid/java/util/common/StringUtils.java
 ##########
 @@ -360,4 +360,93 @@ public static String encodeBase64String(byte[] input)
   {
     return BASE64_DECODER.decode(input);
   }
+
+  /**
+   * Returns the string left-padded with the string pad to a length of len 
characters.
+   * If str is longer than len, the return value is shortened to len 
characters. 
+   *
+   * @param base The base string to be padded
+   * @param len The length of padded string
+   * @param pad The pad string
+   * @return the string left-padded with pad to a lenght of len
+   */
+  public static String lpad(String base, Integer len, String pad)
+  {
+    if (len < 0) {
+      return null;
+    } else if (len == 0) {
+      return "";
+    }
+
+    char[] data = new char[len];
+    char[] baseChars = base.toCharArray();
+    char[] padChars = pad.toCharArray();
+
+    // The length of the padding needed
+    int pos = Math.max(len - base.length(), 0);
+
+    // Copy the padding
 
 Review comment:
   For the sake of conciseness, can this be shortened to use for loops?
   
   ```
       // Copy the padding
       for (int i = 0; i < pos; i += pad.length()) {
         for (int j = 0; j < pad.length() && j < pos - i; j++) {
           data[i + j] = padChars[j];
         }
       }
   
       // Copy the base
       for (int i = 0; pos + i < len && i < base.length(); i++) {
         data[pos + i] = baseChars[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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to