Skip to content

Commit

Permalink
Create repeat() in String.java (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
eklaDFF authored May 24, 2024
1 parent e5d88e4 commit 77befe2
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/classes/modules/java.base/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,24 @@ private static byte[] encodeWithEncoder(Charset cs, byte coder, byte[] val, bool
return safeTrim(ba, bb.position(), cs.getClass().getClassLoader() == null);
}

public String repeat(int count){
if(count < 0){
throw new IllegalArgumentException("count is negative : " + count);
}else if(count == 0){
return "";
}else {
int length = value.length;
int newLength = length * count;
final byte[] newString = new byte[newLength];
int i = 0;
while (i<newLength){
newString[i] = value[(i%length)];
i++;
}
return new String(newString,coder);
}
}

private static byte[] safeTrim(byte[] ba, int len, boolean isTrusted) {
if (len == ba.length && (isTrusted || System.getSecurityManager() == null)) {
return ba;
Expand Down

0 comments on commit 77befe2

Please sign in to comment.