digitalmars.D.learn - comparing with c strings
- denis (4/4) Nov 23 2023 Let's say I have a D application, with some callbacks from C,
- Jonathan M Davis (11/15) Nov 23 2023 std.string.fromStringz will slice the char* to give you a char[] (using
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
Nov 23 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 possiblestd.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
Nov 23 2023