VBspeed / VB6 to VB5 / StrReverse
VBspeed © 2000-10, updated: 21-Nov-2001
StrReverse


The Definition
Function StrReverse
Returns a string in which the character order of a specified string is reversed.
Native to VB6, but not to VB5.
Declaration:
Function StrReverse(sExpression As String) As String
Arguments:
sExpressionthe string whose characters are to be reversed
Argument Values:Return Values:
sExpression is """" (zero-length string)


The Charts
Calls
 sRet = StrReverse(sString)
Call 1 Len(sString) is 50
Call 2 Len(sString) is 5000
 VB5
CodeAuthorDopingNotes
StrReverseVB6  
StrReverse01 [traditional]  
StrReverse02 [traditional]  
StrReverse03 Donald  
StrReverse04 Donald  
StrReverse05 GuidoAPI 
StrReverse06 Peter  
StrReverse07 MattTLB 
StrReverse08 PaulAPI,ASM 
StrReverse09 GuyAPI,TLB 
StrReverse10 GuyAPI 
Call 1
   
940.55120.262µs
725.9176.829µs
56.0217.852µs
69.9129.399µs
21.073.183µs
826.6979.137µs
11.002.965µs
X0.902.683µs
41.674.963µs
31.514.472µs
Call 2
   
9398.9468,152µs
744.257,559µs
53.45590µs
64.39750µs
41.17200µs
844.997,686µs
31.16198µs
X0.4781µs
11.00171µs
21.14195µs
 VB6
CodeAuthorDopingNotes
StrReverse VB6  
StrReverse01 [traditional]  
StrReverse02 [traditional]  
StrReverse03 Donald  
StrReverse04 Donald  
StrReverse05 GuidoAPI 
StrReverse06 Peter  
StrReverse07 MattTLB 
StrReverse08 PaulAPI,ASM 
StrReverse09 GuyAPI,TLB 
StrReverse10 GuyAPI 
Call 1
11.001.476µs
1095.59141.060µs
865.6796.904µs
612.2518.078µs
723.4634.619µs
32.203.250µs
966.2997.820µs
22.032.990µs
X2.063.040µs
53.805.607µs
42.643.903µs
Call 2
11.0064µs
101,125.6571,858µs
8149.999,575µs
69.25590µs
712.11773µs
53.13200µs
9151.299,658µs
43.13200µs
X1.2479µs
22.97189µs
33.06195µs
Conclusions
If you're really into reversing strings upgrade to VB6, but you can come pretty close ...
And, see how the traditional versions suck when the strings get longer (Call 2)!
Mail your code! How to read all those numbers


The Code
StrReverse01
submitted 10-Sep-2000 by Donald Lessau  
Doping: none
Public Function StrReverse01(sExpression As String) As String
' by [traditional]
  Dim i As Integer
  For i = Len(sExpression) To 1 Step -1
    StrReverse01 = StrReverse01 & Mid$(sExpression, i, 1)
  Next
End Function
Author's comments:
Donald's comments:

top | charts


StrReverse02
submitted 10-Sep-2000 by Donald Lessau  
Doping: none
Public Function StrReverse02(sExpression As String) As String
' by [traditional]
  Dim i As Long
  Dim lenExpr As Long
  Dim charTmp As String
  
  lenExpr = Len(sExpression)
  StrReverse02 = sExpression
  
  For i = 1 To lenExpr \ 2
    charTmp = Mid$(StrReverse02, lenExpr - i + 1, 1)
    Mid$(StrReverse02, lenExpr - i + 1, 1) = Mid$(StrReverse02, i, 1)
    Mid$(StrReverse02, i, 1) = charTmp
  Next
  
End Function
Author's comments:
Donald's comments:

top | charts


StrReverse03
submitted 13-Sep-2000 by Donald Lessau  
Doping: none
Public Function StrReverse03(sExpression As String) As String
' by Donald, donald@xbeat.net, 20000913
  Dim i As Long
  Dim ubArr As Long
  Dim arrByte() As Byte
  Dim arrByteRev() As Byte
  
  If Len(sExpression) Then
    
    ' no need to ReDim
    arrByte = sExpression
    ubArr = UBound(arrByte)
    ReDim arrByteRev(ubArr)
    
    ' Step 2: it's Unicode
    For i = 0 To ubArr Step 2
      ' set double bytes pairwise
      ' eg. pos 01 = 67, 23 = 45, 45 = 23, 67 = 01
      arrByteRev(i) = arrByte(ubArr - i - 1)
      arrByteRev(i + 1) = arrByte(ubArr - i)
    Next
  
    StrReverse03 = arrByteRev
  
  End If
  
End Function
Author's comments: the previous version (10-sep-2000) was buggy: (a) error on sExpression = "", (b) incorrect with Unicode chars using HiByte (thanx Guido!)
Donald's comments:

top | charts


StrReverse04
submitted 13-Sep-2000 by Donald Lessau  
Doping: none
Public Function StrReverse04(sExpression As String) As String
' by Donald, donald@xbeat.net, 20000913
  Dim i As Long
  Dim ubArr As Long
  Dim arrByte() As Byte
  Dim arrByteRev() As Byte
  
  If Len(sExpression) Then
    
    ' no need to ReDim
    arrByte = StrConv(sExpression, vbFromUnicode)
    ubArr = UBound(arrByte)
    ReDim arrByteRev(ubArr)
    
    For i = 0 To ubArr
      arrByteRev(i) = arrByte(ubArr - i)
    Next
  
    StrReverse04 = StrConv(arrByteRev, vbUnicode)
  
  End If
  
End Function
Author's comments:
Donald's comments:

top | charts


StrReverse05
submitted 14-Sep-2000 by Guido Beckmann  
Doping: API, wrapped in class
' © G.Beckmann   eMail: G.Beckmann@NikoCity.de

Option Explicit

'// API
' 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 SAFEARRAYBOUND
    cElements       As Long
    lLbound         As Long
End Type

Private Type SAFEARRAY1D
    cDims           As Integer
    fFeatures       As Integer
    cbElements      As Long
    cLocks          As Long
    pvData          As Long
    Bounds(0 To 0)  As SAFEARRAYBOUND
End Type


'// Locales
Private m_aIntSrc%()
Private m_aIntDst%()
Private m_pArrSrc&
Private m_pArrDst&
Private m_saSrc     As SAFEARRAY1D
Private m_saDst     As SAFEARRAY1D


Private Sub Class_Initialize()
    m_saSrc.cbElements = 2
    m_saSrc.cDims = 1
    m_saSrc.Bounds(0).cElements = &H7FFFFFFF
    m_saDst = m_saSrc
    
    m_pArrSrc = VarPtrArray(m_aIntSrc)
    m_pArrDst = VarPtrArray(m_aIntDst)
        
    RtlMoveMemory ByVal m_pArrSrc, VarPtr(m_saSrc), 4
    RtlMoveMemory ByVal m_pArrDst, VarPtr(m_saDst), 4

End Sub

Private Sub Class_Terminate()
    RtlZeroMemory ByVal m_pArrSrc, 4
    RtlZeroMemory ByVal m_pArrDst, 4
End Sub


' rev. 1.1 09/14/2000
Friend Function Reverse(s As String) As String
    Dim iLen&, c&
    
    iLen = Len(s)
    Reverse = Space$(iLen)
    
    m_saSrc.pvData = StrPtr(s)
    m_saDst.pvData = StrPtr(Reverse)
    
    iLen = iLen - 1
    For c = 0 To iLen
        m_aIntDst(c) = m_aIntSrc(iLen - c)
    Next c
End Function
Author's comments:
Donald's comments: As you see, a simple problem can be wrapped into a pretty advanced class, and it even pays: it's about 3-4 times faster than the next best routine!
  Download this class (VB5-compatible, zipped, 1KB)

top | charts


StrReverse06
submitted 26-Sep-2000 by Peter Weighill  
Doping: none
Public Function StrReverse06(sExpression As String) As String
' by Peter Weighill, pweighill@btinternet.com, 000926
' [another traditional]
  Dim i As Long
  Dim lenExpr As Long

  lenExpr = Len(sExpression)
  StrReverse06 = sExpression

  For i = 1 To lenExpr
    Mid$(StrReverse06, lenExpr - i + 1, 1) = Mid$(sExpression, i, 1)
  Next i
  
End Function
Author's comments:
Donald's comments: Looking at the code StrReverse06 should be a bit faster then StrReverse02, but it isn't. Compilation to native code isn't a 1:1 affair ...

top | charts


StrReverse07
submitted 07-Oct-2000 by Matt Curland    PowerVB
Doping: needs typelib JoinDecl.tlb and module ArrayOwnerIgnoreOnly.bas (download these files, VB5-compatible).
Private Type OwnedInteger
    Owner As ArrayOwner
    pSA() As Integer
End Type
Private m_DerefInteger As OwnedInteger


Public Function StrReverse07(sExpression As String) As String ' by Matt Curland, mattcur@microsoft.com, www.PowerVB.com, 20001007 Dim i As Long Dim cch As Long Dim iTemp As Integer If Len(sExpression) = 0 Then Exit Function StrReverse07 = sExpression With m_DerefInteger If .Owner.SA.cDims = 0 Then #If NOVBOOST = 0 Then If VBoost Is Nothing Then InitVBoost #End If InitArrayOwner .Owner, LenB(.pSA(0)), 0 End If .Owner.SA.pvData = StrPtr(StrReverse07) cch = Len(StrReverse07) .Owner.SA.cElements = cch For i = 0 To cch \ 2 - 1 iTemp = .pSA(i) .pSA(i) = .pSA(cch - i - 1) .pSA(cch - i - 1) = iTemp Next i End With End Function
Author's comments:
Donald's comments:

top | charts


StrReverse08
submitted 25-Sep-2001 by Paul  
Doping: API and ASM (Assembly Snippets), wrapped in class (download the class, VB5-compatible, zipped, 2KB)
The code is a bit too long to be displayed here ... download it to have a look.
Author's comments:
Donald's comments: Very, very fast -- even in the IDE!

top | charts


StrReverse09
submitted 19-Nov-2001 by Guy Gervais  
Doping: API, TLB StringHelpers
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
Author's comments:
Donald's comments:

top | charts


StrReverse10
submitted 21-Nov-2001 by Guy Gervais  
Doping: API (cf. Dope'n'Declarations)
Public Function StrReverse10(ByRef Text As String) As String
' By Guy Gervais, ggervais@videotron.ca, 21 Nov 2001

Dim iLTx    As Long         ' Length of "Text" parameter
Dim iStr()  As Integer
Dim SAiStr  As SafeArray1D
Dim iBgn    As Long         ' Our pointer from the beginning
Dim iEnd    As Long         ' Our pointer from the end
Dim iTmp    As Integer      ' Temp var for swap
    
    iLTx = Len(Text)
    If iLTx = 0 Then Exit Function

    StrReverse10 = Text

    With SAiStr
        .cDims = 1
        .cbElements = 2
        .cElements = iLTx
        .fFeatures = FADF_AUTO Or FADF_FIXEDSIZE
        .pvData = StrPtr(StrReverse10)
    End With
    CopyMemory ByVal VarPtrArray(iStr), VarPtr(SAiStr), 4

    iEnd = UBound(iStr)
    For iBgn = 0 To iEnd \ 2
        iTmp = iStr(iBgn)
        iStr(iBgn) = iStr(iEnd)
        iStr(iEnd) = iTmp
        iEnd = iEnd - 1
    Next

    SAiStr.pvData = 0

End Function
Author's comments :
Donald's comments :

top | charts




VBspeed © 2000-10 by Donald Lessau