@@ -3202,6 +3202,9 @@ public String replaceAll(String regex, String substitute) {
3202
3202
int length = lengthInternal ();
3203
3203
if (substituteLength < 2 ) {
3204
3204
if (COMPACT_STRINGS && isCompressed () && (substituteLength == 0 || substitute .isCompressed ())) {
3205
+ if (!regex .isCompressed ()) {
3206
+ return this ;
3207
+ }
3205
3208
byte [] newChars = new byte [length ];
3206
3209
byte toReplace = helpers .getByteFromArrayByIndex (regex .value , 0 );
3207
3210
byte replacement = (byte )0 ; // assign dummy value that isn't used
@@ -3210,14 +3213,21 @@ public String replaceAll(String regex, String substitute) {
3210
3213
checkLastChar ((char )replacement );
3211
3214
}
3212
3215
int newCharIndex = 0 ;
3216
+ boolean replaced = false ;
3213
3217
for (int i = 0 ; i < length ; ++i ) {
3214
3218
byte current = helpers .getByteFromArrayByIndex (value , i );
3215
3219
if (current != toReplace ) {
3216
3220
helpers .putByteInArrayByIndex (newChars , newCharIndex ++, current );
3217
- } else if (substituteLength == 1 ) {
3218
- helpers .putByteInArrayByIndex (newChars , newCharIndex ++, replacement );
3221
+ } else {
3222
+ replaced = true ;
3223
+ if (substituteLength == 1 ) {
3224
+ helpers .putByteInArrayByIndex (newChars , newCharIndex ++, replacement );
3225
+ }
3219
3226
}
3220
3227
}
3228
+ if (!replaced ) {
3229
+ return this ;
3230
+ }
3221
3231
return new String (newChars , 0 , newCharIndex , true );
3222
3232
} else if (!COMPACT_STRINGS || !isCompressed ()) {
3223
3233
byte [] newChars = StringUTF16 .newBytesFor (length );
@@ -3228,14 +3238,21 @@ public String replaceAll(String regex, String substitute) {
3228
3238
checkLastChar (replacement );
3229
3239
}
3230
3240
int newCharIndex = 0 ;
3241
+ boolean replaced = false ;
3231
3242
for (int i = 0 ; i < length ; ++i ) {
3232
3243
char current = helpers .getCharFromArrayByIndex (value , i );
3233
3244
if (current != toReplace ) {
3234
3245
helpers .putCharInArrayByIndex (newChars , newCharIndex ++, current );
3235
- } else if (substituteLength == 1 ) {
3236
- helpers .putCharInArrayByIndex (newChars , newCharIndex ++, replacement );
3246
+ } else {
3247
+ replaced = true ;
3248
+ if (substituteLength == 1 ) {
3249
+ helpers .putCharInArrayByIndex (newChars , newCharIndex ++, replacement );
3250
+ }
3237
3251
}
3238
3252
}
3253
+ if (!replaced ) {
3254
+ return this ;
3255
+ }
3239
3256
if (replacement > 255 ) {
3240
3257
// If the original String isn't compressed and the replacement character isn't Latin1, the result is uncompressed.
3241
3258
return new String (newChars , UTF16 );
@@ -7601,6 +7618,9 @@ public String replaceAll(String regex, String substitute) {
7601
7618
int length = lengthInternal ();
7602
7619
if (substituteLength < 2 ) {
7603
7620
if (COMPACT_STRINGS && isCompressed () && (substituteLength == 0 || substitute .isCompressed ())) {
7621
+ if (!regex .isCompressed ()) {
7622
+ return this ;
7623
+ }
7604
7624
char [] newChars = new char [(length + 1 ) >>> 1 ];
7605
7625
byte toReplace = helpers .getByteFromArrayByIndex (regex .value , 0 );
7606
7626
byte replacement = (byte )-1 ; // assign dummy value that will never be used
@@ -7609,14 +7629,21 @@ public String replaceAll(String regex, String substitute) {
7609
7629
checkLastChar ((char )replacement );
7610
7630
}
7611
7631
int newCharIndex = 0 ;
7632
+ boolean replaced = false ;
7612
7633
for (int i = 0 ; i < length ; ++i ) {
7613
7634
byte current = helpers .getByteFromArrayByIndex (value , i );
7614
7635
if (current != toReplace ) {
7615
7636
helpers .putByteInArrayByIndex (newChars , newCharIndex ++, current );
7616
- } else if (substituteLength == 1 ) {
7617
- helpers .putByteInArrayByIndex (newChars , newCharIndex ++, replacement );
7637
+ } else {
7638
+ replaced = true ;
7639
+ if (substituteLength == 1 ) {
7640
+ helpers .putByteInArrayByIndex (newChars , newCharIndex ++, replacement );
7641
+ }
7618
7642
}
7619
7643
}
7644
+ if (!replaced ) {
7645
+ return this ;
7646
+ }
7620
7647
return new String (newChars , 0 , newCharIndex , true );
7621
7648
} else if (!COMPACT_STRINGS || !isCompressed ()) {
7622
7649
char [] newChars = new char [length ];
@@ -7627,14 +7654,21 @@ public String replaceAll(String regex, String substitute) {
7627
7654
checkLastChar (replacement );
7628
7655
}
7629
7656
int newCharIndex = 0 ;
7657
+ boolean replaced = false ;
7630
7658
for (int i = 0 ; i < length ; ++i ) {
7631
7659
char current = helpers .getCharFromArrayByIndex (value , i );
7632
7660
if (current != toReplace ) {
7633
7661
helpers .putCharInArrayByIndex (newChars , newCharIndex ++, current );
7634
- } else if (substituteLength == 1 ) {
7635
- helpers .putCharInArrayByIndex (newChars , newCharIndex ++, replacement );
7662
+ } else {
7663
+ replaced = true ;
7664
+ if (substituteLength == 1 ) {
7665
+ helpers .putCharInArrayByIndex (newChars , newCharIndex ++, replacement );
7666
+ }
7636
7667
}
7637
7668
}
7669
+ if (!replaced ) {
7670
+ return this ;
7671
+ }
7638
7672
return new String (newChars , 0 , newCharIndex , false );
7639
7673
}
7640
7674
}
0 commit comments