Private Declare Sub CopyMemLng Lib "kernel32.dll" Alias "RtlMoveMemory" ( _
ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Public Function Replicate08(ByVal Number As Long, ByRef Pattern As String) As String
' by Nick Paldino, nicholas.paldino@exisconsulting.com, 20001206, rev 001 20011123
' Only Replicate if there is a desire to Replicate
' (the number of times is greater than zero).
If (Number > 0) Then
' Get the length of the string (in bytes).
Dim plngPatternLength As Long
plngPatternLength = LenB(Pattern)
' Allocate the length of the string.
Replicate08 = Space$(Number * Len(Pattern))
''was: Replicate08 = Space(Number * Len(Pattern))
' Store the bytes copied, as well as the bytes left. The current number of bytes left
' is the length of the string in bytes.
Dim plngBytesCopied As Long, plngBytesLeft As Long
plngBytesLeft = LenB(Replicate08)
' The pointer to the beginning of the source string, and the destination string.
' Also store a pointer to the beginning of the replicated string. This is where the
' already replicated string lies.
Dim plngSourcePointer As Long, plngDestPointer As Long, plngOriginalDestPointer As Long
plngSourcePointer = StrPtr(Pattern)
plngOriginalDestPointer = StrPtr(Replicate08)
plngDestPointer = plngOriginalDestPointer
' Copy the first section.
CopyMemLng plngDestPointer, plngSourcePointer, plngPatternLength
' Increment and decrement for the first copy.
plngBytesLeft = plngBytesLeft - plngPatternLength
plngBytesCopied = plngPatternLength
plngDestPointer = plngDestPointer + plngPatternLength
' Loop
Do While (plngBytesCopied < plngBytesLeft)
' Copy the number of characters copied into the buffer at the next available space.
CopyMemLng plngDestPointer, plngOriginalDestPointer, plngBytesCopied
' Reduce the characters left.
plngBytesLeft = plngBytesLeft - plngBytesCopied
' Increment the destination pointer.
plngDestPointer = plngDestPointer + plngBytesCopied
' Double the bytes copied.
plngBytesCopied = plngBytesCopied * 2
Loop
' Copy for the last time.
CopyMemLng plngDestPointer, plngOriginalDestPointer, plngBytesLeft
End If
' That's all folks.
End Function
|