Skip to content

Commit

Permalink
set capacity for StringBuilder in String#repeat
Browse files Browse the repository at this point in the history
  • Loading branch information
uchida_t committed May 8, 2015
1 parent 1ac0c75 commit 90cb55d
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/org/mozilla/javascript/NativeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,12 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,

if (cnt < 0 || cnt == Double.POSITIVE_INFINITY) throw rangeError("Invalid count value");

StringBuilder retval = new StringBuilder("");
long size = str.length() * (long) cnt;
// Check for overflow
if (size >= Integer.MAX_VALUE) {
size = Integer.MAX_VALUE;
}
StringBuilder retval = new StringBuilder((int) size);
while (cnt-- > 0) retval.append(str);
return retval.toString();
}
Expand Down

0 comments on commit 90cb55d

Please sign in to comment.