comparing with c strings
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Thu Nov 23 19:30:35 UTC 2023
On Thursday, November 23, 2023 11:29:09 AM MST denis via Digitalmars-d-learn
wrote:
> Let's say I have a D application, with some callbacks from C,
> where some arguments to the callbacks are `const char* path`.
> What is the recommended way to compare them to D strings? Without
> making allocations, if that's possible
std.string.fromStringz will slice the char* to give you a char[] (using
strlen to find the end of the string). Then you can operate on the C string
as a char[] - though since it's a slice of the char*, you'll want to dup or
idup it if the char[] risks living longer than the char* that it's a slice
of. But if all you're doing is comparing it against a D string, then
presumably, you don't need to keep the char[] around, and you won't have to
allocate a copy.
https://dlang.org/phobos/std_string.html#.fromStringz
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list