Development Anybody want to try my new parser?

Discussion in 'Software' started by djengiz, 8 Dec 2003.

  1. djengiz

    djengiz Pointless.

    Joined:
    16 Aug 2002
    Posts:
    1,129
    Likes Received:
    0
    Hi All,

    I wrote a parser last week. It is very fast. Wanna try the code? It is written in VB.

    Code:
    Public Function fParseString(strString As String, 
                                 lTokenNr As Long, _
                                 strDelim As String, 
                                 Optional bIncDelim As Boolean = False) As String
      Dim RetVal      As String
      Dim lStartPos   As Long
      Dim lDelimPos   As Long
      Dim x           As Long
      Dim lLenDelim   As Long
      
      lLenDelim = Len(strDelim)
      lStartPos = 1
      
      For x = 0 To lTokenNr
        lDelimPos = InStr(lStartPos, strString, strDelim)
        If lDelimPos = 0 Then
          RetVal = Mid(strString, _
                       lStartPos - IIf(bIncDelim = False, _
                                       0, _
                                       lLenDelim), _
                       (Len(strString) + 1) - lStartPos)
          Exit For
        ElseIf x = lTokenNr Then
          RetVal = Mid(strString, _
                       lStartPos, _
                       IIf(bIncDelim = False, _
                           lDelimPos - lStartPos, _
                           (lDelimPos - lStartPos) + lLenDelim))
          Exit For
        End If
        lStartPos = lDelimPos + lLenDelim
      Next x
      
      fParseString = RetVal
      
    End Function
    
     
  2. LaughingJon

    LaughingJon What's a Dremel?

    Joined:
    12 Feb 2003
    Posts:
    282
    Likes Received:
    0
    /me looks through his half cut eyes..

    wots it menna parse? :worried:
     
  3. michaeln3

    michaeln3 What's a Dremel?

    Joined:
    20 May 2003
    Posts:
    49
    Likes Received:
    0
    I've never used VB before, but it looks to me like you send it two strings, your data and your delimiter, and it splits it up according to the delimiter and gives it back to you.

    I think it's like php's explode();
     
  4. djengiz

    djengiz Pointless.

    Joined:
    16 Aug 2002
    Posts:
    1,129
    Likes Received:
    0
    It will parse a string. You pass the string that you want to parse, give the token number, the delimiter and you can choose if you want to add the delimiter to the token or not.

    for example:
    Code:
    Private sub s()
       Dim s as string
       s = fParseString("a b c d", 1, " ",true)
    end sub
    
    "b " will be returned
     
  5. djengiz

    djengiz Pointless.

    Joined:
    16 Aug 2002
    Posts:
    1,129
    Likes Received:
    0
    Well actually it does not split a string up. It checks if there are delimiters in the string with Instr. If so the function checks if its the correct token. If not it searches for the next delimiter.

    It is quite efficient because it doesnt use split. Split uses a lot of memory if the data to be parsed is large. Also my parser only loops once through the entire string.

    Another thing that I find handy is that tokens start with 0 instead of 1.
     

Share This Page