' VB5 -> msvbvm50.dll
Private Declare Function VarPtrArray& Lib "msvbvm60.dll" Alias "VarPtr" (ptr() As Any)
Private Declare Sub RtlMoveMemory Lib "kernel32" (dst As Any, src As Any, ByVal nBytes&)
Private Declare Sub RtlZeroMemory Lib "kernel32" (dst As Any, ByVal nBytes&)
Private Type SAFEARRAY1D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
cElements As Long
lLbound As Long
End Type
Public Function Tokenize04&(Expression$, ResultTokens$(), Delimiters$, Optional IncludeEmpty As Boolean)
' Tokenize02 by Donald, donald@xbeat.net
' modified by G.Beckmann, G.Beckmann@NikoCity.de
Const ARR_CHUNK& = 1024
Dim cExp&, ubExpr&
Dim cDel&, ubDelim&
Dim aExpr%(), aDelim%()
Dim sa1 As SAFEARRAY1D, sa2 As SAFEARRAY1D
Dim cTokens&, iPos&
ubExpr = Len(Expression)
ubDelim = Len(Delimiters)
sa1.cbElements = 2: sa1.cElements = ubExpr
sa1.cDims = 1: sa1.pvData = StrPtr(Expression)
RtlMoveMemory ByVal VarPtrArray(aExpr), VarPtr(sa1), 4
sa2.cbElements = 2: sa2.cElements = ubDelim
sa2.cDims = 1: sa2.pvData = StrPtr(Delimiters)
RtlMoveMemory ByVal VarPtrArray(aDelim), VarPtr(sa2), 4
If IncludeEmpty Then
ReDim Preserve ResultTokens(ubExpr)
Else
ReDim Preserve ResultTokens(ubExpr \ 2)
End If
ubDelim = ubDelim - 1
For cExp = 0 To ubExpr - 1
For cDel = 0 To ubDelim
If aExpr(cExp) = aDelim(cDel) Then
If cExp > iPos Then
ResultTokens(cTokens) = Mid$(Expression, iPos + 1, cExp - iPos)
cTokens = cTokens + 1
ElseIf IncludeEmpty Then
ResultTokens(cTokens) = vbNullString
cTokens = cTokens + 1
End If
iPos = cExp + 1
Exit For
End If
Next cDel
Next cExp
'/ remainder
If (cExp > iPos) Or IncludeEmpty Then
ResultTokens(cTokens) = Mid$(Expression, iPos + 1)
cTokens = cTokens + 1
End If
'/ erase or shrink
If cTokens = 0 Then
Erase ResultTokens()
Else
ReDim Preserve ResultTokens(cTokens - 1)
End If
'/ return ubound
Tokenize04 = cTokens - 1
'/ tidy up
RtlZeroMemory ByVal VarPtrArray(aExpr), 4
RtlZeroMemory ByVal VarPtrArray(aDelim), 4
End Function
|