From 81f559d918847e5701995a9a043ea2112f522101 Mon Sep 17 00:00:00 2001 From: zoltan Date: Sun, 1 Mar 2009 08:57:13 +0000 Subject: [PATCH] 2009-03-01 Zoltan Varga * NameValueCollection.cs (AsSingleString): Avoid the allocation of a stringbuilder for collections with only one element. git-svn-id: svn+ssh://mono-cvs.ximian.com/source/trunk/mcs@128295 e3ebcda4-bce8-0310-ba0a-eca2169e7518 --- .../System.Collections.Specialized/ChangeLog | 5 +++++ .../NameValueCollection.cs | 25 +++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/class/System/System.Collections.Specialized/ChangeLog b/class/System/System.Collections.Specialized/ChangeLog index 879abc43a4..35b5f1f951 100644 --- a/class/System/System.Collections.Specialized/ChangeLog +++ b/class/System/System.Collections.Specialized/ChangeLog @@ -1,3 +1,8 @@ +2009-03-01 Zoltan Varga + + * NameValueCollection.cs (AsSingleString): Avoid the allocation of + a stringbuilder for collections with only one element. + 2008-09-09 Scott Peterson * BitVector32.cs: Minor fixes. Simplified Section.ToString and diff --git a/class/System/System.Collections.Specialized/NameValueCollection.cs b/class/System/System.Collections.Specialized/NameValueCollection.cs index 5aa73d9285..2743291201 100644 --- a/class/System/System.Collections.Specialized/NameValueCollection.cs +++ b/class/System/System.Collections.Specialized/NameValueCollection.cs @@ -257,16 +257,25 @@ namespace System.Collections.Specialized{ return null; int max = values.Count; - if (max == 0) + switch (max) { + case 0: return null; - //TODO: reimplement this - StringBuilder sb = new StringBuilder ((string)values [0]); - for (int i = 1; i < max; i++){ - sb.Append (separator); - sb.Append (values [i]); - } + case 1: + return (string)values [0]; + case 2: + return String.Concat ((string)values [0], separator, (string)values [1]); + default: + int len = max; + for (int i = 0; i < max; i++) + len += ((string)values [i]).Length; + StringBuilder sb = new StringBuilder ((string)values [0], len); + for (int i = 1; i < max; i++){ + sb.Append (separator); + sb.Append (values [i]); + } - return sb.ToString (); + return sb.ToString (); + } } -- 2.11.4.GIT