1 //------------------------------------------------------------------------------
2 // <copyright file="StringConcat.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
8 using System
.Collections
.Generic
;
9 using System
.Diagnostics
;
10 using System
.ComponentModel
;
12 namespace System
.Xml
.Xsl
.Runtime
{
15 /// Efficiently concatenates strings when the number of string is not known beforehand, and
16 /// yet it is common for only one string to be concatenated. StringBuilder is not good for
17 /// this purpose, since it *always* allocates objects, even if only one string is appended.
19 [EditorBrowsable(EditorBrowsableState
.Never
)]
20 public struct StringConcat
{
21 private string s1
, s2
, s3
, s4
;
22 private string delimiter
;
23 private List
<string> strList
;
27 /// Clear the result string.
31 this.delimiter
= null;
35 /// Gets or sets the string that delimits concatenated strings.
37 public string Delimiter
{
38 get { return this.delimiter; }
39 set { this.delimiter = value; }
43 /// Return the number of concatenated strings, including delimiters.
46 get { return this.idxStr; }
50 /// Concatenate a new string to the result.
52 public void Concat(string value) {
53 Debug
.Assert(value != null);
55 if (this.delimiter
!= null && this.idxStr
!= 0) {
57 ConcatNoDelimiter(this.delimiter
);
60 ConcatNoDelimiter(value);
64 /// Get the result string.
66 public string GetResult() {
67 switch (this.idxStr
) {
68 case 0: return string.Empty
;
69 case 1: return this.s1
;
70 case 2: return string.Concat(this.s1
, this.s2
);
71 case 3: return string.Concat(this.s1
, this.s2
, this.s3
);
72 case 4: return string.Concat(this.s1
, this.s2
, this.s3
, this.s4
);
74 return string.Concat(this.strList
.ToArray());
78 /// Concatenate a new string to the result without adding a delimiter.
80 internal void ConcatNoDelimiter(string s
) {
81 switch (this.idxStr
) {
82 case 0: this.s1
= s
; break;
83 case 1: this.s2
= s
; break;
84 case 2: this.s3
= s
; break;
85 case 3: this.s4
= s
; break;
87 // Calling Clear() is expensive, allocate a new List instead
88 int capacity
= (this.strList
== null) ? 8 : this.strList
.Count
;
89 List
<string> strList
= this.strList
= new List
<string>(capacity
);