Results 1 to 7 of 7

Thread: Need help optimizing URLEncode, or rewriting it

Threaded View

  1. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Need help optimizing URLEncode, or rewriting it

    You can also always get entirely rid of the slow string processing routines, such as Mid$:
    VB Code:
    1. Public Function URLencode_Merri(ByRef URL As String) As String
    2.     Static barOut() As Byte, barURL() As Byte
    3.     Dim lngA As Long, bytChar As Byte, bytNew As Byte, lngPos As Long
    4.     ' check length
    5.     If LenB(URL) = 0 Then Exit Function
    6.     ' convert input string to byte array
    7.     barURL = URL
    8.     ' reserve space for output string
    9.     ReDim Preserve barOut(LenB(URL) * 3 - 1)
    10.     ' happily loop ever after
    11.     For lngA = 0 To UBound(barURL) Step 2
    12.         ' minimal optimization trick
    13.         bytChar = barURL(lngA)
    14.         ' check what we have here
    15.         Select Case bytChar
    16.             ' numbers and letters
    17.             Case 48 To 57, 65 To 90, 97 To 122
    18.                 barOut(lngPos) = bytChar
    19.                 lngPos = lngPos + 2
    20.             ' space
    21.             Case 32
    22.                 barOut(lngPos) = 43 ' "+"
    23.                 lngPos = lngPos + 2
    24.             ' anything else
    25.             Case Else
    26.                 barOut(lngPos) = 37 ' "%"
    27.                 ' four upper bits
    28.                 bytNew = ((bytChar And &HF0) \ &H10) Or 48
    29.                 If bytNew < 58 Then
    30.                     barOut(lngPos + 2) = bytNew
    31.                 Else
    32.                     barOut(lngPos + 2) = bytNew + 7
    33.                 End If
    34.                 ' four lower bits
    35.                 bytNew = (bytChar And &HF) Or 48
    36.                 If bytNew < 58 Then
    37.                     barOut(lngPos + 4) = bytNew
    38.                 Else
    39.                     barOut(lngPos + 4) = bytNew + 7
    40.                 End If
    41.                 ' next starting position
    42.                 lngPos = lngPos + 6
    43.         End Select
    44.     Next lngA
    45.     ' error check
    46.     If lngPos = 0 Then Exit Function
    47.     ' resize output
    48.     ReDim Preserve barOut(lngPos - 1)
    49.     ' convert output to a string
    50.     URLencode_Merri = CStr(barOut)
    51. End Function

    Compile and you're closer to programmer's heaven. Closer, but not there.



    If you still need more...
    VB Code:
    1. Option Explicit
    2.  
    3. Dim URLencode_table(255) As Byte
    4. Dim HexTableUpper(255) As Byte
    5. Dim HexTableLower(255) As Byte
    6.  
    7. Public Sub Init_URLencode_table()
    8.     Dim bytNew As Byte, lngA As Long
    9.     For lngA = 0 To 255
    10.         Select Case lngA
    11.             Case 48 To 57, 65 To 90, 97 To 122
    12.                 URLencode_table(lngA) = CByte(lngA)
    13.             Case 32
    14.                 URLencode_table(lngA) = 43
    15.             Case Else
    16.                 URLencode_table(lngA) = 37
    17.         End Select
    18.        
    19.         bytNew = ((lngA And &HF0) \ &H10) Or 48
    20.         If bytNew < 58 Then
    21.             HexTableUpper(lngA) = bytNew
    22.         Else
    23.             HexTableUpper(lngA) = bytNew + 7
    24.         End If
    25.        
    26.         bytNew = (lngA And &HF) Or 48
    27.         If bytNew < 58 Then
    28.             HexTableLower(lngA) = bytNew
    29.         Else
    30.             HexTableLower(lngA) = bytNew + 7
    31.         End If
    32.     Next lngA
    33. End Sub
    34. Public Function URLencode_Merri(ByRef URL As String) As String
    35.     Static barOut() As Byte, barURL() As Byte
    36.     Dim lngA As Long, bytChar As Byte, lngPos As Long
    37.     ' init the table we need
    38.     If URLencode_table(0) = 0 Then Init_URLencode_table
    39.     ' check length
    40.     If LenB(URL) = 0 Then Exit Function
    41.     ' convert input string to byte array
    42.     barURL = URL
    43.     ' reserve space for output string
    44.     ReDim Preserve barOut(LenB(URL) * 3 - 1)
    45.     ' happily loop ever after
    46.     For lngA = 0 To UBound(barURL) Step 2
    47.         bytChar = URLencode_table(barURL(lngA))
    48.         barOut(lngPos) = bytChar
    49.         If bytChar <> 37 Then
    50.             lngPos = lngPos + 2
    51.         Else
    52.             barOut(lngPos + 2) = HexTableUpper(barURL(lngA))
    53.             barOut(lngPos + 4) = HexTableLower(barURL(lngA))
    54.             lngPos = lngPos + 6
    55.         End If
    56.     Next lngA
    57.     ' error check
    58.     If lngPos = 0 Then Exit Function
    59.     ' resize output
    60.     ReDim Preserve barOut(lngPos - 1)
    61.     ' convert output to a string
    62.     URLencode_Merri = CStr(barOut)
    63. End Function

    Minimizing all calculations within the loop.
    Last edited by Merri; Jul 11th, 2006 at 03:35 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width