Message Boards Message Boards

4 Replies
4 Total Likes
View groups...
Share this post:

LibraryLink: How to handle null characters in strings?

Posted 9 years ago

LibraryLink supports a "UTF8String" data type. Are null characters supported in strings? Is it possible to separately retrieve the length of the string (MArgument_getUTF8String)? Or should I just use MathLink if I need this feature?

POSTED BY: Szabolcs Horvát
4 Replies

Let me clarify:

My question was if it is possible to work with strings containing null characters with LibraryLink. Null-terminated strings obviously cannot contain null characters as the null character is the end marker.

strlen simply measures the length of a null-terminated string in C.

Try passing "asd\000xyz" to your function. This string has 7 characters.

POSTED BY: Szabolcs Horvát

I doubt if there is a built in library link function that gives string length containing null characters. The only way is to create a custom function. To me the problem then becomes more of creating C function of such kind than library link.

POSTED BY: Damanjit Singh

This still misses the point. The issue was how to transfer such a string at all. It seems it's not possible with LibraryLink. "Writing a custom function" must still rely on the LibraryLink API. If it is possible to write such a custom function, that would solve the problem. But I don't see how to use the LibraryLink API to do this. If you see a way, can you elaborate?

I am aware that the MathLink API does support strings with null bytes and that it can be used in LibraryLink code at the cost of some complexity and performance. It seems this is the way to go.

One difficulty with using LibraryLink is that it is not fully documented, so sometimes I'm not sure if something is not possible or just not well documented. I see in the header files that there were many additions to the API in v9, but not all are described in the documentation.

POSTED BY: Szabolcs Horvát

There is no explicit library link function to get string length, but you can always create your own like:

enter image description here

and then call it as:

lib = CreateLibrary[{"StringLength.c"},"StrLength"];   
StrLength =LibraryFunctionLoad[lib,"StringLength",{ LibraryDataType[String] }, LibraryDataType[Integer] ];   
StrLength["my string"]
POSTED BY: Damanjit Singh
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract