Thanks! I'll simplify this check for overflow, as you're suggesting.

Sincerely yours,
Ivan

On 31.05.2015 10:01, Xueming Shen wrote:

On 5/30/2015 11:38 PM, Peter Levart wrote:


Hi,

Yes, this one is much easier to grasp.

As I understand the check is to avoid overflow in calculation of StringBuilder initial capacity (newLenHint). If overflow happened, newLenHint would be negative and StringBuilder construction would fail with NegativeArraySizeException. But the calculation of newLenHint:

    int newLenHint = value.length - targLen + replValue.length;

...considering the following:

    value.length >= 0
    targLength >= 0
    replValue.length >= 0
    targLength <= value.length

in case of overflow, it can only produce a negative value. So the check could simply be:

    if (newLenHint < 0) {
        throw new OutOfMemoryError();
    }

Right?

Regards, Peter


agreed.


Reply via email to