
Code: Select all
Procedure FindStringWithHoles(sRead.s, sPattern.s, pos=1, Mode=#PB_String_NoCase)
; This function will be called by StringIsLike()
; Created on May20,2014
Protected c, cMax, MarkCount, Lastpos, posMatch, sCheckThis.s
cMax = CountString(sPattern, Chr(63))+1 ; ? is Chr(63)
For c=1 To cMax
sCheckThis = StringField(sPattern,c,Chr(63))
If sCheckThis=""
pos + 1
MarkCount + 1
Continue
EndIf
posMatch = FindString(sRead, sCheckThis, pos, Mode)
If posMatch=0
Lastpos = 0 ; reset for next seek from C=1
Break
Else
If c=1
If Lastpos = 0 : Lastpos = posMatch : EndIf
pos = posMatch + Len(sCheckThis) + 1 ; keep checking with next forward position
Else
If posMatch > pos ; failed if matching is not from the first seeking position
c = 0 ; re-seek sPattern at last position
MarkCount = 0 : Lastpos = 0 ; reset for next seek from C=1
Continue
Else ; match at the first seeking position
If Lastpos = 0
Lastpos = posMatch - MarkCount
Else
If c = cMax : Break : EndIf ; mission succeeded
EndIf
pos = posMatch + Len(sCheckThis) + 1 ; keep checking with next forward position
If pos>Len(sRead) : Break : EndIf
EndIf
EndIf
EndIf
Next c
ProcedureReturn Lastpos
EndProcedure
Procedure StringIsLike(sRead.s, sPattern.s, Mode=#PB_String_NoCase)
; Test two strings whether they match
; sPattern allows wild card character only like * Chr(42)
; Created on May20,2014
Protected c, cMax, pos, posMatch, IsLike=1, sCheckThis.s
cMax = CountString(sPattern, Chr(42))+1
For c=1 To cMax
sCheckThis = StringField(sPattern,c,Chr(42))
If sCheckThis="" : Continue : EndIf
posMatch = FindStringWithHoles(sRead, sCheckThis, pos, Mode)
If posMatch=0
IsLike = 0
Break
Else
If c=cMax ; last check unit
If Len(sRead)-posMatch+1<> Len(sCheckThis)
IsLike = 0
Break
EndIf
Else
pos = posMatch + Len(sCheckThis) ; new position to start seeking
EndIf
EndIf
Next c
ProcedureReturn IsLike
EndProcedure
Debug FindStringWithHoles("This is likle a cake", "?a", 15) ; 17
Debug StringIsLike("This is lik1e a cake", "T?is is*") ; 1
Debug StringIsLike("This is lik1e a cake", "*is *is*") ; 1