[Gambas-devel] [Gambas-user] Error in Left$ and Mid$ functions to the gb.ncurses component
Benoît Minisini
gambas at ...1...
Sun May 20 18:56:02 CEST 2012
Le 20/05/2012 18:43, tobi a écrit :
>
> So my question bounces to Benoît: It's said in the docs that the three string functions used above
> are optimised so that they don't duplicate strings. Does that mean or is it a general fact that I
> cannot rely on the STRING() macro to extract a NUL-terminated string from a given GB_STRING
> argument? At least there doesn't seem to be a NUL byte which causes the component to print
> everything that follows until the end of the original string...
> Brief explanation and after lunch, it's done.
>
> Regards,
> Tobi
>
No problem, I never told you how it works, so you couldn't guess!
Gambas strings are not C strings (i.e. null byte terminated), but a
combination of a string pointer (char *) and a length.
To get the string pointer from a string argument, you must use the
STRING() macro, but you already knew.
To get the length of a string argument, you must use the LENGTH() macro.
Now, the problem is that sometimes you cannot deal with such strings,
and need a C-string.
Hopefully, the interpreter provides the GB.ToZeroString() API that takes
the string argument (through the ARG() macro) and returns a temporary
C-string that will be automatically freed later.
So, either you have:
BEGIN_METHOD(MyMethod, GB_STRING arg)
my_function(STRING(arg), LENGTH(arg));
END_METHOD
or (if you really need a C-string):
BEGIN_METHOD(MyMethod, GB_STRING arg)
my_function(GB.ToZeroString(ARG(arg)));
END_METHOD
Regards,
--
Benoît Minisini
More information about the Devel
mailing list