The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl.Runtime \ StringConcat

  1. //------------------------------------------------------------------------------
  2. // <copyright file="StringConcat.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Collections;
  17. using System.Diagnostics;
  18. using System.ComponentModel;
  19. namespace System.Xml.Xsl.Runtime
  20. {
  21.    
  22.     /// <summary>
  23.     /// Efficiently concatenates strings when the number of string is not known beforehand, and
  24.     /// yet it is common for only one string to be concatenated. StringBuilder is not good for
  25.     /// this purpose, since it *always* allocates objects, even if only one string is appended.
  26.     /// </summary>
  27.     [EditorBrowsable(EditorBrowsableState.Never)]
  28.     public struct StringConcat
  29.     {
  30.         private string s1, s2, s3, s4;
  31.         private string delimiter;
  32.         private ArrayList strList;
  33.         int idxStr;
  34.        
  35.         /// <summary>
  36.         /// Clear the result string.
  37.         /// </summary>
  38.         public void Clear()
  39.         {
  40.             this.idxStr = 0;
  41.             this.delimiter = null;
  42.         }
  43.        
  44.         /// <summary>
  45.         /// Gets or sets the string that delimits concatenated strings.
  46.         /// </summary>
  47.         public string Delimiter {
  48.             get { return this.delimiter; }
  49.             set { this.delimiter = value; }
  50.         }
  51.        
  52.         /// <summary>
  53.         /// Concatenate a new string to the result.
  54.         /// </summary>
  55.         public void Concat(string value)
  56.         {
  57.             Debug.Assert(value != null);
  58.            
  59.             if (this.delimiter != null && this.idxStr != 0) {
  60.                 // Add delimiter
  61.                 ConcatNoDelimiter(this.delimiter);
  62.             }
  63.            
  64.             ConcatNoDelimiter(value);
  65.         }
  66.        
  67.         /// <summary>
  68.         /// Get the result string.
  69.         /// </summary>
  70.         public string GetResult()
  71.         {
  72.             switch (this.idxStr) {
  73.                 case 0:
  74.                     return string.Empty;
  75.                 case 1:
  76.                     return this.s1;
  77.                 case 2:
  78.                     return string.Concat(this.s1, this.s2);
  79.                 case 3:
  80.                     return string.Concat(this.s1, this.s2, this.s3);
  81.                 case 4:
  82.                     return string.Concat(this.s1, this.s2, this.s3, this.s4);
  83.             }
  84.             return string.Concat(this.strList.ToArray());
  85.         }
  86.        
  87.         /// <summary>
  88.         /// Concatenate a new string to the result without adding a delimiter.
  89.         /// </summary>
  90.         private void ConcatNoDelimiter(string s)
  91.         {
  92.             switch (this.idxStr) {
  93.                 case 0:
  94.                     this.s1 = s;
  95.                     break;
  96.                 case 1:
  97.                     this.s2 = s;
  98.                     break;
  99.                 case 2:
  100.                     this.s3 = s;
  101.                     break;
  102.                 case 3:
  103.                     this.s4 = s;
  104.                     break;
  105.                 default:
  106.                     if (this.idxStr == 4) {
  107.                         this.strList = new ArrayList(8);
  108.                         this.strList.Add(this.s1);
  109.                         this.strList.Add(s2);
  110.                         this.strList.Add(s3);
  111.                         this.strList.Add(s4);
  112.                     }
  113.                     this.strList.Add(s);
  114.                     break;
  115.             }
  116.            
  117.             this.idxStr++;
  118.         }
  119.     }
  120. }

Developer Fusion