Hi,

I think that for char[] based implementation of CharSequence (mainly
StringBuffer/StringBuilder),
it would be faster to convert to String (copy char[] once) then invoke
to charArray method (copy char[] twice).

Indeed, invoking many times charAt(index) may be costly since the
range check would be done every time,
and the operations (affect char in char[]) would be done as many time
as the CharSequence length.

But I may be wrong on this one (with jit optimization magic).

Anyway, thanks for the work on Lang3.

Regards,
Julien

2011/4/7  <bay...@apache.org>:
> Author: bayard
> Date: Thu Apr  7 04:03:55 2011
> New Revision: 1089724
>
> URL: http://svn.apache.org/viewvc?rev=1089724&view=rev
> Log:
> Implemented the native CharSequence version of toCharArray
>
> Modified:
>    
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
>
> Modified: 
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
> URL: 
> http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java?rev=1089724&r1=1089723&r2=1089724&view=diff
> ==============================================================================
> --- 
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
>  (original)
> +++ 
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
>  Thu Apr  7 04:03:55 2011
> @@ -6489,8 +6489,12 @@ public class StringUtils {
>         if (cs instanceof String) {
>             return ((String) cs).toCharArray();
>         } else {
> -            // TODO: Implement rather than convert to String
> -            return cs.toString().toCharArray();
> +            int sz = cs.length();
> +            char[] array = new char[cs.length()];
> +            for (int i=0; i < sz; i++) {
> +                array[i] = cs.charAt(i);
> +            }
> +            return array;
>         }
>     }
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to