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 authored and tuchida committed May 8, 2015
1 parent 1ac0c75 commit a20f76d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/org/mozilla/javascript/NativeString.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,28 @@ public Object execIdCall(IdFunctionObject f, Context cx, Scriptable scope,
String str = ScriptRuntime.toString(requireObjectCoercible(thisObj, f));
double cnt = ScriptRuntime.toInteger(args, 0);

if (cnt == 0) {
return "";
}

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

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

StringBuilder retval = new StringBuilder((int) size);
retval.append(str);

int i = 1;
while (i <= cnt/2) {
retval.append(retval);
i *= 2;
}
while (i++ < cnt) retval.append(str);

return retval.toString();
}
case Id_codePointAt:
Expand Down

0 comments on commit a20f76d

Please sign in to comment.