From 58be04e2d5ae97d2e742db74cfbdb44b09ce48cf Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Thu, 4 Jul 2024 09:31:12 +0700 Subject: [PATCH] :sparkles: feat: add function for String4j #4 --- .../groovy/org/unify4j/common/String4j.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/plugin/src/main/groovy/org/unify4j/common/String4j.java b/plugin/src/main/groovy/org/unify4j/common/String4j.java index 3ad7799..8361c83 100644 --- a/plugin/src/main/groovy/org/unify4j/common/String4j.java +++ b/plugin/src/main/groovy/org/unify4j/common/String4j.java @@ -1126,4 +1126,31 @@ public static String createUtf8String(byte[] bytes) { public static byte[] getUTF8Bytes(String s) { return getBytes(s, "UTF-8"); } + + /** + * Repeats the given string a specified number of times. + * + * @param s the string to be repeated + * @param cnt the number of times to repeat the string + * @return a new string consisting of the original string repeated the specified number of times, + * or the original string if it is empty + * @throws IllegalArgumentException if the repeat count is negative + *

+ * This method suppresses the "StringRepeatCanBeUsed" warning as it manually implements the repeat logic + * to maintain compatibility with Java versions prior to 11 where the String::repeat method is not available. + */ + @SuppressWarnings({"StringRepeatCanBeUsed"}) + public static String repeat(String s, int cnt) { + if (isEmpty(s)) { + return s; + } + if (cnt < 0) { + throw new IllegalArgumentException("Repeat count must be non-negative"); + } + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < cnt; i++) { + builder.append(s); + } + return builder.toString(); + } }