[ https://issues.apache.org/jira/browse/LANG-604?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12846475#action_12846475 ]
Henri Yandell commented on LANG-604: ------------------------------------ I'm not sure I buy that most of the strings checked are untrimmed. I'd expect most to be either a) blank or b) normal user input. If the first character is whitespace; then I could believe that it's untrimmed. So: + public static boolean isBlank(CharSequence cs) { + int strLen; + if (cs == null || (strLen = cs.length()) == 0) { + return true; + } + // Optimized - check first character + if (!Character.isWhitespace(cs.charAt(0))) { + return false; + } + // Optimized - starts in the middle and works out with the assumption that + // most input starting with whitespace are untrimmed strings + for (int m = 1 + strLen / 2, i = 1; m < strLen; m++, i++) { + if (!Character.isWhitespace(cs.charAt(m)) || !Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + return true; + } Any thoughts? > Optimize isBlank() for untrimmed strings > ---------------------------------------- > > Key: LANG-604 > URL: https://issues.apache.org/jira/browse/LANG-604 > Project: Commons Lang > Issue Type: Improvement > Components: lang.* > Affects Versions: 3.0 > Reporter: Kai Gülzau > Priority: Minor > > Change isBlank() to start iteration in the middle of the String. > So you get better performance for untrimmed Strings like " dummy ". > Here is my proposal: > public static boolean isBlank(CharSequence cs) { > int strLen; > if (cs == null || (strLen = cs.length()) == 0) { > return true; > } > int mid = strLen / 2, i = mid; > for (; i < strLen; i++) { > if (!Character.isWhitespace(cs.charAt(i))) { > return false; > } > } > for (i = 0; i < mid; i++) { > if (!Character.isWhitespace(cs.charAt(i))) { > return false; > } > } > return true; > } -- This message is automatically generated by JIRA. - You can reply to this email to add a comment to the issue online.