leventov commented on a change in pull request #6607: Prohibit String.replace()
and String.replaceAll(), fix and prohibit some toString()-related redundancies
URL: https://github.com/apache/incubator-druid/pull/6607#discussion_r233960721
##########
File path:
core/src/main/java/org/apache/druid/java/util/common/StringUtils.java
##########
@@ -173,16 +163,81 @@ public static String urlEncode(String s)
}
}
- private static String removeChar(String s, char c, int firstOccurranceIndex)
+ /**
+ * Removes all occurrences of the given char from the given string. This
method is an optimal version of
+ * {@link String#replace(CharSequence, CharSequence) s.replace("c", "")}.
+ */
+ public static String removeChar(String s, char c)
{
+ int pos = s.indexOf(c);
+ if (pos < 0) {
+ return s;
+ }
StringBuilder sb = new StringBuilder(s.length() - 1);
- sb.append(s, 0, firstOccurranceIndex);
- for (int i = firstOccurranceIndex + 1; i < s.length(); i++) {
- char charOfString = s.charAt(i);
- if (charOfString != c) {
- sb.append(charOfString);
- }
+ int prevPos = 0;
+ do {
+ sb.append(s, prevPos, pos);
+ prevPos = pos + 1;
+ pos = s.indexOf(c, pos + 1);
+ } while (pos > 0);
+ sb.append(s, prevPos, s.length());
+ return sb.toString();
+ }
+
+ /**
+ * Replaces all occurrences of the given char in the given string with the
given replacement string. This method is an
+ * optimal version of {@link String#replace(CharSequence, CharSequence)
s.replace("c", replacement)}.
+ */
+ public static String replaceChar(String s, char c, String replacement)
+ {
+ int pos = s.indexOf(c);
+ if (pos < 0) {
+ return s;
+ }
+ StringBuilder sb = new StringBuilder(s.length() - 1 +
replacement.length());
+ int prevPos = 0;
+ do {
+ sb.append(s, prevPos, pos);
+ sb.append(replacement);
+ prevPos = pos + 1;
+ pos = s.indexOf(c, pos + 1);
+ } while (pos > 0);
+ sb.append(s, prevPos, s.length());
+ return sb.toString();
+ }
+
+ /**
+ * Replaces all occurrences of the given target substring in the given
string with the given replacement string. This
+ * method is an optimal version of {@link String#replace(CharSequence,
CharSequence) s.replace(target, replacement)}.
+ */
+ public static String replace(String s, String target, String replacement)
Review comment:
There are uses for it yet. BTW even the new OpenJDK 9+ version converts the
`target` to String internally because the implementation relies on `indexOf()`
that is optimized only for two Strings, i. e. making the `target` a
`CharSequence` will only give false sense of "optimality". The replacement
could be made `CharSequence` but there is no need for that yet.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]