Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

ParseReplace function

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • ParseReplace function

    Code:
    ' -------------------------------------------------------------------------------
    ' Replaces a string inside the specified Parse$ field
    ' -------------------------------------------------------------------------------
    ' sSource           - The string to modify (must contain at least iIndex fields)
    ' sSep              - The field delimeter(s)
    ' iIndex            - The index of the field to replace
    ' sRepStr           - The new text
    '
    '  - If the source string is empty, the replacement string is copied
    '  - If the replacement string is empty, the field is set to empty
    '  - If the number of fields is less than the specified field, the function quits
    '  - If the separating character is empty, a comma is used
    '  - Only accepts 1 separating character
    ' -------------------------------------------------------------------------------
    Sub ParseReplace(ByRef sSource As String, ByVal sSep As String, ByVal iIndex As Long, ByVal sRepStr As String)
     Register i As Long, c As Long: Local b As Byte Ptr, s As Byte
     If Len(sSource) = 0 Then sSource = sRepStr: Exit Sub
     If Len(sSep) Then s = Asc(sSep) Else s = 44
     c = 1: b = StrPtr(sSource)
     For i = 0 To Len(sSource)
         If c = iIndex Then
            sSource = Left$(sSource, i) + sRepStr + Mid$(sSource, Instr(i+1, sSource, Chr$(s)))
            Exit Sub
         End If
         If @b[i] = s Then Incr c
     Next
    End Sub
    -------------
    Kev G Peel
    KGP Software
    Bridgwater, UK.
    mailto:[email protected][email protected]</A>

    [This message has been edited by K Peel (edited November 06, 2001).]

  • #2
    Updated November 6th 2001: I have updated the above code to use a byte pointer for a substantial speed increase.
    kgpsoftware.com | Slam DBMS | PrpT Control | Other Downloads | Contact Me

    Comment


    • #3
      Hi Kev,

      Here is another apprach. I would expect it to be much faster
      than parse$ in PB. But take care! It is destructive!!! All
      the delimiters are replaced with chr$(0).

      Regards
      Peter
      Code:
      #compile exe
      #dim all
      #register none
       
      function DParse(Source as string, byval Delim as string, pPart() as dword) as long
       
          register s as long, d as long    
       
          local pSource as byte pointer : pSource = strptr(Source)    
          local pDelim as byte pointer  : pDelim  = strptr(Delim)    
          local nSource as long         : nSource = len(Source)
          local nDelim as long          : nDelim  = len(Delim)
          local flag as long, count as long
       
          flag = 1
          for s = 0 to nSource-1
              for d = 0 to nDelim-1
                  if @pSource[s] = @pDelim[d] then
                      @pSource[s] = 0
                      flag = 1 
                      goto next_s
                  end if
              next d    
              if flag then
                  redim preserve pPart(count) 
                  pPart(count) = pSource + s
                  incr count
                  flag = 0
              end if
          next_s:    
          next s
       
          function = count
       
      end function
       
      function pbmain
       
          dim p(0) as dword
          local pz as asciiz pointer
          local i as long
           
          local s as string : s = " This is/ \  /// a     test-string    "    
       
          for i = 0 to DParse(s," /", p()) - 1
              pz = p(i) : msgbox "'" + @pz + "'"
          next i
       
      end function
      ------------------
      [email protected]

      [This message has been edited by Peter P Stephensen (edited November 07, 2001).]
      [email protected]
      www.dreammodel.dk

      Comment

      Working...
      X