home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_StringWalker_vb________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2002-06-18  |  6.1 KB  |  179 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. Imports System
  7. Imports System.Globalization
  8. Imports System.Text
  9.  
  10.   Public Class StringWalkerException
  11.     Inherits Exception
  12.  
  13.     Public Sub New()
  14.       MyBase.New()
  15.     End Sub
  16.  
  17.     Public Sub New(ByVal str As String)
  18.       MyBase.New(str)
  19.       End Sub
  20.  
  21.    End Class
  22.  
  23.    Public Class StringWalker
  24.       ' private members
  25.       Private myString As String = String.Empty
  26.       Private myIndex() As Integer
  27.       Private myPos As Integer
  28.  
  29.       ' public property
  30.  
  31.       Public ReadOnly Property Length() As Integer
  32.          Get
  33.            If myIndex Is Nothing
  34.              Return 0
  35.            Else
  36.              Return myIndex.Length
  37.            End If
  38.          End Get
  39.       End Property
  40.  
  41.       ' constructor
  42.       Public Sub New(ByVal str As String)
  43.          Initialize(str)
  44.       End Sub 'New
  45.  
  46.       ' ToString overriden method
  47.       Public Overrides Function ToString() As String
  48.          Return myString
  49.       End Function 'ToString
  50.  
  51.       ' "easy" walking methods: GetFirst, GetNext, GetPrev, GetLast, Get
  52.       ' these are basically wrapper around the GetSubString method
  53.       Public Function GetFirst(ByRef str As String) As Boolean
  54.          myPos = 0
  55.          Return GetElement(myPos, str)
  56.       End Function 'GetFirst
  57.  
  58.       Public Function GetLast(ByRef str As String) As Boolean
  59.          myPos = Length - 1
  60.          Return GetElement(myPos, str)
  61.       End Function 'GetLast
  62.  
  63.       Public Function GetNext(ByRef str As String) As Boolean
  64.        myPos = myPos + 1
  65.         Return GetElement(myPos, str)
  66.       End Function 'GetNext
  67.  
  68.       Public Function GetPrev(ByRef str As String) As Boolean
  69.         myPos = myPos - 1
  70.          Return GetElement(myPos, str)
  71.       End Function 'GetPrev
  72.  
  73.       Public Function GetElement(ByVal index As Integer, ByRef str As String) As Boolean
  74.          Return 0 <> GetSubString(index, 1, str)
  75.       End Function 'Get
  76.  
  77.       ' GetSubString method
  78.       Public Function GetSubString(ByVal index As Integer, ByVal count As Integer, ByRef str As String) As Integer
  79.          ' check for index within bounds and non zero count
  80.          If 1 <= count And 0 <= index And index < Length Then
  81.             Try
  82.                Dim lastindex As Integer = index + count
  83.  
  84.                ' if we are past the last char, then we get the string
  85.                ' up to the last char and return the actual count
  86.                If lastindex > Length - 1 Then
  87.                   str = myString.Substring(myIndex(index))
  88.                   Return Length - index
  89.                Else
  90.                   str = myString.Substring(myIndex(index), myIndex(lastindex) - myIndex(index))
  91.                   Return count
  92.                End If
  93.             Catch ' catch all and throw our exception
  94.               Throw (New StringWalkerException())
  95.             End Try
  96.          End If
  97.          str = String.Empty
  98.          Return 0
  99.       End Function 'GetSubString
  100.  
  101.       ' Insert, Remove: both methods return true if the operation was succesful and false otherwise
  102.       ' Insert: inserts a string at the specified position
  103.       Public Function Insert(ByVal index As Integer, ByVal str As String) As Boolean
  104.          If 0 <= index And index <= Length Then
  105.             Try
  106.                If index = Length Then
  107.                   Initialize(myString.Insert(myString.Length, str))
  108.                Else
  109.                   Initialize(myString.Insert(myIndex(index), str))
  110.                End If
  111.                Return True
  112.             Catch ' catch all and throw our exception
  113.               Throw (New StringWalkerException())
  114.             End Try
  115.          End If
  116.          Return False
  117.       End Function 'Insert
  118.  
  119.       ' Remove: removes the specified number of text elements starting at the specified position
  120.       Public Function Remove(ByVal index As Integer, ByVal count As Integer) As Boolean
  121.          If count > 0 And 0 <= index And index < Length Then
  122.             Try
  123.                Dim idxLast As Integer = index + count
  124.                Dim charcount As Integer
  125.  
  126.                If idxLast < Length Then
  127.                   charcount = myIndex(idxLast) - myIndex(index)
  128.                Else
  129.                   charcount = myString.Length - myIndex(index)
  130.                End If
  131.  
  132.                Initialize(myString.Remove(myIndex(index), charcount))
  133.                Return True
  134.             Catch ' catch all and throw our exception
  135.               Throw (New StringWalkerException())
  136.             End Try
  137.          End If
  138.          Return False
  139.       End Function 'Remove
  140.  
  141.       ' IndexOf: 
  142.       Public Function IndexOf(ByVal str As String, ByVal index As Integer) As Integer
  143.          If 0 <= index And index < Length Then
  144.             Try
  145.                ' try and find the input string in the current string
  146.                Dim position As Integer = myIndex(index)
  147.                Dim foundAt As Integer = myString.IndexOf(str, position)
  148.  
  149.                ' if the string is found, then we need to see if it
  150.                ' can be matched to a text element index.
  151.                If -1 <> foundAt Then
  152.                   Dim i As Integer
  153.                   For i = 0 To myIndex.Length - 1
  154.                      If myIndex(i) = foundAt Then
  155.                         Return i
  156.                      End If
  157.                   Next i
  158.                End If
  159.             Catch ' catch all and throw our exception
  160.               Throw (New StringWalkerException())
  161.             End Try
  162.          End If
  163.          Return -1
  164.       End Function 'IndexOf
  165.  
  166.       ' private initialization method
  167.       Private Sub Initialize(ByVal str As String)
  168.          Try
  169.             myPos = 0
  170.             myString = str
  171.             myIndex = StringInfo.ParseCombiningCharacters(myString)
  172.  
  173.           Catch ' catch all and throw our exception
  174.             Throw (New StringWalkerException())
  175.           End Try
  176.       End Sub 'Initialize
  177.  
  178.    End Class 'StringWalker
  179.