How can I convert zstring ptr to standard string
How can I convert zstring ptr to standard string
I wanted to ask if there was a way to convert the two types in a way that was almost lossless in performance.
...Or tell me if have some function like VB split() .
It's painful to have no Split function in Freebasic.
...Or tell me if have some function like VB split() .
It's painful to have no Split function in Freebasic.
Re: How can I convert zstring ptr to standard string
How would split be used to convert a zstring to a standard string (with lossless performance)?
Re: How can I convert zstring ptr to standard string
To do what with this string?How can I convert zstring ptr to standard string
Re: How can I convert zstring ptr to standard string
He is complaining FB doesn't have the SPLIT function as VB does.
https://docs.microsoft.com/en-us/office ... t-function
I checked FB string keywords list and it's really has no SPLIT.
https://docs.microsoft.com/en-us/office ... t-function
I checked FB string keywords list and it's really has no SPLIT.
Re: How can I convert zstring ptr to standard string
I don't know a way to convert a ZSTRING PTR to a STRING but you can get a ZSTRING PTR from a STRING by STRPTR.
https://www.freebasic.net/wiki/KeyPgOpStrptr
https://www.freebasic.net/wiki/KeyPgOpStrptr
Re: How can I convert zstring ptr to standard string
Split function
delimiter : by default one space
limit by default all the substrings (could be 1, 2,..)
delimiter : by default one space
limit by default all the substrings (could be 1, 2,..)
Code: Select all
sub split(res() as string,strg as string,delim as string=" ",limit as integer=-1)
dim as integer resmax=0
dim as integer strglen=len(strg),posinstrg=1,delimlen=len(delim),delimfound
if strglen=0 then ''len = zero --> empty array
return
End If
if len(delimlen)=0 or instr(posinstrg,strg,delim)=0 then ''One element the original string
redim res(0)
res(0)=strg
return
End If
if limit=-1 then
limit=9999999999
End If
while posinstrg<strglen and resmax<limit
delimfound=instr(posinstrg,strg,delim)
redim preserve res(resmax)
if delimfound then
res(resmax)=mid(strg,posinstrg,delimfound-posinstrg)
posinstrg+=delimlen+len(res(resmax))
else
res(resmax)=mid(strg,posinstrg)
posinstrg+=len(res(resmax))
end if
resmax+=1
Wend
return
End sub
redim as string rstrg()
split(rstrg(),"abc - def - fgh"," - ")
for i as integer =lbound(rstrg) to ubound(rstrg)
print i,rstrg(i)
Next
print "end"
sleep
Re: How can I convert zstring ptr to standard string
There is a possible hacking to quickly build/destruct a string issued from a zstring ptr, but the constraint when using such a string is that its length should be never changed (to avoid any memory reallocation).How can I convert zstring ptr to standard string
Example:
Code: Select all
Dim As Zstring * 18 z = "FreeBASIC zstring"
Dim As Zstring Ptr pz = @z
'then, only 'pz' is known
'zstring -> string
Dim As String s
Cptr(Zstring Ptr Ptr, @s)[0] = pz
Cptr(Integer Ptr, @s)[1] = Len(*pz)
Cptr(Integer Ptr, @s)[2] = Len(*pz)
Print "'" & s & "'"
'.....
' Do not modify the size of 's' (to avoid memory reallocation)
'.....
' When 's' is no more usefull (before end of 's' scope), to avoid a double memory deallocation
Cptr(Zstring Ptr Ptr, @s)[0] = 0
Cptr(Integer Ptr, @s)[1] = 0
Cptr(Integer Ptr, @s)[2] = 0
Sleep
Re: How can I convert zstring ptr to standard string
Or, you know, just
Code: Select all
dim as zstring ptr zstr = @"Hi"
dim as string nstr = *zstr
dim as zstring ptr zstr2 = strptr(nstr)
Re: How can I convert zstring ptr to standard string
I would have thought simply
Check with Resource Monitor for leaks.
Code: Select all
screen 17
Dim As Zstring * 18 z
Dim As Zstring Ptr pz
Dim As String s
dim as long counter
do
counter+=1
z = "FreeBASIC zstring"
pz = @z ' zstring ptr
s=*pz 'string
mid(s,11,1)="-"
locate 5,5
print len(s),cast(integer ptr,@s)[1],s,counter
loop
-
- Posts: 534
- Joined: Dec 02, 2011 22:51
- Location: France
Re: How can I convert zstring ptr to standard string
It would be nice if str(*zstringptr) could build a descriptor avoiding string copy, or adding an optionnal parameter to str ?
(ie : str(textvalue,1) => textvalue adress just referenced by descriptor).
Same way, there is no way dereferencing a string descriptor, could be performed just using strptr(legacystring,1), in wich case the string name would be no longer usable, as if scoped out.
On string processing, fast move beetween legacy strings and zstrings, avoiding copy, could be perhaps a valuable improvment of FB's features at few time cost maybe.
(ie : str(textvalue,1) => textvalue adress just referenced by descriptor).
Same way, there is no way dereferencing a string descriptor, could be performed just using strptr(legacystring,1), in wich case the string name would be no longer usable, as if scoped out.
On string processing, fast move beetween legacy strings and zstrings, avoiding copy, could be perhaps a valuable improvment of FB's features at few time cost maybe.
-
- Posts: 862
- Joined: May 05, 2015 5:35
- Location: Germany
Re: How can I convert zstring ptr to standard string
Some time ago I tried something similar, exchanging the text of a string variable by writing the pointer to another text into the string descriptor. It worked quite well, but when I tried to write a new text to the string variable without changing everything back, the program crashed. It seems that under the hood happens more than simply allocating some memory adresses.
Re: How can I convert zstring ptr to standard string
Could be that the string allocator allocates in discrete blocks, and reallocates when the block is used up. Since your zstring is of arbitrary length, nothing gets reallocated until long after you've written past the end of the zstring.
I'm hard pressed to think of any use case for transparently converting a zstring to a normal string. The reason being a dynamic string needs to be reallocated and copied anyway when it grows, so just leave the zstring as a zstring and copy it to a string when needed, such as when doing string concatenation. Hope that makes sense. IE if you're building a dynamic string anyway, just do something like:
I'm hard pressed to think of any use case for transparently converting a zstring to a normal string. The reason being a dynamic string needs to be reallocated and copied anyway when it grows, so just leave the zstring as a zstring and copy it to a string when needed, such as when doing string concatenation. Hope that makes sense. IE if you're building a dynamic string anyway, just do something like:
Code: Select all
mystring = mystring + zstringptrstring
Re: How can I convert zstring ptr to standard string
You can use the mid function to add the necessary accoutrements .Lost Zergling wrote: ↑Apr 08, 2022 12:46 It would be nice if str(*zstringptr) could build a descriptor avoiding string copy, or adding an optionnal parameter to str ?
(ie : str(textvalue,1) => textvalue adress just referenced by descriptor).
Same way, there is no way dereferencing a string descriptor, could be performed just using strptr(legacystring,1), in wich case the string name would be no longer usable, as if scoped out.
On string processing, fast move beetween legacy strings and zstrings, avoiding copy, could be perhaps a valuable improvment of FB's features at few time cost maybe.
Code: Select all
#define convert(a) mid((*(a)),1,len((*(a))))
dim as zstring * 100 s=!"Hello\nWorld!"+!"\nGoodbye"
dim as zstring ptr p=strptr(s)
var k=convert(p)
#print typeof(k)
print "'";k;"'"
sleep
Re: How can I convert zstring ptr to standard string
More simply:
var k = *p
works,
because regardless of the string type, 'var k = any_string' always defines 'k' as a var-len string.
(see VAR)
('var k = *p' is equivalent to 'dim as string k = *p')
var k = *p
works,
because regardless of the string type, 'var k = any_string' always defines 'k' as a var-len string.
(see VAR)
('var k = *p' is equivalent to 'dim as string k = *p')
Re: How can I convert zstring ptr to standard string
This was my initial test
However it is only a curiosity in this stage, of no real value.
Code: Select all
#define convert(a) mid((*(a)),1,len((*(a))))
dim as zstring * 100 z="Hello World again!"
dim as zstring ptr p=strptr(z)
#print typeof(*p)
#print typeof(convert(p))
print "Results on compiler output"
sleep