kinow commented on a change in pull request #496: (doc): Avoid unnecessary
allocation in StringUtils.wrapIfMissing.
URL: https://github.com/apache/commons-lang/pull/496#discussion_r386083762
##########
File path: src/main/java/org/apache/commons/lang3/StringUtils.java
##########
@@ -9508,20 +9514,27 @@ public static String wrapIfMissing(final String str,
final char wrapWith) {
* @param str
* the string to be wrapped, may be {@code null}
* @param wrapWith
- * the char that will wrap {@code str}
+ * the string that will wrap {@code str}
* @return the wrapped string, or {@code null} if {@code str==null}
* @since 3.5
*/
public static String wrapIfMissing(final String str, final String
wrapWith) {
if (isEmpty(str) || isEmpty(wrapWith)) {
return str;
}
+
+ final boolean wrapStart = !str.startsWith(wrapWith);
+ final boolean wrapEnd = !str.endsWith(wrapWith);
+ if (!wrapStart && !wrapEnd) {
+ return str;
+ }
+
final StringBuilder builder = new StringBuilder(str.length() +
wrapWith.length() + wrapWith.length());
Review comment:
Yeah, looks like this is the maximum capacity, not necessarily the exact
capacity we expect to have. Probably harmless/fine?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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