You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2019/03/24 06:16:18 UTC

[GitHub] [incubator-druid] asdf2014 commented on a change in pull request #7334: Add "REVERSE" / "REPEAT" / "RIGHT" / "LEFT" functions

asdf2014 commented on a change in pull request #7334: Add "REVERSE" / "REPEAT" / "RIGHT" / "LEFT" functions
URL: https://github.com/apache/incubator-druid/pull/7334#discussion_r268419709
 
 

 ##########
 File path: core/src/main/java/org/apache/druid/java/util/common/StringUtils.java
 ##########
 @@ -360,4 +361,54 @@ public static String encodeBase64String(byte[] input)
   {
     return BASE64_DECODER.decode(input);
   }
+
+  /**
+   * Returns a string whose value is the concatenation of the
+   * string {@code str} repeated {@code count} times.
+   * <p>
+   * If count or length is zero then the empty string is returned.
+   * <p>
+   * This method may be used to create space padding for
+   * formatting text or zero padding for formatting numbers.
+   *
+   * @param count number of times to repeat
+   *
+   * @return A string composed of this string repeated
+   * {@code count} times or the empty string if count
+   * or length is zero.
+   *
+   * @throws IllegalArgumentException if the {@code count} is negative.
+   * @link https://bugs.openjdk.java.net/browse/JDK-8197594
+   */
+  public static String repeat(String s, int count)
+  {
+    if (count < 0) {
+      throw new IllegalArgumentException("count is negative, " + count);
+    }
+    if (count == 1) {
+      return s;
+    }
+    byte[] value = s.getBytes(StandardCharsets.UTF_8);
+    final int len = value.length;
+    if (len == 0 || count == 0) {
+      return "";
+    }
+    if (len == 1) {
+      final byte[] single = new byte[count];
+      Arrays.fill(single, value[0]);
+      return new String(single, StandardCharsets.UTF_8);
+    }
+    if (Integer.MAX_VALUE / count < len) {
+      throw new OutOfMemoryError();
 
 Review comment:
   @egor-ryashin Thanks, fixed.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org