[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gambas-devel
Subject:    Re: [Gambas-devel] [Gambas-user] Error in Left$ and Mid$ functions to the gb.ncurses component
From:       Benoît_Minisini <gambas () users ! sourceforge ! net>
Date:       2012-05-20 16:56:02
Message-ID: 4FB92222.4060208 () users ! sourceforge ! net
[Download RAW message or body]

Le 20/05/2012 18:43, tobi a =E9crit :
>
> So my question bounces to Beno=EEt: 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 c=
omponent 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=EEt Minisini

---------------------------------------------------------------------------=
---
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and =

threat landscape has changed and how IT managers can respond. Discussions =

will include endpoint security, mobile security and the latest in malware =

threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Gambas-devel mailing list
Gambas-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic