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
Private Const FADF_AUTO As Long = &H1
Private Const FADF_FIXEDSIZE As Long = &H10
' unrem if not declared elsewhere:
'Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dst As Any, src As Any, ByVal nBytes As Long)
'Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (Ptr() As Any) As Long
Public Function StrReverse09(Text As String) As String
' By Guy Gervais, ggervais@videotron.ca, 19 Nov 2001
' - Requires a reference to the "Split03.tlb" type library
Dim iLTx As Long ' Length of "Text" parameter
Dim iInStr() As Integer ' Integer, since we will be flipping a Unicode string
Dim SAiInStr As SafeArray1D
Dim iOutStr() As Integer
Dim SAiOutStr As SafeArray1D
Dim iBgn As Long ' Our pointer from the beginning
Dim iEnd As Long ' Our pointer from the end
iLTx = Len(Text)
If iLTx = 0 Then Exit Function ' Reversing a null is FAST!
' Map Integer array to InString data
With SAiInStr
.cDims = 1
.cbElements = 2&
.cElements = iLTx
.fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
.pvData = StrPtr(Text)
End With
CopyMemory ByVal VarPtrArray(iInStr), VarPtr(SAiInStr), 4&
' Alloc return string
StrReverse09 = StringHelpers.SysAllocStringLen(ByVal 0&, iLTx)
' Map Interger array to OutString data
With SAiOutStr
.cDims = 1
.cbElements = 2&
.cElements = iLTx
.fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
.pvData = StrPtr(StrReverse09)
End With
CopyMemory ByVal VarPtrArray(iOutStr), VarPtr(SAiOutStr), 4&
' Reverse the [in] str to the [out] str
iBgn = 0&
iEnd = iLTx - 1&
Do
iOutStr(iBgn) = iInStr(iEnd)
iOutStr(iEnd) = iInStr(iBgn)
iBgn = iBgn + 1&
iEnd = iEnd - 1&
Loop Until iBgn > iEnd
' Clean up
CopyMemory ByVal VarPtrArray(iOutStr), 0&, 4&
CopyMemory ByVal VarPtrArray(iInStr), 0&, 4&
End Function
|