p-ascii and #null

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

p-ascii and #null

Post by GPI »

Code: Select all

PrototypeC.i __proto_luaL_loadfilex (lua_State.i, filename.p-ascii, mode.p-ascii)
Global luaL_loadfilex.__proto_luaL_loadfilex
it is not possible to call

Code: Select all

luaL_loadfilex(lua_State, "Hallo", #null)
Maybe it is a good idea to allow #null for p-ascii. At the moment it return a compiling error, because a string was expected. The lua.dll akzept for mode "b", "t", "bt" and 0 (0 = default = "bt").
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: p-ascii and #null

Post by Mistrel »

#Null is 0 while #Null$ is null-terminated empty string.

Strings in PureBasic are special as they hold additional internal state beyond a string of bytes, which is why they are handled differently from other languages.

For example, in C/C++, 0 can be considered a string because the byte-character of value '0' is null. In Java it also works with 'null' as a String is an object and null object references are valid.

PureBasic strings accept neither an integer 0 and has no concept of objects or null references, hence why you must provide explicit #Null or #Null$.
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: p-ascii and #null

Post by Oma »

GPI is right.
The same problem i have already posted here http://www.purebasic.fr/english/viewtop ... 23&t=64235 for ImportC on Linux.
The null-pointer-string #Null$ doesn't work with the pseudotypes 'p-ascii', 'p-utf8', ...
It seems that, in this case, the zero pointer is destroyed.

With an Import(C) you can declare the function twice, once with the 'AS' argument and the correct type for '0'.
But with an Prototype(C) no 'AS' is possible.

Regards, Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: p-ascii and #null

Post by GPI »

Mistrel wrote:#Null is 0 while #Null$ is null-terminated empty string.
this is correct. The c-routine want here a "const char *mode" - #null would send a 0, #null$ would send a adress to a null-terminated empty string. The document says, that you should send a Null and not a null-terminated empty string.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: p-ascii and #null

Post by Mistrel »

GPI wrote:The c-routine want here a "const char *mode" - #null would send a 0, #null$ would send a adress to a null-terminated empty string. The document says, that you should send a Null and not a null-terminated empty string.
You can't pass an integer as an argument to any form of PureBasic string. Altering this behavior would be a fundamental change; and in this light doesn't seem feasible.

Imo, this should have been asked in Coding Questions. You can still get the functionality you want:

Code: Select all

PrototypeC.i _null_proto_luaL_loadfilex (lua_State.i, filename.p-ascii, mode.i)
Global _null_luaL_loadfilex._null_proto_luaL_loadfilex
PrototypeC.i _string_proto_luaL_loadfilex (lua_State.i, filename.p-ascii, mode.p-ascii)
Global _string_luaL_loadfilex._string_proto_luaL_loadfilex

Procedure.i luaL_loadfilex(lua_State.i, filename.s, mode.s)
  If mode.s=""
    ProcedureReturn _null_luaL_loadfilex(lua_State, filename.s, #Null)
  EndIf
  
  ProcedureReturn _string_luaL_loadfilex(lua_State, filename.s, mode.s)
EndProcedure
The only thing you can't do with this is perform the original call with an empty string as #Null$ is being used as a conditional to pass #Null.

Does this help?
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: p-ascii and #null

Post by nco2k »

Mistrel wrote:You can't pass an integer as an argument to any form of PureBasic string.
yes, but pseudotypes are meant to be used for lib/api calls.

the pb string gets converted to the required format and the pointer to the converted string gets passed to the function. i dont see a reason why we shouldnt be able to simply pass 0 and simply skip the conversion process.

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: p-ascii and #null

Post by Josh »

Mistrel wrote:... while #Null$ is null-terminated empty string
No! You mix here two different things:

#Null$ is a null-string
#Empty$ would be a null-terminated empty string

Code: Select all

Procedure Test (a$, b$)
  
  Debug @a$
  Debug @b$
  
EndProcedure

Test (#Null$, #Empty$)
sorry for my bad english
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: p-ascii and #null

Post by Oma »

Hi, from Help - General rules:
#Empty$: represents an empty string (exactly the same as "")
#Null$ : represents an null string. This can be used for API
functions requiring a null pointer to a string, or to really free a string.
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
Post Reply