commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Julien Aymé <julien.a...@gmail.com>
Subject Re: svn commit: r1089724 - /commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/StringUtils.java
Date Thu, 07 Apr 2011 09:49:14 GMT
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  <bayard@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-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message