(SOLVED) Operator ! (Escaped String Literal)

General FreeBASIC programming questions.
VANYA
Posts: 1236
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

(SOLVED) Operator ! (Escaped String Literal)

Postby VANYA » Jul 16, 2017 7:38

Hello everybody!

Guys, it seems to me that earlier such code gave the possibility to insert the character "0", but now why then the line is clipped:

Code: Select all

dim as String*20 szStr = !"Hello\0world!"
dim as String szStr2 = !"Hello\0world!"

? szStr
? szStr2
sleep


Result:

Hello.............
Hello
Last edited by VANYA on Jul 16, 2017 12:09, edited 1 time in total.
D.J.Peters
Posts: 7007
Joined: May 28, 2005 3:28
Location: Germany

Re: Operator ! (Escaped String Literal)

Postby D.J.Peters » Jul 16, 2017 8:33

a binary 0 = !"\0" is the string terminator.

Joshy

Code: Select all

dim as String szStr2 = !"Hello\0world!"

? "Hello\0world!"
? szStr2
? "char on position 6 " & asc(mid(szStr2,6,1))
? "number of chars " & len(szStr2)-1 & " !!!"
print "'";
for i as integer = 0 to len(szStr2)-1
  var char = szStr2[i]
  if char<>0 then print chr(char);
next 
print "'"
sleep
VANYA
Posts: 1236
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: Operator ! (Escaped String Literal)

Postby VANYA » Jul 16, 2017 10:14

Thank you Joshy for attention to my question!

In general, of course, a few strange features of the compiler's behavior.

That's how it works:

Code: Select all

   
    .....
    Dim As OPENFILENAME ofn
    ofn.lStructSize       = Sizeof(OPENFILENAME)
    ofn.lpstrFilter       = Strptr(!"Wave Files\0*.wav\0Any Files\0*.*\0")


Here so it works:

Code: Select all

    .....
    Dim As OPENFILENAME ofn
    Dim As String szFilter = "Wave Files" + Chr(0) + "*.wav" +Chr(0)+ "Any Files" + Chr(0)+ "*.*" +Chr(0)
    ofn.lStructSize       = Sizeof(OPENFILENAME)
    ofn.lpstrFilter       = Strptr(szFilter)


And here so does not work:

Code: Select all

    .....
    Dim As OPENFILENAME ofn
    Dim As String szFilter = !"Wave Files\0*.wav\0Any Files\0*.*\0"
    ofn.lStructSize       = Sizeof(OPENFILENAME)
    ofn.lpstrFilter       = Strptr(szFilter)


:))))))
D.J.Peters
Posts: 7007
Joined: May 28, 2005 3:28
Location: Germany

Re: Operator ! (Escaped String Literal)

Postby D.J.Peters » Jul 16, 2017 11:50

The FreeBASIC runtime use the zero as string terminator (left, right, mid, len ...)
but the Windows API use the zero as a marker.

Joshy
VANYA
Posts: 1236
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: (SOLVED) Operator ! (Escaped String Literal)

Postby VANYA » Jul 16, 2017 12:10

Thanks for answers!
fxm
Posts: 7148
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: (SOLVED) Operator ! (Escaped String Literal)

Postby fxm » Jul 16, 2017 15:09

By the way, LEN does not provide the same return information depending on the string type:
- for a fix-len zstring or a var-len string: the number of actually used characters is returned.
- for a fix-len string: the maximum number of usable characters is returned.

But SIZEOF always provides the same return information whatever the string type:
- the total size of allocated memory is returned.

Code: Select all

Dim As Zstring * 10 flz = "Hello!"
Print "Dim As Zstring * 10 flz = ""Hello!"""
Print "  LEN(flz)="&Len(flz), "  SIZEOF(flz)="&Sizeof(flz)
Print

Dim As String * 10 fls = "Hello!"
Print "Dim As String * 10 fls = ""Hello!"""
Print "  LEN(fls)="&Len(fls), "  SIZEOF(fls)="&Sizeof(fls)
Print

Dim As String vls = "Hello!"
Print "Dim As String vls = ""Hello!"""
Print "  LEN(vls)="&Len(vls), "  SIZEOF(vls)="&Sizeof(vls)
Print

Sleep

Code: Select all

Dim As Zstring * 10 flz = "Hello!"
  LEN(flz)=6    SIZEOF(flz)=10

Dim As String * 10 fls = "Hello!"
  LEN(fls)=10   SIZEOF(fls)=11

Dim As String vls = "Hello!"
  LEN(vls)=6    SIZEOF(vls)=12
VANYA
Posts: 1236
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: (SOLVED) Operator ! (Escaped String Literal)

Postby VANYA » Jul 16, 2017 16:01

By the way, LEN does not provide the same return information depending on the string type:
- for a fix-len zstring or a var-len string: the number of actually used characters is returned.
- for a fix-len string: the maximum number of usable characters is returned.

But SIZEOF always provides the same return information whatever the string type:
- the total size of allocated memory is returned.


Hi fxm!

Thank you for joining the discussion. The question was that the compiler handles the same functionality in different ways:

Code: Select all

Dim As String szFilter = "Wave Files" + Chr(0) + "*.wav" +Chr(0)+ "Any Files" + Chr(0)+ "*.*" +Chr(0)
Dim As String szFilter2 = !"Wave Files\0*.wav\0Any Files\0*.*\0"
? szFilter
? szFilter2
sleep


Especially since you correctly noticed that memory for the rows is allocated with excess. But the compiler in the szFilter2 line sees "0" in the middle and trims. And in the case with the string szFilter the string does not truncate.
fxm
Posts: 7148
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: (SOLVED) Operator ! (Escaped String Literal)

Postby fxm » Jul 16, 2017 16:28

Your two string literals are not of the same type:
- the first is a var-len string due to Operator + (string concatenation) between zstring and var-len string(chr(0)):

Code: Select all

#print typeof("Wave Files" + Chr(0) + "*.wav" +Chr(0)+ "Any Files" + Chr(0)+ "*.*" +Chr(0))
#print typeof(!"Wave Files\0*.wav\0Any Files\0*.*\0")
STRING
ZSTRING * 32

Indeed, the two following strings are not of the same type:

Code: Select all

#print typeof(!"\0")
#print typeof(chr(0))
ZSTRING * 2
STRING
VANYA
Posts: 1236
Joined: Oct 24, 2010 15:16
Location: Ярославль
Contact:

Re: (SOLVED) Operator ! (Escaped String Literal)

Postby VANYA » Jul 16, 2017 17:14

fxm wrote:Your two string literals are not of the same type:
- the first is a var-len string due to Operator + (string concatenation) between zstring and var-len string(chr(0)):

Code: Select all

#print typeof("Wave Files" + Chr(0) + "*.wav" +Chr(0)+ "Any Files" + Chr(0)+ "*.*" +Chr(0))
#print typeof(!"Wave Files\0*.wav\0Any Files\0*.*\0")
STRING
ZSTRING * 32

Indeed, the two following strings are not of the same type:

Code: Select all

#print typeof(!"\0")
#print typeof(chr(0))
ZSTRING * 2
STRING


Now I understand why such different compiler behavior. Thanks!

Return to “General”

Who is online

Users browsing this forum: No registered users and 3 guests