How can I convert zstring ptr to standard string

New to FreeBASIC? Post your questions here.
objet-a
Posts: 13
Joined: Jul 13, 2021 0:04
Location: Hubei, China

How can I convert zstring ptr to standard string

Post by objet-a »

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.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: How can I convert zstring ptr to standard string

Post by caseih »

How would split be used to convert a zstring to a standard string (with lossless performance)?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How can I convert zstring ptr to standard string

Post by fxm »

How can I convert zstring ptr to standard string
To do what with this string?
dwzgocl0
Posts: 4
Joined: Apr 05, 2022 10:21

Re: How can I convert zstring ptr to standard string

Post by dwzgocl0 »

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.
dwzgocl0
Posts: 4
Joined: Apr 05, 2022 10:21

Re: How can I convert zstring ptr to standard string

Post by dwzgocl0 »

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
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: How can I convert zstring ptr to standard string

Post by SARG »

Split function
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
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How can I convert zstring ptr to standard string

Post by fxm »

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).

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
But to do what with it?
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: How can I convert zstring ptr to standard string

Post by adeyblue »

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)
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How can I convert zstring ptr to standard string

Post by dodicat »

I would have thought simply

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
 
Check with Resource Monitor for leaks.
Lost Zergling
Posts: 534
Joined: Dec 02, 2011 22:51
Location: France

Re: How can I convert zstring ptr to standard string

Post by Lost Zergling »

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.
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: How can I convert zstring ptr to standard string

Post by grindstone »

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.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: How can I convert zstring ptr to standard string

Post by caseih »

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:

Code: Select all

mystring = mystring + zstringptrstring
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How can I convert zstring ptr to standard string

Post by dodicat »

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.
You can use the mid function to add the necessary accoutrements .

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 
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How can I convert zstring ptr to standard string

Post by fxm »

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')
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How can I convert zstring ptr to standard string

Post by dodicat »

This was my initial test

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
 
However it is only a curiosity in this stage, of no real value.
Post Reply