Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / Runtime / StringConcat.cs
blobb20f293db2336cab63dfcaa6b81367ea0cbea4b2
1 //------------------------------------------------------------------------------
2 // <copyright file="StringConcat.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7 using System;
8 using System.Collections.Generic;
9 using System.Diagnostics;
10 using System.ComponentModel;
12 namespace System.Xml.Xsl.Runtime {
14 /// <summary>
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.
18 /// </summary>
19 [EditorBrowsable(EditorBrowsableState.Never)]
20 public struct StringConcat {
21 private string s1, s2, s3, s4;
22 private string delimiter;
23 private List<string> strList;
24 int idxStr;
26 /// <summary>
27 /// Clear the result string.
28 /// </summary>
29 public void Clear() {
30 this.idxStr = 0;
31 this.delimiter = null;
34 /// <summary>
35 /// Gets or sets the string that delimits concatenated strings.
36 /// </summary>
37 public string Delimiter {
38 get { return this.delimiter; }
39 set { this.delimiter = value; }
42 /// <summary>
43 /// Return the number of concatenated strings, including delimiters.
44 /// </summary>
45 internal int Count {
46 get { return this.idxStr; }
49 /// <summary>
50 /// Concatenate a new string to the result.
51 /// </summary>
52 public void Concat(string value) {
53 Debug.Assert(value != null);
55 if (this.delimiter != null && this.idxStr != 0) {
56 // Add delimiter
57 ConcatNoDelimiter(this.delimiter);
60 ConcatNoDelimiter(value);
63 /// <summary>
64 /// Get the result string.
65 /// </summary>
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());
77 /// <summary>
78 /// Concatenate a new string to the result without adding a delimiter.
79 /// </summary>
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;
86 case 4:
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);
90 strList.Add(this.s1);
91 strList.Add(this.s2);
92 strList.Add(this.s3);
93 strList.Add(this.s4);
94 goto default;
95 default:
96 this.strList.Add(s);
97 break;
100 this.idxStr++;