Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/main/java/org/apache/commons/lang3/Strings.java
Original file line number Diff line number Diff line change
Expand Up @@ -1288,13 +1288,10 @@ public String replace(final String text, final String searchString, final String
* @param max maximum number of values to replace, or {@code -1} if no maximum
* @return the text with any replacements processed, {@code null} if null String input
*/
public String replace(final String text, String searchString, final String replacement, int max) {
public String replace(final String text, final String searchString, final String replacement, int max) {
if (StringUtils.isEmpty(text) || StringUtils.isEmpty(searchString) || replacement == null || max == 0) {
return text;
}
if (ignoreCase) {
searchString = searchString.toLowerCase();
}
int start = 0;
int end = indexOf(text, searchString, start);
if (end == INDEX_NOT_FOUND) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/apache/commons/lang3/StringsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.commons.lang3;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -26,6 +27,7 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junitpioneer.jupiter.DefaultLocale;

/**
* Tests {@link Strings}.
Expand Down Expand Up @@ -78,6 +80,19 @@ void testCaseInsensitiveStartsWithAny() {
assertTrue(Strings.CI.startsWithAny(new StringBuffer("AbCxYz"), new StringBuilder("XyZ"), new StringBuffer("abc")));
}

/**
* {@code U+0130} lower-cases to the two-char sequence {@code "i̇"} outside Turkish locales, so pre-lower-casing the
* search argument made the case-insensitive replace look for a two-char needle that no longer matches the single source
* character.
*/
@Test
@DefaultLocale("en")
void testCaseInsensitiveReplaceLengthChangingLowerCase() {
assertEquals("aXb", Strings.CI.replace("aİb", "İ", "X", -1));
assertEquals("x_y_z", Strings.CI.replace("xİyİz", "İ", "_", -1));
assertEquals("X", Strings.CI.replaceOnce("İ", "İ", "X"));
}

@Test
void testCaseSensitiveConstant() {
assertNotNull(Strings.CS);
Expand Down
Loading