harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Deven You <devyo...@gmail.com>
Subject Re: [classlib][luni] String.concat() behaves differently to the spec
Date Fri, 24 Sep 2010 13:59:38 GMT
+1

2010/9/24 Oliver Deakin <oliver.deakin@googlemail.com>

>  Hi all,
>
> For String.concat(), the spec says:
>
> "If the length of the argument string is 0, then this String object is
> returned. Otherwise, a new String object is created, representing a
> character sequence that is the concatenation of the character sequence
> represented by this String object and the character sequence represented by
> the argument string."
>
> On Harmony the following test prints "Returned the same String":
>
> public class Test {
>    public static void main(String[] args) {
>        String s1 = "";
>        String s2 = "s2";
>        String s3 = s1.concat(s2);
>        if(s3 == s2)
>            System.out.println("Returned the same String");
>    }
> }
>
> This is because in Harmony's String.concat() we check for both the
> parameter string and "this" to have length >0 before we do a concatenation,
> otherwise we just return one or the other of the Strings. On the RI it does
> not print anything, which agrees with the spec as far as I can see. To match
> the spec here, we just need to modify concat to create a new String in all
> cases except when string.count==0 [1]. Since String is a sensitive class, I
> thought I'd run this by the list before I committed it. Any comments?
>
> Regards,
> Oliver
>
> [1]
> Index: String.java
> ===================================================================
> --- String.java    (revision 1000855)
> +++ String.java    (working copy)
> @@ -678,14 +678,17 @@
>      *         specified string.
>      */
>     public String concat(String string) {
> -        if (string.count > 0 && count > 0) {
> -            char[] buffer = new char[count + string.count];
> +        if (string.count == 0) {
> +            return this;
> +        }
> +
> +        char[] buffer = new char[count + string.count];
> +        if (count > 0) {
>             System.arraycopy(value, offset, buffer, 0, count);
> -            System.arraycopy(string.value, string.offset, buffer, count,
> -                    string.count);
> -            return new String(0, buffer.length, buffer);
>         }
> -        return count == 0 ? string : this;
> +        System.arraycopy(string.value, string.offset, buffer, count,
> +                string.count);
> +        return new String(0, buffer.length, buffer);
>     }
>
>     /**
>
>
> --
> Oliver Deakin
> Unless stated otherwise above:
> IBM United Kingdom Limited - Registered in England and Wales with number
> 741598.
> Registered office: PO Box 41, North Harbour, Portsmouth, Hampshire PO6 3AU
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message