Re: Problem with PeekS()
Posted: Tue Sep 24, 2024 4:57 am
I don't know why you want to do it, it's easier to just use *ptr = Ascii("some string")
this won't leak memory
StrAsc(str.s)
StrAscAppend(stra.s,strb.s,mode=#PB_Unicode)
LeftAsc(str.s)
MidAsc(str.s,Startpos,len)
RightAsc(str.s,len)
LenAsc(@str)
FindStringAsc(strAsc.s,find.s,pos=0,mode=#PB_Unicode)
this won't leak memory
StrAsc(str.s)
StrAscAppend(stra.s,strb.s,mode=#PB_Unicode)
LeftAsc(str.s)
MidAsc(str.s,Startpos,len)
RightAsc(str.s,len)
LenAsc(@str)
FindStringAsc(strAsc.s,find.s,pos=0,mode=#PB_Unicode)
Code: Select all
Declare.s StrAsc(str.s)
Declare LenAsc(*str)
Procedure FindStringAsc(strAsc.s,find.s,pos=0,mode=#PB_Unicode)
Structure ara
a.a[0]
EndStructure
;booyer moore search
Protected i,t,len,skip,*input,*pa.Ascii,*pb.Ascii,*pat.ara
Protected findAsc.s = strAsc(find)
inlen = LenAsc(@strAsc)
*pinput = @strAsc
If mode = #PB_Unicode
palen = Len(find)
*pat = @findasc
ElseIf mode = #PB_Ascii
palen = LenAsc(@find)
*pat = @find
EndIf
Structure ST
a.a[256]
EndStructure
Static skiptable.ST
inlen-pos
*input = *pinput+pos
len = inlen - palen
If pos = 0
For i = 0 To 255
Skiptable\a[i] = 255;
Next
t= palen-1
For i = 0 To t
skiptable\a[*pat\a[i]] = i
Next
EndIf
i=0
skip=0
While skip <= len
i = palen - 1;
*pa = (*input + skip + i)
*pb = *pat+i
While (*pb\a = *pa\a)
i-1
*pa - 1
*pb - 1
Wend
If i > 0
t = i - Skiptable\a[*pa\a]
If t > 1
skip + t
Else
skip + 1
EndIf
Else
ProcedureReturn skip + pos
EndIf
Wend
ProcedureReturn -1
EndProcedure
Procedure.s StrAsc(str.s)
Protected var.s = Space(Len(str))
PokeS(@var,str,-1,#PB_Ascii)
ProcedureReturn var
EndProcedure
Procedure LenAsc(*str)
Protected *p.Ascii = *str
While *p\a <> 0
*p+1
Wend
ProcedureReturn *p - *str
EndProcedure
Procedure.s StrAscAppend(stra.s,strb.s,mode=#PB_Unicode)
Protected lena = LenAsc(@stra)
Protected var.s = Space(lena + Len(strb))
PokeS(@var,PeekS(@stra,lena,#PB_Ascii),lena,#PB_Ascii | #PB_String_NoZero)
If mode = #PB_Unicode
PokeS(@var+lena,strb,-1,#PB_Ascii)
Else
PokeS(@var+lena,PeekS(@strb,LenAsc(@strb),#PB_Ascii),-1,#PB_Ascii)
EndIf
ProcedureReturn var
EndProcedure
Procedure.s LeftAsc(str.s,len)
Protected var.s = Space(Len)
PokeS(@var,PeekS(@str,len,#PB_Ascii),len,#PB_Ascii)
ProcedureReturn var
EndProcedure
Procedure.s MidAsc(str.s,Startpos,len)
Protected strlen = LenAsc(@str)
Protected var.s = Space(strlen-startpos+len)
PokeS(@var,PeekS(@str+Startpos,len,#PB_Ascii),len,#PB_Ascii)
ProcedureReturn var
EndProcedure
Procedure.s RightAsc(str.s,len)
Protected strlen = LenAsc(@str)
Protected var.s = Space(len)
PokeS(@var,PeekS(@str+strlen-len,len,#PB_Ascii),len,#PB_Ascii)
ProcedureReturn var
EndProcedure
Procedure DebugAsc(str.s)
Debug PeekS(@str,-1,#PB_Ascii)
EndProcedure
Global var.s = StrAsc("Hello World !") ;adds a literal unicode string
DebugAsc(var)
Debug LenAsc(@var)
var = LeftAsc(var,5)
DebugAsc(var)
Debug LenAsc(@var)
var = StrAscAppend(var," world !") ;appends a unicode to ascii
DebugAsc(var)
Debug LenAsc(@var)
var = StrAscAppend(var,var,#PB_Ascii) ;appends an ascii string
DebugAsc(var)
Debug LenAsc(@var)
var1.s = MidAsc(var,4,3)
DebugAsc(var1)
DebugAsc(RightAsc(var,7))
Debug FindStringAsc(var,"world") ;finds a unicode literal
var3.s = StrAsc("world")
Debug FindStringAsc(var,var3,0,#PB_Ascii) ;finds a ascii string
var1.s = var.s
DebugAsc(var1)