Return-Path: Delivered-To: apmail-harmony-dev-archive@www.apache.org Received: (qmail 1205 invoked from network); 27 Sep 2010 09:59:08 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 27 Sep 2010 09:59:08 -0000 Received: (qmail 42722 invoked by uid 500); 27 Sep 2010 09:59:08 -0000 Delivered-To: apmail-harmony-dev-archive@harmony.apache.org Received: (qmail 42317 invoked by uid 500); 27 Sep 2010 09:59:05 -0000 Mailing-List: contact dev-help@harmony.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@harmony.apache.org Delivered-To: mailing list dev@harmony.apache.org Received: (qmail 42308 invoked by uid 99); 27 Sep 2010 09:59:04 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Sep 2010 09:59:04 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of oliver.deakin@googlemail.com designates 209.85.215.49 as permitted sender) Received: from [209.85.215.49] (HELO mail-ew0-f49.google.com) (209.85.215.49) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Sep 2010 09:58:57 +0000 Received: by ewy9 with SMTP id 9so1497808ewy.36 for ; Mon, 27 Sep 2010 02:58:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=UXctbAldDs26POpQpTpYUXP7tKBRkfVQLyDNLAwQHDs=; b=uKk05FtVqfolbD4uAUnRIjpxAPNMz2UgeFv6/E/4WtFNNQ9iMzsktgvThbnjIyxHL4 8YZJtl1Fbf+nmWReCOgDqZiVeaFhxTG8xDCZPzGT7CRfackVHVKA/aPNQwnFo52oY7ux nLnm6Z+XZwc0ZY4dqobMR+9dbAiDUXKMNUhCM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; b=S7BizKkbtOLcLUKBJwvJNnHIyrLBg46kdmMCAORNXINmkWuXUT+JUSi4EgA6rNEzMY y3GoebvzoV82FeC3+jfrcDjd0adJ3c5OCccnkwpzvZfvNWJYne3mr+pKp37xxawgvfLW KIAx3MO4PO/Vo9qgx2blrh8rC57xD9s5Avqmg= Received: by 10.213.10.198 with SMTP id q6mr5720865ebq.8.1285581516348; Mon, 27 Sep 2010 02:58:36 -0700 (PDT) Received: from [192.168.1.72] ([92.1.65.120]) by mx.google.com with ESMTPS id a48sm8212562eei.1.2010.09.27.02.58.34 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 27 Sep 2010 02:58:34 -0700 (PDT) Message-ID: <4CA06ACB.50802@googlemail.com> Date: Mon, 27 Sep 2010 10:58:35 +0100 From: Oliver Deakin User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.9) Gecko/20100915 Thunderbird/3.1.4 MIME-Version: 1.0 To: dev@harmony.apache.org Subject: Re: [classlib][luni] String.concat() behaves differently to the spec References: <4C9CA984.5040808@googlemail.com> In-Reply-To: <4C9CA984.5040808@googlemail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Committed with regression test at r1001652. Regards, Oliver On 24/09/2010 14:37, Oliver Deakin wrote: > 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