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

Kai Gülzau commented on LANG-604:
---------------------------------

Ok, good point. This way you don't loose performance for trimmed strings.

But the loop doesn't check all chars now:

isBlank("   X  ") returns true!

There are two fixes:
a)
...
for (int m = strLen / 2, i = 1; m < strLen; m++, i++) {
...

Here one char (at strLen / 2) is double checked for even length blank strings.

b)
...
int mid = strLen / 2;
for (int i = 1, m = mid; m < strLen; m++, i++) {
  if (!Character.isWhitespace(cs.charAt(m)) || ( i < mid && 
!Character.isWhitespace(cs.charAt(i))) ) {
...

Here we loose some minor performance on very large blank strings due
to the extra check in the loop.


I opt for b).

Kai



> 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.

Reply via email to