[jira] [Commented] (LANG-604) Optimize isBlank() for untrimmed strings

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Commented] (LANG-604) Optimize isBlank() for untrimmed strings

ASF GitHub Bot (Jira)

    [ https://issues.apache.org/jira/browse/LANG-604?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13102223#comment-13102223 ]

Henri Yandell commented on LANG-604:
------------------------------------

Coming back to this one, I'm unconvinced of the need to improve performance (at the cost of simplicity). There are theory based performance improvements for " 123", but we're talking the difference between 1 and 2 charAt calls. Very small stuff. The same could hold true for much larger strings, but they are going to be rare enough as to require their own optimization techniques if they're expected as the norm (ie: hand roll your own code, possibly switch to a faster system if Java is considered 'too slow').

So while I continue to agree with the performance improvement, I don't believe they actualize as big enough to warrant the code complexity.

Sorry for the big delay.

> 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
>             Fix For: 3.0.2
>
>
> 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.
For more information on JIRA, see: http://www.atlassian.com/software/jira