8058779: Faster implementation of String.replace(CharSequence, CharSequence)

Xueming Shen xueming.shen at oracle.com
Sun May 31 07:01:37 UTC 2015


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.




More information about the core-libs-dev mailing list