Problem with PeekS()

Just starting out? Need help? Post your questions and find answers here.
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Problem with PeekS()

Post by idle »

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)

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)


#NULL
Addict
Addict
Posts: 1499
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Problem with PeekS()

Post by #NULL »

Psychophanta wrote: Mon Sep 23, 2024 7:19 pm i think this is totally safe:
It's totally bonkers.
There are actually 2 leaks. The result from AllocateMemory() is overwritten and thus not freed, and the result from Ascii() is only peeked at but not freed either.
And the question is still what use is a string variable with ascii content. The string functions will look for 2 byte utf-16 sequences

Code: Select all

Debug FindString(c$, "e")    ; 2
Debug FindString(var$, "e")  ; 0
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Problem with PeekS()

Post by idle »

#NULL wrote: Tue Sep 24, 2024 5:36 am
Psychophanta wrote: Mon Sep 23, 2024 7:19 pm i think this is totally safe:
It's totally bonkers.
There are actually 2 leaks. The result from AllocateMemory() is overwritten and thus not freed, and the result from Ascii() is only peeked at but not freed either.
And the question is still what use is a string variable with ascii content. The string functions will look for 2 byte utf-16 sequences

Code: Select all

Debug FindString(c$, "e")    ; 2
Debug FindString(var$, "e")  ; 0
Yes it is a bit bonkers as the string functions need to be added, see my post above

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)

It is easy enough to make an ascii module for windows and linux with ASM backend but I'm not sure about c
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Problem with PeekS()

Post by Psychophanta »

Ok to all, so, isn't this fully safe?

Code: Select all

Procedure$ AsignarCadenaAscii(cadena$)
  ;Carga una cadena de caracteres ascii extendido en una variable
  ProcedureReturn PeekS(Ascii(cadena$),2*Round((Len(cadena$)+1)/2,#PB_Round_Up))
EndProcedure
c$="Hello World !"
var$=AsignarCadenaAscii(c$)
debug var$
ShowMemoryViewer(@var$,2*Round((Len(c$)+1)/2,#PB_Round_Up))
Mijikai wrote: Mon Sep 23, 2024 8:25 pm Btw. what will be the use for this Ascii.s thing?
Time ago (2017, and no Ascii() function then, but needed unicode compilation) i dealed with this, in order to perform safe communication with arduino via wireless COM , and finally I made some functions for it where the exchange was bytes, and I left behind everything related to alphanumeric strings.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Problem with PeekS()

Post by idle »

At least read the thread!

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 

Macro _len(len) 
   len = ((len>>1) + (len&1))
EndMacro  

Procedure.s StrAsc(str.s) 
  Protected var.s,len = Len(str)
  _len(len)
  var.s = Space(Len)
  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 var.s, lena = LenAsc(@stra) 
  Protected len = Len(strb) 
  _len(len)
  var.s = Space(lena + len)
  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,nlen = len  
  _len(nlen)
  var.s = Space(nlen)
  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 nlen = startpos+len
  _len(nlen)
  Protected var.s = Space(strlen-nlen)
  PokeS(@var,PeekS(@str+Startpos,len,#PB_Ascii),len,#PB_Ascii) 
  ProcedureReturn var
    
EndProcedure 

Procedure.s RightAsc(str.s,len) 
  
  Protected var.s,nlen = len  
  Protected strlen = LenAsc(@str)
  _len(nlen)
  var = Space(nlen) 
  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)

ShowMemoryViewer(@Var1,32)


User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Problem with PeekS()

Post by Mijikai »

I also played around a bit but stuck to structs.

Code: Select all

EnableExplicit

Structure STRING_ASCII
  *pointer
  length.i;<- :)
EndStructure

Global *a.String
Global *b.String

Procedure.i AsciiAlloc(String.s)
  Protected *ascii.STRING_ASCII,length.i
  If String
    length = Len(String)
    *ascii = AllocateMemory(SizeOf(STRING_ASCII) + length + 2 + (length % 2))
    If *ascii
      *ascii\pointer = *ascii + SizeOf(STRING_ASCII)
      *ascii\length = length
      PokeS(*ascii\pointer,String,-1,#PB_Ascii)
    EndIf
  EndIf
  ProcedureReturn *ascii
EndProcedure

Procedure.i AsciiBuffer(*String.STRING_ASCII)
  ProcedureReturn *String\pointer
EndProcedure

Procedure.i AsciiLen(*String.STRING_ASCII)
  ProcedureReturn *String\length
EndProcedure

Procedure.i AsciiAppend(*String.STRING_ASCII,String.s)
  ProcedureReturn AsciiAlloc(PeekS(*String\pointer,-1,#PB_Ascii) + String)  
EndProcedure

Procedure.i AsciiMid(*String.STRING_ASCII,Offset.i,Length.i)
  Protected *ascii.STRING_ASCII
  If (*String\length - Offset - Length) >= 0
    *ascii = AllocateMemory(SizeOf(STRING_ASCII) + Length + 2 + (Length % 2))
    If *ascii
      *ascii\pointer = *ascii + SizeOf(STRING_ASCII)
      *ascii\length = Length
      CopyMemory(*String\pointer + Offset,*ascii\pointer,Length)
    EndIf
  EndIf
  ProcedureReturn *ascii
EndProcedure

Procedure.i AsciiLeft(*String.STRING_ASCII,Value.i)
  ProcedureReturn AsciiMid(*String,#Null,Value)
EndProcedure

Procedure.i AsciiRight(*String.STRING_ASCII,Value.i)
  ProcedureReturn AsciiMid(*String,*String\length - Value,Value)
EndProcedure

Procedure.i AsciiFind(*String.STRING_ASCII,String.s)
  Protected *ascii,length.i,steps.i,index.i,found.i
  *ascii = Ascii(String)
  If *ascii
    length = MemorySize(*ascii) - 1
    steps = *String\length - length
    If steps >= 0
      For index = 0 To steps
        If CompareMemory(*String\pointer + index,*ascii,length)
          found = index + 1
          Break
        EndIf
      Next
    EndIf
    FreeMemory(*ascii)
  EndIf
  ProcedureReturn found
EndProcedure

Procedure.s AsciiToUnicode(*String.STRING_ASCII)
  ProcedureReturn PeekS(*String\pointer,-1,#PB_Ascii)
EndProcedure

Procedure.i AsciiFree(*String.STRING_ASCII)
  FreeMemory(*String)
  ProcedureReturn #Null
EndProcedure

Procedure.i AsciiDebug(*String.String);<- only for debugging
  Debug *String\s;<- print ascii string
  Debug AsciiToUnicode(*String);<- print unicode string
  ProcedureReturn #Null
EndProcedure

*a = AsciiAlloc("Hello World !")  :AsciiDebug(*a)
*b = AsciiAppend(*a," :D")        :AsciiDebug(*b) :AsciiFree(*a)
*a = AsciiLeft(*b,4)              :AsciiDebug(*a) :AsciiFree(*a)
*a = AsciiMid(*b,6,5)             :AsciiDebug(*a) :AsciiFree(*a)
*a = AsciiRight(*b,4)             :AsciiDebug(*a) :AsciiFree(*a)

Debug AsciiFind(*b,"H")
Debug AsciiFind(*b,"World")
Debug AsciiFind(*b,"D")

End AsciiFree(*b)
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Problem with PeekS()

Post by idle »

tested with ascii function

Code: Select all

ImportC "" 
  MessageBoxA(hWnd,lpText.s,lpCaption.s,uType.l) 
EndImport 

MessageBoxA(0,StrAsc("world ascii"),strAsc("hello"),0) 

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 

Macro _len(len) 
   len = ((len>>1) + (len&1))
EndMacro  

Procedure.s StrAsc(str.s) 
  Protected var.s,len = Len(str)
  _len(len)
  var.s = Space(Len)
  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 var.s, lena = LenAsc(@stra) 
  Protected len = Len(strb) 
  _len(len)
  var.s = Space(lena + len)
  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,nlen = len  
  _len(nlen)
  var.s = Space(nlen)
  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 nlen = startpos+len
  _len(nlen)
  Protected var.s = Space(strlen-nlen)
  PokeS(@var,PeekS(@str+Startpos,len,#PB_Ascii),len,#PB_Ascii) 
  ProcedureReturn var
    
EndProcedure 

Procedure.s RightAsc(str.s,len) 
  
  Protected var.s,nlen = len  
  Protected strlen = LenAsc(@str)
  _len(nlen)
  var = Space(nlen) 
  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)

ShowMemoryViewer(@Var1,32)

ImportC "" 
  MessageBoxA(hWnd,lpText.s,lpCaption.s,uType.l) 
EndImport 

MessageBoxA(0,StrAsc("world ascii"),strAsc("hello"),0) 



User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Problem with PeekS()

Post by Psychophanta »

@idle
Once i read the thread:
In StrAsc() function, for example, take in account you are returning 'var', which is an unicode variable which is '(Len(var)+1)*2' bytes long in the unicode compiler. So, that function is allocating amount of memory which is never deallocated (freed).

Code: Select all

Procedure.s StrAsc(str.s)
  Protected var.s = Space(Len(str))
  PokeS(@var,str,-1,#PB_Ascii) 
  ProcedureReturn var; <- here is returned an unicode variable which is '(Len(var)+1)*2' bytes long. So this is not correct!
EndProcedure
After it, you try to correct it with

Code: Select all

len = Len(str):len = ((len>>1) + (len&1))
and you then do

Code: Select all

  var.s = Space(Len)
  PokeS(@var,str,-1,#PB_Ascii) 
Well, as easily you can see, with the Space() function you are allocating '(len+1)*2' bytes of memory, so why do you such a thing? :?
After it, your function returns half of those memory bytes, loosing the other bytes in the limb. :cry:

On the other side, my posted function 'AsignarCadenaAscii()' is returning JUST the allocated memory, because it returns an unicode variable which is '2*Round((Len(cadena$)+1)/2,#PB_Round_Up)' bytes long, which is not only correct size, but also safe function, because it uses just the minimum and optimal required memory for the requested alfanumeric variable.

Am i wrong in any point?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Problem with PeekS()

Post by idle »

Psychophanta wrote: Wed Sep 25, 2024 7:58 am @idle
...
my last post had added the corrections for the length, I don't see any issue with the results

Code: Select all

Macro _len(len) 
   len = ((len>>1) + (len&1))
EndMacro  

Procedure.s StrAsc(str.s) 
  Protected var.s,len
  len = Len(str)
  Debug "Input len " + Str(len) 
  _len(len)
  Debug "output len " + Str(len) 
  var.s = Space(Len)
  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  

stru.s = "hello"
For a = 0 To 9
  stru + "hello"
Next   

Debug StringByteLength(stru)
str.s = StrAsc(stru)
len = LenAsc(@str) 
Debug len 
Debug StringByteLength(str) 
Debug Len(str)
ShowMemoryViewer(@str,len+1) 



I spent maybe 1/2 to 3/4 of an hour doing the string functions and then finished it this morning.
the point was using space doesn't leak memory but I didn't have time to address the length.
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Problem with PeekS()

Post by Mijikai »

An small update to the code i posted before: viewtopic.php?p=628257#p628257.

It replaces the old STRING_ASCII struct with a new one.
Use this instead of the standart STRING struct.

This will allow direct access not only to the string but also the length and pointer of the string 8)

Code (trickery ahead):

Code: Select all

Structure STRING_ASCII
  StructureUnion
    s.s
    *pointer
  EndStructureUnion
  length.i
EndStructure

Global *a.STRING_ASCII
Global *b.STRING_ASCII
Union with .s and * :mrgreen:
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Re: Problem with PeekS()

Post by Psychophanta »

Mijikai wrote: Wed Sep 25, 2024 3:42 pm Union with .s and * :mrgreen:
That's a great idea and it's the finishing touch to your good previous code, however mine is just as safe and functional, but waaaay simpler, and it counts with the advantage that it uses not pointers but just a unicode string variable which can be treated as an ascii one. :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Problem with PeekS()

Post by Mijikai »

Psychophanta wrote: Wed Sep 25, 2024 7:14 pm ...just a unicode string variable which can be treated as an ascii one. :wink:
That's what my code does as well (within a struct)...

So this works just fine (following the example of idle):

Code: Select all

ImportC "" 
  MessageBoxA(hWnd,lpText.s,lpCaption.s,uType.l) 
EndImport 

;...

MessageBoxA(#Null,*a\s,*b\s,#Null)
Getting the length of the string:

Code: Select all

Debug *a\length
And if you need the address:

Code: Select all

Debug *a\pointer
Post Reply