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&)
Public Function Replace07( _
Text$, _
OldString$, _
NewString$, _
Optional ByVal Start& = 1, _
Optional ByVal Count& = -1, _
Optional ByVal Compare As VbCompareMethod = vbBinaryCompare _
) As String
' by G.Beckmann, G.Beckmann@NikoCity.de, 20001207
Dim iLenOld&, iLenNew&, iLenDst&
Dim pSrc&, pDst&, ccSrc&, ccDst&, c&
Dim iAsc1%, iAsc2%, SA As BStrAPI.SAFEARRAY1D, aiResult%()
If Start <= 0 Then Start = 1
Start = InStr(Start, Text, OldString, Compare)
If Start Then
If Count < 0 Then Count = &H7FFFFFFF
iLenNew = Len(NewString)
iLenOld = Len(OldString)
Select Case iLenOld
Case 0
Replace07 = Text
Case iLenNew
Replace07 = Text
If iLenNew <= 2 Then
SA.cDims = 1: SA.pvData = StrPtr(Replace07)
SA.lLbound1D = 1: SA.cElements1D = &H7FFFFFFF ' Len(Text)
SA.cbElements = 2
pSrc = VarPtrArray(aiResult)
RtlMoveMemory ByVal pSrc, VarPtr(SA), 4
If iLenNew = 1 Then
iAsc1 = AscW(NewString)
Do
aiResult(Start) = iAsc1
Start = InStr(Start + 1, Text, OldString, Compare)
Count = Count - 1
Loop Until (Start = 0) Or (Count = 0)
Else
RtlMoveMemory iAsc1, ByVal StrPtr(NewString), 2
RtlMoveMemory iAsc2, ByVal StrPtr(NewString) + 2, 2
Do
aiResult(Start) = iAsc1
aiResult(Start + 1) = iAsc2
Start = InStr(Start + 2, Text, OldString, Compare)
Count = Count - 1
Loop Until (Start = 0) Or (Count = 0)
End If
RtlZeroMemory ByVal pSrc, 4
Else
Do
Mid$(Replace07, Start, iLenNew) = NewString
Start = InStr(Start + iLenOld, Text, OldString, Compare)
Count = Count - 1
Loop Until (Start = 0) Or (Count = 0)
End If
Case Else 'ilenNew <> iLenOld
ccSrc = Start
Do
ccSrc = InStr(ccSrc + iLenOld, Text, OldString, Compare)
c = c + 1
Loop Until (ccSrc = 0) Or (Count = c)
iLenDst = Len(Text) + (iLenNew - iLenOld) * c
Replace07 = BStrAPI.SysAllocStringLen(vbNullString, iLenDst)
pSrc = StrPtr(Text) - 2
pDst = StrPtr(Replace07) - 2
ccSrc = 1
If iLenNew Then
ccDst = 1
Do
Count = Start - ccSrc
If Count Then
RtlMoveMemory ByVal pDst + ccDst + ccDst, _
ByVal pSrc + ccSrc + ccSrc, Count + Count
ccDst = ccDst + Count
End If
Mid$(Replace07, ccDst, iLenNew) = NewString
ccDst = ccDst + iLenNew
ccSrc = Start + iLenOld
Start = InStr(ccSrc, Text, OldString, Compare)
c = c - 1
Loop Until c = 0
Count = iLenDst - (ccDst - 1)
If Count > 0 Then
RtlMoveMemory ByVal pDst + ccDst + ccDst, _
ByVal pSrc + ccSrc + ccSrc, Count + Count
End If
Else
ccDst = 2
Do
Count = (Start - ccSrc) * 2
If Count Then
RtlMoveMemory ByVal pDst + ccDst, _
ByVal pSrc + ccSrc + ccSrc, Count
ccDst = ccDst + Count
End If
ccSrc = Start + iLenOld
Start = InStr(ccSrc, Text, OldString, Compare)
c = c - 1
Loop Until c = 0
Count = iLenDst * 2 - (ccDst - 2)
If Count > 0 Then
RtlMoveMemory ByVal pDst + ccDst, _
ByVal pSrc + ccSrc + ccSrc, Count
End If
End If
End Select
Else
Replace07 = Text
End If
End Function
|