home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_StringWalker_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-05-08  |  5.1 KB  |  198 lines

  1. // --------------------------------------------------------------------------
  2. //  StringWalker class: this string encapsulates string operations in order
  3. //  to provide easy methods that handle surrogates and combining characters
  4. // --------------------------------------------------------------------------
  5.  
  6. using System;
  7. using System.Globalization;
  8. using System.Text;
  9.  
  10. namespace StringWalker
  11. {
  12.   public class StringWalkerException : Exception
  13.   {
  14.     public StringWalkerException()           : base()     {}
  15.     public StringWalkerException(String str) : base(str)  {}
  16.   }
  17.  
  18.     public class StringWalker
  19.     {
  20.     // private members
  21.     private string myString = String.Empty;
  22.     private int[]  myIndex;
  23.     private int    myPos;
  24.  
  25.     // public property
  26.     public int Length
  27.     {
  28.       get 
  29.       {
  30.         return (null != myIndex) ? myIndex.Length : 0;
  31.       }
  32.     }
  33.  
  34.     // constructor
  35.         public StringWalker(string str)
  36.         {
  37.       Initialize(str);
  38.         }
  39.  
  40.     // ToString overriden method
  41.     public override string ToString()
  42.     {
  43.       return myString;
  44.     }
  45.  
  46.     // "easy" walking methods: GetFirst, GetNext, GetPrev, GetLast, Get
  47.     // these are basically wrapper around the GetSubString method
  48.     public bool GetFirst(out string str)
  49.     {
  50.       myPos = 0;
  51.       return Get(myPos, out str);
  52.     }
  53.  
  54.     public bool GetLast(out string str)
  55.     {
  56.       myPos = Length - 1;
  57.       return Get(myPos, out str);
  58.     }
  59.  
  60.     public bool GetNext(out string str)
  61.     {
  62.       return Get(++myPos, out str);
  63.     }
  64.  
  65.     public bool GetPrev(out string str)
  66.     {
  67.       return Get(--myPos, out str);
  68.     }
  69.  
  70.     public bool Get(int index, out string str)
  71.     {
  72.       return (0 != GetSubString(index, 1, out str));
  73.     }
  74.  
  75.     // GetSubString method
  76.     public int GetSubString(int index, int count, out string str)
  77.     {
  78.       // check for index within bounds and non zero count
  79.       if((1 <= count) && (0 <= index ) && (index < Length))
  80.       {
  81.         try
  82.         {
  83.           int lastindex = index + count;
  84.  
  85.           // if we are past the last char, then we get the string
  86.           // up to the last char and return the actual count
  87.           if(lastindex > (Length - 1))
  88.           {
  89.             str = myString.Substring(myIndex[index]);
  90.             return Length - index;
  91.           }
  92.           else
  93.           {
  94.             str = myString.Substring(myIndex[index], myIndex[lastindex] - myIndex[index]);
  95.             return count;
  96.           }      
  97.         }
  98.         catch // catch all and throw our exception
  99.         {
  100.           throw(new StringWalkerException());
  101.         }
  102.       }
  103.  
  104.       else
  105.       {
  106.         str = String.Empty;
  107.         return 0;
  108.       }
  109.     }
  110.  
  111.     // Insert, Remove: both methods return true if the operation was succesful and false otherwise
  112.  
  113.     // Insert: inserts a string at the specified position
  114.     public bool Insert(int index, string str)
  115.     {
  116.       if((0 <= index) && (index <= Length))
  117.       {
  118.         try
  119.         {
  120.           if(index == Length)
  121.             Initialize(myString.Insert(myString.Length, str));
  122.           else
  123.             Initialize(myString.Insert(myIndex[index], str));
  124.           return true;
  125.         }
  126.         catch // catch all and throw our exception
  127.         {
  128.           throw(new StringWalkerException());
  129.         }
  130.       }
  131.       return false;
  132.     }
  133.  
  134.     // Remove: removes the specified number of text elements starting at the specified position
  135.     public bool Remove(int index, int count)
  136.     {
  137.       if((count > 0) && (0 <= index) && (index < Length))
  138.       {
  139.         try
  140.         {
  141.           int idxLast = index + count;
  142.           int charcount = (idxLast < Length) ? myIndex[idxLast] - myIndex[index] : myString.Length - myIndex[index];
  143.           Initialize(myString.Remove(myIndex[index], charcount));
  144.           return true;
  145.         }
  146.         catch // catch all and throw our exception
  147.         {
  148.           throw(new StringWalkerException());
  149.         }
  150.       }
  151.       return false;
  152.     }
  153.  
  154.     // IndexOf: 
  155.     public int IndexOf(string str, int index)
  156.     {
  157.       if((0 <= index) && (index < Length))
  158.       {
  159.         try
  160.         {
  161.           // try and find the input string in the current string
  162.           int position = myIndex[index];
  163.           int foundAt  = myString.IndexOf(str, position);
  164.  
  165.           // if the string is found, then we need to see if it
  166.           // can be matched to a text element index.
  167.           if(-1 != foundAt)
  168.           {
  169.             for(int i = 0; i < myIndex.Length; i++)
  170.               if(myIndex[i] == foundAt)
  171.                 return i;
  172.           }
  173.         }
  174.         catch // catch all and throw our exception
  175.         {
  176.           throw(new StringWalkerException());
  177.         }
  178.       } 
  179.       return -1;
  180.     }
  181.  
  182.     // private initialization method
  183.     private void Initialize(string str)
  184.     {
  185.       try
  186.       {
  187.         myPos    = 0;
  188.         myString = str;
  189.         myIndex  = StringInfo.ParseCombiningCharacters(myString);
  190.       }
  191.       catch(ArgumentNullException)
  192.       {
  193.         throw(new StringWalkerException());
  194.       }
  195.     }
  196.   }
  197. }
  198.