'01/12/2023 CP
'01/12/2023
'
function instrp(int i,string s,p,int*en=0) as int
=================================================
'
'i start position
's string to search
'p search string (with optional ? * wildcards
'en boundary of found key string
'
if i<1
i=1
endif
if not s
return 0 'no source string
endif
if not p
return 1 'no search string
endif
'
indexbase 1
byte bs at strptr(s) 'source string bytes
byte bp at strptr(p) 'search string bytes
int js=i 'source string position
int jp=1 'search string position
int ks=i 'next search start
int kt=i 'current search start
int kf=0 'prev key wild card position
int kg=0 'prev source wild card position
int fs=i 'possible found position
int ls=len(s) 'limit of source string
int lp=len(p) 'limit of search string
int ok=1 'vrification flag
byte ka=bp[1] 'first byte to search
do
if jp>lp
exit do
endif
if js>ls
if jp<=lp
ok=0
endif
exit do
endif
select bp[jp]
case 42 '*
ok=1
while bp[jp]=42
jp++
wend
do
if js>ls 'end of string
ok=1
jp--
exit do
endif
if ka=bs[js]
ks=js
endif
if jp>lp 'end of pattern
ok=1
jp--
exit do
endif
if bp[jp]=bs[js] 'possible match char
ok=1
jp--
'these flags used for resuming wild card
kf=jp
kg=js+1
exit do
endif
if bp[jp]=63 'possible match ? char
ok=1
jp--
exit do
endif
js++
loop
case 63 '?
if ka=bs[js]
ks=js
endif
ok=1
js++
case else 'all other chars
if bp[jp]=bs[js] 'byte match
ok=1
if ka=bs[js]
ks=js
endif
js++
else 'mismatch
ok=0
if kf
'prev wildcard positions
jp=kf-1
js=kg
kf=0
kg=0
else
if ka=63
ks=kt '?' was wildcard start
endif
if ks=kt
ks++ 'nudge fwd
endif
kt=ks
js=ks 'restart search
fs=js
jp=0
endif
endif
end select
jp++ 'next pattern byte
loop
if ok
function=fs
else
js=0
endif
en=js
end function
'
'test cases
===========
string s="aabcdefghijklmmnopqrstuvwxyz"
'print instrp 1,s,"ab"
'print instrp 1,s,"abz"
'print instrp 1,s,"xyz"
'print instrp 1,s,"mno"
'print instrp 1,s,"mm?o"
'print instrp 1,s,"m?o"
'print instrp 1,s,"m??"
'print instrp 1,s,"m??p"
'print instrp 1,s,"??m"
'print instrp 1,s,"*m"
'print instrp 1,s,"m*"
'print instrp 1,s,"m*z"
'print instrp 1,s,"m*k*z"
'print instrp 1,s,"m*n"
'print instrp 1,s,"a*b"
'print instrp 1,s,"a?*c"
'print instrp 1,s,"c?d"
'print instrp 1,s,"c*d"
'print instrp 1,s,"c?*e"
'print instrp 1,s,"c**f"
'print instrp 1,s,"c*??f" 'yes
'print instrp 1,s,"c*??g" 'no
'print instrp 1,s,"c??*g" 'yes
int en
'print instrp 1,s,"c??*g",en 'yes
'print en
uses console
s="abcdefghijklmmabcdefghijklmn"
int b
b=instrp(1,s,"d*lmm",en)
print b " - " en cr
print mid(s,b,en-b) cr
print instrp(1,s,"d*lmn",en) " - " en cr
print cr "ok" cr
wait
static KEY_RECORD KeyRecord
'
function inkey(int c=0) as int
================================
static KEY_RECORD k[8]
static int nr
int d
PeekConsoleInputA ConsIn,@k,8, @nr
if nr
if c=0
indexbase 1
int i
for i=1 to nr
if k[i].EventType=1 'KEY_EVENT
if k[i].bKeyDown
KeyRecord=k[i]
d=KeyRecord.wVirtualKeyCode
exit for
endif
endif
next
elseif GetAsyncKeyState(c)
d=c
endif
FlushConsoleInputBuffer ConsIn
return d
endif
end function
/*
type KEY_RECORD
===============
word EventType
word wReserved
dword bKeyDown
word wRepeatCount
word wVirtualKeyCode
word wScanCode
word wAsciiCode
dword dwReserved
end type
*/
uses console
string k
int a,c
do
a=inkey
if a
c=KeyRecord.wAsciiCode
k=a ", " c
print k + cr
if a=13 then break
endif
loop
printl "end"
wait
[/cade]
' Simple pattern matching function
'global vars
#lookahead
Dim in[500] as string
Dim patt[500] as string
'fn
Function strMatchPattern(s[500] as string, pattern[500] as string) as bool
Dim sPos as int : sPos = 1
Dim pPos as int : pPos = 1
Dim sLen as int : slen = len(s[])
Dim pLen as int : pLen = len(pattern[])
Dim match as int : match = 0
Dim star as int : star = 0
While sPos <= sLen
if pPos <= pLen and (pattern[pPos] = s[sPos] or pattern[pPos] = "?") then
pPos = pPos + 1
sPos = sPos + 1
goto cont
end if
if pPos <= pLen and pattern[pPos] = "*" then
star = pPos
match = sPos
pPos = pPos + 1
goto cont
end if
if star <> 0 then
pPos = star + 1
match += 1
sPos = match
goto cont
end if
return false
Wend
'label
cont:
While pPos <= pLen and pattern[pPos] = "*"
pPos = pPos + 1
Wend
'fn return boolean TRUE/FALSE
Return pPos > pLen
End Function
'call function...
in[1] = "testing" : patt[1]="test*"
print strMatchPattern( in[1] , patt[1] )
/*
function GetKey() as int
========================
static long blen,mode
static byte z[4]
GetConsoleMode ConsIn,mode
SetConsoleMode ConsIn,0
ReadConsole ConsIn,@z,1,@blen,null
SetConsoleMode ConsIn,mode
return z
end function
*/
! GetAsyncKeyState lib "user32.dll" (int c) as int
'
function inkey(int c=0) as int
================================
static KEY_RECORD k[8]
static int nr
int d
PeekConsoleInputA ConsIn,@k,8, @nr
if nr
if c=0
indexbase 1
int i
for i=1 to nr
if k[i].EventType=1 'KEY_EVENT
if k[i].bKeyDown
d=k[i].wVirtualKeyCode
exit for
endif
endif
next
elseif GetAsyncKeyState(c)
d=c
endif
FlushConsoleInputBuffer ConsIn
return d
endif
end function
function GetKey(int c=0) as int
===============================
int d
do
d=inkey(c)
if d
if c
if d=c
return c
endif
else 'c=0
return d
endif
endif
loop
end function
function GetKey() as int
========================
static long blen,mode
static byte z[16]
GetConsoleMode ConsIn,mode
SetConsoleMode ConsIn,0
ReadConsole ConsIn,@z,1,@blen,null
SetConsoleMode ConsIn,mode
return z
end function
Page created in 0.095 seconds with 13 queries.