Advertisement
Guest User

StringSplitExtensions-VB

a guest
Feb 20th, 2011
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.57 KB | None | 0 0
  1. Friend NotInheritable Class StringSplitExtensions
  2.     Private Sub New()
  3.     End Sub
  4.     <System.Runtime.CompilerServices.Extension> _
  5.     Public Shared Function Split(this As [String], isSplitSymbol As Predicate(Of Char)) As String()
  6.         Return Split(this, isSplitSymbol, Integer.MaxValue, StringSplitOptions.None)
  7.     End Function
  8.  
  9.     <System.Runtime.CompilerServices.Extension> _
  10.     Public Shared Function Split(this As [String], isSplitSymbol As Predicate(Of Char), options As StringSplitOptions) As String()
  11.         Return Split(this, isSplitSymbol, Integer.MaxValue, options)
  12.     End Function
  13.  
  14.     <System.Runtime.CompilerServices.Extension> _
  15.     Public Shared Function Split(this As [String], isSplitSymbol As Predicate(Of Char), count As Integer, options As StringSplitOptions) As String()
  16.         If count < 0 Then
  17.             Throw New ArgumentOutOfRangeException("count", "Count cannot be less than zero.")
  18.         End If
  19.         If (options < StringSplitOptions.None) OrElse (options > StringSplitOptions.RemoveEmptyEntries) Then
  20.             Throw New ArgumentException([String].Format("Illegal enum value: {0}.", CInt(options)))
  21.         End If
  22.  
  23.         Dim removeEmptyEntries As Boolean = options = StringSplitOptions.RemoveEmptyEntries
  24.         If (count = 0) OrElse (removeEmptyEntries AndAlso (this.Length = 0)) Then
  25.             Return New String(-1) {}
  26.         End If
  27.  
  28.         Dim sepList As Integer() = New Integer(this.Length - 1) {}
  29.         Dim numReplaces As Integer = this.MakeSeparatorList(isSplitSymbol, sepList)
  30.         If (numReplaces = 0) OrElse (count = 1) Then
  31.             Return New String() {this}
  32.         End If
  33.  
  34.         If removeEmptyEntries Then
  35.             Return this.InternalSplitOmitEmptyEntries(sepList, numReplaces, count)
  36.         Else
  37.             Return this.InternalSplitKeepEmptyEntries(sepList, numReplaces, count)
  38.         End If
  39.  
  40.     End Function
  41.  
  42.     <System.Runtime.CompilerServices.Extension> _
  43.     Private Shared Function MakeSeparatorList(this As [String], isSplitSymbol As Predicate(Of Char), ByRef sepList As Integer()) As Integer
  44.         Dim numReplaces As Integer = 0
  45.  
  46.         Dim length As Integer = sepList.Length, j As Integer = 0
  47.         While (j < this.Length) AndAlso (numReplaces < length)
  48.             If isSplitSymbol(this(j)) Then
  49.                 sepList(System.Math.Max(System.Threading.Interlocked.Increment(numReplaces),numReplaces - 1)) = j
  50.             End If
  51.             j += 1
  52.         End While
  53.  
  54.         Return numReplaces
  55.     End Function
  56.  
  57.     <System.Runtime.CompilerServices.Extension> _
  58.     Private Shared Function InternalSplitKeepEmptyEntries(this As [String], sepList As Integer(), numReplaces As Integer, count As Integer) As String()
  59.         Dim startIndex As Integer = 0
  60.         Dim index As Integer = 0
  61.         count -= 1
  62.         Dim actualCount As Integer = If((numReplaces < count), numReplaces, count)
  63.         Dim strArray As String() = New String(actualCount) {}
  64.         Dim i As Integer = 0
  65.         While (i < actualCount) AndAlso (startIndex < this.Length)
  66.             strArray(System.Math.Max(System.Threading.Interlocked.Increment(index),index - 1)) = this.Substring(startIndex, sepList(i) - startIndex)
  67.             startIndex = sepList(i) + 1
  68.             i += 1
  69.         End While
  70.         If (startIndex < this.Length) AndAlso (actualCount >= 0) Then
  71.             strArray(index) = this.Substring(startIndex)
  72.             Return strArray
  73.         End If
  74.         If index = actualCount Then
  75.             strArray(index) = [String].Empty
  76.         End If
  77.         Return strArray
  78.     End Function
  79.  
  80.     <System.Runtime.CompilerServices.Extension> _
  81.     Private Shared Function InternalSplitOmitEmptyEntries(this As [String], sepList As Integer(), numReplaces As Integer, count As Integer) As String()
  82.         Dim actualCount As Integer = If((numReplaces < count), (numReplaces + 1), count)
  83.         Dim strArray As String() = New String(actualCount - 1) {}
  84.         Dim startIndex As Integer = 0
  85.         Dim replaceIndex As Integer = 0
  86.         Dim i As Integer = 0
  87.         While (i < numReplaces) AndAlso (startIndex < this.Length)
  88.             If (sepList(i) - startIndex) > 0 Then
  89.                 strArray(System.Math.Max(System.Threading.Interlocked.Increment(replaceIndex),replaceIndex - 1)) = this.Substring(startIndex, sepList(i) - startIndex)
  90.             End If
  91.             startIndex = sepList(i) + 1
  92.             If replaceIndex = (count - 1) Then
  93.                 While (i < (numReplaces - 1)) AndAlso (startIndex = sepList(System.Threading.Interlocked.Increment(i)))
  94.                     startIndex += 1
  95.                 End While
  96.                 Exit While
  97.             End If
  98.             i += 1
  99.         End While
  100.         If startIndex < this.Length Then
  101.             strArray(System.Math.Max(System.Threading.Interlocked.Increment(replaceIndex),replaceIndex - 1)) = this.Substring(startIndex)
  102.         End If
  103.         Dim strArray2 As String() = strArray
  104.         If replaceIndex <> actualCount Then
  105.             strArray2 = New String(replaceIndex - 1) {}
  106.             For j As Integer = 0 To replaceIndex - 1
  107.                 strArray2(j) = strArray(j)
  108.             Next
  109.         End If
  110.         Return strArray2
  111.     End Function
  112. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement