Skip to content

Commit

Permalink
✨ feat: add function for String4j #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jul 4, 2024
1 parent 549b38f commit 58be04e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions plugin/src/main/groovy/org/unify4j/common/String4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <p>
* 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();
}
}

0 comments on commit 58be04e

Please sign in to comment.