FF_PARSE, FF_PARSEANY, FF_PARSECOUNT, FF_PARSECOUNTANY

Started by Paul Squires, August 19, 2015, 01:39:49 PM

Previous topic - Next topic

Paul Squires




''
''  _FF_PARSE_INTERNAL
''  Used by both FF_Parse and FF_ParseAny (internal function)
''
Function _FF_Parse_Internal( ByRef sMainString   As String, _
                             ByRef sDelimiter    As String, _
                             ByRef nPosition     As Integer, _
                             ByRef nIsAny        As Integer, _
                             ByRef nLenDelimiter As Integer _
                             ) As String
                   
    ' Returns the nPosition-th substring in a string sMainString with separations sDelimiter (one or more charcters),
    ' beginning with nPosition = 1

    Dim As Integer i, j, count
    Dim s As String
    Dim fReverse As Integer = Iif(nPosition < 0, True, False)
   
    nPosition = Abs(nPosition)
    count = 0
    i = 1
    s = sMainString
   
    If fReverse Then
       ' Reverse search
       ' Get the start of the token (j) by searching in reverse
       If nIsAny Then
          i = InstrRev(sMainString, Any sDelimiter)
       Else
          i = InstrRev(sMainString, sDelimiter)
       End If
       Do While i > 0        ' if not found loop will be skipped
          j = i + nLenDelimiter
          count += 1
          i = i - nLenDelimiter
          If count = nPosition Then Exit Do
          If nIsAny Then
             i = InStrRev(sMainString, Any sDelimiter, i )
          Else
             i = InStrRev(sMainString, sDelimiter, i )
          End If   
       Loop
       If i = 0 Then j = 1

       ' Now continue forward to get the end of the token
       If nIsAny Then
          i = Instr(j, sMainString, Any sDelimiter)
       Else
          i = Instr(j, sMainString, sDelimiter)
       End If
       If (i > 0) Or (count = nPosition) Then
           If i = 0 Then
               s = Mid(sMainString, j)
           Else
               s = Mid(sMainString, j, i - j)
           End If
       End If
   
    Else             
   
       ' Forward search
       Do
           j = i
           If nIsAny Then
              i = Instr(i, sMainString, Any sDelimiter)
           Else
              i = Instr(i, sMainString, sDelimiter)
           End If   
           If i > 0 Then count += 1: i += nLenDelimiter
       Loop Until (i = 0) Or (count = nPosition)

       If (i > 0) Or (count = nPosition - 1) Then
           If i = 0 Then
               s = Mid(sMainString, j)
           Else
               s = Mid(sMainString, j, i - nLenDelimiter - j)
           End If
       End If
    End If
     
    Return s

End Function



''
''  FF_PARSE
''  Return a delimited field from a string expression.
''
''  Delimiter contains a string of one or more characters that must be fully matched to be successful.
''  If nPosition evaluates to zero or is outside of the actual field count, an empty string is returned.
''  If nPosition is negative then fields are searched from the right to left of the MainString.
''  Delimiters are case-sensitive,
''
Function FF_Parse( ByRef sMainString As String, _
                   ByRef sDelimiter  As String, _
                   ByVal nPosition   As Integer _
                   ) As String
   ' The parse must match the entire deliminter string
   Function = _FF_Parse_Internal( sMainString, sDelimiter, nPosition, False, Len(sDelimiter) )
End Function


''
''  FF_PARSEANY
''  Return a delimited field from a string expression.
''
''  Delimiter contains a set of characters (one or more), any of which may act as a delimiter character.
''  If nPosition evaluates to zero or is outside of the actual field count, an empty string is returned.
''  If nPosition is negative then fields are searched from the right to left of the MainString.
''  Delimiters are case-sensitive,
''
Function FF_ParseAny( ByRef sMainString As String, _
                      ByRef sDelimiter  As String, _
                      ByVal nPosition   As Integer _
                      ) As String
   ' The parse must match one character (len=1) in the delimiter string
   Function = _FF_Parse_Internal( sMainString, sDelimiter, nPosition, True, 1 )
End Function




''
''  FF_PARSECOUNT
''  Return the count of delimited fields from a string expression.
''
''  If MainString is empty (a null string) or contains no delimiter character(s), the string
''  is considered to contain exactly one sub-field. In this case, FF_PARSECOUNT returns the value 1.
''  Delimiter contains a string (one or more characters) that must be fully matched.
''  Delimiters are case-sensitive,
''
Function FF_ParseCount( ByRef sMainString As String, _
                        ByRef sDelimiter  As String _
                        ) As Integer
                   
    Dim count As Integer = 1
    Dim i     As Integer = 1
   
    Do
        i = Instr(i, sMainString, sDelimiter)
        If i > 0 Then count += 1: i += Len(sDelimiter)
    Loop Until i = 0
   
    Return count
   
End Function


''
''  FF_PARSECOUNTANY
''  Return the count of delimited fields from a string expression.
''
''  If MainString is empty (a null string) or contains no delimiter character(s), the string
''  is considered to contain exactly one sub-field. In this case, FF_PARSECOUNTANY returns the value 1.
''  Delimiter contains a set of characters (one or more), any of which may act as a delimiter character.
''  Delimiters are case-sensitive,
''
Function FF_ParseCountAny( ByRef sMainString As String, _
                           ByRef sDelimiter  As String _
                           ) As Integer
                   
    Dim count As Integer = 1
    Dim i     As Integer = 1
   
    Do
        i = Instr(i, sMainString, Any sDelimiter)
        If i > 0 Then count += 1: i += 1
    Loop Until i = 0
   
    Return count
   
End Function

Paul Squires
PlanetSquires Software

Paul Squires

These were probably some of the more complicated functions because of the requirement to search from left to right and also right to left.
Paul Squires
PlanetSquires Software

Marc Pons

hi Paul

I've also done many string functions to compensate the missing string functions in fb.

I was trying to make them optimimized on speed
I've done most of them in c, and large use of pointers,
you can check here

http://www.freebasic.net/forum/viewtopic.php?f=8&t=22340