nstdio commented on a change in pull request #496: LANG-1523: Avoid unnecessary allocation in StringUtils.wrapIfMissing. URL: https://github.com/apache/commons-lang/pull/496#discussion_r387595656
########## 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: @garydgregory Sure, that makes sense. Done. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services