Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.1 KiB

  1. VERSION 1.0 CLASS
  2. BEGIN
  3. MultiUse = -1 'True
  4. Persistable = 0 'NotPersistable
  5. DataBindingBehavior = 0 'vbNone
  6. DataSourceBehavior = 0 'vbNone
  7. MTSTransactionMode = 0 'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "Tokenizer"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15. Private p_strTokens As String
  16. Private p_intIndex As Long
  17. Private p_arrNextWord(1) As String
  18. Private Sub Class_Initialize()
  19. p_arrNextWord(0) = " "
  20. p_arrNextWord(1) = vbTab
  21. End Sub
  22. Public Sub Init(ByVal i_strTokens As String)
  23. p_strTokens = i_strTokens
  24. p_intIndex = 1
  25. End Sub
  26. Public Sub NormalizeTokens(ByRef i_arrNormalTokens() As String)
  27. Dim intIndex As Long
  28. For intIndex = LBound(i_arrNormalTokens) To UBound(i_arrNormalTokens)
  29. p_strTokens = Replace(p_strTokens, i_arrNormalTokens(intIndex), _
  30. i_arrNormalTokens(intIndex), Compare:=vbTextCompare)
  31. Next
  32. End Sub
  33. Public Function GetUpTo( _
  34. ByVal i_strMatch As String, _
  35. Optional ByVal i_blnIncludeMatch As Boolean = True, _
  36. Optional ByVal i_CompareMethod As VbCompareMethod = vbBinaryCompare _
  37. ) As String
  38. Dim intPosMatch As Long
  39. Dim intLength As Long
  40. GetUpTo = ""
  41. intPosMatch = InStr(p_intIndex, p_strTokens, i_strMatch, i_CompareMethod)
  42. If (intPosMatch > 0) Then
  43. If (i_blnIncludeMatch) Then
  44. intLength = Len(i_strMatch)
  45. End If
  46. GetUpTo = Mid$(p_strTokens, p_intIndex, intPosMatch + intLength - p_intIndex)
  47. p_intIndex = intPosMatch + intLength
  48. End If
  49. End Function
  50. Public Function ClosestMatch( _
  51. ByRef i_arrMatches() As String, _
  52. Optional ByRef o_intMatchPosition As Long, _
  53. Optional ByVal i_CompareMethod As VbCompareMethod = vbBinaryCompare _
  54. ) As String
  55. Dim intMatchPosition As Long
  56. Dim intPosMatch As Long
  57. Dim intIndex As Long
  58. ' The only difference between intMatchPosition and o_intMatchPosition is that
  59. ' if there is no match, the former is Len(p_strTokens) and the latter is 0.
  60. ClosestMatch = ""
  61. o_intMatchPosition = 0
  62. intMatchPosition = Len(p_strTokens)
  63. For intIndex = LBound(i_arrMatches) To UBound(i_arrMatches)
  64. intPosMatch = InStr(p_intIndex, p_strTokens, i_arrMatches(intIndex), i_CompareMethod)
  65. If (intPosMatch > 0 And intPosMatch < intMatchPosition) Then
  66. ClosestMatch = i_arrMatches(intIndex)
  67. o_intMatchPosition = intPosMatch
  68. intMatchPosition = intPosMatch
  69. End If
  70. Next
  71. End Function
  72. Public Function GetUpToClosestMatch( _
  73. ByRef i_arrMatches() As String, _
  74. Optional ByRef o_strMatchedToken As String, _
  75. Optional ByVal i_blnIncludeMatch As Boolean = True, _
  76. Optional ByVal i_CompareMethod As VbCompareMethod = vbBinaryCompare _
  77. ) As String
  78. Dim intPosMatch As Long
  79. Dim intLength As Long
  80. GetUpToClosestMatch = ""
  81. o_strMatchedToken = ClosestMatch(i_arrMatches, intPosMatch, i_CompareMethod)
  82. If (intPosMatch > 0) Then
  83. If (i_blnIncludeMatch) Then
  84. intLength = Len(o_strMatchedToken)
  85. End If
  86. GetUpToClosestMatch = Mid$(p_strTokens, p_intIndex, _
  87. intPosMatch + intLength - p_intIndex)
  88. p_intIndex = intPosMatch + intLength
  89. End If
  90. End Function
  91. Public Function GetAfter( _
  92. ByVal i_strMatch As String, _
  93. Optional ByVal i_CompareMethod As VbCompareMethod = vbBinaryCompare _
  94. ) As String
  95. Dim intPosMatch As Long
  96. GetAfter = ""
  97. intPosMatch = InStr(p_intIndex, p_strTokens, i_strMatch, i_CompareMethod)
  98. If (intPosMatch > 0) Then
  99. p_intIndex = intPosMatch + Len(i_strMatch)
  100. GetAfter = Mid$(p_strTokens, p_intIndex)
  101. End If
  102. End Function
  103. Sub PushBack(ByRef TokenString As String)
  104. Me.Init TokenString + Mid$(p_strTokens, p_intIndex)
  105. End Sub
  106. Private Sub EatWsp()
  107. Dim cNext As String
  108. Do While (Not EOF())
  109. cNext = (Mid$(p_strTokens, p_intIndex, 1))
  110. Select Case cNext
  111. Case " ", vbTab
  112. p_intIndex = p_intIndex + 1
  113. Case Else
  114. Exit Do
  115. End Select
  116. Loop
  117. End Sub
  118. Public Property Get EOF() As Boolean
  119. EOF = (p_intIndex > Len(p_strTokens))
  120. End Property
  121. Public Property Get NextWordOrString(Optional ByRef bIsString As Boolean) As String
  122. If (Mid$(p_strTokens, 1, 1) = """") Then
  123. p_intIndex = p_intIndex + 1
  124. NextWordOrString = Me.GetUpTo("""", i_blnIncludeMatch:=False)
  125. p_intIndex = p_intIndex + 1
  126. bIsString = True
  127. Else
  128. NextWordOrString = Me.NextWord
  129. bIsString = False
  130. End If
  131. ' EatWsp
  132. End Property
  133. 'Public Property Let NextWordOrString(ByVal strNextWordOrString As String)
  134. ' Me.PushBack strNextWordString + " "
  135. 'End Property
  136. Public Property Get NextWord() As String
  137. NextWord = Me.GetUpToClosestMatch(p_arrNextWord)
  138. If (Len(NextWord) = 0) Then
  139. NextWord = Mid$(p_strTokens, p_intIndex)
  140. p_intIndex = Len(p_strTokens) + 1
  141. Else
  142. EatWsp
  143. NextWord = Trim$(NextWord)
  144. End If
  145. End Property
  146. Public Property Let NextWord(ByVal strNextWord As String)
  147. Me.PushBack strNextWord + " "
  148. End Property