Good Topic...
I modified my code and noticed the FindString behavior changed with 4.6.
Code: Select all
Debug FindString("text","x") ; Normal behavior
Debug FindString("text","x", 0) ; Used to cause an error
Debug FindString("text","x",-1) ; Used to cause an error
Here is what I use for "between text" searches. I'm sure it's not the fastest.
Code: Select all
CompilerIf #PB_Compiler_Unicode
Macro MidF(inString, StartPos, Length=-1)
PeekS(@inString + ((StartPos - 1) * SizeOf(Character)), Length, #PB_Unicode)
EndMacro
CompilerElse
Macro MidF(inString, StartPos, Length=-1)
PeekS(@inString + ((StartPos - 1) * SizeOf(Character)), Length, #PB_Ascii)
EndMacro
CompilerEndIf
Procedure.s SF_Between(sSearchIn.s, sFrom.s, sTo.s, *PosAfter.integer=0, StartPos.i=1)
; REV: 110519, skywalk
; PB 4.6 FindString() added default StartPos and accepts <=0 entries without error.
; Returns a string between 2 multi-char delimiters
; Parameters:
; sSearchIn: String to search
; sFrom: 1st keyword
; sTo: 2nd keyword
; PosAfter: Text position after found String
; StartPos: Position to start search
; Syntax:
; Debug SF_Between("<html>some text here</html>", "<html>", "</html>")
; Debug SF_Between(r, "some", " ", @PosAfter, StartPos)
Protected.i nLen1,nLen2,nLen,nLen3
Protected.s sFound
nLen1 = FindString(sSearchIn, sFrom, StartPos)
If nLen1
nLen2 = FindString(sSearchIn, sTo, nLen1 + Len(sFrom))
If nLen2
nLen = nLen1 + Len(sFrom)
nLen3 = nLen2 - nLen
sFound = MidF(sSearchIn, nLen, nLen3)
If (nLen + nLen3 > 0) And *PosAfter ; Avoid Null Pointer error
*PosAfter\i = nLen2 ; used to be -> nLen
EndIf
EndIf
EndIf
ProcedureReturn sFound
EndProcedure
Define.s r = "<html>some text here</html> <html> and again </html>"
Define.i PosAfter
Debug "1-> " + SF_Between(r, "<html>", "</html>")
Debug "2-> " + SF_Between(r, "<html>", "")
Debug "3-> " + SF_Between(r, " ", " ", @PosAfter, 20)
Debug "4-> " + SF_Between(r, "some", " ", @PosAfter)
Debug "5-> " + "PosAfter = " + Str(PosAfter) + " = " + #DQUOTE$ + midf(r,PosAfter) + #DQUOTE$
Debug "6-> " + SF_Between(r, "some", " ", @PosAfter, PosAfter)
Debug "7-> " + SF_Between(r, "some", "<", @PosAfter, -1)
Debug "8-> " + "PosAfter = " + Str(PosAfter) + " = " + #DQUOTE$ + midf(r,PosAfter) + #DQUOTE$
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum