mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-07 02:28:25 +00:00
LANG-410 - Ambiguous / confusing names in StringUtils replace* methods
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@627588 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1fe5439baf
commit
5cbca4d5f5
@ -3420,15 +3420,15 @@ public static String remove(String str, char remove) {
|
|||||||
* StringUtils.replaceOnce("aba", "a", "z") = "zba"
|
* StringUtils.replaceOnce("aba", "a", "z") = "zba"
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @see #replace(String text, String repl, String with, int max)
|
* @see #replace(String text, String searchString, String replacement, int max)
|
||||||
* @param text text to search and replace in, may be null
|
* @param text text to search and replace in, may be null
|
||||||
* @param repl the String to search for, may be null
|
* @param searchString the String to search for, may be null
|
||||||
* @param with the String to replace with, may be null
|
* @param replacement the String to replace with, may be null
|
||||||
* @return the text with any replacements processed,
|
* @return the text with any replacements processed,
|
||||||
* <code>null</code> if null String input
|
* <code>null</code> if null String input
|
||||||
*/
|
*/
|
||||||
public static String replaceOnce(String text, String repl, String with) {
|
public static String replaceOnce(String text, String searchString, String replacement) {
|
||||||
return replace(text, repl, with, 1);
|
return replace(text, searchString, replacement, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -3447,15 +3447,15 @@ public static String replaceOnce(String text, String repl, String with) {
|
|||||||
* StringUtils.replace("aba", "a", "z") = "zbz"
|
* StringUtils.replace("aba", "a", "z") = "zbz"
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @see #replace(String text, String repl, String with, int max)
|
* @see #replace(String text, String searchString, String replacement, int max)
|
||||||
* @param text text to search and replace in, may be null
|
* @param text text to search and replace in, may be null
|
||||||
* @param repl the String to search for, may be null
|
* @param searchString the String to search for, may be null
|
||||||
* @param with the String to replace with, may be null
|
* @param replacement the String to replace it with, may be null
|
||||||
* @return the text with any replacements processed,
|
* @return the text with any replacements processed,
|
||||||
* <code>null</code> if null String input
|
* <code>null</code> if null String input
|
||||||
*/
|
*/
|
||||||
public static String replace(String text, String repl, String with) {
|
public static String replace(String text, String searchString, String replacement) {
|
||||||
return replace(text, repl, with, -1);
|
return replace(text, searchString, replacement, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -3480,33 +3480,33 @@ public static String replace(String text, String repl, String with) {
|
|||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param text text to search and replace in, may be null
|
* @param text text to search and replace in, may be null
|
||||||
* @param repl the String to search for, may be null
|
* @param searchString the String to search for, may be null
|
||||||
* @param with the String to replace with, may be null
|
* @param replacement the String to replace it with, may be null
|
||||||
* @param max maximum number of values to replace, or <code>-1</code> if no maximum
|
* @param max maximum number of values to replace, or <code>-1</code> if no maximum
|
||||||
* @return the text with any replacements processed,
|
* @return the text with any replacements processed,
|
||||||
* <code>null</code> if null String input
|
* <code>null</code> if null String input
|
||||||
*/
|
*/
|
||||||
public static String replace(String text, String repl, String with, int max) {
|
public static String replace(String text, String searchString, String replacement, int max) {
|
||||||
if (isEmpty(text) || isEmpty(repl) || with == null || max == 0) {
|
if (isEmpty(text) || isEmpty(searchString) || replacement == null || max == 0) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
int start = 0;
|
int start = 0;
|
||||||
int end = text.indexOf(repl, start);
|
int end = text.indexOf(searchString, start);
|
||||||
if (end == -1) {
|
if (end == -1) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
int replLength = repl.length();
|
int replLength = searchString.length();
|
||||||
int increase = with.length() - replLength;
|
int increase = replacement.length() - replLength;
|
||||||
increase = (increase < 0 ? 0 : increase);
|
increase = (increase < 0 ? 0 : increase);
|
||||||
increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));
|
increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));
|
||||||
StringBuffer buf = new StringBuffer(text.length() + increase);
|
StringBuffer buf = new StringBuffer(text.length() + increase);
|
||||||
while (end != -1) {
|
while (end != -1) {
|
||||||
buf.append(text.substring(start, end)).append(with);
|
buf.append(text.substring(start, end)).append(replacement);
|
||||||
start = end + replLength;
|
start = end + replLength;
|
||||||
if (--max == 0) {
|
if (--max == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
end = text.indexOf(repl, start);
|
end = text.indexOf(searchString, start);
|
||||||
}
|
}
|
||||||
buf.append(text.substring(start));
|
buf.append(text.substring(start));
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
@ -3540,10 +3540,10 @@ public static String replace(String text, String repl, String with, int max) {
|
|||||||
*
|
*
|
||||||
* @param text
|
* @param text
|
||||||
* text to search and replace in, no-op if null
|
* text to search and replace in, no-op if null
|
||||||
* @param repl
|
* @param searchList
|
||||||
* the Strings to search for, no-op if null
|
* the Strings to search for, no-op if null
|
||||||
* @param with
|
* @param replacementList
|
||||||
* the Strings to replace with, no-op if null
|
* the Strings to replace them with, no-op if null
|
||||||
* @return the text with any replacements processed, <code>null</code> if
|
* @return the text with any replacements processed, <code>null</code> if
|
||||||
* null String input
|
* null String input
|
||||||
* @throws IndexOutOfBoundsException
|
* @throws IndexOutOfBoundsException
|
||||||
@ -3551,8 +3551,8 @@ public static String replace(String text, String repl, String with, int max) {
|
|||||||
* and/or size 0)
|
* and/or size 0)
|
||||||
* @since 2.4
|
* @since 2.4
|
||||||
*/
|
*/
|
||||||
public static String replaceEach(String text, String[] repl, String[] with) {
|
public static String replaceEach(String text, String[] searchList, String[] replacementList) {
|
||||||
return replaceEach(text, repl, with, false, 0);
|
return replaceEach(text, searchList, replacementList, false, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -3586,10 +3586,10 @@ public static String replaceEach(String text, String[] repl, String[] with) {
|
|||||||
*
|
*
|
||||||
* @param text
|
* @param text
|
||||||
* text to search and replace in, no-op if null
|
* text to search and replace in, no-op if null
|
||||||
* @param repl
|
* @param searchList
|
||||||
* the Strings to search for, no-op if null
|
* the Strings to search for, no-op if null
|
||||||
* @param with
|
* @param replacementList
|
||||||
* the Strings to replace with, no-op if null
|
* the Strings to replace them with, no-op if null
|
||||||
* @return the text with any replacements processed, <code>null</code> if
|
* @return the text with any replacements processed, <code>null</code> if
|
||||||
* null String input
|
* null String input
|
||||||
* @throws IllegalArgumentException
|
* @throws IllegalArgumentException
|
||||||
@ -3600,11 +3600,11 @@ public static String replaceEach(String text, String[] repl, String[] with) {
|
|||||||
* and/or size 0)
|
* and/or size 0)
|
||||||
* @since 2.4
|
* @since 2.4
|
||||||
*/
|
*/
|
||||||
public static String replaceEachRepeatedly(String text, String[] repl, String[] with) {
|
public static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList) {
|
||||||
// timeToLive should be 0 if not used or nothing to replace, else it's
|
// timeToLive should be 0 if not used or nothing to replace, else it's
|
||||||
// the length of the replace array
|
// the length of the replace array
|
||||||
int timeToLive = repl == null ? 0 : repl.length;
|
int timeToLive = searchList == null ? 0 : searchList.length;
|
||||||
return replaceEach(text, repl, with, true, timeToLive);
|
return replaceEach(text, searchList, replacementList, true, timeToLive);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -3636,10 +3636,12 @@ public static String replaceEachRepeatedly(String text, String[] repl, String[]
|
|||||||
*
|
*
|
||||||
* @param text
|
* @param text
|
||||||
* text to search and replace in, no-op if null
|
* text to search and replace in, no-op if null
|
||||||
* @param repl
|
* @param searchList
|
||||||
* the Strings to search for, no-op if null
|
* the Strings to search for, no-op if null
|
||||||
* @param with
|
* @param replacementList
|
||||||
* the Strings to replace with, no-op if null
|
* the Strings to replace them with, no-op if null
|
||||||
|
* @param repeat if true, then replace repeatedly
|
||||||
|
* until there are no more possible replacements or timeToLive < 0
|
||||||
* @param timeToLive
|
* @param timeToLive
|
||||||
* if less than 0 then there is a circular reference and endless
|
* if less than 0 then there is a circular reference and endless
|
||||||
* loop
|
* loop
|
||||||
@ -3653,12 +3655,12 @@ public static String replaceEachRepeatedly(String text, String[] repl, String[]
|
|||||||
* and/or size 0)
|
* and/or size 0)
|
||||||
* @since 2.4
|
* @since 2.4
|
||||||
*/
|
*/
|
||||||
private static String replaceEach(String text, String[] repl, String[] with, boolean repeat, int timeToLive) {
|
private static String replaceEach(String text, String[] searchList, String[] replacementList, boolean repeat, int timeToLive) {
|
||||||
|
|
||||||
// mchyzer Performance note: This creates very few new objects (one major goal)
|
// mchyzer Performance note: This creates very few new objects (one major goal)
|
||||||
// let me know if there are performance requests, we can create a harness to measure
|
// let me know if there are performance requests, we can create a harness to measure
|
||||||
|
|
||||||
if (text == null || text.length() == 0 || repl == null || repl.length == 0 || with == null || with.length == 0) {
|
if (text == null || text.length() == 0 || searchList == null || searchList.length == 0 || replacementList == null || replacementList.length == 0) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3667,19 +3669,19 @@ private static String replaceEach(String text, String[] repl, String[] with, boo
|
|||||||
throw new IllegalStateException("TimeToLive of " + timeToLive + " is less than 0: " + text);
|
throw new IllegalStateException("TimeToLive of " + timeToLive + " is less than 0: " + text);
|
||||||
}
|
}
|
||||||
|
|
||||||
int replLength = repl.length;
|
int searchLength = searchList.length;
|
||||||
int withLength = with.length;
|
int replacementLength = replacementList.length;
|
||||||
|
|
||||||
// make sure lengths are ok, these need to be equal
|
// make sure lengths are ok, these need to be equal
|
||||||
if (replLength != withLength) {
|
if (searchLength != replacementLength) {
|
||||||
throw new IllegalArgumentException("Search and Replace array lengths don't match: "
|
throw new IllegalArgumentException("Search and Replace array lengths don't match: "
|
||||||
+ replLength
|
+ searchLength
|
||||||
+ " vs "
|
+ " vs "
|
||||||
+ withLength);
|
+ replacementLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
// keep track of which still have matches
|
// keep track of which still have matches
|
||||||
boolean[] noMoreMatchesForReplIndex = new boolean[replLength];
|
boolean[] noMoreMatchesForReplIndex = new boolean[searchLength];
|
||||||
|
|
||||||
// index on index that the match was found
|
// index on index that the match was found
|
||||||
int textIndex = -1;
|
int textIndex = -1;
|
||||||
@ -3688,11 +3690,11 @@ private static String replaceEach(String text, String[] repl, String[] with, boo
|
|||||||
|
|
||||||
// index of replace array that will replace the search string found
|
// index of replace array that will replace the search string found
|
||||||
// NOTE: logic duplicated below START
|
// NOTE: logic duplicated below START
|
||||||
for (int i = 0; i < replLength; i++) {
|
for (int i = 0; i < searchLength; i++) {
|
||||||
if (noMoreMatchesForReplIndex[i] || repl[i] == null || repl[i].length() == 0 || with[i] == null) {
|
if (noMoreMatchesForReplIndex[i] || searchList[i] == null || searchList[i].length() == 0 || replacementList[i] == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
tempIndex = text.indexOf(repl[i]);
|
tempIndex = text.indexOf(searchList[i]);
|
||||||
|
|
||||||
// see if we need to keep searching for this
|
// see if we need to keep searching for this
|
||||||
if (tempIndex == -1) {
|
if (tempIndex == -1) {
|
||||||
@ -3717,8 +3719,8 @@ private static String replaceEach(String text, String[] repl, String[] with, boo
|
|||||||
int increase = 0;
|
int increase = 0;
|
||||||
|
|
||||||
// count the replacement text elements that are larger than their corresponding text being replaced
|
// count the replacement text elements that are larger than their corresponding text being replaced
|
||||||
for (int i = 0; i < repl.length; i++) {
|
for (int i = 0; i < searchList.length; i++) {
|
||||||
int greater = with[i].length() - repl[i].length();
|
int greater = replacementList[i].length() - searchList[i].length();
|
||||||
if (greater > 0) {
|
if (greater > 0) {
|
||||||
increase += 3 * greater; // assume 3 matches
|
increase += 3 * greater; // assume 3 matches
|
||||||
}
|
}
|
||||||
@ -3733,20 +3735,20 @@ private static String replaceEach(String text, String[] repl, String[] with, boo
|
|||||||
for (int i = start; i < textIndex; i++) {
|
for (int i = start; i < textIndex; i++) {
|
||||||
buf.append(text.charAt(i));
|
buf.append(text.charAt(i));
|
||||||
}
|
}
|
||||||
buf.append(with[replaceIndex]);
|
buf.append(replacementList[replaceIndex]);
|
||||||
|
|
||||||
start = textIndex + repl[replaceIndex].length();
|
start = textIndex + searchList[replaceIndex].length();
|
||||||
|
|
||||||
textIndex = -1;
|
textIndex = -1;
|
||||||
replaceIndex = -1;
|
replaceIndex = -1;
|
||||||
tempIndex = -1;
|
tempIndex = -1;
|
||||||
// find the next earliest match
|
// find the next earliest match
|
||||||
// NOTE: logic mostly duplicated above START
|
// NOTE: logic mostly duplicated above START
|
||||||
for (int i = 0; i < replLength; i++) {
|
for (int i = 0; i < searchLength; i++) {
|
||||||
if (noMoreMatchesForReplIndex[i] || repl[i] == null || repl[i].length() == 0 || with[i] == null) {
|
if (noMoreMatchesForReplIndex[i] || searchList[i] == null || searchList[i].length() == 0 || replacementList[i] == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
tempIndex = text.indexOf(repl[i], start);
|
tempIndex = text.indexOf(searchList[i], start);
|
||||||
|
|
||||||
// see if we need to keep searching for this
|
// see if we need to keep searching for this
|
||||||
if (tempIndex == -1) {
|
if (tempIndex == -1) {
|
||||||
@ -3770,7 +3772,7 @@ private static String replaceEach(String text, String[] repl, String[] with, boo
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
return replaceEach(result, repl, with, repeat, timeToLive - 1);
|
return replaceEach(result, searchList, replacementList, repeat, timeToLive - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace, character based
|
// Replace, character based
|
||||||
|
Loading…
x
Reference in New Issue
Block a user